a42d034a367150b8f24c1bcccab83c7adbd6bf25
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27
28 import java.lang.invoke.MethodHandles;
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.concurrent.atomic.AtomicInteger;
33
34 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
35 import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyInfo;
36 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
41 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
42 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
43 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.ResponseEntity;
48
49 /**
50  * Invoke operations over the NBI and start synchronizations in a separate
51  * thread. For test of robustness using concurrent clients.
52  */
53 class ConcurrencyTestRunnable implements Runnable {
54     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55     private final AsyncRestClient webClient;
56     static AtomicInteger nextCount = new AtomicInteger(0);
57     private final int count;
58     private final RicSupervision supervision;
59     private final MockA1ClientFactory a1ClientFactory;
60     private final Rics rics;
61     private final PolicyTypes types;
62     private boolean failed = false;
63
64     private ObjectMapper objectMapper = new ObjectMapper();
65
66     private static Gson gson = new GsonBuilder().create();
67
68     ConcurrencyTestRunnable(AsyncRestClient client, RicSupervision supervision, MockA1ClientFactory a1ClientFactory,
69             Rics rics, PolicyTypes types) {
70         this.count = nextCount.incrementAndGet();
71         this.supervision = supervision;
72         this.a1ClientFactory = a1ClientFactory;
73         this.rics = rics;
74         this.types = types;
75         this.webClient = client;
76     }
77
78     private void printStatusInfo() {
79         try {
80             String url = "/actuator/metrics/jvm.threads.live";
81             ResponseEntity<String> result = webClient.getForEntity(url).block();
82             System.out.println(Thread.currentThread() + result.getBody());
83
84             url = "/rics";
85             result = webClient.getForEntity(url).block();
86             System.out.println(Thread.currentThread() + result.getBody());
87
88         } catch (Exception e) {
89             logger.error("{} Concurrency test printStatusInfo exception {}", Thread.currentThread(), e.toString());
90         }
91     }
92
93     @Override
94     public void run() {
95         try {
96             for (int i = 0; i < 500; ++i) {
97                 if (i % 100 == 0) {
98                     createInconsistency();
99                     this.supervision.checkAllRics();
100                 }
101                 String name = "policy_" + count + "_" + i;
102                 putPolicy(name);
103                 putPolicy(name + "-");
104                 listPolicies();
105                 listTypes();
106                 deletePolicy(name);
107                 deletePolicy(name + "-");
108             }
109         } catch (Exception e) {
110             logger.error("Concurrency test exception {}", e.toString());
111             printStatusInfo();
112             failed = true;
113         }
114     }
115
116     public boolean isFailed() {
117         return this.failed;
118     }
119
120     private Policy createPolicyObject(String id) {
121         Ric ric = this.rics.get("ric");
122         PolicyType type = this.types.get("type1");
123         return Policy.builder() //
124                 .id(id) //
125                 .json("{}") //
126                 .type(type) //
127                 .ric(ric) //
128                 .ownerServiceId("") //
129                 .lastModified(Instant.now()) //
130                 .isTransient(false) //
131                 .statusNotificationUri("/policy_status?id=XXX") //
132                 .build();
133     }
134
135     private void createInconsistency() {
136         MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
137         Policy policy = createPolicyObject("junk");
138         client.putPolicy(policy).block();
139     }
140
141     private void listPolicies() {
142         String uri = "/policy-instances";
143         webClient.getForEntity(uri).block();
144     }
145
146     private void listTypes() {
147         String uri = "/policy-types";
148         webClient.getForEntity(uri).block();
149     }
150
151     private void putPolicy(String name) throws JsonProcessingException {
152         String putUrl = "/policies";
153         String body = putPolicyBody("service1", "ric", "type1", name, false);
154         webClient.putForEntity(putUrl, body).block();
155     }
156
157     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
158             boolean isTransient) throws JsonProcessingException {
159
160         PolicyInfo policyInfo = new PolicyInfo();
161         policyInfo.setPolicyId(policyInstanceId);
162         policyInfo.setPolicytypeId(policyTypeName);
163         policyInfo.setRicId(ricId);
164         policyInfo.setServiceId(serviceName);
165         policyInfo.setPolicyData(policyData());
166         policyInfo.setStatusNotificationUri("/status");
167         policyInfo.setTransient(isTransient);
168         return objectMapper.writeValueAsString(policyInfo);
169     }
170
171     private Map<String,String> policyData() {
172         Map<String,String> policyDataInMap = new HashMap<>();
173         policyDataInMap.put("servingCellNrcgi","1");
174         return policyDataInMap;
175     }
176
177     private void deletePolicy(String name) {
178         String deleteUrl = "/policies/" + name;
179         webClient.delete(deleteUrl).block();
180     }
181 }