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;
39 * Invoke operations over the NBI and start synchronizations in a separate
40 * thread. For test of robustness using concurrent clients.
42 class ConcurrencyTestRunnable implements Runnable {
43 private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
44 private final AsyncRestClient webClient;
45 static AtomicInteger nextCount = new AtomicInteger(0);
46 private final int count;
47 private final RicSupervision supervision;
48 private final MockA1ClientFactory a1ClientFactory;
49 private final Rics rics;
50 private final PolicyTypes types;
52 ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision, MockA1ClientFactory a1ClientFactory, Rics rics,
54 this.count = nextCount.incrementAndGet();
55 this.supervision = supervision;
56 this.a1ClientFactory = a1ClientFactory;
59 this.webClient = new AsyncRestClient(baseUrl);
65 for (int i = 0; i < 100; ++i) {
67 createInconsistency();
68 this.supervision.checkAllRics();
70 String name = "policy:" + count + ":" + i;
72 putPolicy(name + "-");
76 deletePolicy(name + "-");
78 } catch (Exception e) {
79 logger.error("Concurrency test exception " + e.toString());
83 private Policy createPolicyObject(String id) {
84 Ric ric = this.rics.get("ric");
85 PolicyType type = this.types.get("type1");
86 return ImmutablePolicy.builder() //
91 .ownerServiceName("") //
96 private void createInconsistency() {
97 MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
98 Policy policy = createPolicyObject("junk");
99 client.putPolicy(policy).block();
103 private void listPolicies() {
104 String uri = "/policies";
105 webClient.getForEntity(uri).block();
108 private void listTypes() {
109 String uri = "/policy_types";
110 webClient.getForEntity(uri).block();
113 private void putPolicy(String name) {
114 String putUrl = "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
115 webClient.putForEntity(putUrl, "{}").block();
118 private void deletePolicy(String name) {
119 String deleteUrl = "/policy?id=" + name;
120 webClient.delete(deleteUrl).block();