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