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