Fixed concurrency problems
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / ApplicationTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonParser;
31
32 import java.io.IOException;
33 import java.time.Duration;
34 import java.time.Instant;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Vector;
38 import java.util.concurrent.atomic.AtomicInteger;
39
40 import org.junit.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.oransc.policyagent.configuration.ApplicationConfig;
45 import org.oransc.policyagent.configuration.ImmutableRicConfig;
46 import org.oransc.policyagent.configuration.RicConfig;
47 import org.oransc.policyagent.controllers.PolicyInfo;
48 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
49 import org.oransc.policyagent.controllers.ServiceStatus;
50 import org.oransc.policyagent.exceptions.ServiceException;
51 import org.oransc.policyagent.repository.ImmutablePolicy;
52 import org.oransc.policyagent.repository.ImmutablePolicyType;
53 import org.oransc.policyagent.repository.Lock.LockType;
54 import org.oransc.policyagent.repository.Policies;
55 import org.oransc.policyagent.repository.Policy;
56 import org.oransc.policyagent.repository.PolicyType;
57 import org.oransc.policyagent.repository.PolicyTypes;
58 import org.oransc.policyagent.repository.Ric;
59 import org.oransc.policyagent.repository.Ric.RicState;
60 import org.oransc.policyagent.repository.Rics;
61 import org.oransc.policyagent.repository.Services;
62 import org.oransc.policyagent.tasks.RepositorySupervision;
63 import org.oransc.policyagent.utils.MockA1Client;
64 import org.oransc.policyagent.utils.MockA1ClientFactory;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.boot.test.context.SpringBootTest;
67 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
68 import org.springframework.boot.test.context.TestConfiguration;
69 import org.springframework.boot.web.server.LocalServerPort;
70 import org.springframework.context.ApplicationContext;
71 import org.springframework.context.annotation.Bean;
72 import org.springframework.http.HttpEntity;
73 import org.springframework.http.HttpHeaders;
74 import org.springframework.http.HttpMethod;
75 import org.springframework.http.HttpStatus;
76 import org.springframework.http.HttpStatus.Series;
77 import org.springframework.http.MediaType;
78 import org.springframework.http.ResponseEntity;
79 import org.springframework.http.client.ClientHttpResponse;
80 import org.springframework.test.context.junit.jupiter.SpringExtension;
81 import org.springframework.web.client.ResponseErrorHandler;
82 import org.springframework.web.client.RestTemplate;
83
84 @ExtendWith(SpringExtension.class)
85 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
86 public class ApplicationTest {
87     @Autowired
88     ApplicationContext context;
89
90     @Autowired
91     private Rics rics;
92
93     @Autowired
94     private Policies policies;
95
96     @Autowired
97     private PolicyTypes policyTypes;
98
99     @Autowired
100     MockA1ClientFactory a1ClientFactory;
101
102     @Autowired
103     RepositorySupervision supervision;
104
105     @Autowired
106     Services services;
107
108     private static Gson gson = new GsonBuilder() //
109         .serializeNulls() //
110         .create(); //
111
112     public static class MockApplicationConfig extends ApplicationConfig {
113         @Override
114         public String getLocalConfigurationFilePath() {
115             return ""; // No config file loaded for the test
116         }
117     }
118
119     /**
120      * Overrides the BeanFactory.
121      */
122     @TestConfiguration
123     static class TestBeanFactory {
124         private final PolicyTypes policyTypes = new PolicyTypes();
125
126         @Bean
127         public ApplicationConfig getApplicationConfig() {
128             return new MockApplicationConfig();
129         }
130
131         @Bean
132         MockA1ClientFactory getA1ClientFactory() {
133             return new MockA1ClientFactory(this.policyTypes);
134         }
135
136         @Bean
137         public PolicyTypes getPolicyTypes() {
138             return this.policyTypes;
139         }
140     }
141
142     @LocalServerPort
143     private int port;
144
145     private final RestTemplate restTemplate = new RestTemplate();
146
147     public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
148
149         @Override
150         public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
151             return (httpResponse.getStatusCode().series() == Series.CLIENT_ERROR
152                 || httpResponse.getStatusCode().series() == Series.SERVER_ERROR);
153         }
154
155         @Override
156         public void handleError(ClientHttpResponse httpResponse) throws IOException {
157             System.out.println("Error " + httpResponse.toString());
158         }
159     }
160
161     private void setRestErrorhandler() {
162         restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
163     }
164
165     @BeforeEach
166     public void reset() {
167         rics.clear();
168         policies.clear();
169         policyTypes.clear();
170         services.clear();
171     }
172
173     @AfterEach
174     public void verifyNoRicLocks() {
175         for (Ric ric : this.rics.getRics()) {
176             ric.getLock().lockBlocking(LockType.EXCLUSIVE);
177             ric.getLock().unlockBlocking();
178             assertThat(ric.getLock().getLockCounter()).isEqualTo(0);
179             assertThat(ric.getState()).isEqualTo(Ric.RicState.IDLE);
180         }
181     }
182
183     @Test
184     public void testGetRics() throws Exception {
185         addRic("kista_1");
186         String url = baseUrl() + "/rics";
187         String rsp = this.restTemplate.getForObject(url, String.class);
188         System.out.println(rsp);
189         assertThat(rsp).contains("kista_1");
190
191         url = baseUrl() + "/rics?policyType=STD_PolicyModelUnconstrained_0.2.0";
192         rsp = this.restTemplate.getForObject(url, String.class);
193         assertThat(rsp).isEqualTo("[]");
194     }
195
196     @Test
197     public void testRecovery() throws Exception {
198         addRic("ric").setState(Ric.RicState.UNDEFINED);
199         String ricName = "ric";
200         Policy policy2 = addPolicy("policyId2", "typeName", "service", ricName);
201
202         getA1Client(ricName).putPolicy(policy2); // put it in the RIC
203         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
204
205         String policyId = "policyId";
206         Policy policy = addPolicy(policyId, "typeName", "service", ricName); // This should be created in the RIC
207         supervision.checkAllRics(); // The created policy should be put in the RIC
208         await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ricName).getState()));
209         await().untilAsserted(() -> RicState.IDLE.equals(rics.getRic(ricName).getState()));
210
211         Policies ricPolicies = getA1Client(ricName).getPolicies();
212         assertThat(ricPolicies.size()).isEqualTo(1);
213         Policy ricPolicy = ricPolicies.get(policyId);
214         assertThat(ricPolicy.json()).isEqualTo(policy.json());
215     }
216
217     @Test
218     public void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
219         String ricName = "ric1";
220         Ric ric = addRic(ricName);
221         String managedElementId = "kista_1";
222         ric.addManagedElement(managedElementId);
223
224         String url = baseUrl() + "/ric?managedElementId=" + managedElementId;
225         String rsp = this.restTemplate.getForObject(url, String.class);
226
227         assertThat(rsp).isEqualTo(ricName);
228     }
229
230     @Test
231     public void testGetRicForManagedElementThatDoesNotExist() throws Exception {
232         this.setRestErrorhandler();
233         String url = baseUrl() + "/ric?managedElementId=kista_1";
234         ResponseEntity<String> entity = this.restTemplate.getForEntity(url, String.class);
235         assertThat(entity.getStatusCode().equals(HttpStatus.NOT_FOUND));
236     }
237
238     @Test
239     public void testPutPolicy() throws Exception {
240         String serviceName = "service1";
241         String ricName = "ric1";
242         String policyTypeName = "type1";
243         String policyInstanceId = "instance1";
244
245         putService(serviceName);
246         addPolicyType(policyTypeName, ricName);
247
248         String url = baseUrl() + "/policy?type=" + policyTypeName + "&instance=" + policyInstanceId + "&ric=" + ricName
249             + "&service=" + serviceName;
250         final String json = jsonString();
251         this.rics.getRic(ricName).setState(Ric.RicState.IDLE);
252
253         this.restTemplate.put(url, createJsonHttpEntity(json));
254
255         Policy policy = policies.getPolicy(policyInstanceId);
256         assertThat(policy).isNotNull();
257         assertThat(policy.id()).isEqualTo(policyInstanceId);
258         assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
259         assertThat(policy.ric().name()).isEqualTo("ric1");
260
261         url = baseUrl() + "/policies";
262         String rsp = this.restTemplate.getForObject(url, String.class);
263         assertThat(rsp.contains(policyInstanceId)).isTrue();
264
265     }
266
267     @Test
268     public void testRefuseToUpdatePolicy() throws Exception {
269         // Test that only the json can be changed for a already created policy
270         // In this case service is attempted to be changed
271         this.addRic("ric1");
272         this.addRic("ricXXX");
273
274         this.addPolicy("instance1", "type1", "service1", "ric1");
275         this.setRestErrorhandler();
276         String urlWrongRic = baseUrl() + "/policy?type=type1&instance=instance1&ric=ricXXX&service=service1";
277         ResponseEntity<String> entity = this.putForEntity(urlWrongRic, jsonString());
278         assertThat(entity.getStatusCode().equals(HttpStatus.METHOD_NOT_ALLOWED));
279
280         Policy policy = policies.getPolicy("instance1");
281         assertThat(policy.ric().name()).isEqualTo("ric1"); // Not changed
282     }
283
284     @Test
285     public void testGetPolicy() throws Exception {
286         String url = baseUrl() + "/policy?instance=id";
287         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
288         {
289             String rsp = this.restTemplate.getForObject(url, String.class);
290             assertThat(rsp).isEqualTo(policy.json());
291         }
292         {
293             policies.remove(policy);
294             ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
295             assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
296         }
297     }
298
299     @Test
300     public void testDeletePolicy() throws Exception {
301         String url = baseUrl() + "/policy?instance=id";
302         addPolicy("id", "typeName", "service1", "ric1");
303         assertThat(policies.size()).isEqualTo(1);
304
305         this.restTemplate.delete(url);
306
307         assertThat(policies.size()).isEqualTo(0);
308     }
309
310     @Test
311     public void testGetPolicySchemas() throws Exception {
312         addPolicyType("type1", "ric1");
313         addPolicyType("type2", "ric2");
314
315         String url = baseUrl() + "/policy_schemas";
316         String rsp = this.restTemplate.getForObject(url, String.class);
317         System.out.println("*** " + rsp);
318         assertThat(rsp).contains("type1");
319         assertThat(rsp).contains("[{\"title\":\"type2\"}");
320
321         List<String> info = parseSchemas(rsp);
322         assertThat(info.size()).isEqualTo(2);
323
324         url = baseUrl() + "/policy_schemas?ric=ric1";
325         rsp = this.restTemplate.getForObject(url, String.class);
326         assertThat(rsp).contains("type1");
327         info = parseSchemas(rsp);
328         assertThat(info.size()).isEqualTo(1);
329     }
330
331     @Test
332     public void testGetPolicySchema() throws Exception {
333         addPolicyType("type1", "ric1");
334         addPolicyType("type2", "ric2");
335
336         String url = baseUrl() + "/policy_schema?id=type1";
337         String rsp = this.restTemplate.getForObject(url, String.class);
338         System.out.println(rsp);
339         assertThat(rsp).contains("type1");
340         assertThat(rsp).contains("title");
341     }
342
343     @Test
344     public void testGetPolicyTypes() throws Exception {
345         addPolicyType("type1", "ric1");
346         addPolicyType("type2", "ric2");
347
348         String url = baseUrl() + "/policy_types";
349         String rsp = this.restTemplate.getForObject(url, String.class);
350         assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
351
352         url = baseUrl() + "/policy_types?ric=ric1";
353         rsp = this.restTemplate.getForObject(url, String.class);
354         assertThat(rsp).isEqualTo("[\"type1\"]");
355     }
356
357     @Test
358     public void testGetPolicies() throws Exception {
359         reset();
360         String url = baseUrl() + "/policies";
361         addPolicy("id1", "type1", "service1");
362
363         String rsp = this.restTemplate.getForObject(url, String.class);
364         System.out.println(rsp);
365         List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
366         assertThat(info).size().isEqualTo(1);
367         PolicyInfo policyInfo = info.get(0);
368         assert (policyInfo.validate());
369         assertThat(policyInfo.id).isEqualTo("id1");
370         assertThat(policyInfo.type).isEqualTo("type1");
371         assertThat(policyInfo.service).isEqualTo("service1");
372     }
373
374     @Test
375     public void testGetPoliciesFilter() throws Exception {
376         addPolicy("id1", "type1", "service1");
377         addPolicy("id2", "type1", "service2");
378         addPolicy("id3", "type2", "service1");
379
380         String url = baseUrl() + "/policies?type=type1";
381         String rsp = this.restTemplate.getForObject(url, String.class);
382         System.out.println(rsp);
383         assertThat(rsp).contains("id1");
384         assertThat(rsp).contains("id2");
385         assertThat(rsp.contains("id3")).isFalse();
386
387         url = baseUrl() + "/policies?type=type1&service=service2";
388         rsp = this.restTemplate.getForObject(url, String.class);
389         System.out.println(rsp);
390         assertThat(rsp.contains("id1")).isFalse();
391         assertThat(rsp).contains("id2");
392         assertThat(rsp.contains("id3")).isFalse();
393     }
394
395     @Test
396     public void testPutAndGetService() throws Exception {
397         // PUT
398         putService("name");
399
400         // GET
401         String url = baseUrl() + "/services?serviceName=name";
402         String rsp = this.restTemplate.getForObject(url, String.class);
403         List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
404         assertThat(info.size()).isEqualTo(1);
405         ServiceStatus status = info.iterator().next();
406         assertThat(status.keepAliveIntervalSeconds).isEqualTo(1);
407         assertThat(status.serviceName).isEqualTo("name");
408
409         // GET (all)
410         url = baseUrl() + "/services";
411         rsp = this.restTemplate.getForObject(url, String.class);
412         assertThat(rsp.contains("name")).isTrue();
413         System.out.println(rsp);
414
415         // Keep alive
416         url = baseUrl() + "/services/keepalive?name=name";
417         ResponseEntity<String> entity = this.restTemplate.postForEntity(url, null, String.class);
418         assertThat(entity.getStatusCode().equals(HttpStatus.OK));
419
420         // DELETE
421         assertThat(services.size()).isEqualTo(1);
422         url = baseUrl() + "/services?name=name";
423         this.restTemplate.delete(url);
424         assertThat(services.size()).isEqualTo(0);
425
426         // Keep alive, no registerred service
427         url = baseUrl() + "/services/keepalive?name=name";
428         setRestErrorhandler();
429         entity = this.restTemplate.postForEntity(url, null, String.class);
430         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
431     }
432
433     @Test
434     public void testGetPolicyStatus() throws Exception {
435         addPolicy("id", "typeName", "service1", "ric1");
436         assertThat(policies.size()).isEqualTo(1);
437
438         String url = baseUrl() + "/policy_status?instance=id";
439         String rsp = this.restTemplate.getForObject(url, String.class);
440         assertThat(rsp.equals("OK")).isTrue();
441     }
442
443     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
444         addRic(ric);
445         Policy p = ImmutablePolicy.builder().id(id) //
446             .json(jsonString()) //
447             .ownerServiceName(service) //
448             .ric(rics.getRic(ric)) //
449             .type(addPolicyType(typeName, ric)) //
450             .lastModified("lastModified").build();
451         policies.put(p);
452         return p;
453     }
454
455     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
456         return addPolicy(id, typeName, service, "ric");
457     }
458
459     private String createServiceJson(String name) {
460         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, 1, "callbackUrl");
461
462         String json = gson.toJson(service);
463         return json;
464     }
465
466     private void putService(String name) {
467         String url = baseUrl() + "/service";
468         HttpEntity<String> entity = createJsonHttpEntity(createServiceJson(name));
469         this.restTemplate.put(url, entity);
470     }
471
472     private String baseUrl() {
473         return "http://localhost:" + port;
474     }
475
476     private String jsonString() {
477         return "{\n  \"servingCellNrcgi\": \"1\"\n }";
478     }
479
480     private static class ConcurrencyTestRunnable implements Runnable {
481         private final RestTemplate restTemplate = new RestTemplate();
482         private final String baseUrl;
483         static AtomicInteger nextCount = new AtomicInteger(0);
484         private final int count;
485         private final RepositorySupervision supervision;
486
487         ConcurrencyTestRunnable(String baseUrl, RepositorySupervision supervision) {
488             this.baseUrl = baseUrl;
489             this.count = nextCount.incrementAndGet();
490             this.supervision = supervision;
491         }
492
493         public void run() {
494             for (int i = 0; i < 100; ++i) {
495                 if (i % 10 == 0) {
496                     this.supervision.checkAllRics();
497                 }
498                 String name = "policy:" + count + ":" + i;
499                 putPolicy(name);
500                 deletePolicy(name);
501             }
502         }
503
504         private void putPolicy(String name) {
505             String putUrl = baseUrl + "/policy?type=type1&instance=" + name + "&ric=ric1&service=service1";
506             this.restTemplate.put(putUrl, createJsonHttpEntity("{}"));
507         }
508
509         private void deletePolicy(String name) {
510             String deleteUrl = baseUrl + "/policy?instance=" + name;
511             this.restTemplate.delete(deleteUrl);
512         }
513     }
514
515     @Test
516     public void testConcurrency() throws Exception {
517         final Instant startTime = Instant.now();
518         List<Thread> threads = new ArrayList<>();
519         addRic("ric1");
520         addPolicyType("type1", "ric1");
521
522         for (int i = 0; i < 100; ++i) {
523             Thread t = new Thread(new ConcurrencyTestRunnable(baseUrl(), this.supervision), "TestThread_" + i);
524             t.start();
525             threads.add(t);
526         }
527         for (Thread t : threads) {
528             t.join();
529         }
530         assertThat(policies.size()).isEqualTo(0);
531         System.out.println("Concurrency test took " + Duration.between(startTime, Instant.now()));
532     }
533
534     private MockA1Client getA1Client(String ricName) throws ServiceException {
535         return a1ClientFactory.getOrCreateA1Client(ricName);
536     }
537
538     private PolicyType addPolicyType(String policyTypeName, String ricName) {
539         PolicyType type = ImmutablePolicyType.builder() //
540             .name(policyTypeName) //
541             .schema("{\"title\":\"" + policyTypeName + "\"}") //
542             .build();
543
544         policyTypes.put(type);
545         addRic(ricName).addSupportedPolicyType(type);
546         return type;
547     }
548
549     private Ric addRic(String ricName) {
550         if (rics.get(ricName) != null) {
551             return rics.get(ricName);
552         }
553         Vector<String> mes = new Vector<>();
554         RicConfig conf = ImmutableRicConfig.builder() //
555             .name(ricName) //
556             .baseUrl(ricName) //
557             .managedElementIds(mes) //
558             .build();
559         Ric ric = new Ric(conf);
560         ric.setState(Ric.RicState.IDLE);
561         this.rics.put(ric);
562         return ric;
563     }
564
565     private static HttpEntity<String> createJsonHttpEntity(String content) {
566         HttpHeaders headers = new HttpHeaders();
567         headers.setContentType(MediaType.APPLICATION_JSON);
568         return new HttpEntity<String>(content, headers);
569     }
570
571     private ResponseEntity<String> putForEntity(String url, String jsonBody) {
572         return restTemplate.exchange(url, HttpMethod.PUT, createJsonHttpEntity(jsonBody), String.class);
573     }
574
575     private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
576         List<T> result = new ArrayList<>();
577         JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
578         for (JsonElement jsonElement : jsonArr) {
579             T o = gson.fromJson(jsonElement.toString(), clazz);
580             result.add(o);
581         }
582         return result;
583     }
584
585     private static List<String> parseSchemas(String jsonString) {
586         JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
587         List<String> result = new ArrayList<>();
588         for (JsonElement schemaObject : arrayOfSchema) {
589             result.add(schemaObject.toString());
590         }
591         return result;
592     }
593 }