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