Merge "Change formatting of API documentation"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / ConcurrencyTestRunnable.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent;
22
23 import java.util.concurrent.atomic.AtomicInteger;
24
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
38 /**
39  * Invoke operations over the NBI and start synchronizations in a separate
40  * thread. For test of robustness using concurrent clients.
41  */
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;
51
52     ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision, MockA1ClientFactory a1ClientFactory, Rics rics,
53         PolicyTypes types) {
54         this.count = nextCount.incrementAndGet();
55         this.supervision = supervision;
56         this.a1ClientFactory = a1ClientFactory;
57         this.rics = rics;
58         this.types = types;
59         this.webClient = new AsyncRestClient(baseUrl);
60     }
61
62     @Override
63     public void run() {
64         try {
65             for (int i = 0; i < 100; ++i) {
66                 if (i % 10 == 0) {
67                     createInconsistency();
68                     this.supervision.checkAllRics();
69                 }
70                 String name = "policy:" + count + ":" + i;
71                 putPolicy(name);
72                 putPolicy(name + "-");
73                 listPolicies();
74                 listTypes();
75                 deletePolicy(name);
76                 deletePolicy(name + "-");
77             }
78         } catch (Exception e) {
79             logger.error("Concurrency test exception " + e.toString());
80         }
81     }
82
83     private Policy createPolicyObject(String id) {
84         Ric ric = this.rics.get("ric");
85         PolicyType type = this.types.get("type1");
86         return ImmutablePolicy.builder() //
87             .id(id) //
88             .json("{}") //
89             .type(type) //
90             .ric(ric) //
91             .ownerServiceName("") //
92             .lastModified("") //
93             .build();
94     }
95
96     private void createInconsistency() {
97         MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
98         Policy policy = createPolicyObject("junk");
99         client.putPolicy(policy).block();
100
101     }
102
103     private void listPolicies() {
104         String uri = "/policies";
105         webClient.getForEntity(uri).block();
106     }
107
108     private void listTypes() {
109         String uri = "/policy_types";
110         webClient.getForEntity(uri).block();
111     }
112
113     private void putPolicy(String name) {
114         String putUrl = "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
115         webClient.putForEntity(putUrl, "{}").block();
116     }
117
118     private void deletePolicy(String name) {
119         String deleteUrl = "/policy?id=" + name;
120         webClient.delete(deleteUrl).block();
121     }
122 }