bec557352fc4461b78c5edb8ed7b58526a2b8c67
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2022 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 static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.io.PrintStream;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.time.Duration;
41 import java.time.Instant;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45
46 import org.json.JSONObject;
47 import org.junit.jupiter.api.AfterAll;
48 import org.junit.jupiter.api.AfterEach;
49 import org.junit.jupiter.api.Test;
50 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
51 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
52 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
53 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
54 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
55 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
56 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
57 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
58 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
59 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
71 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
72 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
73 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
74 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
75 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78 import org.springframework.beans.factory.annotation.Autowired;
79 import org.springframework.boot.test.context.SpringBootTest;
80 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
81 import org.springframework.boot.test.context.TestConfiguration;
82 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
83 import org.springframework.boot.web.server.LocalServerPort;
84 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
85 import org.springframework.context.ApplicationContext;
86 import org.springframework.context.annotation.Bean;
87 import org.springframework.http.HttpStatus;
88 import org.springframework.http.MediaType;
89 import org.springframework.http.ResponseEntity;
90 import org.springframework.test.context.TestPropertySource;
91 import org.springframework.util.FileSystemUtils;
92 import org.springframework.web.reactive.function.client.WebClientResponseException;
93
94 import reactor.core.publisher.Mono;
95 import reactor.test.StepVerifier;
96 import reactor.util.annotation.Nullable;
97
98 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
99 @TestPropertySource(properties = { //
100         "server.ssl.key-store=./config/keystore.jks", //
101         "app.webclient.trust-store=./config/truststore.jks", //
102         "app.webclient.trust-store-used=true", //
103         "app.vardata-directory=/tmp/pmstest", //
104         "app.filepath=", //
105         "app.s3.bucket=" // If this is set, S3 will be used to store data.
106 })
107 class ApplicationTest {
108     private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
109
110     @Autowired
111     ApplicationContext context;
112
113     @Autowired
114     private Rics rics;
115
116     @Autowired
117     private Policies policies;
118
119     @Autowired
120     private PolicyTypes policyTypes;
121
122     @Autowired
123     MockA1ClientFactory a1ClientFactory;
124
125     @Autowired
126     RicSupervision supervision;
127
128     @Autowired
129     ApplicationConfig applicationConfig;
130
131     @Autowired
132     Services services;
133
134     @Autowired
135     RappSimulatorController rAppSimulator;
136
137     @Autowired
138     RefreshConfigTask refreshConfigTask;
139
140     @Autowired
141     SecurityContext securityContext;
142
143     private static Gson gson = new GsonBuilder().create();
144
145     /**
146      * Overrides the BeanFactory.
147      */
148     @TestConfiguration
149     static class TestBeanFactory {
150
151         @Bean
152         A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
153             return new MockA1ClientFactory(appConfig, types);
154         }
155
156         @Bean
157         public ServiceSupervision getServiceSupervision(@Autowired Services services,
158                 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
159             Duration checkInterval = Duration.ofMillis(1);
160             return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
161         }
162
163         @Bean
164         public ServletWebServerFactory servletContainer() {
165             return new TomcatServletWebServerFactory();
166         }
167     }
168
169     @LocalServerPort
170     private int port;
171
172     @AfterEach
173     void reset() {
174         rics.clear();
175         policies.clear();
176         policyTypes.clear();
177         services.clear();
178         a1ClientFactory.reset();
179         this.rAppSimulator.getTestResults().clear();
180         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
181         this.securityContext.setAuthTokenFilePath(null);
182     }
183
184     @AfterAll
185     static void clearTestDir() {
186         try {
187             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstest"));
188         } catch (Exception e) {
189             logger.warn("Could test directory : {}", e.getMessage());
190         }
191     }
192
193     @AfterEach
194     void verifyNoRicLocks() {
195         for (Ric ric : this.rics.getRics()) {
196             Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "verifyNoRicLocks");
197             grant.unlockBlocking();
198             assertThat(ric.getLock().getLockCounter()).isZero();
199             assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
200         }
201     }
202
203     @Test
204     void generateApiDoc() throws IOException {
205         String url = "https://localhost:" + this.port + "/v3/api-docs";
206         ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
207         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
208         JSONObject jsonObj = new JSONObject(resp.getBody());
209         assertThat(jsonObj.remove("servers")).isNotNull();
210
211         String indented = (jsonObj).toString(4);
212         String docDir = "api/";
213         Files.createDirectories(Paths.get(docDir));
214         try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
215             out.print(indented);
216         }
217     }
218
219     @Test
220     void testPersistencyPolicies() throws Exception {
221         Ric ric = this.addRic("ric1");
222         PolicyType type = this.addPolicyType("type1", ric.id());
223
224         final int noOfPolicies = 100;
225         for (int i = 0; i < noOfPolicies; ++i) {
226             addPolicy("id" + i, type.getId(), "service", ric.id());
227         }
228         waitforS3();
229
230         {
231             Policies policies = new Policies(this.applicationConfig);
232             policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
233             assertThat(policies.size()).isEqualTo(noOfPolicies);
234         }
235
236         {
237             restClient().delete("/policies/id2").block();
238             Policies policies = new Policies(this.applicationConfig);
239             policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
240             assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
241         }
242     }
243
244     @Test
245     void testPersistencyPolicyTypes() throws Exception {
246         Ric ric = this.addRic("ric1");
247         this.addPolicyType("type1", ric.id());
248         waitforS3();
249
250         PolicyTypes types = new PolicyTypes(this.applicationConfig);
251         types.restoreFromDatabase().blockLast();
252         assertThat(types.size()).isEqualTo(1);
253     }
254
255     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
256     private void waitforS3() throws Exception {
257         if (applicationConfig.isS3Enabled()) {
258             Thread.sleep(1000);
259         }
260     }
261
262     @Test
263     void testPersistencyService() throws Exception {
264         final String SERVICE = "serviceName";
265         putService(SERVICE, 1234, HttpStatus.CREATED);
266         assertThat(this.services.size()).isEqualTo(1);
267         Service service = this.services.getService(SERVICE);
268         waitforS3();
269
270         Services servicesRestored = new Services(this.applicationConfig);
271         servicesRestored.restoreFromDatabase().blockLast();
272         Service serviceRestored = servicesRestored.getService(SERVICE);
273         assertThat(servicesRestored.size()).isEqualTo(1);
274         assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
275         assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
276
277         // check that the service can be deleted
278         this.services.remove(SERVICE);
279         servicesRestored = new Services(this.applicationConfig);
280         assertThat(servicesRestored.size()).isZero();
281     }
282
283     @Test
284     void testAddingRicFromConfiguration() throws Exception {
285         // Test adding the RIC from configuration
286
287         final String RIC = "ric1";
288         final String TYPE = "type123";
289         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
290         nearRtRicPolicyTypes.put(createPolicyType(TYPE));
291         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
292
293         putService("service");
294
295         refreshConfigTask.handleUpdatedRicConfig( //
296                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
297                 .block();
298         waitForRicState(RIC, RicState.AVAILABLE);
299
300         // Test that the type has been synched
301         Ric addedRic = this.rics.getRic(RIC);
302         assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
303         assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
304
305         // Check that a service callback for the AVAILABLE RIC is invoked
306         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
307         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
308         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
309         assertThat(callbackInfo.ricId).isEqualTo(RIC);
310         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
311     }
312
313     @Test
314     void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
315         putService("service");
316
317         final String RIC = "NonRespondingRic";
318         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
319         doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
320
321         refreshConfigTask.handleUpdatedRicConfig( //
322                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
323                 .block();
324
325         waitForRicState(RIC, RicState.UNAVAILABLE);
326
327         // Check that no service callback for the UNAVAILABLE RIC is invoked
328         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
329         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
330
331         // Run a synch and check that the AVAILABLE notification is received
332         a1ClientFactory.reset();
333         supervision.checkAllRics();
334         waitForRicState(RIC, RicState.AVAILABLE);
335
336         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
337     }
338
339     @Test
340     void testTrustValidation() {
341         addRic("ric1");
342
343         String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
344         assertThat(rsp).contains("ric1");
345
346     }
347
348     @Test
349     void testGetRics() throws Exception {
350         addRic("ric1");
351         this.addPolicyType("type1", "ric1");
352         String url = "/rics?policytype_id=type1";
353         String rsp = restClient().get(url).block();
354         assertThat(rsp).contains("ric1");
355
356         // nameless type for ORAN A1 1.1
357         addRic("ric2");
358         this.addPolicyType("", "ric2");
359         url = "/rics?policytype_id=";
360         rsp = restClient().get(url).block();
361         assertThat(rsp).contains("ric2") //
362                 .doesNotContain("ric1") //
363                 .contains("AVAILABLE");
364
365         // All RICs
366         rsp = restClient().get("/rics").block();
367         assertThat(rsp).contains("ric2") //
368                 .contains("ric1");
369
370         // Non existing policy type
371         url = "/rics?policytype_id=XXXX";
372         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
373     }
374
375     @Test
376     void testSynchronization() throws Exception {
377         // Two polictypes will be put in the NearRT RICs
378         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
379         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
380         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
381         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
382
383         // One type and one instance added to the Policy Management Service's storage
384         final String ric1Name = "ric1";
385         Ric ric1 = addRic(ric1Name);
386         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
387         Ric ric2 = addRic("ric2");
388
389         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
390         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
391
392         String policyId = "policyId";
393         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
394         supervision.checkAllRics(); // The created policy should be put in the RIC
395
396         // Wait until synch is completed
397         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
398         waitForRicState(ric1Name, RicState.AVAILABLE);
399         waitForRicState("ric2", RicState.AVAILABLE);
400
401         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
402         assertThat(ricPolicies.size()).isEqualTo(1);
403         Policy ricPolicy = ricPolicies.get(policyId);
404         assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
405
406         // Both types should be in the Policy Management Service's storage after the
407         // synch
408         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
409         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
410     }
411
412     @Test
413     void testGetRic() throws Exception {
414         String ricId = "ric1";
415         String managedElementId = "kista_1";
416         addRic(ricId, managedElementId);
417
418         String url = "/rics/ric?managed_element_id=" + managedElementId;
419         String rsp = restClient().get(url).block();
420         RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
421         assertThat(ricInfo.ricId).isEqualTo(ricId);
422
423         url = "/rics/ric?ric_id=" + ricId;
424         rsp = restClient().get(url).block();
425         ricInfo = gson.fromJson(rsp, RicInfo.class);
426         assertThat(ricInfo.ricId).isEqualTo(ricId);
427
428         // test GET RIC for ManagedElement that does not exist
429         url = "/rics/ric?managed_element_id=" + "junk";
430         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
431
432         url = "/rics/ric";
433         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
434     }
435
436     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
437             boolean isTransient, String statusNotificationUri) {
438         PolicyInfo info = new PolicyInfo();
439         info.policyId = policyInstanceId;
440         info.policyTypeId = policyTypeName;
441         info.ricId = ricId;
442         info.serviceId = serviceName;
443         info.policyData = gson.fromJson(jsonString(), Object.class);
444
445         if (isTransient) {
446             info.isTransient = isTransient;
447         }
448         info.statusNotificationUri = statusNotificationUri;
449         return gson.toJson(info);
450     }
451
452     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
453         return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false, "statusUri");
454     }
455
456     @Test
457     void testPutPolicy() throws Exception {
458         String serviceName = "service.1";
459         String ricId = "ric.1";
460         String policyTypeName = "type1_1.2.3";
461         String policyInstanceId = "instance_1.2.3";
462
463         putService(serviceName);
464         addPolicyType(policyTypeName, ricId);
465
466         // PUT a transient policy
467         String url = "/policies";
468         String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true, "statusNotif");
469         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
470
471         restClient().put(url, policyBody).block();
472
473         Policy policy = policies.getPolicy(policyInstanceId);
474         assertThat(policy).isNotNull();
475         assertThat(policy.getId()).isEqualTo(policyInstanceId);
476         assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
477         assertThat(policy.getRic().id()).isEqualTo(ricId);
478         assertThat(policy.isTransient()).isTrue();
479
480         // Put a non transient policy
481         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
482         restClient().put(url, policyBody).block();
483         policy = policies.getPolicy(policyInstanceId);
484         assertThat(policy.isTransient()).isFalse();
485
486         url = "/policy-instances";
487         String rsp = restClient().get(url).block();
488         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
489
490         url = "/policies/" + policyInstanceId;
491         rsp = restClient().get(url).block();
492         assertThat(rsp).contains(policyBody);
493
494         // Test of error codes
495         url = "/policies";
496         policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
497         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
498
499         policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
500         addPolicyType(policyTypeName + "XX", "otherRic");
501         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
502
503         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
504         this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
505         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
506         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
507     }
508
509     @Test
510     void testPutPolicy_NoServiceNoStatusUri() throws Exception {
511         String ricId = "ric.1";
512         String policyTypeName = "type1_1.2.3";
513         String policyInstanceId = "instance_1.2.3";
514
515         addPolicyType(policyTypeName, ricId);
516
517         // PUT a transient policy
518         String url = "/policies";
519         String policyBody = putPolicyBody(null, ricId, policyTypeName, policyInstanceId, true, null);
520         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
521
522         restClient().put(url, policyBody).block();
523
524         Policy policy = policies.getPolicy(policyInstanceId);
525         assertThat(policy).isNotNull();
526         assertThat(policy.getOwnerServiceId()).isBlank();
527         assertThat(policy.getStatusNotificationUri()).isBlank();
528     }
529
530     @Test
531     /**
532      * Test that HttpStatus and body from failing REST call to A1 is passed on to
533      * the caller.
534      *
535      * @throws ServiceException
536      */
537     void testErrorFromRic() throws ServiceException {
538         putService("service1");
539         addPolicyType("type1", "ric1");
540
541         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
542         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
543         String responseBody = "Refused";
544         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
545
546         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
547                 responseBodyBytes, StandardCharsets.UTF_8, null);
548         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
549
550         // PUT Policy
551         String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
552         String url = "/policies";
553         testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
554
555         // DELETE POLICY
556         this.addPolicy("instance1", "type1", "service1", "ric1");
557         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
558         testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
559
560     }
561
562     @Test
563     void testPutTypelessPolicy() throws Exception {
564         putService("service1");
565         addPolicyType("", "ric1");
566         String body = putPolicyBody("service1", "ric1", "", "id1");
567         restClient().put("/policies", body).block();
568
569         String rsp = restClient().get("/policy-instances").block();
570         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
571         assertThat(info.policies).hasSize(1);
572         PolicyInfo policyInfo = info.policies.iterator().next();
573         assertThat(policyInfo.policyId).isEqualTo("id1");
574         assertThat(policyInfo.policyTypeId).isEmpty();
575     }
576
577     @Test
578     void testUpdateService() throws Exception {
579         this.addRic("ric1");
580         this.addPolicy("p", "type1", "", "ric1");
581
582         String url = "/policies?service_id=";
583         String resp = restClient().get(url).block();
584         assertThat(resp).contains("[\"p\"]");
585
586         this.addPolicy("p", "type1", "service", "ric1");
587         url = "/policies?service_id=";
588         resp = restClient().get(url).block();
589         assertThat(resp).contains("[]");
590
591         url = "/policies?service_id=service";
592         resp = restClient().get(url).block();
593         assertThat(resp).contains("[\"p\"]");
594     }
595
596     @Test
597     void testRefuseToUpdatePolicy() throws Exception {
598         // Test that only the json can be changed for a already created policy
599         // In this case service is attempted to be changed
600         this.addRic("ric1");
601         this.addRic("ricXXX");
602         this.addPolicy("instance1", "type1", "service1", "ric1");
603         this.addPolicy("instance2", "type1", "service1", "ricXXX");
604
605         // Try change ric1 -> ricXXX
606         String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
607         testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
608     }
609
610     @Test
611     void testGetPolicy() throws Exception {
612         String url = "/policies/id";
613         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
614         {
615             String rsp = restClient().get(url).block();
616             PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
617             String policyStr = gson.toJson(info.policyData);
618             assertThat(policyStr).isEqualTo(policy.getJson());
619         }
620         {
621             policies.remove(policy);
622             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
623         }
624     }
625
626     @Test
627     void testDeletePolicy() throws Exception {
628         String policyId = "id.1";
629         addPolicy(policyId, "typeName", "service1", "ric1");
630         assertThat(policies.size()).isEqualTo(1);
631
632         String url = "/policies/" + policyId;
633         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
634
635         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
636         assertThat(policies.size()).isZero();
637
638         // Delete a non existing policy
639         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
640     }
641
642     @Test
643     void testGetPolicyType() throws Exception {
644         String typeId = "AC.D";
645         addPolicyType(typeId, "ric1");
646
647         waitForRicState("ric1", RicState.AVAILABLE);
648
649         String url = "/policy-types/" + typeId;
650
651         String rsp = this.restClient().get(url).block();
652
653         PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
654         assertThat(info.schema).isNotNull();
655
656         // Get non existing schema
657         url = "/policy-types/JUNK";
658         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
659     }
660
661     String createPolicyTypesJson(String... types) {
662         List<String> list = new ArrayList<>();
663         Collections.addAll(list, types);
664         PolicyTypeIdList ids = new PolicyTypeIdList(list);
665         return gson.toJson(ids);
666     }
667
668     @Test
669     void testGetPolicyTypes() throws Exception {
670         String TYPE_ID_1 = "A_type1_1.9.0";
671         String TYPE_ID_2 = "A_type1_2.0.0";
672         String TYPE_ID_3 = "A_type1_1.5.0";
673         String TYPE_ID_4 = "type3_1.9.0";
674         addPolicyType(TYPE_ID_1, "ric1");
675         addPolicyType(TYPE_ID_2, "ric2");
676         addPolicyType(TYPE_ID_3, "ric2");
677         addPolicyType(TYPE_ID_4, "ric2");
678
679         addPolicyType("junk", "ric2");
680         addPolicyType("junk_a.b.c", "ric2");
681
682         String url = "/policy-types";
683         String rsp = restClient().get(url).block();
684         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
685
686         url = "/policy-types?ric_id=ric1";
687         rsp = restClient().get(url).block();
688         String expResp = createPolicyTypesJson(TYPE_ID_1);
689         assertThat(rsp).isEqualTo(expResp);
690
691         // Get policy types for non existing RIC
692         url = "/policy-types?ric_id=ric1XXX";
693         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
694
695         // All types with a type_name
696         url = "/policy-types?type_name=A_type1";
697         rsp = restClient().get(url).block();
698         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
699
700         // All types compatible with type1_1.5.0 (which is type1_1.9.0)
701         url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
702         rsp = restClient().get(url).block();
703         expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
704         assertThat(rsp).isEqualTo(expResp);
705
706         url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
707         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
708
709         url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
710         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
711
712         url = "/policy-types?compatible_with_version=1.5.0";
713         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
714     }
715
716     @Test
717     void testGetPolicyInstances() throws Exception {
718         addPolicy("id1", "type1", "service1");
719
720         String url = "/policy-instances";
721         String rsp = restClient().get(url).block();
722         logger.info(rsp);
723         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
724         assertThat(info.policies).hasSize(1);
725         PolicyInfo policyInfo = info.policies.iterator().next();
726         assert (policyInfo.validate());
727         assertThat(policyInfo.policyId).isEqualTo("id1");
728         assertThat(policyInfo.policyTypeId).isEqualTo("type1");
729         assertThat(policyInfo.serviceId).isEqualTo("service1");
730     }
731
732     @Test
733     void testGetPolicyInstancesFilter() throws Exception {
734         addPolicy("id1", "type1", "service1");
735         addPolicy("id2", "type1", "service2");
736         addPolicy("id3", "type2", "service1");
737         addPolicy("id4", "type1_1.0.0", "service1");
738
739         String url = "/policy-instances?policytype_id=type1";
740         String rsp = restClient().get(url).block();
741         logger.info(rsp);
742         assertThat(rsp).contains("id1") //
743                 .contains("id2") //
744                 .doesNotContain("id3");
745
746         url = "/policy-instances?policytype_id=type1&service_id=service2";
747         rsp = restClient().get(url).block();
748         logger.info(rsp);
749         assertThat(rsp).doesNotContain("id1") //
750                 .contains("id2") //
751                 .doesNotContain("id3");
752
753         url = "/policy-instances?type_name=type1";
754         rsp = restClient().get(url).block();
755         assertThat(rsp).contains("id1") //
756                 .contains("id2") //
757                 .doesNotContain("id3") //
758                 .contains("id4");
759
760         // Test get policies for non existing type
761         url = "/policy-instances?policytype_id=type1XXX";
762         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
763
764         // Test get policies for non existing RIC
765         url = "/policy-instances?ric_id=XXX";
766         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
767     }
768
769     @Test
770     void testGetPolicyIdsFilter() throws Exception {
771         addPolicy("id1", "type1", "service1", "ric1");
772         addPolicy("id2", "type1", "service2", "ric1");
773         addPolicy("id3", "type2", "service1", "ric1");
774         addPolicy("id4", "type1_1.0.0", "service1");
775
776         String url = "/policies?policytype_id=type1";
777         String rsp = restClient().get(url).block();
778         logger.info(rsp);
779         assertThat(rsp).contains("id1") //
780                 .contains("id2") //
781                 .doesNotContain("id3");
782
783         url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
784         rsp = restClient().get(url).block();
785         PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
786         assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
787
788         url = "/policies?policytype_name=type1&service_id=service1";
789         rsp = restClient().get(url).block();
790         assertThat(rsp).contains("id1") //
791                 .contains("id3") //
792                 .contains("id4");
793         assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
794
795         // Test get policy ids for non existing type
796         url = "/policies?policytype_id=type1XXX";
797         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
798
799         // Test get policy ids for non existing RIC
800         url = "/policies?ric_id=XXX";
801         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
802     }
803
804     @Test
805     void testPutAndGetService() throws Exception {
806         // PUT
807         String serviceName = "ac.dc";
808         putService(serviceName, 0, HttpStatus.CREATED);
809         putService(serviceName, 0, HttpStatus.OK);
810
811         // GET one service
812         String url = "/services?service_id=" + serviceName;
813         String rsp = restClient().get(url).block();
814         ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
815         assertThat(info.statusList).hasSize(1);
816         ServiceStatus status = info.statusList.iterator().next();
817         assertThat(status.keepAliveIntervalSeconds).isZero();
818         assertThat(status.serviceId).isEqualTo(serviceName);
819
820         // GET (all)
821         url = "/services";
822         rsp = restClient().get(url).block();
823         assertThat(rsp).as("Response contains service name").contains(serviceName);
824         logger.info(rsp);
825
826         // Keep alive
827         url = "/services/" + serviceName + "/keepalive";
828         ResponseEntity<?> entity = restClient().putForEntity(url).block();
829         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
830
831         // DELETE service
832         assertThat(services.size()).isEqualTo(1);
833         url = "/services/" + serviceName;
834         restClient().delete(url).block();
835         assertThat(services.size()).isZero();
836
837         // Keep alive, no registered service
838         testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
839
840         // PUT service with bad payload
841         testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
842         testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
843         testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
844                 false);
845         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
846                 HttpStatus.BAD_REQUEST, false);
847
848         // GET non existing service
849         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
850     }
851
852     @Test
853     void testServiceSupervision() throws Exception {
854         putService("service1", 1, HttpStatus.CREATED);
855         addPolicyType("type1", "ric1");
856
857         String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
858         restClient().put("/policies", policyBody).block();
859
860         assertThat(policies.size()).isEqualTo(1);
861         assertThat(services.size()).isEqualTo(1);
862
863         // Timeout after ~1 second
864         await().untilAsserted(() -> assertThat(policies.size()).isZero());
865         assertThat(services.size()).isZero();
866     }
867
868     @Test
869     void testGetPolicyStatus() throws Exception {
870         addPolicy("id", "typeName", "service1", "ric1");
871         assertThat(policies.size()).isEqualTo(1);
872
873         String url = "/policies/id/status";
874         String rsp = restClient().get(url).block();
875         PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
876         assertThat(info.status).isEqualTo("OK");
877
878         // GET non existing policy status
879         url = "/policies/XXX/status";
880         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
881
882         // GET STATUS, the NearRT RIC returns error
883         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
884         url = "/policies/id/status";
885         WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
886         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
887         rsp = restClient().get(url).block();
888         info = gson.fromJson(rsp, PolicyStatusInfo.class);
889         assertThat(info.status).hasToString("{}");
890     }
891
892     @Test
893     void testGetServiceStatus() throws Exception {
894         String url = "/status";
895         String rsp = restClient().get(url).block();
896         assertThat(rsp).contains("hunky dory");
897
898         rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
899         assertThat(rsp).isEqualTo("hunky dory");
900     }
901
902     @Test
903     void testServiceNotification() throws Exception {
904
905         final String AUTH_TOKEN = "testToken";
906         Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
907         Files.write(authFile, AUTH_TOKEN.getBytes());
908         this.securityContext.setAuthTokenFilePath(authFile);
909
910         putService("junkService");
911         Service junkService = this.services.get("junkService");
912         junkService.setCallbackUrl("https://junk");
913         putService("service");
914
915         Ric ric = addRic("ric1");
916         ric.setState(Ric.RicState.UNAVAILABLE);
917         supervision.checkAllRics();
918         waitForRicState("ric1", RicState.AVAILABLE);
919
920         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
921         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
922         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
923         assertThat(callbackInfo.ricId).isEqualTo("ric1");
924         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
925
926         var headers = receivedCallbacks.receivedHeaders.get(0);
927         assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
928
929         Files.delete(authFile);
930     }
931
932     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
933         addRic(ric);
934         Policy policy = Policy.builder() //
935                 .id(id) //
936                 .json(jsonString()) //
937                 .ownerServiceId(service) //
938                 .ric(rics.getRic(ric)) //
939                 .type(addPolicyType(typeName, ric)) //
940                 .lastModified(Instant.now()) //
941                 .isTransient(false) //
942                 .statusNotificationUri("/policy-status?id=XXX") //
943                 .build();
944         policies.put(policy);
945         return policy;
946     }
947
948     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
949         return addPolicy(id, typeName, service, "ric");
950     }
951
952     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
953         String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
954         return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
955     }
956
957     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
958         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
959
960         String json = gson.toJson(service);
961         return json;
962     }
963
964     private void putService(String name) {
965         putService(name, 0, null);
966     }
967
968     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
969         String url = "/services";
970         String body = createServiceJson(name, keepAliveIntervalSeconds);
971         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
972         if (expectedStatus != null) {
973             assertEquals(expectedStatus, resp.getStatusCode(), "");
974         }
975     }
976
977     private String jsonString() {
978         return "{\"servingCellNrcgi\":\"1\"}";
979     }
980
981     @Test
982     void testConcurrency() throws Exception {
983         logger.info("Concurrency test starting");
984         final Instant startTime = Instant.now();
985         List<Thread> threads = new ArrayList<>();
986         List<ConcurrencyTestRunnable> tests = new ArrayList<>();
987         a1ClientFactory.setResponseDelay(Duration.ofMillis(2));
988         addRic("ric");
989         addPolicyType("type1", "ric");
990         addPolicyType("type2", "ric");
991
992         final String NON_RESPONDING_RIC = "NonRespondingRic";
993         Ric nonRespondingRic = addRic(NON_RESPONDING_RIC);
994         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(NON_RESPONDING_RIC);
995         a1Client.setErrorInject("errorInject");
996
997         for (int i = 0; i < 10; ++i) {
998             AsyncRestClient restClient = restClient();
999             ConcurrencyTestRunnable test =
1000                     new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
1001             Thread thread = new Thread(test, "TestThread_" + i);
1002             thread.start();
1003             threads.add(thread);
1004             tests.add(test);
1005         }
1006         for (Thread t : threads) {
1007             t.join();
1008         }
1009         for (ConcurrencyTestRunnable test : tests) {
1010             assertThat(test.isFailed()).isFalse();
1011         }
1012         assertThat(policies.size()).isZero();
1013         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
1014
1015         assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
1016         nonRespondingRic.setState(RicState.AVAILABLE);
1017     }
1018
1019     private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
1020         WebClientConfig config = this.applicationConfig.getWebClientConfig();
1021         config = WebClientConfig.builder() //
1022                 .keyStoreType(config.getKeyStoreType()) //
1023                 .keyStorePassword(config.getKeyStorePassword()) //
1024                 .keyStore(config.getKeyStore()) //
1025                 .keyPassword(config.getKeyPassword()) //
1026                 .isTrustStoreUsed(useTrustValidation) //
1027                 .trustStore(config.getTrustStore()) //
1028                 .trustStorePassword(config.getTrustStorePassword()) //
1029                 .httpProxyConfig(config.getHttpProxyConfig()) //
1030                 .build();
1031
1032         AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
1033         return f.createRestClientNoHttpProxy(baseUrl);
1034
1035     }
1036
1037     private String baseUrl() {
1038         return "https://localhost:" + port;
1039     }
1040
1041     private AsyncRestClient restClient(boolean useTrustValidation) {
1042         return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
1043     }
1044
1045     private AsyncRestClient restClient() {
1046         return restClient(false);
1047     }
1048
1049     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
1050         testErrorCode(request, expStatus, "", true);
1051     }
1052
1053     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
1054         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
1055     }
1056
1057     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
1058         testErrorCode(request, expStatus, responseContains, true);
1059     }
1060
1061     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
1062             boolean expectApplicationProblemJsonMediaType) {
1063         StepVerifier.create(request) //
1064                 .expectSubscription() //
1065                 .expectErrorMatches(
1066                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1067                 .verify();
1068     }
1069
1070     private void waitForRicState(String ricId, RicState state) throws ServiceException {
1071         Ric ric = rics.getRic(ricId);
1072         await().untilAsserted(() -> state.equals(ric.getState()));
1073     }
1074
1075     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1076             boolean expectApplicationProblemJsonMediaType) {
1077         assertTrue(throwable instanceof WebClientResponseException);
1078         WebClientResponseException responseException = (WebClientResponseException) throwable;
1079         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1080         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1081         if (expectApplicationProblemJsonMediaType) {
1082             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1083         }
1084         return true;
1085     }
1086
1087     private MockA1Client getA1Client(String ricId) throws ServiceException {
1088         return a1ClientFactory.getOrCreateA1Client(ricId);
1089     }
1090
1091     private PolicyType createPolicyType(String policyTypeName) {
1092         return PolicyType.builder() //
1093                 .id(policyTypeName) //
1094                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1095                 .build();
1096     }
1097
1098     private PolicyType addPolicyType(String policyTypeName, String ricId) {
1099         PolicyType type = createPolicyType(policyTypeName);
1100         policyTypes.put(type);
1101         addRic(ricId).addSupportedPolicyType(type);
1102         return type;
1103     }
1104
1105     private Ric addRic(String ricId) {
1106         return addRic(ricId, null);
1107     }
1108
1109     private RicConfig ricConfig(String ricId, String managedElement) {
1110         List<String> mes = new ArrayList<>();
1111         if (managedElement != null) {
1112             mes.add(managedElement);
1113         }
1114         return RicConfig.builder() //
1115                 .ricId(ricId) //
1116                 .baseUrl(ricId) //
1117                 .managedElementIds(mes) //
1118                 .build();
1119     }
1120
1121     private Ric addRic(String ricId, String managedElement) {
1122         if (rics.get(ricId) != null) {
1123             return rics.get(ricId);
1124         }
1125
1126         RicConfig conf = ricConfig(ricId, managedElement);
1127         Ric ric = new Ric(conf);
1128         ric.setState(Ric.RicState.AVAILABLE);
1129         this.rics.put(ric);
1130         return ric;
1131     }
1132
1133 }