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;
52 private boolean failed = false;
54 ConcurrencyTestRunnable(AsyncRestClient webClient, RicSupervision supervision, MockA1ClientFactory a1ClientFactory,
55 Rics rics, PolicyTypes types) {
56 this.count = nextCount.incrementAndGet();
57 this.supervision = supervision;
58 this.a1ClientFactory = a1ClientFactory;
61 this.webClient = webClient;
64 private void printStatusInfo() {
66 String url = "/actuator/metrics/jvm.threads.live";
67 ResponseEntity<String> result = webClient.getForEntity(url).block();
68 System.out.println(Thread.currentThread() + result.getBody());
71 result = webClient.getForEntity(url).block();
72 System.out.println(Thread.currentThread() + result.getBody());
74 } catch (Exception e) {
75 logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
82 for (int i = 0; i < 500; ++i) {
84 createInconsistency();
85 this.supervision.checkAllRics();
87 String name = "policy:" + count + ":" + i;
89 putPolicy(name + "-");
93 deletePolicy(name + "-");
95 } catch (Exception e) {
96 logger.error("Concurrency test exception " + e.toString());
102 public boolean isFailed() {
106 private Policy createPolicyObject(String id) {
107 Ric ric = this.rics.get("ric");
108 PolicyType type = this.types.get("type1");
109 return ImmutablePolicy.builder() //
114 .ownerServiceName("") //
116 .isTransient(false) //
120 private void createInconsistency() {
121 MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
122 Policy policy = createPolicyObject("junk");
123 client.putPolicy(policy).block();
127 private void listPolicies() {
128 String uri = "/policies";
129 webClient.getForEntity(uri).block();
132 private void listTypes() {
133 String uri = "/policy_types";
134 webClient.getForEntity(uri).block();
137 private void putPolicy(String name) {
138 String putUrl = "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
139 webClient.putForEntity(putUrl, "{}").block();
142 private void deletePolicy(String name) {
143 String deleteUrl = "/policy?id=" + name;
144 webClient.delete(deleteUrl).block();