0ca553407a41fe3cbff751160fd9856f4baa060f
[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 import org.springframework.http.ResponseEntity;
38
39 /**
40  * Invoke operations over the NBI and start synchronizations in a separate
41  * thread. For test of robustness using concurrent clients.
42  */
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;
53
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;
59         this.rics = rics;
60         this.types = types;
61         this.webClient = webClient;
62     }
63
64     private void printStatusInfo() {
65         try {
66             String url = "/actuator/metrics/jvm.threads.live";
67             ResponseEntity<String> result = webClient.getForEntity(url).block();
68             System.out.println(Thread.currentThread() + result.getBody());
69
70             url = "/rics";
71             result = webClient.getForEntity(url).block();
72             System.out.println(Thread.currentThread() + result.getBody());
73
74         } catch (Exception e) {
75             logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
76         }
77     }
78
79     @Override
80     public void run() {
81         try {
82             for (int i = 0; i < 500; ++i) {
83                 if (i % 100 == 0) {
84                     createInconsistency();
85                     this.supervision.checkAllRics();
86                 }
87                 String name = "policy:" + count + ":" + i;
88                 putPolicy(name);
89                 putPolicy(name + "-");
90                 listPolicies();
91                 listTypes();
92                 deletePolicy(name);
93                 deletePolicy(name + "-");
94             }
95         } catch (Exception e) {
96             logger.error("Concurrency test exception " + e.toString());
97             printStatusInfo();
98             failed = true;
99         }
100     }
101
102     public boolean isFailed() {
103         return this.failed;
104     }
105
106     private Policy createPolicyObject(String id) {
107         Ric ric = this.rics.get("ric");
108         PolicyType type = this.types.get("type1");
109         return ImmutablePolicy.builder() //
110             .id(id) //
111             .json("{}") //
112             .type(type) //
113             .ric(ric) //
114             .ownerServiceName("") //
115             .lastModified("") //
116             .isTransient(false) //
117             .build();
118     }
119
120     private void createInconsistency() {
121         MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
122         Policy policy = createPolicyObject("junk");
123         client.putPolicy(policy).block();
124
125     }
126
127     private void listPolicies() {
128         String uri = "/policies";
129         webClient.getForEntity(uri).block();
130     }
131
132     private void listTypes() {
133         String uri = "/policy_types";
134         webClient.getForEntity(uri).block();
135     }
136
137     private void putPolicy(String name) {
138         String putUrl = "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
139         webClient.putForEntity(putUrl, "{}").block();
140     }
141
142     private void deletePolicy(String name) {
143         String deleteUrl = "/policy?id=" + name;
144         webClient.delete(deleteUrl).block();
145     }
146 }