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.ImmutablePolicy;
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;
44 * Invoke operations over the NBI and start synchronizations in a separate
45 * thread. For test of robustness using concurrent clients.
47 class ConcurrencyTestRunnable implements Runnable {
48 private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
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;
58 private static Gson gson = new GsonBuilder().create();
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;
67 this.webClient = client;
70 private void printStatusInfo() {
72 String url = "/actuator/metrics/jvm.threads.live";
73 ResponseEntity<String> result = webClient.getForEntity(url).block();
74 System.out.println(Thread.currentThread() + result.getBody());
77 result = webClient.getForEntity(url).block();
78 System.out.println(Thread.currentThread() + result.getBody());
80 } catch (Exception e) {
81 logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
88 for (int i = 0; i < 500; ++i) {
90 createInconsistency();
91 this.supervision.checkAllRics();
93 String name = "policy_" + count + "_" + i;
95 putPolicy(name + "-");
99 deletePolicy(name + "-");
101 } catch (Exception e) {
102 logger.error("Concurrency test exception " + e.toString());
108 public boolean isFailed() {
112 private Policy createPolicyObject(String id) {
113 Ric ric = this.rics.get("ric");
114 PolicyType type = this.types.get("type1");
115 return ImmutablePolicy.builder() //
120 .ownerServiceId("") //
121 .lastModified(Instant.now()) //
122 .isTransient(false) //
123 .statusNotificationUri("/policy_status?id=XXX") //
127 private void createInconsistency() {
128 MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
129 Policy policy = createPolicyObject("junk");
130 client.putPolicy(policy).block();
133 private void listPolicies() {
134 String uri = "/policy-instances";
135 webClient.getForEntity(uri).block();
138 private void listTypes() {
139 String uri = "/policy-types";
140 webClient.getForEntity(uri).block();
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();
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;
155 info.serviceId = serviceName;
156 info.policyData = gson.fromJson("{}", Object.class);
159 info.isTransient = isTransient;
161 return gson.toJson(info);
164 private void deletePolicy(String name) {
165 String deleteUrl = "/policies/" + name;
166 webClient.delete(deleteUrl).block();