From: John Keeney Date: Fri, 20 Mar 2020 08:46:52 +0000 (+0000) Subject: Merge "Renamed things to fit with namechange of RicSynchronizationTask" X-Git-Tag: 2.0.0~109 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=96805287db06a91e569c184cc34ba4e69e4d22b2;hp=a2ad32a98e7a3f32214d3ecd7ca9730e3602d11f;p=nonrtric.git Merge "Renamed things to fit with namechange of RicSynchronizationTask" --- diff --git a/policy-agent/docs/api.yaml b/policy-agent/docs/api.yaml index bd1ba686..3dd78621 100644 --- a/policy-agent/docs/api.yaml +++ b/policy-agent/docs/api.yaml @@ -484,6 +484,46 @@ paths: schema: type: string deprecated: false + /policy_ids: + get: + tags: + - A1 Policy Management + summary: 'Query policies, only IDs returned' + operationId: getPolicyIdsUsingGET + produces: + - '*/*' + parameters: + - name: ric + in: query + description: ric + required: false + type: string + - name: service + in: query + description: service + required: false + type: string + - name: type + in: query + description: type + required: false + type: string + responses: + '200': + description: Policy ids + schema: + type: array + items: + type: string + '401': + description: Unauthorized + '403': + description: Forbidden + '404': + description: RIC or type not found + schema: + type: string + deprecated: false /policy_schema: get: tags: diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java index 4936893c..8715e88d 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java @@ -31,7 +31,6 @@ public interface A1Client { public enum A1ProtocolType { UNKNOWN, // - STD_V1, // STD_V1_1, // version 1.1 OSC_V1, // SDNC_OSC, // diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java index 0efec60a..0be4b31b 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java @@ -66,10 +66,8 @@ public class A1ClientFactory { } A1Client createClient(Ric ric, A1ProtocolType version) { - if (version == A1ProtocolType.STD_V1) { + if (version == A1ProtocolType.STD_V1_1) { return new StdA1ClientVersion1(ric.getConfig()); - } else if (version == A1ProtocolType.STD_V1_1) { - return new StdA1ClientVersion2(ric.getConfig()); } else if (version == A1ProtocolType.OSC_V1) { return new OscA1Client(ric.getConfig()); } else if (version == A1ProtocolType.SDNC_OSC) { @@ -91,8 +89,7 @@ public class A1ClientFactory { private Mono getProtocolVersion(Ric ric) { if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) { - return fetchVersion(createClient(ric, A1ProtocolType.STD_V1)) // - .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1))) // + return fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1)) // .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.OSC_V1))) // .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_OSC))) // .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_ONAP))) // diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java index 06259c7a..804513ad 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java @@ -21,6 +21,7 @@ package org.oransc.policyagent.clients; import java.lang.invoke.MethodHandles; +import java.util.Arrays; import java.util.List; import org.oransc.policyagent.configuration.RicConfig; @@ -31,11 +32,11 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally public class SdncOscA1Client implements A1Client { - private static final String URL_PREFIX = "/A1-ADAPTER-API:"; + private static final String URL_PREFIX = "/A1-ADAPTER-API:"; private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private final String a1ControllerUsername; private final String a1ControllerPassword; private final RicConfig ricConfig; @@ -43,9 +44,7 @@ public class SdncOscA1Client implements A1Client { public SdncOscA1Client(RicConfig ricConfig, String baseUrl, String username, String password) { this(ricConfig, username, password, new AsyncRestClient(baseUrl + "/restconf/operations")); - if (logger.isDebugEnabled()) { - logger.debug("SdncOscA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), baseUrl); - } + logger.debug("SdncOscA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), baseUrl); } public SdncOscA1Client(RicConfig ricConfig, String username, String password, AsyncRestClient restClient) { @@ -57,18 +56,7 @@ public class SdncOscA1Client implements A1Client { @Override public Mono> getPolicyTypeIdentities() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(ricConfig.baseUrl()) // - .build(); - String inputJsonString = JsonHelper.createInputJsonString(inputParams); - logger.debug("POST getPolicyTypeIdentities inputJsonString = {}", inputJsonString); - - return restClient - .postWithAuthHeader(URL_PREFIX + "getPolicyTypeIdentities", inputJsonString, a1ControllerUsername, - a1ControllerPassword) // - .flatMap(response -> JsonHelper.getValueFromResponse(response, "policy-type-id-list")) // - .flatMapMany(JsonHelper::parseJsonArrayOfString) // - .collectList(); + return Mono.just(Arrays.asList("")); } @Override @@ -79,18 +67,7 @@ public class SdncOscA1Client implements A1Client { @Override public Mono getPolicyTypeSchema(String policyTypeId) { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(ricConfig.baseUrl()) // - .policyTypeId(policyTypeId) // - .build(); - String inputJsonString = JsonHelper.createInputJsonString(inputParams); - logger.debug("POST getPolicyType inputJsonString = {}", inputJsonString); - - return restClient - .postWithAuthHeader(URL_PREFIX + "getPolicyType", inputJsonString, a1ControllerUsername, - a1ControllerPassword) // - .flatMap(response -> JsonHelper.getValueFromResponse(response, "policy-type")) // - .flatMap(JsonHelper::extractPolicySchema); + return Mono.just("{}"); } @Override @@ -102,8 +79,6 @@ public class SdncOscA1Client implements A1Client { .policy(policy.json()) // .build(); String inputJsonString = JsonHelper.createInputJsonString(inputParams); - logger.debug("POST putPolicy inputJsonString = {}", inputJsonString); - return restClient .postWithAuthHeader(URL_PREFIX + "putPolicy", inputJsonString, a1ControllerUsername, a1ControllerPassword) .flatMap(response -> JsonHelper.getValueFromResponse(response, "returned-policy")) // @@ -123,7 +98,7 @@ public class SdncOscA1Client implements A1Client { @Override public Mono getProtocolVersion() { - return getPolicyTypeIdentities() // + return getPolicyIdentities() // .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC)); } @@ -147,8 +122,6 @@ public class SdncOscA1Client implements A1Client { .nearRtRicUrl(ricConfig.baseUrl()) // .build(); String inputJsonString = JsonHelper.createInputJsonString(inputParams); - logger.debug("POST getPolicyIdentities inputJsonString = {}", inputJsonString); - return restClient .postWithAuthHeader(URL_PREFIX + "getPolicyIdentities", inputJsonString, a1ControllerUsername, a1ControllerPassword) // @@ -163,8 +136,6 @@ public class SdncOscA1Client implements A1Client { .build(); String inputJsonString = JsonHelper.createInputJsonString(inputParams); - logger.debug("POST deletePolicy inputJsonString = {}", inputJsonString); - return restClient.postWithAuthHeader(URL_PREFIX + "deletePolicy", inputJsonString, a1ControllerUsername, a1ControllerPassword); } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1ClientVersion1.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1ClientVersion1.java index f8ea2ee9..7b194b44 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1ClientVersion1.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1ClientVersion1.java @@ -20,6 +20,7 @@ package org.oransc.policyagent.clients; +import java.util.Arrays; import java.util.List; import org.oransc.policyagent.configuration.RicConfig; @@ -30,29 +31,11 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class StdA1ClientVersion1 implements A1Client { - private static final String URL_PREFIX = "/A1-P/v1"; - - private static final String POLICY_TYPES_URI = "/policytypes"; - private static final String POLICY_TYPE_ID = "policyTypeId"; - - private static final String POLICIES_URI = "/policies"; - - private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI = - UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}"); - - private static final UriComponentsBuilder POLICY_URI = - UriComponentsBuilder.fromPath("/policies/{policy-id}").queryParam(POLICY_TYPE_ID, "{policy-type-name}"); - - private static final UriComponentsBuilder POLICY_DELETE_URI = - UriComponentsBuilder.fromPath("/policies/{policy-id}"); - - private static final UriComponentsBuilder POLICY_STATUS_URI = - UriComponentsBuilder.fromPath("/policies/{policy-id}/status"); - private final AsyncRestClient restClient; public StdA1ClientVersion1(RicConfig ricConfig) { - String baseUrl = ricConfig.baseUrl() + URL_PREFIX; + final String urlPrefix = "/A1-P/v1"; + String baseUrl = ricConfig.baseUrl() + urlPrefix; this.restClient = new AsyncRestClient(baseUrl); } @@ -68,23 +51,20 @@ public class StdA1ClientVersion1 implements A1Client { @Override public Mono putPolicy(Policy policy) { - String uri = POLICY_URI.buildAndExpand(policy.id(), policy.type().name()).toUriString(); + final UriComponentsBuilder policyUri = UriComponentsBuilder.fromPath("/policies/{policy-id}"); + final String uri = policyUri.buildAndExpand(policy.id()).toUriString(); return restClient.put(uri, policy.json()) // .flatMap(JsonHelper::validateJson); } @Override public Mono> getPolicyTypeIdentities() { - return restClient.get(POLICY_TYPES_URI) // - .flatMapMany(JsonHelper::parseJsonArrayOfString) // - .collectList(); + return Mono.just(Arrays.asList("")); } @Override public Mono getPolicyTypeSchema(String policyTypeId) { - String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString(); - return restClient.get(uri) // - .flatMap(JsonHelper::extractPolicySchema); + return Mono.just("{}"); } @Override @@ -101,22 +81,25 @@ public class StdA1ClientVersion1 implements A1Client { @Override public Mono getProtocolVersion() { return getPolicyTypeIdentities() // - .flatMap(x -> Mono.just(A1ProtocolType.STD_V1)); + .flatMap(x -> Mono.just(A1ProtocolType.STD_V1_1)); } @Override public Mono getPolicyStatus(Policy policy) { - String uri = POLICY_STATUS_URI.buildAndExpand(policy.id()).toUriString(); + final UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/policies/{policy-id}/status"); + String uri = builder.buildAndExpand(policy.id()).toUriString(); return restClient.get(uri); } private Flux getPolicyIds() { - return restClient.get(POLICIES_URI) // + final String uri = "/policies"; + return restClient.get(uri) // .flatMapMany(JsonHelper::parseJsonArrayOfString); } private Mono deletePolicyById(String policyId) { - String uri = POLICY_DELETE_URI.buildAndExpand(policyId).toUriString(); + final UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/policies/{policy-id}"); + String uri = builder.buildAndExpand(policyId).toUriString(); return restClient.delete(uri); } } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1ClientVersion2.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1ClientVersion2.java deleted file mode 100644 index cd8c6849..00000000 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1ClientVersion2.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * O-RAN-SC - * %% - * Copyright (C) 2019 Nordix Foundation - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ========================LICENSE_END=================================== - */ - -package org.oransc.policyagent.clients; - -import java.util.Arrays; -import java.util.List; - -import org.oransc.policyagent.configuration.RicConfig; -import reactor.core.publisher.Mono; - -public class StdA1ClientVersion2 extends StdA1ClientVersion1 { - - public StdA1ClientVersion2(RicConfig ricConfig) { - super(ricConfig); - } - - public StdA1ClientVersion2(AsyncRestClient restClient) { - super(restClient); - } - - @Override - public Mono> getPolicyTypeIdentities() { - return Mono.just(Arrays.asList("")); - } - - @Override - public Mono getPolicyTypeSchema(String policyTypeId) { - return Mono.just("{}"); - } - - @Override - public Mono getProtocolVersion() { - return getPolicyIdentities() // - .flatMap(x -> Mono.just(A1ProtocolType.STD_V1_1)); - } -} diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java index 0ef1679c..0f3b391f 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java @@ -293,27 +293,30 @@ public class PolicyController { return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND); } synchronized (policies) { - Collection result = null; - - if (type != null) { - result = policies.getForType(type); - result = filter(result, null, ric, service); - } else if (service != null) { - result = policies.getForService(service); - result = filter(result, type, ric, null); - } else if (ric != null) { - result = filter(policies.getForRic(ric), type, null, service); - } else { - result = policies.getAll(); - } + String filteredPolicies = policiesToJson(filter(type, ric, service)); + return new ResponseEntity<>(filteredPolicies, HttpStatus.OK); + } + } - String policiesJson; - try { - policiesJson = policiesToJson(result); - } catch (ServiceException e) { - return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - } - return new ResponseEntity<>(policiesJson, HttpStatus.OK); + @GetMapping("/policy_ids") + @ApiOperation(value = "Query policies, only IDs returned") + @ApiResponses( + value = {@ApiResponse(code = 200, message = "Policy ids", response = String.class, responseContainer = "List"), + @ApiResponse(code = 404, message = "RIC or type not found", response = String.class)}) + public ResponseEntity getPolicyIds( // + @RequestParam(name = "type", required = false) String type, // + @RequestParam(name = "ric", required = false) String ric, // + @RequestParam(name = "service", required = false) String service) // + { + if ((type != null && this.policyTypes.get(type) == null)) { + return new ResponseEntity<>("Policy type not found", HttpStatus.NOT_FOUND); + } + if ((ric != null && this.rics.get(ric) == null)) { + return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND); + } + synchronized (policies) { + String policyIdsJson = toPolicyIdsJson(filter(type, ric, service)); + return new ResponseEntity<>(policyIdsJson, HttpStatus.OK); } } @@ -363,7 +366,21 @@ public class PolicyController { return filtered; } - private String policiesToJson(Collection policies) throws ServiceException { + private Collection filter(String type, String ric, String service) { + synchronized (policies) { + if (type != null) { + return filter(policies.getForType(type), null, ric, service); + } else if (service != null) { + return filter(policies.getForService(service), type, ric, null); + } else if (ric != null) { + return filter(policies.getForRic(ric), type, null, service); + } else { + return policies.getAll(); + } + } + } + + private String policiesToJson(Collection policies) { List v = new ArrayList<>(policies.size()); for (Policy p : policies) { PolicyInfo policyInfo = new PolicyInfo(); @@ -374,7 +391,7 @@ public class PolicyController { policyInfo.service = p.ownerServiceName(); policyInfo.lastModified = p.lastModified(); if (!policyInfo.validate()) { - throw new ServiceException("BUG, all fields must be set"); + throw new NullPointerException("BUG, all fields must be set"); } v.add(policyInfo); } @@ -408,6 +425,14 @@ public class PolicyController { return gson.toJson(v); } + private String toPolicyIdsJson(Collection policies) { + List v = new ArrayList<>(policies.size()); + for (Policy p : policies) { + v.add(p.id()); + } + return gson.toJson(v); + } + private String getTimeStampUtc() { return java.time.Instant.now().toString(); } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java index 3d362282..2a346431 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java @@ -105,15 +105,17 @@ public class ServiceController { @ApiOperation(value = "Register a service") @ApiResponses( value = { // - @ApiResponse(code = 200, message = "OK", response = String.class), + @ApiResponse(code = 200, message = "Service updated", response = String.class), + @ApiResponse(code = 201, message = "Service created", response = String.class), // @ApiResponse(code = 400, message = "Cannot parse the ServiceRegistrationInfo", response = String.class)}) @PutMapping("/service") public ResponseEntity putService(// @RequestBody ServiceRegistrationInfo registrationInfo) { try { validateRegistrationInfo(registrationInfo); + final boolean isCreate = this.services.get(registrationInfo.serviceName) == null; this.services.put(toService(registrationInfo)); - return new ResponseEntity<>("OK", HttpStatus.OK); + return new ResponseEntity<>("OK", isCreate ? HttpStatus.CREATED : HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageHandler.java b/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageHandler.java index 6d1603c0..fc6e439a 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageHandler.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageHandler.java @@ -156,12 +156,12 @@ public class DmaapMessageHandler { HttpStatus status) { DmaapResponseMessage dmaapResponseMessage = ImmutableDmaapResponseMessage.builder() // .status(status.toString()) // - .message(response) // + .message(response == null ? "" : response) // .type("response") // - .correlationId(dmaapRequestMessage.correlationId()) // - .originatorId(dmaapRequestMessage.originatorId()) // - .requestId(dmaapRequestMessage.requestId()) // - .timestamp(dmaapRequestMessage.timestamp()) // + .correlationId(dmaapRequestMessage.correlationId() == null ? "" : dmaapRequestMessage.correlationId()) // + .originatorId(dmaapRequestMessage.originatorId() == null ? "" : dmaapRequestMessage.originatorId()) // + .requestId(dmaapRequestMessage.requestId() == null ? "" : dmaapRequestMessage.requestId()) // + .timestamp(dmaapRequestMessage.timestamp() == null ? "" : dmaapRequestMessage.timestamp()) // .build(); String str = gson.toJson(dmaapResponseMessage); return Mono.just(str); diff --git a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java index 2308fcf1..71bd9a5e 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java @@ -22,6 +22,7 @@ package org.oransc.policyagent; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; @@ -86,6 +87,7 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import reactor.util.annotation.Nullable; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @@ -496,10 +498,37 @@ public class ApplicationTest { testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND); } + @Test + public void testGetPolicyIdsFilter() throws Exception { + addPolicy("id1", "type1", "service1", "ric1"); + addPolicy("id2", "type1", "service2", "ric1"); + addPolicy("id3", "type2", "service1", "ric1"); + + String url = "/policy_ids?type=type1"; + String rsp = restClient().get(url).block(); + logger.info(rsp); + assertThat(rsp).contains("id1"); + assertThat(rsp).contains("id2"); + assertThat(rsp.contains("id3")).isFalse(); + + url = "/policy_ids?type=type1&service=service1&ric=ric1"; + rsp = restClient().get(url).block(); + assertThat(rsp).isEqualTo("[\"id1\"]"); + + // Test get policy ids for non existing type + url = "/policy_ids?type=type1XXX"; + testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND); + + // Test get policy ids for non existing RIC + url = "/policy_ids?ric=XXX"; + testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND); + } + @Test public void testPutAndGetService() throws Exception { // PUT - putService("name", 0); + putService("name", 0, HttpStatus.CREATED); + putService("name", 0, HttpStatus.OK); // GET one service String url = "/services?name=name"; @@ -540,7 +569,7 @@ public class ApplicationTest { @Test public void testServiceSupervision() throws Exception { - putService("service1", 1); + putService("service1", 1, HttpStatus.CREATED); addPolicyType("type1", "ric1"); String url = putPolicyUrl("service1", "ric1", "type1", "instance1"); @@ -593,13 +622,16 @@ public class ApplicationTest { } private void putService(String name) { - putService(name, 0); + putService(name, 0, null); } - private void putService(String name, long keepAliveIntervalSeconds) { + private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) { String url = "/service"; String body = createServiceJson(name, keepAliveIntervalSeconds); - restClient().put(url, body).block(); + ResponseEntity resp = restClient().putForEntity(url, body).block(); + if (expectedStatus != null) { + assertEquals(expectedStatus, resp.getStatusCode(), ""); + } } private String baseUrl() { diff --git a/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java b/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java index efbd5765..481a1fe1 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java @@ -129,15 +129,13 @@ public class MockPolicyAgent { @LocalServerPort private int port; - private void keepServerAlive() { + private void keepServerAlive() throws InterruptedException { logger.info("Keeping server alive!"); - try { - synchronized (this) { - this.wait(); - } - } catch (Exception ex) { - logger.error("Unexpected: " + ex); + + synchronized (this) { + this.wait(); } + } private static String title(String jsonSchema) { @@ -147,7 +145,8 @@ public class MockPolicyAgent { } @Test - @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server alive, + @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server + // alive, // so it will only be confusing to add an assertion. public void runMock() throws Exception { keepServerAlive(); diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java index a7495b9f..df26d27d 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java @@ -62,9 +62,6 @@ public class A1ClientFactoryTest { @Mock A1Client clientMock4; - @Mock - A1Client clientMock5; - private ImmutableRicConfig ricConfig = ImmutableRicConfig.builder().name(RIC_NAME).baseUrl("baseUrl").managedElementIds(new Vector<>()).build(); private Ric ric = new Ric(ricConfig); @@ -79,33 +76,31 @@ public class A1ClientFactoryTest { @Test public void getProtocolVersion_ok() { whenGetProtocolVersionThrowException(clientMock1); - whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1); + whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1); doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any()); A1Client client = factoryUnderTest.createA1Client(ric).block(); assertEquals(clientMock2, client, "Not correct client returned"); - assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol"); + assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol"); } @Test public void getProtocolVersion_ok_Last() { - whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4); - whenGetProtocolVersionReturn(clientMock5, A1ProtocolType.STD_V1_1); - doReturn(clientMock1, clientMock2, clientMock3, clientMock4, clientMock5).when(factoryUnderTest) - .createClient(any(), any()); + whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3); + whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1); + doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any()); A1Client client = factoryUnderTest.createA1Client(ric).block(); - assertEquals(clientMock5, client, "Not correct client returned"); + assertEquals(clientMock4, client, "Not correct client returned"); assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol"); } @Test public void getProtocolVersion_error() { - whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4, clientMock5); - doReturn(clientMock1, clientMock2, clientMock3, clientMock4, clientMock5).when(factoryUnderTest) - .createClient(any(), any()); + whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4); + doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any()); StepVerifier.create(factoryUnderTest.createA1Client(ric)) // .expectSubscription() // @@ -121,8 +116,7 @@ public class A1ClientFactoryTest { @Test public void create_check_types() { - assertTrue(createClient(A1ProtocolType.STD_V1) instanceof StdA1ClientVersion1); - assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion2); + assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1); assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client); assertTrue(createClient(A1ProtocolType.SDNC_ONAP) instanceof SdncOnapA1Client); assertTrue(createClient(A1ProtocolType.SDNC_OSC) instanceof SdncOscA1Client); diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java index 885db0f6..2ccd3b3c 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java @@ -46,16 +46,10 @@ public class SdncOscA1ClientTest { private static final String CONTROLLER_USERNAME = "username"; private static final String CONTROLLER_PASSWORD = "password"; private static final String RIC_1_URL = "RicUrl"; - private static final String POLICY_TYPES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyTypeIdentities"; private static final String POLICY_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyIdentities"; - private static final String POLICY_TYPES_URL = "/A1-ADAPTER-API:getPolicyType"; private static final String PUT_POLICY_URL = "/A1-ADAPTER-API:putPolicy"; private static final String DELETE_POLICY_URL = "/A1-ADAPTER-API:deletePolicy"; - private static final String POLICY_TYPE_1_ID = "type1"; - private static final String POLICY_TYPE_2_ID = "type2"; - private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}"; - private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}"; private static final String POLICY_1_ID = "policy1"; private static final String POLICY_2_ID = "policy2"; private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}"; @@ -74,21 +68,9 @@ public class SdncOscA1ClientTest { @Test public void testGetPolicyTypeIdentities() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); - - List policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID); - Mono policyTypeIdsResp = - A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString()); - whenAsyncPostThenReturn(policyTypeIdsResp); - - Mono> returnedMono = clientUnderTest.getPolicyTypeIdentities(); - - verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_IDENTITIES_URL, inputJsonString, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify(); + List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); + assertEquals(1, policyTypeIds.size(), "should hardcoded to one"); + assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); } @Test @@ -111,40 +93,8 @@ public class SdncOscA1ClientTest { @Test public void testGetValidPolicyType() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); - - String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }"; - Mono policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType); - whenAsyncPostThenReturn(policyTypeResp); - - Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); - - verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify(); - } - - @Test - public void testGetInvalidPolicyType() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); - - String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + ", \"statusSchema\": {} }"; - Mono policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType); - whenAsyncPostThenReturn(policyTypeResp); - - Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); - - verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); + String policyType = clientUnderTest.getPolicyTypeSchema("").block(); + assertEquals("{}", policyType, ""); } @Test diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java index 68b56f3e..90e0ed8a 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java @@ -20,11 +20,13 @@ package org.oransc.policyagent.clients; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Arrays; +import java.util.List; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; @@ -41,15 +43,9 @@ import reactor.test.StepVerifier; @ExtendWith(MockitoExtension.class) public class StdA1ClientTest { private static final String RIC_URL = "RicUrl"; - private static final String POLICY_TYPES_IDENTITIES_URL = "/policytypes"; private static final String POLICIES_IDENTITIES_URL = "/policies"; - private static final String POLICY_TYPES_URL = "/policytypes/"; private static final String POLICIES_URL = "/policies/"; - private static final String POLICY_TYPE_1_NAME = "type1"; - private static final String POLICY_TYPE_2_NAME = "type2"; - private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}"; - private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}"; private static final String POLICY_1_ID = "policy1"; private static final String POLICY_2_ID = "policy2"; private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}"; @@ -68,13 +64,9 @@ public class StdA1ClientTest { @Test public void testGetPolicyTypeIdentities() { - Mono policyTypeIds = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).toString()); - when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIds); - - Mono policyTypeIdsFlux = clientUnderTest.getPolicyTypeIdentities(); - - verify(asyncRestClientMock).get(POLICY_TYPES_IDENTITIES_URL); - StepVerifier.create(policyTypeIdsFlux).expectNextCount(1).expectComplete().verify(); + List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); + assertEquals(1, policyTypeIds.size(), "should hardcoded to one"); + assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); } @Test @@ -82,34 +74,16 @@ public class StdA1ClientTest { Mono policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString()); when(asyncRestClientMock.get(anyString())).thenReturn(policyIds); - Mono policyIdsFlux = clientUnderTest.getPolicyIdentities(); + List result = clientUnderTest.getPolicyIdentities().block(); + assertEquals(2, result.size(), ""); verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL); - StepVerifier.create(policyIdsFlux).expectNextCount(1).expectComplete().verify(); } @Test public void testGetValidPolicyType() { - Mono policyTypeResp = - Mono.just("{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }"); - - when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp); - - Mono policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME); - - verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME); - StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify(); - } - - @Test - public void testGetInvalidPolicyType() { - when(asyncRestClientMock.get(anyString())).thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID)); - - Mono policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME); - - verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME); - StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException) - .verify(); + String policyType = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME).block(); + assertEquals("{}", policyType, ""); } @Test @@ -119,7 +93,7 @@ public class StdA1ClientTest { Mono policyMono = clientUnderTest .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE)); - verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID + "?policyTypeId=" + POLICY_TYPE, POLICY_JSON_VALID); + verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID); StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify(); } diff --git a/policy-agent/src/test/java/org/oransc/policyagent/utils/MockA1Client.java b/policy-agent/src/test/java/org/oransc/policyagent/utils/MockA1Client.java index b765f571..af8ecd47 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/utils/MockA1Client.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/utils/MockA1Client.java @@ -90,7 +90,7 @@ public class MockA1Client implements A1Client { @Override public Mono getProtocolVersion() { - return Mono.just(A1ProtocolType.STD_V1); + return Mono.just(A1ProtocolType.STD_V1_1); } @Override diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadapter/NearRicUrlProvider.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadapter/NearRicUrlProvider.java index c4ca335a..df83226b 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadapter/NearRicUrlProvider.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadapter/NearRicUrlProvider.java @@ -38,8 +38,7 @@ public class NearRicUrlProvider { * @return the base url */ public String getBaseUrl(final String nearRtRicUrl) { - String baseUrl = nearRtRicUrl + "/A1-P/v1"; - return UriComponentsBuilder.fromUriString(baseUrl).build().toString(); + return nearRtRicUrl + "/A1-P/v1"; } /** @@ -59,7 +58,7 @@ public class NearRicUrlProvider { * @return the policies url */ public String policiesUrl(final String nearRtRicUrl) { - return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicUrl)).pathSegment("policies").build().toString(); + return getBaseUrl(nearRtRicUrl) + "/policies"; } /** @@ -83,8 +82,12 @@ public class NearRicUrlProvider { * @return the putPolicy url */ public String putPolicyUrl(final String nearRtRicUrl, final String policyId, final String policyTypeId) { - return UriComponentsBuilder.fromUriString(policiesUrl(nearRtRicUrl)) - .pathSegment(policyId + "?policyTypeId=" + policyTypeId).build().toString(); + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(policiesUrl(nearRtRicUrl)).pathSegment(policyId); + + if (!policyTypeId.isEmpty()) { + builder.queryParam("policyTypeId", policyTypeId); + } + return builder.build().toString(); } /**