2 * ========================LICENSE_START=================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
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;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.io.PrintStream;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.time.Duration;
41 import java.time.Instant;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
46 import org.json.JSONObject;
47 import org.junit.jupiter.api.AfterEach;
48 import org.junit.jupiter.api.Test;
49 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
50 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
51 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
52 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
53 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
54 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
55 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
56 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
57 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
58 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
70 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
71 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
72 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
73 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
74 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77 import org.springframework.beans.factory.annotation.Autowired;
78 import org.springframework.boot.test.context.SpringBootTest;
79 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
80 import org.springframework.boot.test.context.TestConfiguration;
81 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
82 import org.springframework.boot.web.server.LocalServerPort;
83 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
84 import org.springframework.context.ApplicationContext;
85 import org.springframework.context.annotation.Bean;
86 import org.springframework.http.HttpStatus;
87 import org.springframework.http.MediaType;
88 import org.springframework.http.ResponseEntity;
89 import org.springframework.test.context.TestPropertySource;
90 import org.springframework.web.reactive.function.client.WebClientResponseException;
92 import reactor.core.publisher.Mono;
93 import reactor.test.StepVerifier;
94 import reactor.util.annotation.Nullable;
96 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
97 @TestPropertySource(properties = { //
98 "server.ssl.key-store=./config/keystore.jks", //
99 "app.webclient.trust-store=./config/truststore.jks", //
100 "app.webclient.trust-store-used=true", //
101 "app.vardata-directory=./target/testdata", //
104 class ApplicationTest {
105 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
108 ApplicationContext context;
114 private Policies policies;
117 private PolicyTypes policyTypes;
120 MockA1ClientFactory a1ClientFactory;
123 RicSupervision supervision;
126 ApplicationConfig applicationConfig;
132 RappSimulatorController rAppSimulator;
135 RefreshConfigTask refreshConfigTask;
138 SecurityContext securityContext;
140 private static Gson gson = new GsonBuilder().create();
143 * Overrides the BeanFactory.
146 static class TestBeanFactory {
149 A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
150 return new MockA1ClientFactory(appConfig, types);
154 public ServiceSupervision getServiceSupervision(@Autowired Services services,
155 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
156 Duration checkInterval = Duration.ofMillis(1);
157 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
161 public ServletWebServerFactory servletContainer() {
162 return new TomcatServletWebServerFactory();
175 a1ClientFactory.reset();
176 this.rAppSimulator.getTestResults().clear();
177 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
178 this.securityContext.setAuthTokenFilePath(null);
182 void verifyNoRicLocks() {
183 for (Ric ric : this.rics.getRics()) {
184 Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "");
185 grant.unlockBlocking();
186 assertThat(ric.getLock().getLockCounter()).isZero();
187 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
192 void generateApiDoc() throws IOException {
193 String url = "https://localhost:" + this.port + "/v3/api-docs";
194 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
195 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
196 JSONObject jsonObj = new JSONObject(resp.getBody());
197 assertThat(jsonObj.remove("servers")).isNotNull();
199 String indented = (jsonObj).toString(4);
200 String docDir = "api/";
201 Files.createDirectories(Paths.get(docDir));
202 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
208 void testPersistencyPolicies() throws ServiceException {
209 Ric ric = this.addRic("ric1");
210 PolicyType type = this.addPolicyType("type1", ric.id());
212 final int noOfPolicies = 100;
213 for (int i = 0; i < noOfPolicies; ++i) {
214 addPolicy("id" + i, type.getId(), "service", ric.id());
218 Policies policies = new Policies(this.applicationConfig);
219 policies.restoreFromDatabase(ric, this.policyTypes);
220 assertThat(policies.size()).isEqualTo(noOfPolicies);
224 restClient().delete("/policies/id2").block();
225 Policies policies = new Policies(this.applicationConfig);
226 policies.restoreFromDatabase(ric, this.policyTypes);
227 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
232 void testPersistencyPolicyTypes() throws ServiceException {
233 Ric ric = this.addRic("ric1");
234 this.addPolicyType("type1", ric.id());
235 PolicyTypes types = new PolicyTypes(this.applicationConfig);
236 assertThat(types.size()).isEqualTo(1);
240 void testPersistencyService() throws ServiceException {
241 final String SERVICE = "serviceName";
242 putService(SERVICE, 1234, HttpStatus.CREATED);
243 assertThat(this.services.size()).isEqualTo(1);
244 Service service = this.services.getService(SERVICE);
246 Services servicesRestored = new Services(this.applicationConfig);
247 Service serviceRestored = servicesRestored.getService(SERVICE);
248 assertThat(servicesRestored.size()).isEqualTo(1);
249 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
250 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
252 // check that the service can be deleted
253 this.services.remove(SERVICE);
254 servicesRestored = new Services(this.applicationConfig);
255 assertThat(servicesRestored.size()).isZero();
259 void testAddingRicFromConfiguration() throws Exception {
260 // Test adding the RIC from configuration
262 final String RIC = "ric1";
263 final String TYPE = "type123";
264 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
265 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
266 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
268 putService("service");
270 refreshConfigTask.handleUpdatedRicConfig( //
271 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
273 waitForRicState(RIC, RicState.AVAILABLE);
275 // Test that the type has been synched
276 Ric addedRic = this.rics.getRic(RIC);
277 assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
278 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
280 // Check that a service callback for the AVAILABLE RIC is invoked
281 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
282 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
283 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
284 assertThat(callbackInfo.ricId).isEqualTo(RIC);
285 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
289 void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
290 putService("service");
292 final String RIC = "NonRespondingRic";
293 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
294 doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
296 refreshConfigTask.handleUpdatedRicConfig( //
297 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
300 waitForRicState(RIC, RicState.UNAVAILABLE);
302 // Check that no service callback for the UNAVAILABLE RIC is invoked
303 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
304 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
306 // Run a synch and check that the AVAILABLE notification is received
307 a1ClientFactory.reset();
308 supervision.checkAllRics();
309 waitForRicState(RIC, RicState.AVAILABLE);
311 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
315 void testTrustValidation() {
318 String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
319 assertThat(rsp).contains("ric1");
324 void testGetRics() throws Exception {
326 this.addPolicyType("type1", "ric1");
327 String url = "/rics?policytype_id=type1";
328 String rsp = restClient().get(url).block();
329 assertThat(rsp).contains("ric1");
331 // nameless type for ORAN A1 1.1
333 this.addPolicyType("", "ric2");
334 url = "/rics?policytype_id=";
335 rsp = restClient().get(url).block();
336 assertThat(rsp).contains("ric2") //
337 .doesNotContain("ric1") //
338 .contains("AVAILABLE");
341 rsp = restClient().get("/rics").block();
342 assertThat(rsp).contains("ric2") //
345 // Non existing policy type
346 url = "/rics?policytype_id=XXXX";
347 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
351 void testSynchronization() throws Exception {
352 // Two polictypes will be put in the NearRT RICs
353 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
354 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
355 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
356 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
358 // One type and one instance added to the Policy Management Service's storage
359 final String ric1Name = "ric1";
360 Ric ric1 = addRic(ric1Name);
361 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
362 Ric ric2 = addRic("ric2");
364 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
365 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
367 String policyId = "policyId";
368 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
369 supervision.checkAllRics(); // The created policy should be put in the RIC
371 // Wait until synch is completed
372 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
373 waitForRicState(ric1Name, RicState.AVAILABLE);
374 waitForRicState("ric2", RicState.AVAILABLE);
376 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
377 assertThat(ricPolicies.size()).isEqualTo(1);
378 Policy ricPolicy = ricPolicies.get(policyId);
379 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
381 // Both types should be in the Policy Management Service's storage after the
383 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
384 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
388 void testGetRic() throws Exception {
389 String ricId = "ric1";
390 String managedElementId = "kista_1";
391 addRic(ricId, managedElementId);
393 String url = "/rics/ric?managed_element_id=" + managedElementId;
394 String rsp = restClient().get(url).block();
395 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
396 assertThat(ricInfo.ricId).isEqualTo(ricId);
398 url = "/rics/ric?ric_id=" + ricId;
399 rsp = restClient().get(url).block();
400 ricInfo = gson.fromJson(rsp, RicInfo.class);
401 assertThat(ricInfo.ricId).isEqualTo(ricId);
403 // test GET RIC for ManagedElement that does not exist
404 url = "/rics/ric?managed_element_id=" + "junk";
405 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
408 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
411 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
412 boolean isTransient) {
413 PolicyInfo info = new PolicyInfo();
414 info.policyId = policyInstanceId;
415 info.policyTypeId = policyTypeName;
417 info.serviceId = serviceName;
418 info.policyData = gson.fromJson(jsonString(), Object.class);
421 info.isTransient = isTransient;
423 info.statusNotificationUri = "statusNotificationUri";
424 return gson.toJson(info);
427 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
428 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
432 void testPutPolicy() throws Exception {
433 String serviceName = "service.1";
434 String ricId = "ric.1";
435 String policyTypeName = "type1_1.2.3";
436 String policyInstanceId = "instance_1.2.3";
438 putService(serviceName);
439 addPolicyType(policyTypeName, ricId);
441 // PUT a transient policy
442 String url = "/policies";
443 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
444 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
446 restClient().put(url, policyBody).block();
448 Policy policy = policies.getPolicy(policyInstanceId);
449 assertThat(policy).isNotNull();
450 assertThat(policy.getId()).isEqualTo(policyInstanceId);
451 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
452 assertThat(policy.getRic().id()).isEqualTo(ricId);
453 assertThat(policy.isTransient()).isTrue();
455 // Put a non transient policy
456 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
457 restClient().put(url, policyBody).block();
458 policy = policies.getPolicy(policyInstanceId);
459 assertThat(policy.isTransient()).isFalse();
461 url = "/policy-instances";
462 String rsp = restClient().get(url).block();
463 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
465 url = "/policies/" + policyInstanceId;
466 rsp = restClient().get(url).block();
467 assertThat(rsp).contains(policyBody);
469 // Test of error codes
471 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
472 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
474 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
475 addPolicyType(policyTypeName + "XX", "otherRic");
476 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
478 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
479 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
480 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
481 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
486 * Test that HttpStatus and body from failing REST call to A1 is passed on to
489 * @throws ServiceException
491 void testErrorFromRic() throws ServiceException {
492 putService("service1");
493 addPolicyType("type1", "ric1");
495 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
496 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
497 String responseBody = "Refused";
498 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
500 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
501 responseBodyBytes, StandardCharsets.UTF_8, null);
502 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
505 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
506 String url = "/policies";
507 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
510 this.addPolicy("instance1", "type1", "service1", "ric1");
511 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
512 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
517 void testPutTypelessPolicy() throws Exception {
518 putService("service1");
519 addPolicyType("", "ric1");
520 String body = putPolicyBody("service1", "ric1", "", "id1");
521 restClient().put("/policies", body).block();
523 String rsp = restClient().get("/policy-instances").block();
524 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
525 assertThat(info.policies).hasSize(1);
526 PolicyInfo policyInfo = info.policies.iterator().next();
527 assertThat(policyInfo.policyId).isEqualTo("id1");
528 assertThat(policyInfo.policyTypeId).isEmpty();
532 void testRefuseToUpdatePolicy() throws Exception {
533 // Test that only the json can be changed for a already created policy
534 // In this case service is attempted to be changed
536 this.addRic("ricXXX");
537 this.addPolicy("instance1", "type1", "service1", "ric1");
538 this.addPolicy("instance2", "type1", "service1", "ricXXX");
540 // Try change ric1 -> ricXXX
541 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
542 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
546 void testGetPolicy() throws Exception {
547 String url = "/policies/id";
548 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
550 String rsp = restClient().get(url).block();
551 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
552 String policyStr = gson.toJson(info.policyData);
553 assertThat(policyStr).isEqualTo(policy.getJson());
556 policies.remove(policy);
557 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
562 void testDeletePolicy() throws Exception {
563 String policyId = "id.1";
564 addPolicy(policyId, "typeName", "service1", "ric1");
565 assertThat(policies.size()).isEqualTo(1);
567 String url = "/policies/" + policyId;
568 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
570 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
571 assertThat(policies.size()).isZero();
573 // Delete a non existing policy
574 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
578 void testGetPolicyType() throws Exception {
579 String typeId = "AC.D";
580 addPolicyType(typeId, "ric1");
582 waitForRicState("ric1", RicState.AVAILABLE);
584 String url = "/policy-types/" + typeId;
586 String rsp = this.restClient().get(url).block();
588 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
589 assertThat(info.schema).isNotNull();
591 // Get non existing schema
592 url = "/policy-types/JUNK";
593 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
596 String createPolicyTypesJson(String... types) {
597 List<String> list = new ArrayList<>();
598 Collections.addAll(list, types);
599 PolicyTypeIdList ids = new PolicyTypeIdList(list);
600 return gson.toJson(ids);
604 void testGetPolicyTypes() throws Exception {
605 String TYPE_ID_1 = "A_type1_1.9.0";
606 String TYPE_ID_2 = "A_type1_2.0.0";
607 String TYPE_ID_3 = "A_type1_1.5.0";
608 String TYPE_ID_4 = "type3_1.9.0";
609 addPolicyType(TYPE_ID_1, "ric1");
610 addPolicyType(TYPE_ID_2, "ric2");
611 addPolicyType(TYPE_ID_3, "ric2");
612 addPolicyType(TYPE_ID_4, "ric2");
614 addPolicyType("junk", "ric2");
615 addPolicyType("junk_a.b.c", "ric2");
617 String url = "/policy-types";
618 String rsp = restClient().get(url).block();
619 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
621 url = "/policy-types?ric_id=ric1";
622 rsp = restClient().get(url).block();
623 String expResp = createPolicyTypesJson(TYPE_ID_1);
624 assertThat(rsp).isEqualTo(expResp);
626 // Get policy types for non existing RIC
627 url = "/policy-types?ric_id=ric1XXX";
628 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
630 // All types with a type_name
631 url = "/policy-types?type_name=A_type1";
632 rsp = restClient().get(url).block();
633 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
635 // All types compatible with type1_1.5.0 (which is type1_1.9.0)
636 url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
637 rsp = restClient().get(url).block();
638 expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
639 assertThat(rsp).isEqualTo(expResp);
641 url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
642 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
644 url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
645 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
647 url = "/policy-types?compatible_with_version=1.5.0";
648 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
652 void testGetPolicyInstances() throws Exception {
653 addPolicy("id1", "type1", "service1");
655 String url = "/policy-instances";
656 String rsp = restClient().get(url).block();
658 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
659 assertThat(info.policies).hasSize(1);
660 PolicyInfo policyInfo = info.policies.iterator().next();
661 assert (policyInfo.validate());
662 assertThat(policyInfo.policyId).isEqualTo("id1");
663 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
664 assertThat(policyInfo.serviceId).isEqualTo("service1");
668 void testGetPolicyInstancesFilter() throws Exception {
669 addPolicy("id1", "type1", "service1");
670 addPolicy("id2", "type1", "service2");
671 addPolicy("id3", "type2", "service1");
672 addPolicy("id4", "type1_1.0.0", "service1");
674 String url = "/policy-instances?policytype_id=type1";
675 String rsp = restClient().get(url).block();
677 assertThat(rsp).contains("id1") //
679 .doesNotContain("id3");
681 url = "/policy-instances?policytype_id=type1&service_id=service2";
682 rsp = restClient().get(url).block();
684 assertThat(rsp).doesNotContain("id1") //
686 .doesNotContain("id3");
688 url = "/policy-instances?type_name=type1";
689 rsp = restClient().get(url).block();
690 assertThat(rsp).contains("id1") //
692 .doesNotContain("id3") //
695 // Test get policies for non existing type
696 url = "/policy-instances?policytype_id=type1XXX";
697 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
699 // Test get policies for non existing RIC
700 url = "/policy-instances?ric_id=XXX";
701 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
705 void testGetPolicyIdsFilter() throws Exception {
706 addPolicy("id1", "type1", "service1", "ric1");
707 addPolicy("id2", "type1", "service2", "ric1");
708 addPolicy("id3", "type2", "service1", "ric1");
709 addPolicy("id4", "type1_1.0.0", "service1");
711 String url = "/policies?policytype_id=type1";
712 String rsp = restClient().get(url).block();
714 assertThat(rsp).contains("id1") //
716 .doesNotContain("id3");
718 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
719 rsp = restClient().get(url).block();
720 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
721 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
723 url = "/policies?policytype_name=type1&service_id=service1";
724 rsp = restClient().get(url).block();
725 assertThat(rsp).contains("id1") //
728 assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
730 // Test get policy ids for non existing type
731 url = "/policies?policytype_id=type1XXX";
732 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
734 // Test get policy ids for non existing RIC
735 url = "/policies?ric_id=XXX";
736 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
740 void testPutAndGetService() throws Exception {
742 String serviceName = "ac.dc";
743 putService(serviceName, 0, HttpStatus.CREATED);
744 putService(serviceName, 0, HttpStatus.OK);
747 String url = "/services?service_id=" + serviceName;
748 String rsp = restClient().get(url).block();
749 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
750 assertThat(info.statusList).hasSize(1);
751 ServiceStatus status = info.statusList.iterator().next();
752 assertThat(status.keepAliveIntervalSeconds).isZero();
753 assertThat(status.serviceId).isEqualTo(serviceName);
757 rsp = restClient().get(url).block();
758 assertThat(rsp).as("Response contains service name").contains(serviceName);
762 url = "/services/" + serviceName + "/keepalive";
763 ResponseEntity<?> entity = restClient().putForEntity(url).block();
764 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
767 assertThat(services.size()).isEqualTo(1);
768 url = "/services/" + serviceName;
769 restClient().delete(url).block();
770 assertThat(services.size()).isZero();
772 // Keep alive, no registered service
773 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
775 // PUT service with bad payload
776 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
777 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
778 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
780 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
781 HttpStatus.BAD_REQUEST, false);
783 // GET non existing service
784 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
788 void testServiceSupervision() throws Exception {
789 putService("service1", 1, HttpStatus.CREATED);
790 addPolicyType("type1", "ric1");
792 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
793 restClient().put("/policies", policyBody).block();
795 assertThat(policies.size()).isEqualTo(1);
796 assertThat(services.size()).isEqualTo(1);
798 // Timeout after ~1 second
799 await().untilAsserted(() -> assertThat(policies.size()).isZero());
800 assertThat(services.size()).isZero();
804 void testGetPolicyStatus() throws Exception {
805 addPolicy("id", "typeName", "service1", "ric1");
806 assertThat(policies.size()).isEqualTo(1);
808 String url = "/policies/id/status";
809 String rsp = restClient().get(url).block();
810 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
811 assertThat(info.status).isEqualTo("OK");
813 // GET non existing policy status
814 url = "/policies/XXX/status";
815 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
817 // GET STATUS, the NearRT RIC returns error
818 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
819 url = "/policies/id/status";
820 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
821 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
822 rsp = restClient().get(url).block();
823 info = gson.fromJson(rsp, PolicyStatusInfo.class);
824 assertThat(info.status).hasToString("{}");
828 void testGetServiceStatus() throws Exception {
829 String url = "/status";
830 String rsp = restClient().get(url).block();
831 assertThat(rsp).contains("hunky dory");
833 rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
834 assertThat(rsp).isEqualTo("hunky dory");
838 void testServiceNotification() throws Exception {
840 final String AUTH_TOKEN = "testToken";
841 Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
842 Files.write(authFile, AUTH_TOKEN.getBytes());
843 this.securityContext.setAuthTokenFilePath(authFile);
845 putService("junkService");
846 Service junkService = this.services.get("junkService");
847 junkService.setCallbackUrl("https://junk");
848 putService("service");
850 Ric ric = addRic("ric1");
851 ric.setState(Ric.RicState.UNAVAILABLE);
852 supervision.checkAllRics();
853 waitForRicState("ric1", RicState.AVAILABLE);
855 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
856 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
857 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
858 assertThat(callbackInfo.ricId).isEqualTo("ric1");
859 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
861 var headers = receivedCallbacks.receivedHeaders.get(0);
862 assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
864 Files.delete(authFile);
867 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
869 Policy policy = Policy.builder() //
871 .json(jsonString()) //
872 .ownerServiceId(service) //
873 .ric(rics.getRic(ric)) //
874 .type(addPolicyType(typeName, ric)) //
875 .lastModified(Instant.now()) //
876 .isTransient(false) //
877 .statusNotificationUri("/policy-status?id=XXX") //
879 policies.put(policy);
883 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
884 return addPolicy(id, typeName, service, "ric");
887 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
888 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
889 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
892 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
893 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
895 String json = gson.toJson(service);
899 private void putService(String name) {
900 putService(name, 0, null);
903 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
904 String url = "/services";
905 String body = createServiceJson(name, keepAliveIntervalSeconds);
906 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
907 if (expectedStatus != null) {
908 assertEquals(expectedStatus, resp.getStatusCode(), "");
912 private String jsonString() {
913 return "{\"servingCellNrcgi\":\"1\"}";
917 void testConcurrency() throws Exception {
918 logger.info("Concurrency test starting");
919 final Instant startTime = Instant.now();
920 List<Thread> threads = new ArrayList<>();
921 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
922 a1ClientFactory.setResponseDelay(Duration.ofMillis(2));
924 addPolicyType("type1", "ric");
925 addPolicyType("type2", "ric");
927 final String NON_RESPONDING_RIC = "NonRespondingRic";
928 Ric nonRespondingRic = addRic(NON_RESPONDING_RIC);
929 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(NON_RESPONDING_RIC);
930 a1Client.setErrorInject("errorInject");
932 for (int i = 0; i < 10; ++i) {
933 AsyncRestClient restClient = restClient();
934 ConcurrencyTestRunnable test = new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics,
936 Thread thread = new Thread(test, "TestThread_" + i);
941 for (Thread t : threads) {
944 for (ConcurrencyTestRunnable test : tests) {
945 assertThat(test.isFailed()).isFalse();
947 assertThat(policies.size()).isZero();
948 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
950 assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
951 nonRespondingRic.setState(RicState.AVAILABLE);
954 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
955 WebClientConfig config = this.applicationConfig.getWebClientConfig();
956 config = WebClientConfig.builder() //
957 .keyStoreType(config.getKeyStoreType()) //
958 .keyStorePassword(config.getKeyStorePassword()) //
959 .keyStore(config.getKeyStore()) //
960 .keyPassword(config.getKeyPassword()) //
961 .isTrustStoreUsed(useTrustValidation) //
962 .trustStore(config.getTrustStore()) //
963 .trustStorePassword(config.getTrustStorePassword()) //
964 .httpProxyConfig(config.getHttpProxyConfig()) //
967 AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
968 return f.createRestClientNoHttpProxy(baseUrl);
972 private String baseUrl() {
973 return "https://localhost:" + port;
976 private AsyncRestClient restClient(boolean useTrustValidation) {
977 return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
980 private AsyncRestClient restClient() {
981 return restClient(false);
984 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
985 testErrorCode(request, expStatus, "", true);
988 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
989 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
992 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
993 testErrorCode(request, expStatus, responseContains, true);
996 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
997 boolean expectApplicationProblemJsonMediaType) {
998 StepVerifier.create(request) //
999 .expectSubscription() //
1000 .expectErrorMatches(
1001 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1005 private void waitForRicState(String ricId, RicState state) throws ServiceException {
1006 Ric ric = rics.getRic(ricId);
1007 await().untilAsserted(() -> state.equals(ric.getState()));
1010 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1011 boolean expectApplicationProblemJsonMediaType) {
1012 assertTrue(throwable instanceof WebClientResponseException);
1013 WebClientResponseException responseException = (WebClientResponseException) throwable;
1014 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1015 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1016 if (expectApplicationProblemJsonMediaType) {
1017 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1022 private MockA1Client getA1Client(String ricId) throws ServiceException {
1023 return a1ClientFactory.getOrCreateA1Client(ricId);
1026 private PolicyType createPolicyType(String policyTypeName) {
1027 return PolicyType.builder() //
1028 .id(policyTypeName) //
1029 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1033 private PolicyType addPolicyType(String policyTypeName, String ricId) {
1034 PolicyType type = createPolicyType(policyTypeName);
1035 policyTypes.put(type);
1036 addRic(ricId).addSupportedPolicyType(type);
1040 private Ric addRic(String ricId) {
1041 return addRic(ricId, null);
1044 private RicConfig ricConfig(String ricId, String managedElement) {
1045 List<String> mes = new ArrayList<>();
1046 if (managedElement != null) {
1047 mes.add(managedElement);
1049 return RicConfig.builder() //
1052 .managedElementIds(mes) //
1056 private Ric addRic(String ricId, String managedElement) {
1057 if (rics.get(ricId) != null) {
1058 return rics.get(ricId);
1061 RicConfig conf = ricConfig(ricId, managedElement);
1062 Ric ric = new Ric(conf);
1063 ric.setState(Ric.RicState.AVAILABLE);