2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent;
23 import java.util.concurrent.atomic.AtomicInteger;
25 import org.oransc.policyagent.repository.ImmutablePolicy;
26 import org.oransc.policyagent.repository.Policy;
27 import org.oransc.policyagent.repository.PolicyType;
28 import org.oransc.policyagent.repository.PolicyTypes;
29 import org.oransc.policyagent.repository.Ric;
30 import org.oransc.policyagent.repository.Rics;
31 import org.oransc.policyagent.tasks.RicSupervision;
32 import org.oransc.policyagent.utils.MockA1Client;
33 import org.oransc.policyagent.utils.MockA1ClientFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.http.HttpEntity;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.MediaType;
39 import org.springframework.web.client.RestTemplate;
42 * Invoke operations over the NBI and start synchronizations in a separate
43 * thread. For test of robustness using concurrent clients.
45 class ConcurrencyTestRunnable implements Runnable {
46 private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
47 private final RestTemplate restTemplate = new RestTemplate();
48 private final String baseUrl;
49 static AtomicInteger nextCount = new AtomicInteger(0);
50 private final int count;
51 private final RicSupervision supervision;
52 private final MockA1ClientFactory a1ClientFactory;
53 private final Rics rics;
54 private final PolicyTypes types;
56 ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision, MockA1ClientFactory a1ClientFactory, Rics rics,
58 this.baseUrl = baseUrl;
59 this.count = nextCount.incrementAndGet();
60 this.supervision = supervision;
61 this.a1ClientFactory = a1ClientFactory;
69 for (int i = 0; i < 100; ++i) {
71 createInconsistency();
72 this.supervision.checkAllRics();
74 String name = "policy:" + count + ":" + i;
76 putPolicy(name + "-");
80 deletePolicy(name + "-");
82 } catch (Exception e) {
83 logger.error("Concurrency exception " + e.toString());
87 private Policy createPolicyObject(String id) {
88 Ric ric = this.rics.get("ric");
89 PolicyType type = this.types.get("type1");
90 return ImmutablePolicy.builder() //
95 .ownerServiceName("") //
100 private void createInconsistency() {
101 MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
102 Policy policy = createPolicyObject("junk");
103 client.putPolicy(policy).block();
107 private void listPolicies() {
108 String uri = baseUrl + "/policies";
109 restTemplate.getForObject(uri, String.class);
112 private void listTypes() {
113 String uri = baseUrl + "/policy_types";
114 restTemplate.getForObject(uri, String.class);
117 private void putPolicy(String name) {
118 String putUrl = baseUrl + "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
119 restTemplate.put(putUrl, createJsonHttpEntity("{}"));
122 private void deletePolicy(String name) {
123 String deleteUrl = baseUrl + "/policy?id=" + name;
124 restTemplate.delete(deleteUrl);
127 private static HttpEntity<String> createJsonHttpEntity(String content) {
128 HttpHeaders headers = new HttpHeaders();
129 headers.setContentType(MediaType.APPLICATION_JSON);
130 return new HttpEntity<String>(content, headers);