From beb7ba7d7fbccc421d7c2c16b8942bb5da7fc71c Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Mon, 23 Mar 2020 07:17:28 +0100 Subject: [PATCH] Generalization of controller Prepare for controller supporting several A1 influenced APIs (southbound). Change-Id: I7748dc834f8c24d4a9877f158d17b84d43a1bb53 Issue-ID: NONRTRIC-164 Signed-off-by: PatrikBuhr --- ...{SdncOscAdapterInput.java => A1UriBuilder.java} | 20 +-- .../org/oransc/policyagent/clients/JsonHelper.java | 3 +- .../policyagent/clients/SdncOscA1Client.java | 110 +++++++----- .../policyagent/clients/StdA1ClientVersion1.java | 26 ++- .../clients/StdA1UriBuilderVersion1.java | 66 ++++++++ .../policyagent/clients/SdncOscA1ClientTest.java | 188 ++++++++++----------- .../policyagent/clients/StdA1ClientTest.java | 38 +++-- .../model/src/main/yang/NONRT-RIC-API.yang | 80 ++++----- .../northbound/provider/NonrtRicApiProvider.java | 168 +++++++++--------- .../northbound/restadapter/NearRicUrlProvider.java | 107 ------------ .../sdnc/northbound/NonrtRicApiProviderTest.java | 125 ++++++-------- 11 files changed, 435 insertions(+), 496 deletions(-) rename policy-agent/src/main/java/org/oransc/policyagent/clients/{SdncOscAdapterInput.java => A1UriBuilder.java} (72%) create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1UriBuilderVersion1.java delete mode 100644 sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadapter/NearRicUrlProvider.java diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscAdapterInput.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1UriBuilder.java similarity index 72% rename from policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscAdapterInput.java rename to policy-agent/src/main/java/org/oransc/policyagent/clients/A1UriBuilder.java index 5fbfd3a7..e32220dc 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscAdapterInput.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1UriBuilder.java @@ -20,19 +20,17 @@ package org.oransc.policyagent.clients; -import java.util.Optional; +import org.oransc.policyagent.repository.Policy; -import org.immutables.gson.Gson; -import org.immutables.value.Value; - -@Value.Immutable -@Gson.TypeAdapters -interface SdncOscAdapterInput { - public String nearRtRicUrl(); +/** + * Builder for A1 influenced REST APIs + */ +interface A1UriBuilder { + String createPutPolicyUri(Policy policy); - public Optional policyTypeId(); + String createGetPolicyIdsUri(); - public Optional policyId(); + String createDeleteUri(String policyId); - public Optional policy(); + String createGetPolicyStatusUri(String policyId); } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/JsonHelper.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/JsonHelper.java index 68ffb10b..96c9ac96 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/JsonHelper.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/JsonHelper.java @@ -34,13 +34,12 @@ import org.json.JSONObject; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -public class JsonHelper { +class JsonHelper { private static Gson gson = new GsonBuilder() // .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) // .create(); private JsonHelper() { - } public static Flux parseJsonArrayOfString(String inputString) { 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 804513ad..25fadbf5 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 @@ -20,14 +20,22 @@ package org.oransc.policyagent.clients; +import com.google.gson.FieldNamingPolicy; +import com.google.gson.GsonBuilder; + import java.lang.invoke.MethodHandles; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; +import java.util.Optional; +import org.immutables.value.Value; import org.oransc.policyagent.configuration.RicConfig; import org.oransc.policyagent.repository.Policy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -35,23 +43,42 @@ 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:"; + @Value.Immutable + @org.immutables.gson.Gson.TypeAdapters + public interface AdapterRequest { + public String nearRtRicUrl(); + + public Optional body(); + } + + @Value.Immutable + @org.immutables.gson.Gson.TypeAdapters + public interface AdapterResponse { + public String body(); + + public int httpStatus(); + } + + static com.google.gson.Gson gson = new GsonBuilder() // + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) // + .create(); // + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final String a1ControllerUsername; private final String a1ControllerPassword; - private final RicConfig ricConfig; private final AsyncRestClient restClient; + private final A1UriBuilder uri; - public SdncOscA1Client(RicConfig ricConfig, String baseUrl, String username, String password) { - this(ricConfig, username, password, new AsyncRestClient(baseUrl + "/restconf/operations")); - logger.debug("SdncOscA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), baseUrl); + public SdncOscA1Client(RicConfig ricConfig, String controllerBaseUrl, String username, String password) { + this(ricConfig, username, password, new AsyncRestClient(controllerBaseUrl + "/restconf/operations")); + logger.debug("SdncOscA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), controllerBaseUrl); } public SdncOscA1Client(RicConfig ricConfig, String username, String password, AsyncRestClient restClient) { - this.ricConfig = ricConfig; this.a1ControllerUsername = username; this.a1ControllerPassword = password; this.restClient = restClient; + this.uri = new StdA1UriBuilderVersion1(ricConfig); } @Override @@ -72,17 +99,8 @@ public class SdncOscA1Client implements A1Client { @Override public Mono putPolicy(Policy policy) { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(ricConfig.baseUrl()) // - .policyTypeId(policy.type().name()) // - .policyId(policy.id()) // - .policy(policy.json()) // - .build(); - String inputJsonString = JsonHelper.createInputJsonString(inputParams); - return restClient - .postWithAuthHeader(URL_PREFIX + "putPolicy", inputJsonString, a1ControllerUsername, a1ControllerPassword) - .flatMap(response -> JsonHelper.getValueFromResponse(response, "returned-policy")) // - .flatMap(JsonHelper::validateJson); + final String ricUrl = uri.createPutPolicyUri(policy); + return post("putA1Policy", ricUrl, Optional.of(policy.json())); } @Override @@ -104,39 +122,47 @@ public class SdncOscA1Client implements A1Client { @Override public Mono getPolicyStatus(Policy policy) { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(ricConfig.baseUrl()) // - .policyId(policy.id()) // - .build(); - String inputJsonString = JsonHelper.createInputJsonString(inputParams); - logger.debug("POST getPolicyStatus inputJsonString = {}", inputJsonString); - - return restClient - .postWithAuthHeader(URL_PREFIX + "getPolicyStatus", inputJsonString, a1ControllerUsername, - a1ControllerPassword) // - .flatMap(response -> JsonHelper.getValueFromResponse(response, "policy-status")); + final String ricUrl = uri.createGetPolicyStatusUri(policy.id()); + return post("getA1PolicyStatus", ricUrl, Optional.empty()); } private Flux getPolicyIds() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(ricConfig.baseUrl()) // - .build(); - String inputJsonString = JsonHelper.createInputJsonString(inputParams); - return restClient - .postWithAuthHeader(URL_PREFIX + "getPolicyIdentities", inputJsonString, a1ControllerUsername, - a1ControllerPassword) // - .flatMap(response -> JsonHelper.getValueFromResponse(response, "policy-id-list")) // + final String ricUrl = uri.createGetPolicyIdsUri(); + return post("getA1Policy", ricUrl, Optional.empty()) // .flatMapMany(JsonHelper::parseJsonArrayOfString); } private Mono deletePolicyById(String policyId) { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(ricConfig.baseUrl()) // - .policyId(policyId) // + final String ricUrl = uri.createDeleteUri(policyId); + return post("deleteA1Policy", ricUrl, Optional.empty()); + } + + private Mono post(String rpcName, String ricUrl, Optional body) { + AdapterRequest inputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(ricUrl) // + .body(body) // .build(); + final String inputJsonString = JsonHelper.createInputJsonString(inputParams); + + return restClient + .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, a1ControllerUsername, a1ControllerPassword) + .flatMap(this::extractResponseBody); + } + + private Mono extractResponseBody(String response) { + AdapterResponse output = gson.fromJson(response, ImmutableAdapterResponse.class); + String body = output.body(); + if (HttpStatus.valueOf(output.httpStatus()).is2xxSuccessful()) { + return Mono.just(body); + } + byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8); + WebClientResponseException e = new WebClientResponseException(output.httpStatus(), "statusText", null, + responseBodyBytes, StandardCharsets.UTF_8, null); + + return Mono.error(e); + } - String inputJsonString = JsonHelper.createInputJsonString(inputParams); - return restClient.postWithAuthHeader(URL_PREFIX + "deletePolicy", inputJsonString, a1ControllerUsername, - a1ControllerPassword); + private String controllerUrl(String rpcName) { + return "/A1-ADAPTER-API:" + rpcName; } } 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 8c1ed38d..6b9ef17c 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 @@ -25,7 +25,6 @@ import java.util.List; import org.oransc.policyagent.configuration.RicConfig; import org.oransc.policyagent.repository.Policy; -import org.springframework.web.util.UriComponentsBuilder; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -33,14 +32,15 @@ import reactor.core.publisher.Mono; public class StdA1ClientVersion1 implements A1Client { private final AsyncRestClient restClient; + private final A1UriBuilder uri; + public StdA1ClientVersion1(RicConfig ricConfig) { - final String urlPrefix = "/A1-P/v1"; - String baseUrl = ricConfig.baseUrl() + urlPrefix; - this.restClient = new AsyncRestClient(baseUrl); + this(new AsyncRestClient(""), ricConfig); } - public StdA1ClientVersion1(AsyncRestClient restClient) { + public StdA1ClientVersion1(AsyncRestClient restClient, RicConfig ricConfig) { this.restClient = restClient; + this.uri = new StdA1UriBuilderVersion1(ricConfig); } @Override @@ -51,9 +51,8 @@ public class StdA1ClientVersion1 implements A1Client { @Override public Mono putPolicy(Policy policy) { - final UriComponentsBuilder policyUri = UriComponentsBuilder.fromPath("/policies/{policy-id}"); - final String uri = policyUri.buildAndExpand(policy.id()).toUriString(); - return restClient.put(uri, policy.json()) // + + return restClient.put(uri.createPutPolicyUri(policy), policy.json()) // .flatMap(JsonHelper::validateJson); } @@ -86,20 +85,15 @@ public class StdA1ClientVersion1 implements A1Client { @Override public Mono getPolicyStatus(Policy policy) { - final UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/policies/{policy-id}/status"); - String uri = builder.buildAndExpand(policy.id()).toUriString(); - return restClient.get(uri); + return restClient.get(uri.createGetPolicyStatusUri(policy.id())); } private Flux getPolicyIds() { - final String uri = "/policies"; - return restClient.get(uri) // + return restClient.get(uri.createGetPolicyIdsUri()) // .flatMapMany(JsonHelper::parseJsonArrayOfString); } private Mono deletePolicyById(String policyId) { - final UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/policies/{policy-id}"); - String uri = builder.buildAndExpand(policyId).toUriString(); - return restClient.delete(uri); + return restClient.delete(uri.createDeleteUri(policyId)); } } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1UriBuilderVersion1.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1UriBuilderVersion1.java new file mode 100644 index 00000000..6d836564 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1UriBuilderVersion1.java @@ -0,0 +1,66 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2020 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 org.oransc.policyagent.configuration.RicConfig; +import org.oransc.policyagent.repository.Policy; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * URI builder for A1 STD version 1.1 + */ +public class StdA1UriBuilderVersion1 implements A1UriBuilder { + + private final RicConfig ricConfig; + + @Autowired + public StdA1UriBuilderVersion1(RicConfig ricConfig) { + this.ricConfig = ricConfig; + } + + @Override + public String createPutPolicyUri(Policy policy) { + return policiesBaseUri() + policy.id(); + } + + @Override + public String createGetPolicyIdsUri() { + return baseUri() + "/policies"; + } + + @Override + public String createDeleteUri(String policyId) { + return policiesBaseUri() + policyId; + } + + @Override + public String createGetPolicyStatusUri(String policyId) { + return policiesBaseUri() + policyId + "/status"; + } + + private String baseUri() { + return ricConfig.baseUrl() + "/A1-P/v1"; + } + + private String policiesBaseUri() { + return createGetPolicyIdsUri() + "/"; + } +} 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 2ccd3b3c..68c61ee6 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 @@ -26,18 +26,21 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.google.gson.Gson; + import java.util.Arrays; import java.util.List; -import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.OngoingStubbing; +import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest; +import org.oransc.policyagent.clients.SdncOscA1Client.AdapterResponse; import org.oransc.policyagent.repository.Policy; +import org.springframework.web.reactive.function.client.WebClientResponseException; -import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -46,14 +49,14 @@ 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_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyIdentities"; - 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 GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy"; + private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy"; + private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy"; + private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus"; private static final String POLICY_TYPE_1_ID = "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\"}}"; - private static final String POLICY_JSON_INVALID = "\"scope\":{\"ueId\":\"ue1\"}}"; SdncOscA1Client clientUnderTest; @@ -73,22 +76,36 @@ public class SdncOscA1ClientTest { assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); } + private String policiesUrl() { + return RIC_1_URL + "/A1-P/v1/policies"; + } + + private Gson gson() { + return SdncOscA1Client.gson; + } + @Test public void testGetPolicyIdentities() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); List policyIds = Arrays.asList(POLICY_1_ID, POLICY_2_ID); - Mono policyIdsResp = A1ClientHelper.createOutputJsonResponse("policy-id-list", policyIds.toString()); - whenAsyncPostThenReturn(policyIdsResp); + AdapterResponse output = ImmutableAdapterResponse.builder() // + .body(gson().toJson(policyIds)) // + .httpStatus(200) // + .build(); - Mono> returnedMono = clientUnderTest.getPolicyIdentities(); + String policyIdsResp = gson().toJson(output); + whenAsyncPostThenReturn(Mono.just(policyIdsResp)); - verify(asyncRestClientMock).postWithAuthHeader(POLICY_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME, + List returned = clientUnderTest.getPolicyIdentities().block(); + assertEquals(2, returned.size(), ""); + + AdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(policiesUrl()) // + .build(); + String expInput = A1ClientHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(policyIds).expectComplete().verify(); + } @Test @@ -99,119 +116,96 @@ public class SdncOscA1ClientTest { @Test public void testPutPolicyValidResponse() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .policyId(POLICY_1_ID) // - .policy(POLICY_JSON_VALID) // + whenPostReturnOkResponse(); + + String returned = clientUnderTest + .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) + .block(); + assertEquals("OK", returned, ""); + final String expUrl = policiesUrl() + "/" + POLICY_1_ID; + AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .body(POLICY_JSON_VALID) // .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); - - Mono policyResp = A1ClientHelper.createOutputJsonResponse("returned-policy", POLICY_JSON_VALID); - whenAsyncPostThenReturn(policyResp); - - Mono returnedMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + String expInput = A1ClientHelper.createInputJsonString(expectedInputParams); - verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(POLICY_JSON_VALID).expectComplete().verify(); + verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); } @Test - public void testPutPolicyInvalidResponse() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .policyId(POLICY_1_ID) // - .policy(POLICY_JSON_VALID) // + public void testPutPolicyRejected() { + final String policyJson = "{}"; + AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() // + .body("NOK") // + .httpStatus(400) // ERROR .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); - Mono policyResp = A1ClientHelper.createOutputJsonResponse("returned-policy", POLICY_JSON_INVALID); - whenAsyncPostThenReturn(policyResp); + String resp = gson().toJson(adapterResponse); + whenAsyncPostThenReturn(Mono.just(resp)); Mono returnedMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID)); - verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, + final String expUrl = policiesUrl() + "/" + POLICY_1_ID; + AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .body(policyJson) // + .build(); + String expRequest = A1ClientHelper.createInputJsonString(expRequestParams); + verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); + StepVerifier.create(returnedMono) + .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify(); } @Test public void testDeletePolicy() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_1_ID) // + whenPostReturnOkResponse(); + + String returned = clientUnderTest + .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) + .block(); + assertEquals("OK", returned, ""); + final String expUrl = policiesUrl() + "/" + POLICY_1_ID; + AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); - - whenAsyncPostThenReturn(Mono.empty()); - - Mono returnedMono = clientUnderTest - .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + String expInput = A1ClientHelper.createInputJsonString(expectedInputParams); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, + verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectComplete().verify(); - } - - @Test - public void testDeleteAllPolicies() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .build(); - String inputJsonStringGetIds = A1ClientHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_1_ID) // - .build(); - String inputJsonStringDeletePolicy1 = A1ClientHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_2_ID) // - .build(); - String inputJsonStringDeletePolicy2 = A1ClientHelper.createInputJsonString(inputParams); - - List policyIds = Arrays.asList(POLICY_1_ID, POLICY_2_ID); - Mono policyIdsResp = A1ClientHelper.createOutputJsonResponse("policy-id-list", policyIds.toString()); - whenAsyncPostThenReturn(policyIdsResp).thenReturn(Mono.empty()); - - Flux returnedFlux = clientUnderTest.deleteAllPolicies(); - - StepVerifier.create(returnedFlux).expectComplete().verify(); - verify(asyncRestClientMock).postWithAuthHeader(POLICY_IDENTITIES_URL, inputJsonStringGetIds, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy1, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy2, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); } @Test public void testGetStatus() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_1_ID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); - - String status = "STATUS"; - Mono policyStatusResp = A1ClientHelper.createOutputJsonResponse("policy-status", status); - whenAsyncPostThenReturn(policyStatusResp); + whenPostReturnOkResponse(); Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); String returnedStatus = clientUnderTest.getPolicyStatus(policy).block(); - assertEquals(status, returnedStatus, "unexpexted status"); + assertEquals("OK", returnedStatus, "unexpeted status"); + + final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status"; + AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = A1ClientHelper.createInputJsonString(expectedInputParams); - final String expectedUrl = "/A1-ADAPTER-API:getPolicyStatus"; - verify(asyncRestClientMock).postWithAuthHeader(expectedUrl, inputJsonString, CONTROLLER_USERNAME, + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); } + private void whenPostReturnOkResponse() { + AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() // + .body("OK") // + .httpStatus(200) // + .build(); + + String resp = gson().toJson(adapterResponse); + whenAsyncPostThenReturn(Mono.just(resp)); + } + private OngoingStubbing> whenAsyncPostThenReturn(Mono response) { return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString())) .thenReturn(response); 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 90e0ed8a..ca6c8a90 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 @@ -22,6 +22,7 @@ package org.oransc.policyagent.clients; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -34,6 +35,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.oransc.policyagent.configuration.RicConfig; import org.oransc.policyagent.repository.Policy; import reactor.core.publisher.Flux; @@ -43,8 +45,6 @@ import reactor.test.StepVerifier; @ExtendWith(MockitoExtension.class) public class StdA1ClientTest { private static final String RIC_URL = "RicUrl"; - private static final String POLICIES_IDENTITIES_URL = "/policies"; - private static final String POLICIES_URL = "/policies/"; private static final String POLICY_TYPE_1_NAME = "type1"; private static final String POLICY_1_ID = "policy1"; private static final String POLICY_2_ID = "policy2"; @@ -57,9 +57,20 @@ public class StdA1ClientTest { @Mock AsyncRestClient asyncRestClientMock; + @Mock + RicConfig ricConfigMock; + @BeforeEach public void init() { - clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock); + clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock, ricConfigMock); + } + + private String policiesUrl() { + return RIC_URL + "/A1-P/v1/policies"; + } + + private String policiesBaseUrl() { + return policiesUrl() + "/"; } @Test @@ -71,13 +82,14 @@ public class StdA1ClientTest { @Test public void testGetPolicyIdentities() { + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); Mono policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString()); when(asyncRestClientMock.get(anyString())).thenReturn(policyIds); List result = clientUnderTest.getPolicyIdentities().block(); assertEquals(2, result.size(), ""); - verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL); + verify(asyncRestClientMock).get(policiesUrl()); } @Test @@ -88,12 +100,13 @@ public class StdA1ClientTest { @Test public void testPutPolicyValidResponse() { + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID)); Mono policyMono = clientUnderTest .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE)); - verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID); + verify(asyncRestClientMock).put(policiesBaseUrl() + POLICY_1_ID, POLICY_JSON_VALID); StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify(); } @@ -109,24 +122,27 @@ public class StdA1ClientTest { @Test public void testDeletePolicy() { - when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty()); + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); + final String url = policiesBaseUrl() + POLICY_1_ID; + when(asyncRestClientMock.delete(url)).thenReturn(Mono.empty()); Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE); Mono responseMono = clientUnderTest.deletePolicy(policy); - verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID); + verify(asyncRestClientMock).delete(url); StepVerifier.create(responseMono).expectComplete().verify(); } @Test public void testDeleteAllPolicies() { + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); Mono policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString()); - when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds); + when(asyncRestClientMock.get(policiesUrl())).thenReturn(policyIds); when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty()); Flux responseFlux = clientUnderTest.deleteAllPolicies(); StepVerifier.create(responseFlux).expectComplete().verify(); - verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL); - verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID); - verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID); + verify(asyncRestClientMock).get(policiesUrl()); + verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_1_ID); + verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_2_ID); } } diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang b/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang index a9cf825d..19336603 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang @@ -37,104 +37,86 @@ module A1-ADAPTER-API { "A1 adapter"; } - // Get an array of integer policy type ids - // Each item in the returned array will be regarded as one policy-type-id. - rpc getPolicyTypeIdentities { + rpc putA1Policy { input { leaf near-rt-ric-url { + type inet:uri; + } + leaf body { type string; } } - output { - leaf-list policy-type-id-list { + leaf body { type string; } + leaf http-status { + type int32; + } } } - // Get an array of integer policy ids - // Each item in the returned array will be regarded as one policy-id. - rpc getPolicyIdentities { + rpc getA1Policy { input { leaf near-rt-ric-url { - type string; + type inet:uri; } } - output { - leaf-list policy-id-list { + leaf body { type string; } + leaf http-status { + type int32; + } } } - // Get a policy type - rpc getPolicyType { + rpc getA1PolicyStatus { input { leaf near-rt-ric-url { - type string; - } - leaf policy-type-id { - type string; + type inet:uri; } } output { - leaf policy-type { + leaf body { type string; } + leaf http-status { + type int32; + } } } - // Create a policy - rpc putPolicy { + rpc getA1PolicyType { input { leaf near-rt-ric-url { - type string; - } - leaf policy-id { - type string; - } - leaf policy-type-id { - type string; - } - leaf policy { - type string; + type inet:uri; } } output { - leaf returned-policy { - type string; - } - } - } - - // Delete a policy - rpc deletePolicy { - input { - leaf near-rt-ric-url { + leaf body { type string; } - leaf policy-id { - type string; + leaf http-status { + type int32; } } } - // Get a policy status - rpc getPolicyStatus { + rpc deleteA1Policy { input { leaf near-rt-ric-url { - type string; - } - leaf policy-id { - type string; + type inet:uri; } } output { - leaf policy-status { + leaf body { type string; } + leaf http-status { + type int32; + } } } } \ No newline at end of file diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java index 7e47b980..b41615b5 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java @@ -23,11 +23,9 @@ package org.onap.sdnc.northbound.provider; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; -import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import org.onap.sdnc.northbound.restadapter.NearRicUrlProvider; import org.onap.sdnc.northbound.restadapter.RestAdapter; import org.onap.sdnc.northbound.restadapter.RestAdapterImpl; import org.opendaylight.controller.md.sal.binding.api.DataBroker; @@ -37,24 +35,23 @@ import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFaile import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.A1ADAPTERAPIService; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyInput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyOutputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesInput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusInput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusOutputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesInput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeInput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyInput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeleteA1PolicyInput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeleteA1PolicyOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeleteA1PolicyOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyInput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyStatusInput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyTypeInput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutA1PolicyInput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutA1PolicyOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutA1PolicyOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.slf4j.Logger; @@ -89,7 +86,6 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { protected RpcProviderRegistry rpcRegistry; protected BindingAwareBroker.RpcRegistration rpcRegistration; private RestAdapter restAdapter; - private NearRicUrlProvider nearRicUrlProvider; public NonrtRicApiProvider(DataBroker dataBroker, NotificationPublishService notificationPublishService, RpcProviderRegistry rpcProviderRegistry) { @@ -106,7 +102,6 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { log.info("Initializing provider for {}", APP_NAME); createContainers(); restAdapter = new RestAdapterImpl(); - nearRicUrlProvider = new NearRicUrlProvider(); log.info("Initialization complete for {}", APP_NAME); } @@ -159,98 +154,91 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { } @Override - public ListenableFuture> getPolicyTypeIdentities( - GetPolicyTypeIdentitiesInput input) { - log.info("Start of getPolicyTypeIdentities"); - GetPolicyTypeIdentitiesOutputBuilder responseBuilder = new GetPolicyTypeIdentitiesOutputBuilder(); - String uri = nearRicUrlProvider.policyTypesUrl(String.valueOf(input.getNearRtRicUrl())); - ResponseEntity> response = restAdapter.get(uri, List.class); + public ListenableFuture> putA1Policy(PutA1PolicyInput input) { + log.info("Start of putPolicy"); + final Uri uri = input.getNearRtRicUrl(); + + log.info("PUT Request input.GetA1Policy() : {} ", uri); + ResponseEntity response = restAdapter.put(uri.getValue(), input.getBody(), String.class); + PutA1PolicyOutputBuilder responseBuilder = new PutA1PolicyOutputBuilder(); if (response.hasBody()) { - log.info("Response getPolicyTypeIdentities : {} ", response.getBody()); - responseBuilder.setPolicyTypeIdList(response.getBody()); + log.info("Response PutA1Policy : {} ", response.getBody()); + responseBuilder.setBody(response.getBody()); } - log.info("End of getPolicyTypeIdentities"); - RpcResult rpcResult = RpcResultBuilder.status(true) + responseBuilder.setHttpStatus(response.getStatusCodeValue()); + log.info("End of PutA1Policy"); + RpcResult rpcResult = RpcResultBuilder.status(true) .withResult(responseBuilder.build()).build(); return Futures.immediateFuture(rpcResult); } @Override - public ListenableFuture> getPolicyIdentities(GetPolicyIdentitiesInput input) { - log.info("Start of getPolicyIdentities"); - GetPolicyIdentitiesOutputBuilder responseBuilder = new GetPolicyIdentitiesOutputBuilder(); - String uri = nearRicUrlProvider.policiesUrl(String.valueOf(input.getNearRtRicUrl())); - ResponseEntity> response = restAdapter.get(uri, List.class); + public ListenableFuture> deleteA1Policy(DeleteA1PolicyInput input) { + log.info("Start of DeleteA1Policy"); + final Uri uri = input.getNearRtRicUrl(); + ResponseEntity response = restAdapter.delete(uri.getValue()); + log.info("End of DeleteA1Policy"); + DeleteA1PolicyOutputBuilder responseBuilder = new DeleteA1PolicyOutputBuilder(); if (response.hasBody()) { - log.info("Response getPolicyIdentities : {} ", response.getBody()); - responseBuilder.setPolicyIdList(response.getBody()); + log.info("Response PutA1Policy : {} ", response.getBody()); + responseBuilder.setBody(response.getBody().toString()); } - log.info("End of getPolicyIdentities"); - RpcResult rpcResult = RpcResultBuilder.status(true) + responseBuilder.setHttpStatus(response.getStatusCodeValue()); + RpcResult rpcResult = RpcResultBuilder.status(true) .withResult(responseBuilder.build()).build(); return Futures.immediateFuture(rpcResult); } - @Override - public ListenableFuture> getPolicyType(GetPolicyTypeInput input) { - log.info("Start of getPolicyType; Policy Type Id : {} ", input.getPolicyTypeId()); - GetPolicyTypeOutputBuilder responseBuilder = new GetPolicyTypeOutputBuilder(); - String uri = nearRicUrlProvider.getPolicyTypeUrl(String.valueOf(input.getNearRtRicUrl()), - String.valueOf(input.getPolicyTypeId())); - ResponseEntity response = restAdapter.get(uri, String.class); + private GetA1PolicyOutput getA1(GetA1PolicyInput input) { + log.info("Start of getA1"); + final Uri uri = input.getNearRtRicUrl(); + ResponseEntity response = restAdapter.get(uri.getValue(), String.class); + log.info("End of getA1"); + GetA1PolicyOutputBuilder responseBuilder = new GetA1PolicyOutputBuilder(); if (response.hasBody()) { - log.info("Response getPolicyType : {} ", response.getBody()); - responseBuilder.setPolicyType(response.getBody()); + log.info("Response getA1 : {} ", response.getBody()); + responseBuilder.setBody(response.getBody()); } - log.info("End of getPolicyType"); - RpcResult rpcResult = RpcResultBuilder.status(true) - .withResult(responseBuilder.build()).build(); - return Futures.immediateFuture(rpcResult); + responseBuilder.setHttpStatus(response.getStatusCodeValue()); + return responseBuilder.build(); } @Override - public ListenableFuture> putPolicy(PutPolicyInput input) { - log.info("Start of putPolicy"); - PutPolicyOutputBuilder responseBuilder = new PutPolicyOutputBuilder(); - String uri = nearRicUrlProvider.putPolicyUrl(String.valueOf(input.getNearRtRicUrl()), - String.valueOf(input.getPolicyId()), String.valueOf(input.getPolicyTypeId())); - log.info("PUT Request input.getPolicy() : {} ", input.getPolicy()); - ResponseEntity response = restAdapter.put(uri, input.getPolicy(), String.class); - if (response.hasBody()) { - log.info("Response putPolicy : {} ", response.getBody()); - responseBuilder.setReturnedPolicy(response.getBody()); - } - log.info("End of putPolicy"); - RpcResult rpcResult = RpcResultBuilder.status(true) - .withResult(responseBuilder.build()).build(); + public ListenableFuture> getA1Policy(GetA1PolicyInput input) { + GetA1PolicyOutput output = getA1(input); + RpcResult rpcResult = RpcResultBuilder.status(true).withResult(output) + .build(); return Futures.immediateFuture(rpcResult); } @Override - public ListenableFuture> deletePolicy(DeletePolicyInput input) { - log.info("Start of deletePolicy"); - DeletePolicyOutputBuilder responseBuilder = new DeletePolicyOutputBuilder(); - String uri = nearRicUrlProvider.deletePolicyUrl(String.valueOf(input.getNearRtRicUrl()), - String.valueOf(input.getPolicyId())); - restAdapter.delete(uri); - log.info("End of deletePolicy"); - RpcResult rpcResult = RpcResultBuilder.status(true) - .withResult(responseBuilder.build()).build(); - return Futures.immediateFuture(rpcResult); + public ListenableFuture> getA1PolicyStatus(GetA1PolicyStatusInput input) { + GetA1PolicyInputBuilder getInputBuilder = new GetA1PolicyInputBuilder(); + getInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl()); + GetA1PolicyOutput getOutput = getA1(getInputBuilder.build()); + + GetA1PolicyStatusOutputBuilder outputBuilder = new GetA1PolicyStatusOutputBuilder(); + outputBuilder.setBody(getOutput.getBody()); + outputBuilder.setHttpStatus(getOutput.getHttpStatus()); + + return Futures.immediateFuture(RpcResultBuilder.status(true) // + .withResult(outputBuilder.build()) // + .build()); } @Override - public ListenableFuture> getPolicyStatus(GetPolicyStatusInput input) { - log.debug("Policy Id : {} ", input.getPolicyId()); - GetPolicyStatusOutputBuilder responseBuilder = new GetPolicyStatusOutputBuilder(); - String uri = nearRicUrlProvider.getPolicyStatusUrl(input.getNearRtRicUrl(), input.getPolicyId()); - ResponseEntity response = restAdapter.get(uri, String.class); - if (response.hasBody()) { - log.info("Response getPolicyStatus : {} ", response.getBody()); - responseBuilder.setPolicyStatus(response.getBody()); - } - RpcResult rpcResult = RpcResultBuilder.status(true) - .withResult(responseBuilder.build()).build(); - return Futures.immediateFuture(rpcResult); + public ListenableFuture> getA1PolicyType(GetA1PolicyTypeInput input) { + GetA1PolicyInputBuilder getInputBuilder = new GetA1PolicyInputBuilder(); + getInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl()); + GetA1PolicyOutput getOutput = getA1(getInputBuilder.build()); + + GetA1PolicyTypeOutputBuilder outputBuilder = new GetA1PolicyTypeOutputBuilder(); + outputBuilder.setBody(getOutput.getBody()); + outputBuilder.setHttpStatus(getOutput.getHttpStatus()); + + return Futures.immediateFuture(RpcResultBuilder.status(true) // + .withResult(outputBuilder.build()) // + .build()); } + } 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 deleted file mode 100644 index df83226b..00000000 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadapter/NearRicUrlProvider.java +++ /dev/null @@ -1,107 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2020 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.sdnc.northbound.restadapter; - -import org.springframework.web.util.UriComponentsBuilder; - -/** - * This class provides Near-RIC api to invoke the A1 interface - * - * @author lathishbabu.ganesan@est.tech - * - */ - -public class NearRicUrlProvider { - - /** - * Retrieve the base url of the Near-RIC - * - * @param nearRtRicUrl the near-rt-ric url - * @return the base url - */ - public String getBaseUrl(final String nearRtRicUrl) { - return nearRtRicUrl + "/A1-P/v1"; - } - - /** - * Retrieve the policytypes url - * - * @param nearRtRicUrl the near-rt-ric url - * @return the policytypes url - */ - public String policyTypesUrl(final String nearRtRicUrl) { - return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicUrl)).pathSegment("policytypes").build().toString(); - } - - /** - * Retrieve the policies url - * - * @param nearRtRicUrl the near-rt-ric url - * @return the policies url - */ - public String policiesUrl(final String nearRtRicUrl) { - return getBaseUrl(nearRtRicUrl) + "/policies"; - } - - /** - * Retrieve the url of policy type - * - * @param nearRtRicUrl the near-rt-ric url - * @param policyTypeId Policy Type Id - * @return the policy type url - */ - public String getPolicyTypeUrl(final String nearRtRicUrl, final String policyTypeId) { - return UriComponentsBuilder.fromUriString(policyTypesUrl(nearRtRicUrl)).pathSegment(policyTypeId).build() - .toString(); - } - - /** - * Retrieve the url of putPolicy - * - * @param nearRtRicUrl the near-rt-ric url - * @param policyId Policy Id - * @param policyTypeId Policy Type Id - * @return the putPolicy url - */ - public String putPolicyUrl(final String nearRtRicUrl, final String policyId, final String policyTypeId) { - UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(policiesUrl(nearRtRicUrl)).pathSegment(policyId); - - if (!policyTypeId.isEmpty()) { - builder.queryParam("policyTypeId", policyTypeId); - } - return builder.build().toString(); - } - - /** - * Retrieve the url of deletePolicy - * - * @param nearRtRicUrl the near-rt-ric url - * @param policyId Policy Id - * @return the deletePolicy url - */ - public String deletePolicyUrl(final String nearRtRicUrl, final String policyId) { - return UriComponentsBuilder.fromUriString(policiesUrl(nearRtRicUrl)).pathSegment(policyId).build().toString(); - } - - public String getPolicyStatusUrl(String nearRtRicUrl, String policyId) { - return policiesUrl(nearRtRicUrl) + "/" + policyId + "/status"; - } -} diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java index 11464071..94b2f923 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java @@ -22,10 +22,9 @@ package org.onap.sdnc.northbound; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; -import com.google.common.util.concurrent.ListenableFuture; -import java.util.ArrayList; -import java.util.List; + import java.util.concurrent.ExecutionException; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -34,23 +33,22 @@ import org.mockito.Mock; import org.mockito.internal.util.reflection.Whitebox; import org.mockito.runners.MockitoJUnitRunner; import org.onap.sdnc.northbound.provider.NonrtRicApiProvider; -import org.onap.sdnc.northbound.restadapter.NearRicUrlProvider; import org.onap.sdnc.northbound.restadapter.RestAdapter; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesInputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusInputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesInputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeInputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutput; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyInputBuilder; -import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutput; -import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeleteA1PolicyInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeleteA1PolicyOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyStatusInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyTypeInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutA1PolicyInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutA1PolicyOutput; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -75,93 +73,78 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { protected RpcProviderRegistry mockRpcProviderRegistry; @Mock private RestAdapter restAdapter; - private NearRicUrlProvider nearRicUrlProvider; - private static String nearRtRicUrl = "http://ric1:8085"; - private static String policyTypeId = "STD_QoSNudging_0.1.0"; - private static String policyId = "3d2157af-6a8f-4a7c-810f-38c2f824bf12"; + private static Uri nearRtRicUrl = new Uri("http://ric1:8085"); @Before public void setUp() throws Exception { - nearRicUrlProvider = new NearRicUrlProvider(); dataBroker = getDataBroker(); nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService, mockRpcProviderRegistry); } @Test - public void testGetPolicyTypeIdentities() throws InterruptedException, ExecutionException { - GetPolicyTypeIdentitiesInputBuilder inputBuilder = new GetPolicyTypeIdentitiesInputBuilder(); + public void testGetA1Policy() throws InterruptedException, ExecutionException { + GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); - String uri = nearRicUrlProvider.policyTypesUrl(inputBuilder.build().getNearRtRicUrl()); - List policyTypeIdentities = new ArrayList<>(); - policyTypeIdentities.add(policyTypeId); - ResponseEntity getPolicyTypeIdentitiesResponse = new ResponseEntity<>(policyTypeIdentities, HttpStatus.OK); - when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(getPolicyTypeIdentitiesResponse); - ListenableFuture> result = nonrtRicApiProvider - .getPolicyTypeIdentities(inputBuilder.build()); - Assert.assertEquals(policyTypeIdentities, result.get().getResult().getPolicyTypeIdList()); + String returnedBody = "returned body"; + ResponseEntity getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK); + when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse); + GetA1PolicyOutput result = nonrtRicApiProvider.getA1Policy(inputBuilder.build()).get().getResult(); + Assert.assertEquals(returnedBody, result.getBody()); + Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus()); } @Test - public void testGetPolicyIdentities() throws InterruptedException, ExecutionException { - GetPolicyIdentitiesInputBuilder inputBuilder = new GetPolicyIdentitiesInputBuilder(); + public void testGetA1PolicyType() throws InterruptedException, ExecutionException { + GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); - String uri = nearRicUrlProvider.policiesUrl(inputBuilder.build().getNearRtRicUrl()); - List policyIdentities = new ArrayList<>(); - policyIdentities.add(policyId); - ResponseEntity getPolicyIdentitiesResponse = new ResponseEntity<>(policyIdentities, HttpStatus.OK); - when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(getPolicyIdentitiesResponse); - ListenableFuture> result = nonrtRicApiProvider - .getPolicyIdentities(inputBuilder.build()); - Assert.assertEquals(policyIdentities, result.get().getResult().getPolicyIdList()); + String returnedBody = "returned body"; + ResponseEntity getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK); + when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse); + GetA1PolicyTypeOutput result = nonrtRicApiProvider.getA1PolicyType(inputBuilder.build()).get().getResult(); + Assert.assertEquals(returnedBody, result.getBody()); + Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus()); } @Test - public void testGetPolicyType() throws InterruptedException, ExecutionException { - GetPolicyTypeInputBuilder inputBuilder = new GetPolicyTypeInputBuilder(); + public void testGetA1PolicyStatus() throws InterruptedException, ExecutionException { + GetA1PolicyStatusInputBuilder inputBuilder = new GetA1PolicyStatusInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); - inputBuilder.setPolicyTypeId(policyTypeId); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); - String uri = nearRicUrlProvider.getPolicyTypeUrl(inputBuilder.build().getNearRtRicUrl(), - String.valueOf(inputBuilder.build().getPolicyTypeId())); - String testPolicyType = "{}"; - ResponseEntity getPolicyTypeResponse = new ResponseEntity<>(testPolicyType, HttpStatus.OK); - when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(getPolicyTypeResponse); - ListenableFuture> result = nonrtRicApiProvider.getPolicyType(inputBuilder.build()); - Assert.assertEquals(testPolicyType, result.get().getResult().getPolicyType()); + String returnedBody = "returned body"; + ResponseEntity getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK); + when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse); + GetA1PolicyStatusOutput result = nonrtRicApiProvider.getA1PolicyStatus(inputBuilder.build()).get().getResult(); + Assert.assertEquals(returnedBody, result.getBody()); + Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus()); } @Test - public void testPutPolicy() throws InterruptedException, ExecutionException { - PutPolicyInputBuilder inputBuilder = new PutPolicyInputBuilder(); + public void testPutA1Policy() throws InterruptedException, ExecutionException { + PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder(); String testPolicy = "{}"; inputBuilder.setNearRtRicUrl(nearRtRicUrl); - inputBuilder.setPolicyId(policyId); - inputBuilder.setPolicyTypeId(policyTypeId); - inputBuilder.setPolicy(testPolicy); + inputBuilder.setBody(testPolicy); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); - String uri = nearRicUrlProvider.putPolicyUrl(inputBuilder.build().getNearRtRicUrl(), inputBuilder.getPolicyId(), - inputBuilder.getPolicyTypeId()); - ResponseEntity putPolicyResponse = new ResponseEntity<>(testPolicy, HttpStatus.CREATED); - when(restAdapter.put(eq(uri), eq(testPolicy), eq(String.class))).thenReturn(putPolicyResponse); - ListenableFuture> result = nonrtRicApiProvider.putPolicy(inputBuilder.build()); - Assert.assertEquals(testPolicy, result.get().getResult().getReturnedPolicy()); + String returnedBody = "returned body"; + ResponseEntity putResponse = new ResponseEntity<>(returnedBody, HttpStatus.CREATED); + when(restAdapter.put(eq(nearRtRicUrl.getValue()), eq(testPolicy), eq(String.class))).thenReturn(putResponse); + PutA1PolicyOutput result = nonrtRicApiProvider.putA1Policy(inputBuilder.build()).get().getResult(); + Assert.assertEquals(returnedBody, result.getBody()); + Assert.assertTrue(HttpStatus.CREATED.value() == result.getHttpStatus()); } @Test - public void testGetPolicyStatus() throws InterruptedException, ExecutionException { - GetPolicyStatusInputBuilder inputBuilder = new GetPolicyStatusInputBuilder(); + public void testDeleteA1() throws InterruptedException, ExecutionException { + DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); - inputBuilder.setPolicyId(policyId); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); - String uri = nearRicUrlProvider.getPolicyStatusUrl(nearRtRicUrl, policyId); - String testPolicyStatus = "STATUS"; - ResponseEntity getPolicyStatusResponse = new ResponseEntity<>(testPolicyStatus, HttpStatus.OK); - when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(getPolicyStatusResponse); - ListenableFuture> result = nonrtRicApiProvider - .getPolicyStatus(inputBuilder.build()); - Assert.assertEquals(testPolicyStatus, result.get().getResult().getPolicyStatus()); + + ResponseEntity getResponse = new ResponseEntity<>(HttpStatus.NO_CONTENT); + when(restAdapter.delete(nearRtRicUrl.getValue())).thenReturn(getResponse); + DeleteA1PolicyOutput result = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult(); + Assert.assertTrue(HttpStatus.NO_CONTENT.value() == result.getHttpStatus()); } } -- 2.16.6