Support for transient policies not recreated at synchronization
[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
53     ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision, MockA1ClientFactory a1ClientFactory, Rics rics,
54         PolicyTypes types) {
55         this.count = nextCount.incrementAndGet();
56         this.supervision = supervision;
57         this.a1ClientFactory = a1ClientFactory;
58         this.rics = rics;
59         this.types = types;
60         this.webClient = new AsyncRestClient(baseUrl);
61     }
62
63     private void printStatusInfo() {
64         try {
65             String url = "/actuator/metrics/jvm.threads.live";
66             ResponseEntity<String> result = webClient.getForEntity(url).block();
67             System.out.println(Thread.currentThread() + result.getBody());
68
69             url = "/rics";
70             result = webClient.getForEntity(url).block();
71             System.out.println(Thread.currentThread() + result.getBody());
72
73         } catch (Exception e) {
74             logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
75         }
76     }
77
78     @Override
79     public void run() {
80         try {
81             for (int i = 0; i < 500; ++i) {
82                 if (i % 100 == 0) {
83                     createInconsistency();
84                     this.supervision.checkAllRics();
85                 }
86                 String name = "policy:" + count + ":" + i;
87                 putPolicy(name);
88                 putPolicy(name + "-");
89                 listPolicies();
90                 listTypes();
91                 deletePolicy(name);
92                 deletePolicy(name + "-");
93             }
94         } catch (Exception e) {
95             logger.error("Concurrency test exception " + e.toString());
96             printStatusInfo();
97         }
98     }
99
100     private Policy createPolicyObject(String id) {
101         Ric ric = this.rics.get("ric");
102         PolicyType type = this.types.get("type1");
103         return ImmutablePolicy.builder() //
104             .id(id) //
105             .json("{}") //
106             .type(type) //
107             .ric(ric) //
108             .ownerServiceName("") //
109             .lastModified("") //
110             .isTransient(false) //
111             .build();
112     }
113
114     private void createInconsistency() {
115         MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
116         Policy policy = createPolicyObject("junk");
117         client.putPolicy(policy).block();
118
119     }
120
121     private void listPolicies() {
122         String uri = "/policies";
123         webClient.getForEntity(uri).block();
124     }
125
126     private void listTypes() {
127         String uri = "/policy_types";
128         webClient.getForEntity(uri).block();
129     }
130
131     private void putPolicy(String name) {
132         String putUrl = "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
133         webClient.putForEntity(putUrl, "{}").block();
134     }
135
136     private void deletePolicy(String name) {
137         String deleteUrl = "/policy?id=" + name;
138         webClient.delete(deleteUrl).block();
139     }
140 }