89a76e95688b7328e442f3c51a304111c61d0221
[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 java.time.Instant;
24 import java.util.concurrent.atomic.AtomicInteger;
25
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;
39
40 /**
41  * Invoke operations over the NBI and start synchronizations in a separate
42  * thread. For test of robustness using concurrent clients.
43  */
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;
54
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;
60         this.rics = rics;
61         this.types = types;
62         this.webClient = client;
63     }
64
65     private void printStatusInfo() {
66         try {
67             String url = "/actuator/metrics/jvm.threads.live";
68             ResponseEntity<String> result = webClient.getForEntity(url).block();
69             System.out.println(Thread.currentThread() + result.getBody());
70
71             url = "/rics";
72             result = webClient.getForEntity(url).block();
73             System.out.println(Thread.currentThread() + result.getBody());
74
75         } catch (Exception e) {
76             logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
77         }
78     }
79
80     @Override
81     public void run() {
82         try {
83             for (int i = 0; i < 500; ++i) {
84                 if (i % 100 == 0) {
85                     createInconsistency();
86                     this.supervision.checkAllRics();
87                 }
88                 String name = "policy:" + count + ":" + i;
89                 putPolicy(name);
90                 putPolicy(name + "-");
91                 listPolicies();
92                 listTypes();
93                 deletePolicy(name);
94                 deletePolicy(name + "-");
95             }
96         } catch (Exception e) {
97             logger.error("Concurrency test exception " + e.toString());
98             printStatusInfo();
99             failed = true;
100         }
101     }
102
103     public boolean isFailed() {
104         return this.failed;
105     }
106
107     private Policy createPolicyObject(String id) {
108         Ric ric = this.rics.get("ric");
109         PolicyType type = this.types.get("type1");
110         return ImmutablePolicy.builder() //
111                 .id(id) //
112                 .json("{}") //
113                 .type(type) //
114                 .ric(ric) //
115                 .ownerServiceId("") //
116                 .lastModified(Instant.now()) //
117                 .isTransient(false) //
118                 .build();
119     }
120
121     private void createInconsistency() {
122         MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
123         Policy policy = createPolicyObject("junk");
124         client.putPolicy(policy).block();
125
126     }
127
128     private void listPolicies() {
129         String uri = "/policies";
130         webClient.getForEntity(uri).block();
131     }
132
133     private void listTypes() {
134         String uri = "/policy-types";
135         webClient.getForEntity(uri).block();
136     }
137
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();
141     }
142
143     private void deletePolicy(String name) {
144         String deleteUrl = "/policy?policy_id=" + name;
145         webClient.delete(deleteUrl).block();
146     }
147 }