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