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