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.clients.AsyncRestClient;
26 import org.oransc.policyagent.repository.ImmutablePolicy;
27 import org.oransc.policyagent.repository.Policy;
28 import org.oransc.policyagent.repository.PolicyType;
29 import org.oransc.policyagent.repository.PolicyTypes;
30 import org.oransc.policyagent.repository.Ric;
31 import org.oransc.policyagent.repository.Rics;
32 import org.oransc.policyagent.tasks.RicSupervision;
33 import org.oransc.policyagent.utils.MockA1Client;
34 import org.oransc.policyagent.utils.MockA1ClientFactory;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.http.ResponseEntity;
40 * Invoke operations over the NBI and start synchronizations in a separate
41 * thread. For test of robustness using concurrent clients.
43 class ConcurrencyTestRunnable implements Runnable {
44 private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
45 private final AsyncRestClient webClient;
46 static AtomicInteger nextCount = new AtomicInteger(0);
47 private final int count;
48 private final RicSupervision supervision;
49 private final MockA1ClientFactory a1ClientFactory;
50 private final Rics rics;
51 private final PolicyTypes types;
53 ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision, MockA1ClientFactory a1ClientFactory, Rics rics,
55 this.count = nextCount.incrementAndGet();
56 this.supervision = supervision;
57 this.a1ClientFactory = a1ClientFactory;
60 this.webClient = new AsyncRestClient(baseUrl);
63 private void printStatusInfo() {
65 String url = "/actuator/metrics/jvm.threads.live";
66 ResponseEntity<String> result = webClient.getForEntity(url).block();
67 System.out.println(Thread.currentThread() + result.getBody());
70 result = webClient.getForEntity(url).block();
71 System.out.println(Thread.currentThread() + result.getBody());
73 } catch (Exception e) {
74 logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
81 for (int i = 0; i < 500; ++i) {
83 createInconsistency();
84 this.supervision.checkAllRics();
86 String name = "policy:" + count + ":" + i;
88 putPolicy(name + "-");
92 deletePolicy(name + "-");
94 } catch (Exception e) {
95 logger.error("Concurrency test exception " + e.toString());
100 private Policy createPolicyObject(String id) {
101 Ric ric = this.rics.get("ric");
102 PolicyType type = this.types.get("type1");
103 return ImmutablePolicy.builder() //
108 .ownerServiceName("") //
110 .isTransient(false) //
114 private void createInconsistency() {
115 MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
116 Policy policy = createPolicyObject("junk");
117 client.putPolicy(policy).block();
121 private void listPolicies() {
122 String uri = "/policies";
123 webClient.getForEntity(uri).block();
126 private void listTypes() {
127 String uri = "/policy_types";
128 webClient.getForEntity(uri).block();
131 private void putPolicy(String name) {
132 String putUrl = "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
133 webClient.putForEntity(putUrl, "{}").block();
136 private void deletePolicy(String name) {
137 String deleteUrl = "/policy?id=" + name;
138 webClient.delete(deleteUrl).block();