2 * ========================LICENSE_START=================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.time.Instant;
27 import java.util.concurrent.atomic.AtomicInteger;
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;
43 * Invoke operations over the NBI and start synchronizations in a separate
44 * thread. For test of robustness using concurrent clients.
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;
57 private static Gson gson = new GsonBuilder().create();
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;
66 this.webClient = client;
69 private void printStatusInfo() {
71 String url = "/actuator/metrics/jvm.threads.live";
72 ResponseEntity<String> result = webClient.getForEntity(url).block();
73 System.out.println(Thread.currentThread() + result.getBody());
76 result = webClient.getForEntity(url).block();
77 System.out.println(Thread.currentThread() + result.getBody());
79 } catch (Exception e) {
80 logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
87 for (int i = 0; i < 500; ++i) {
89 createInconsistency();
90 this.supervision.checkAllRics();
92 String name = "policy_" + count + "_" + i;
94 putPolicy(name + "-");
98 deletePolicy(name + "-");
100 } catch (Exception e) {
101 logger.error("Concurrency test exception " + e.toString());
107 public boolean isFailed() {
111 private Policy createPolicyObject(String id) {
112 Ric ric = this.rics.get("ric");
113 PolicyType type = this.types.get("type1");
114 return Policy.builder() //
119 .ownerServiceId("") //
120 .lastModified(Instant.now()) //
121 .isTransient(false) //
122 .statusNotificationUri("/policy_status?id=XXX") //
126 private void createInconsistency() {
127 MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
128 Policy policy = createPolicyObject("junk");
129 client.putPolicy(policy).block();
132 private void listPolicies() {
133 String uri = "/policy-instances";
134 webClient.getForEntity(uri).block();
137 private void listTypes() {
138 String uri = "/policy-types";
139 webClient.getForEntity(uri).block();
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();
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;
154 info.serviceId = serviceName;
155 info.policyData = gson.fromJson("{}", Object.class);
158 info.isTransient = isTransient;
160 return gson.toJson(info);
163 private void deletePolicy(String name) {
164 String deleteUrl = "/policies/" + name;
165 webClient.delete(deleteUrl).block();