Change formatting of API documentation
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / ConcurrencyTestRunnable.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent;
22
23 import java.util.concurrent.atomic.AtomicInteger;
24
25 import org.oransc.policyagent.repository.ImmutablePolicy;
26 import org.oransc.policyagent.repository.Policy;
27 import org.oransc.policyagent.repository.PolicyType;
28 import org.oransc.policyagent.repository.PolicyTypes;
29 import org.oransc.policyagent.repository.Ric;
30 import org.oransc.policyagent.repository.Rics;
31 import org.oransc.policyagent.tasks.RicSupervision;
32 import org.oransc.policyagent.utils.MockA1Client;
33 import org.oransc.policyagent.utils.MockA1ClientFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.http.HttpEntity;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.MediaType;
39 import org.springframework.web.client.RestTemplate;
40
41 /**
42  * Invoke operations over the NBI and start synchronizations in a separate
43  * thread. For test of robustness using concurrent clients.
44  */
45 class ConcurrencyTestRunnable implements Runnable {
46     private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
47     private final RestTemplate restTemplate = new RestTemplate();
48     private final String baseUrl;
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
56     ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision, MockA1ClientFactory a1ClientFactory, Rics rics,
57         PolicyTypes types) {
58         this.baseUrl = baseUrl;
59         this.count = nextCount.incrementAndGet();
60         this.supervision = supervision;
61         this.a1ClientFactory = a1ClientFactory;
62         this.rics = rics;
63         this.types = types;
64     }
65
66     @Override
67     public void run() {
68         try {
69             for (int i = 0; i < 100; ++i) {
70                 if (i % 10 == 0) {
71                     createInconsistency();
72                     this.supervision.checkAllRics();
73                 }
74                 String name = "policy:" + count + ":" + i;
75                 putPolicy(name);
76                 putPolicy(name + "-");
77                 listPolicies();
78                 listTypes();
79                 deletePolicy(name);
80                 deletePolicy(name + "-");
81             }
82         } catch (Exception e) {
83             logger.error("Concurrency exception " + e.toString());
84         }
85     }
86
87     private Policy createPolicyObject(String id) {
88         Ric ric = this.rics.get("ric");
89         PolicyType type = this.types.get("type1");
90         return ImmutablePolicy.builder() //
91             .id(id) //
92             .json("{}") //
93             .type(type) //
94             .ric(ric) //
95             .ownerServiceName("") //
96             .lastModified("") //
97             .build();
98     }
99
100     private void createInconsistency() {
101         MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
102         Policy policy = createPolicyObject("junk");
103         client.putPolicy(policy).block();
104
105     }
106
107     private void listPolicies() {
108         String uri = baseUrl + "/policies";
109         restTemplate.getForObject(uri, String.class);
110     }
111
112     private void listTypes() {
113         String uri = baseUrl + "/policy_types";
114         restTemplate.getForObject(uri, String.class);
115     }
116
117     private void putPolicy(String name) {
118         String putUrl = baseUrl + "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
119         restTemplate.put(putUrl, createJsonHttpEntity("{}"));
120     }
121
122     private void deletePolicy(String name) {
123         String deleteUrl = baseUrl + "/policy?id=" + name;
124         restTemplate.delete(deleteUrl);
125     }
126
127     private static HttpEntity<String> createJsonHttpEntity(String content) {
128         HttpHeaders headers = new HttpHeaders();
129         headers.setContentType(MediaType.APPLICATION_JSON);
130         return new HttpEntity<String>(content, headers);
131     }
132
133 }