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 java.time.Instant;
24 import java.util.concurrent.atomic.AtomicInteger;
26 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
27 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
28 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
33 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
34 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.http.ResponseEntity;
41 * Invoke operations over the NBI and start synchronizations in a separate
42 * thread. For test of robustness using concurrent clients.
44 class ConcurrencyTestRunnable implements Runnable {
45 private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
46 private final AsyncRestClient webClient;
47 static AtomicInteger nextCount = new AtomicInteger(0);
48 private final int count;
49 private final RicSupervision supervision;
50 private final MockA1ClientFactory a1ClientFactory;
51 private final Rics rics;
52 private final PolicyTypes types;
53 private boolean failed = false;
55 ConcurrencyTestRunnable(AsyncRestClient client, RicSupervision supervision, MockA1ClientFactory a1ClientFactory,
56 Rics rics, PolicyTypes types) {
57 this.count = nextCount.incrementAndGet();
58 this.supervision = supervision;
59 this.a1ClientFactory = a1ClientFactory;
62 this.webClient = client;
65 private void printStatusInfo() {
67 String url = "/actuator/metrics/jvm.threads.live";
68 ResponseEntity<String> result = webClient.getForEntity(url).block();
69 System.out.println(Thread.currentThread() + result.getBody());
72 result = webClient.getForEntity(url).block();
73 System.out.println(Thread.currentThread() + result.getBody());
75 } catch (Exception e) {
76 logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
83 for (int i = 0; i < 500; ++i) {
85 createInconsistency();
86 this.supervision.checkAllRics();
88 String name = "policy:" + count + ":" + i;
90 putPolicy(name + "-");
94 deletePolicy(name + "-");
96 } catch (Exception e) {
97 logger.error("Concurrency test exception " + e.toString());
103 public boolean isFailed() {
107 private Policy createPolicyObject(String id) {
108 Ric ric = this.rics.get("ric");
109 PolicyType type = this.types.get("type1");
110 return ImmutablePolicy.builder() //
115 .ownerServiceId("") //
116 .lastModified(Instant.now()) //
117 .isTransient(false) //
121 private void createInconsistency() {
122 MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
123 Policy policy = createPolicyObject("junk");
124 client.putPolicy(policy).block();
128 private void listPolicies() {
129 String uri = "/policies";
130 webClient.getForEntity(uri).block();
133 private void listTypes() {
134 String uri = "/policy-types";
135 webClient.getForEntity(uri).block();
138 private void putPolicy(String name) {
139 String putUrl = "/policy?policytype_id=type1&policy_id=" + name + "&ric_id=ric&service_id=service1";
140 webClient.putForEntity(putUrl, "{}").block();
143 private void deletePolicy(String name) {
144 String deleteUrl = "/policy?policy_id=" + name;
145 webClient.delete(deleteUrl).block();