diff options
Diffstat (limited to 'a1-policy-management')
11 files changed, 685 insertions, 167 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1Client.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1Client.java index 6f1bf357..4321ed48 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1Client.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1Client.java @@ -36,8 +36,10 @@ public interface A1Client { public enum A1ProtocolType { UNKNOWN, // STD_V1_1, // STD A1 version 1.1 + STD_V2_0_0, // STD A1 version 2.0.0 OSC_V1, // OSC 'A1' SDNC_OSC_STD_V1_1, // SDNC_OSC with STD A1 version 1.1 southbound + SDNC_OSC_STD_V2_0_0, // SDNC_OSC with STD A1 version 2.0.0 southbound SDNC_OSC_OSC_V1, // SDNC_OSC with OSC 'A1' southbound SDNC_ONAP } @@ -57,4 +59,5 @@ public interface A1Client { public Flux<String> deleteAllPolicies(); public Mono<String> getPolicyStatus(Policy policy); + } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java index f0f03cfe..66ba60c0 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java @@ -73,10 +73,14 @@ public class A1ClientFactory { if (version == A1ProtocolType.STD_V1_1) { assertNoControllerConfig(ric, version); return new StdA1ClientVersion1(ric.getConfig(), this.restClientFactory); + } else if (version == A1ProtocolType.STD_V2_0_0) { + assertNoControllerConfig(ric, version); + return new StdA1ClientVersion2(ric.getConfig(), this.restClientFactory); } else if (version == A1ProtocolType.OSC_V1) { assertNoControllerConfig(ric, version); return new OscA1Client(ric.getConfig(), this.restClientFactory); - } else if (version == A1ProtocolType.SDNC_OSC_STD_V1_1 || version == A1ProtocolType.SDNC_OSC_OSC_V1) { + } else if (version == A1ProtocolType.SDNC_OSC_STD_V1_1 || version == A1ProtocolType.SDNC_OSC_OSC_V1 + || version == A1ProtocolType.SDNC_OSC_STD_V2_0_0) { return new SdncOscA1Client(version, ric.getConfig(), getControllerConfig(ric), this.restClientFactory); } else if (version == A1ProtocolType.SDNC_ONAP) { return new SdncOnapA1Client(ric.getConfig(), getControllerConfig(ric), this.restClientFactory); @@ -118,7 +122,8 @@ public class A1ClientFactory { private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) { if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) { - return fetchVersion(ric, A1ProtocolType.STD_V1_1) // + return fetchVersion(ric, A1ProtocolType.STD_V2_0_0) // + .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.STD_V1_1)) // .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.OSC_V1)) // .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.SDNC_OSC_STD_V1_1)) // .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.SDNC_ONAP)) // diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1UriBuilder.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1UriBuilder.java index d29226c8..08ea0e8c 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1UriBuilder.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1UriBuilder.java @@ -29,4 +29,10 @@ interface A1UriBuilder { String createDeleteUri(String type, String policyId); String createGetPolicyStatusUri(String type, String policyId); + + String createPolicyTypesUri(); + + String createGetPolicyIdsUri(String type); + + String createGetSchemaUri(String type); } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java index 741ab1b9..3643b8d2 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java @@ -37,7 +37,7 @@ import reactor.core.publisher.Mono; */ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally public class OscA1Client implements A1Client { - static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent to one NearRT RIC + static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC public static class UriBuilder implements A1UriBuilder { private final RicConfig ricConfig; @@ -54,6 +54,7 @@ public class OscA1Client implements A1Client { /** * /a1-p/policytypes/{policy_type_id}/policies */ + @Override public String createGetPolicyIdsUri(String type) { return createPolicyTypeUri(type) + "/policies"; } @@ -81,6 +82,7 @@ public class OscA1Client implements A1Client { /** * /a1-p/policytypes/{policy_type_id} */ + @Override public String createGetSchemaUri(String type) { return this.createPolicyTypeUri(type); } @@ -88,6 +90,7 @@ public class OscA1Client implements A1Client { /** * /a1-p/policytypes/{policy_type_id} */ + @Override public String createPolicyTypesUri() { return baseUri() + "/policytypes"; } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1Client.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1Client.java index 3ba2ba7b..28c27843 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1Client.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1Client.java @@ -48,7 +48,7 @@ import reactor.core.publisher.Mono; @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally public class SdncOscA1Client implements A1Client { - static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent to one NearRT RIC + static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC @Value.Immutable @org.immutables.gson.Gson.TypeAdapters @@ -81,7 +81,8 @@ public class SdncOscA1Client implements A1Client { * Constructor that creates the REST client to use. * * @param protocolType the southbound protocol of the controller. Supported - * protocols are SDNC_OSC_STD_V1_1 and SDNC_OSC_OSC_V1 + * protocols are SDNC_OSC_STD_V1_1, SDNC_OSC_OSC_V1 and + * SDNC_OSC_STD_V2_0_0 with * @param ricConfig the configuration of the Near-RT RIC to communicate * with * @param controllerConfig the configuration of the SDNC controller to use @@ -92,32 +93,35 @@ public class SdncOscA1Client implements A1Client { AsyncRestClientFactory restClientFactory) { this(protocolType, ricConfig, controllerConfig, restClientFactory.createRestClient(controllerConfig.baseUrl() + "/restconf/operations")); - logger.debug("SdncOscA1Client for ric: {}, a1Controller: {}", ricConfig.ricId(), controllerConfig); } /** * Constructor where the REST client to use is provided. * * @param protocolType the southbound protocol of the controller. Supported - * protocols are SDNC_OSC_STD_V1_1 and SDNC_OSC_OSC_V1 + * protocols are SDNC_OSC_STD_V1_1, SDNC_OSC_OSC_V1 and + * SDNC_OSC_STD_V2_0_0 with * @param ricConfig the configuration of the Near-RT RIC to communicate * with * @param controllerConfig the configuration of the SDNC controller to use * @param restClient the REST client to use * - * @throws IllegalArgumentException when the protocolType is wrong. + * @throws IllegalArgumentException when the protocolType is illegal. */ public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig, AsyncRestClient restClient) { - if (!(A1ProtocolType.SDNC_OSC_STD_V1_1.equals(protocolType) - || A1ProtocolType.SDNC_OSC_OSC_V1.equals(protocolType))) { - throw new IllegalArgumentException("Protocol type must be " + A1ProtocolType.SDNC_OSC_STD_V1_1 + " or " - + A1ProtocolType.SDNC_OSC_OSC_V1 + ", was: " + protocolType); + if (A1ProtocolType.SDNC_OSC_STD_V1_1.equals(protocolType) // + || A1ProtocolType.SDNC_OSC_OSC_V1.equals(protocolType) // + || A1ProtocolType.SDNC_OSC_STD_V2_0_0.equals(protocolType)) { + this.restClient = restClient; + this.ricConfig = ricConfig; + this.protocolType = protocolType; + this.controllerConfig = controllerConfig; + logger.debug("SdncOscA1Client for ric: {}, a1Controller: {}", ricConfig.ricId(), controllerConfig); + } else { + throw new IllegalArgumentException("Not handeled protocolversion: " + protocolType); } - this.restClient = restClient; - this.ricConfig = ricConfig; - this.protocolType = protocolType; - this.controllerConfig = controllerConfig; + } @Override @@ -125,9 +129,7 @@ public class SdncOscA1Client implements A1Client { if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { return Mono.just(Arrays.asList("")); } else { - OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig); - final String ricUrl = uri.createPolicyTypesUri(); - return post(GET_POLICY_RPC, ricUrl, Optional.empty()) // + return post(GET_POLICY_RPC, getUriBuilder().createPolicyTypesUri(), Optional.empty()) // .flatMapMany(SdncJsonHelper::parseJsonArrayOfString) // .collectList(); } @@ -145,20 +147,27 @@ public class SdncOscA1Client implements A1Client { if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { return Mono.just("{}"); } else { - OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig); + A1UriBuilder uri = this.getUriBuilder(); final String ricUrl = uri.createGetSchemaUri(policyTypeId); return post(GET_POLICY_RPC, ricUrl, Optional.empty()) // - .flatMap(response -> OscA1Client.extractCreateSchema(response, policyTypeId)); + .flatMap(response -> extractCreateSchema(response, policyTypeId)); + } + } + + private Mono<String> extractCreateSchema(String controllerResponse, String policyTypeId) { + if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { + return OscA1Client.extractCreateSchema(controllerResponse, policyTypeId); + } else if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V2_0_0) { + return StdA1ClientVersion2.extractPolicySchema(controllerResponse, policyTypeId); + } else { + throw new NullPointerException("Not supported"); } } @Override public Mono<String> putPolicy(Policy policy) { - return getUriBuilder() // - .flatMap(builder -> { - String ricUrl = builder.createPutPolicyUri(policy.type().id(), policy.id()); - return post("putA1Policy", ricUrl, Optional.of(policy.json())); - }); + String ricUrl = getUriBuilder().createPutPolicyUri(policy.type().id(), policy.id()); + return post("putA1Policy", ricUrl, Optional.of(policy.json())); } @Override @@ -172,44 +181,46 @@ public class SdncOscA1Client implements A1Client { return getPolicyIds() // .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); // } else { - OscA1Client.UriBuilder uriBuilder = new OscA1Client.UriBuilder(ricConfig); + A1UriBuilder uriBuilder = this.getUriBuilder(); return getPolicyTypeIdentities() // .flatMapMany(Flux::fromIterable) // - .flatMap(type -> oscDeleteInstancesForType(uriBuilder, type), CONCURRENCY_RIC); + .flatMap(type -> deleteAllInstancesForType(uriBuilder, type), CONCURRENCY_RIC); } } - private Flux<String> oscGetInstancesForType(OscA1Client.UriBuilder uriBuilder, String type) { + private Flux<String> getInstancesForType(A1UriBuilder uriBuilder, String type) { return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) // .flatMapMany(SdncJsonHelper::parseJsonArrayOfString); } - private Flux<String> oscDeleteInstancesForType(OscA1Client.UriBuilder uriBuilder, String type) { - return oscGetInstancesForType(uriBuilder, type) // + private Flux<String> deleteAllInstancesForType(A1UriBuilder uriBuilder, String type) { + return getInstancesForType(uriBuilder, type) // .flatMap(instance -> deletePolicyById(type, instance), CONCURRENCY_RIC); } @Override public Mono<A1ProtocolType> getProtocolVersion() { - return tryStdProtocolVersion() // + return tryStdProtocolVersion2() // + .onErrorResume(t -> tryStdProtocolVersion1()) // .onErrorResume(t -> tryOscProtocolVersion()); } @Override public Mono<String> getPolicyStatus(Policy policy) { - return getUriBuilder() // - .flatMap(builder -> { - String ricUrl = builder.createGetPolicyStatusUri(policy.type().id(), policy.id()); - return post("getA1PolicyStatus", ricUrl, Optional.empty()); - }); + String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.type().id(), policy.id()); + return post("getA1PolicyStatus", ricUrl, Optional.empty()); + } - private Mono<A1UriBuilder> getUriBuilder() { + private A1UriBuilder getUriBuilder() { if (protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { - return Mono.just(new StdA1ClientVersion1.UriBuilder(ricConfig)); - } else { - return Mono.just(new OscA1Client.UriBuilder(ricConfig)); + return new StdA1ClientVersion1.UriBuilder(ricConfig); + } else if (protocolType == A1ProtocolType.SDNC_OSC_STD_V2_0_0) { + return new StdA1ClientVersion2.UriBuilder(ricConfig); + } else if (protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { + return new OscA1Client.UriBuilder(ricConfig); } + throw new NullPointerException(); } private Mono<A1ProtocolType> tryOscProtocolVersion() { @@ -218,20 +229,26 @@ public class SdncOscA1Client implements A1Client { .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC_OSC_V1)); } - private Mono<A1ProtocolType> tryStdProtocolVersion() { + private Mono<A1ProtocolType> tryStdProtocolVersion1() { StdA1ClientVersion1.UriBuilder uriBuilder = new StdA1ClientVersion1.UriBuilder(ricConfig); - return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(), Optional.empty()) // + return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(""), Optional.empty()) // .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC_STD_V1_1)); } + private Mono<A1ProtocolType> tryStdProtocolVersion2() { + StdA1ClientVersion2.UriBuilder uriBuilder = new StdA1ClientVersion2.UriBuilder(ricConfig); + return post(GET_POLICY_RPC, uriBuilder.createPolicyTypesUri(), Optional.empty()) // + .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC_STD_V2_0_0)); + } + private Flux<String> getPolicyIds() { if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { StdA1ClientVersion1.UriBuilder uri = new StdA1ClientVersion1.UriBuilder(ricConfig); - final String ricUrl = uri.createGetPolicyIdsUri(); + final String ricUrl = uri.createGetPolicyIdsUri(""); return post(GET_POLICY_RPC, ricUrl, Optional.empty()) // .flatMapMany(SdncJsonHelper::parseJsonArrayOfString); } else { - OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig); + A1UriBuilder uri = this.getUriBuilder(); return getPolicyTypeIdentities() // .flatMapMany(Flux::fromIterable) .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) // @@ -240,11 +257,8 @@ public class SdncOscA1Client implements A1Client { } private Mono<String> deletePolicyById(String type, String policyId) { - return getUriBuilder() // - .flatMap(builder -> { - String ricUrl = builder.createDeleteUri(type, policyId); - return post("deleteA1Policy", ricUrl, Optional.empty()); - }); + String ricUrl = getUriBuilder().createDeleteUri(type, policyId); + return post("deleteA1Policy", ricUrl, Optional.empty()); } private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) { @@ -270,8 +284,9 @@ public class SdncOscA1Client implements A1Client { } else { logger.debug("Error response: {} {}", output.httpStatus(), body); byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8); - WebClientResponseException responseException = new WebClientResponseException(output.httpStatus(), - "statusText", null, responseBodyBytes, StandardCharsets.UTF_8, null); + HttpStatus httpStatus = HttpStatus.valueOf(output.httpStatus()); + WebClientResponseException responseException = new WebClientResponseException(httpStatus.value(), + httpStatus.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null); return Mono.error(responseException); } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java index d094e7a1..94567234 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java @@ -53,7 +53,8 @@ public class StdA1ClientVersion1 implements A1Client { /** * /A1-P/v1/policies */ - public String createGetPolicyIdsUri() { + @Override + public String createGetPolicyIdsUri(String type) { return baseUri() + "/policies"; } @@ -68,6 +69,7 @@ public class StdA1ClientVersion1 implements A1Client { /** * /A1-P/v1/policies/{policyId}/status */ + @Override public String createGetPolicyStatusUri(String type, String policyId) { return policiesBaseUri() + policyId + "/status"; } @@ -77,7 +79,17 @@ public class StdA1ClientVersion1 implements A1Client { } private String policiesBaseUri() { - return createGetPolicyIdsUri() + "/"; + return createGetPolicyIdsUri("") + "/"; + } + + @Override + public String createPolicyTypesUri() { + throw new NullPointerException("Not supported URI"); + } + + @Override + public String createGetSchemaUri(String type) { + throw new NullPointerException("Not supported URI"); } } @@ -137,7 +149,7 @@ public class StdA1ClientVersion1 implements A1Client { } private Flux<String> getPolicyIds() { - return restClient.get(uri.createGetPolicyIdsUri()) // + return restClient.get(uri.createGetPolicyIdsUri("")) // .flatMapMany(SdncJsonHelper::parseJsonArrayOfString); } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion2.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion2.java new file mode 100644 index 00000000..c475b22f --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion2.java @@ -0,0 +1,208 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2020 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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.onap.ccsdk.oran.a1policymanagementservice.clients; + +import java.lang.invoke.MethodHandles; +import java.util.List; + +import org.json.JSONObject; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * Client for accessing ORAN A1-P Vesion 2.0 REST API + */ +@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally +public class StdA1ClientVersion2 implements A1Client { + static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC + + public static class UriBuilder implements A1UriBuilder { + private final RicConfig ricConfig; + + public UriBuilder(RicConfig ricConfig) { + this.ricConfig = ricConfig; + } + + @Override + public String createPutPolicyUri(String type, String policyId) { + return createPolicyUri(type, policyId); + } + + /** + * /A1-P/v2/policytypes/{policy_type_id}/policies + */ + @Override + public String createGetPolicyIdsUri(String type) { + return createPolicyTypeUri(type) + "/policies"; + } + + @Override + public String createDeleteUri(String type, String policyId) { + return createPolicyUri(type, policyId); + } + + /** + * /A1-P/v2/policytypes/{policy_type_id}/policies/{policy_instance_id}/status + */ + @Override + public String createGetPolicyStatusUri(String type, String policyId) { + return createPolicyUri(type, policyId) + "/status"; + } + + /** + * /A1-P/v2/policytypes/{policy_type_id} + */ + @Override + public String createGetSchemaUri(String type) { + return this.createPolicyTypeUri(type); + } + + /** + * /A1-P/v2/policytypes/{policy_type_id} + */ + @Override + public String createPolicyTypesUri() { + return baseUri() + "/policytypes"; + } + + /** + * /A1-P/v2/policytypes/{policy_type_id}/policies/{policy_instance_id} + */ + private String createPolicyUri(String type, String id) { + return createPolicyTypeUri(type) + "/policies/" + id; + } + + /** + * /A1-P/v2/policytypes/{policy_type_id} + */ + private String createPolicyTypeUri(String type) { + return createPolicyTypesUri() + "/" + type; + } + + private String baseUri() { + return ricConfig.baseUrl() + "/A1-P/v2"; + } + } + + private static final String TITLE = "title"; + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final AsyncRestClient restClient; + private final UriBuilder uriBuiler; + + public StdA1ClientVersion2(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) { + this(ricConfig, restClientFactory.createRestClient("")); + } + + public StdA1ClientVersion2(RicConfig ricConfig, AsyncRestClient restClient) { + this.restClient = restClient; + logger.debug("OscA1Client for ric: {}", ricConfig.ricId()); + + uriBuiler = new UriBuilder(ricConfig); + } + + public static Mono<String> extractPolicySchema(String policyTypeResponse, String policyTypeId) { + try { + JSONObject obj = new JSONObject(policyTypeResponse); + JSONObject schemaObj = obj.getJSONObject("policySchema"); + schemaObj.put(TITLE, policyTypeId); + return Mono.just(schemaObj.toString()); + } catch (Exception e) { + String exceptionString = e.toString(); + logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString); + return Mono.error(e); + } + } + + @Override + public Mono<List<String>> getPolicyTypeIdentities() { + return getPolicyTypeIds() // + .collectList(); + } + + @Override + public Mono<List<String>> getPolicyIdentities() { + return getPolicyTypeIds() // + .flatMap(this::getPolicyIdentitiesByType) // + .collectList(); + } + + @Override + public Mono<String> getPolicyTypeSchema(String policyTypeId) { + String schemaUri = uriBuiler.createGetSchemaUri(policyTypeId); + return restClient.get(schemaUri) // + .flatMap(response -> extractPolicySchema(response, policyTypeId)); + } + + @Override + public Mono<String> putPolicy(Policy policy) { + String policyUri = this.uriBuiler.createPutPolicyUri(policy.type().id(), policy.id()); + return restClient.put(policyUri, policy.json()); + } + + @Override + public Mono<String> deletePolicy(Policy policy) { + return deletePolicyById(policy.type().id(), policy.id()); + } + + @Override + public Mono<A1ProtocolType> getProtocolVersion() { + return restClient.get(uriBuiler.createPolicyTypesUri()) // + .flatMap(notUsed -> Mono.just(A1ProtocolType.STD_V2_0_0)); + } + + @Override + public Flux<String> deleteAllPolicies() { + return getPolicyTypeIds() // + .flatMap(this::deletePoliciesForType, CONCURRENCY_RIC); + } + + @Override + public Mono<String> getPolicyStatus(Policy policy) { + String statusUri = uriBuiler.createGetPolicyStatusUri(policy.type().id(), policy.id()); + return restClient.get(statusUri); + + } + + private Flux<String> getPolicyTypeIds() { + return restClient.get(uriBuiler.createPolicyTypesUri()) // + .flatMapMany(SdncJsonHelper::parseJsonArrayOfString); + } + + private Flux<String> getPolicyIdentitiesByType(String typeId) { + return restClient.get(uriBuiler.createGetPolicyIdsUri(typeId)) // + .flatMapMany(SdncJsonHelper::parseJsonArrayOfString); + } + + private Mono<String> deletePolicyById(String typeId, String policyId) { + String policyUri = uriBuiler.createDeleteUri(typeId, policyId); + return restClient.delete(policyUri); + } + + private Flux<String> deletePoliciesForType(String typeId) { + return getPolicyIdentitiesByType(typeId) // + .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RicSynchronizationTask.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RicSynchronizationTask.java index cf7ca74e..88d99232 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RicSynchronizationTask.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RicSynchronizationTask.java @@ -59,7 +59,7 @@ import reactor.core.publisher.SignalType; public class RicSynchronizationTask { private static final Logger logger = LoggerFactory.getLogger(RicSynchronizationTask.class); - static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent to one NearRT RIC + static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC private final A1ClientFactory a1ClientFactory; private final PolicyTypes policyTypes; diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java index bbb2b8e9..a0e95177 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java @@ -37,7 +37,6 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -50,7 +49,6 @@ import org.onap.ccsdk.oran.a1policymanagementservice.clients.SdncOscA1Client.Ada import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableControllerConfig; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.client.WebClientResponseException; @@ -68,7 +66,6 @@ class SdncOscA1ClientTest { 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\"}}"; SdncOscA1Client clientUnderTest; @@ -85,32 +82,25 @@ class SdncOscA1ClientTest { .build(); } - @BeforeEach - void init() { - Ric ric = A1ClientHelper.createRic(RIC_1_URL); - - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(), - asyncRestClientMock); - } - @Test void createClientWithWrongProtocol_thenErrorIsThrown() { - AsyncRestClient asyncRestClient = new AsyncRestClient("", null); assertThrows(IllegalArgumentException.class, () -> { - new SdncOscA1Client(A1ProtocolType.STD_V1_1, null, null, asyncRestClient); + new SdncOscA1Client(A1ProtocolType.STD_V1_1, null, null, new AsyncRestClient("", null)); }); } @Test - void getPolicyTypeIdentities_STD() { + void getPolicyTypeIdentities_STD_V1() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); assertEquals(1, policyTypeIds.size(), "should hardcoded to one"); assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); } - @Test - void getPolicyTypeIdentities_OSC() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + private void testGetPolicyTypeIdentities(A1ProtocolType protocolType, String expUrl) { + clientUnderTest = new SdncOscA1Client(protocolType, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // controllerConfig(), asyncRestClientMock); @@ -122,7 +112,6 @@ class SdncOscA1ClientTest { assertEquals(1, policyTypeIds.size()); assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0)); - String expUrl = RIC_1_URL + "/a1-p/policytypes"; ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .build(); @@ -132,28 +121,64 @@ class SdncOscA1ClientTest { } @Test - void getTypeSchema_STD() { + void getPolicyTypeIdentities_OSC() { + testGetPolicyTypeIdentities(A1ProtocolType.SDNC_OSC_OSC_V1, RIC_1_URL + "/a1-p/policytypes"); + } + + @Test + void getPolicyTypeIdentities_STD_V2() { + testGetPolicyTypeIdentities(A1ProtocolType.SDNC_OSC_STD_V2_0_0, RIC_1_URL + "/A1-P/v2/policytypes"); + } + + @Test + void getTypeSchema_STD_V1() { + + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + String policyType = clientUnderTest.getPolicyTypeSchema("").block(); assertEquals("{}", policyType); } - @Test - void getTypeSchema_OSC() throws IOException { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + private void testGetTypeSchema(A1ProtocolType protocolType, String expUrl, String policyTypeId, + String getSchemaResponseFile) throws IOException { + clientUnderTest = new SdncOscA1Client(protocolType, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // controllerConfig(), asyncRestClientMock); - String ricResponse = loadFile("test_osc_get_schema_response.json"); + String ricResponse = loadFile(getSchemaResponseFile); JsonElement elem = gson().fromJson(ricResponse, JsonElement.class); String responseFromController = createOkResponseWithBody(elem); whenAsyncPostThenReturn(Mono.just(responseFromController)); - String response = clientUnderTest.getPolicyTypeSchema("policyTypeId").block(); + String response = clientUnderTest.getPolicyTypeSchema(policyTypeId).block(); JsonElement respJson = gson().fromJson(response, JsonElement.class); - assertEquals("policyTypeId", respJson.getAsJsonObject().get("title").getAsString(), + assertEquals(policyTypeId, respJson.getAsJsonObject().get("title").getAsString(), "title should be updated to contain policyType ID"); + + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); + + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + } + + @Test + void getTypeSchema_OSC() throws IOException { + String expUrl = RIC_1_URL + "/a1-p/policytypes/policyTypeId"; + testGetTypeSchema(A1ProtocolType.SDNC_OSC_OSC_V1, expUrl, "policyTypeId", "test_osc_get_schema_response.json"); + } + + @Test + void getTypeSchema_STD_V2() throws IOException { + String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/policyTypeId"; + testGetTypeSchema(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrl, "policyTypeId", + "test_oran_get_schema_response.json"); } @Test @@ -167,49 +192,51 @@ class SdncOscA1ClientTest { assertEquals("1", result.get(1)); } - @Test - void getPolicyIdentities_STD() { - - String policyIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); - whenAsyncPostThenReturn(Mono.just(policyIdsResp)); + private void getPolicyIdentities(A1ProtocolType protocolType, String... expUrls) { + clientUnderTest = new SdncOscA1Client(protocolType, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + String resp = createOkResponseWithBody(Arrays.asList("xxx")); + whenAsyncPostThenReturn(Mono.just(resp)); List<String> returned = clientUnderTest.getPolicyIdentities().block(); - assertEquals(2, returned.size()); + assertEquals(1, returned.size()); + for (String expUrl : expUrls) { + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + } + } - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(policiesUrl()) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); + @Test + void getPolicyIdentities_STD_V1() { + String expUrl = RIC_1_URL + "/A1-P/v1/policies"; + getPolicyIdentities(A1ProtocolType.SDNC_OSC_STD_V1_1, expUrl); + } + @Test + void getPolicyIdentities_STD_V2() { + String expUrlPolicies = RIC_1_URL + "/A1-P/v2/policytypes"; + String expUrlInstances = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies"; + getPolicyIdentities(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrlPolicies, expUrlInstances); } @Test void getPolicyIdentities_OSC() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + String expUrlTypes = RIC_1_URL + "/a1-p/policytypes"; + String expUrlInstances = RIC_1_URL + "/a1-p/policytypes/xxx/policies"; + getPolicyIdentities(A1ProtocolType.SDNC_OSC_OSC_V1, expUrlTypes, expUrlInstances); + } + + private void putPolicy(A1ProtocolType protocolType, String expUrl) { + clientUnderTest = new SdncOscA1Client(protocolType, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // controllerConfig(), asyncRestClientMock); - String policytypeIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID)); - String policyIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); - whenAsyncPostThenReturn(Mono.just(policytypeIdsResp)).thenReturn(Mono.just(policyIdsResp)); - - List<String> returned = clientUnderTest.getPolicyIdentities().block(); - - assertEquals(2, returned.size()); - - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(RIC_1_URL + "/a1-p/policytypes/type1/policies") // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - } - - @Test - void putPolicyValidResponse() { whenPostReturnOkResponse(); String returned = clientUnderTest @@ -217,7 +244,6 @@ class SdncOscA1ClientTest { .block(); assertEquals("OK", returned); - final String expUrl = policiesUrl() + "/" + POLICY_1_ID; AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .body(POLICY_JSON_VALID) // @@ -225,10 +251,33 @@ class SdncOscA1ClientTest { String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); + } @Test - void putPolicyRejected() { + void putPolicy_OSC() { + String expUrl = RIC_1_URL + "/a1-p/policytypes/type1/policies/policy1"; + putPolicy(A1ProtocolType.SDNC_OSC_OSC_V1, expUrl); + } + + @Test + void putPolicy_STD_V1() { + String expUrl = RIC_1_URL + "/A1-P/v1/policies/policy1"; + putPolicy(A1ProtocolType.SDNC_OSC_STD_V1_1, expUrl); + } + + @Test + void putPolicy_STD_V2() { + String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1"; + putPolicy(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrl); + } + + @Test + void postRejected() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + final String policyJson = "{}"; AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // .body("NOK") // @@ -245,87 +294,79 @@ class SdncOscA1ClientTest { .expectErrorMatches(t -> t instanceof WebClientResponseException) // .verify(); - final String expUrl = policiesUrl() + "/" + POLICY_1_ID; - AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .body(policyJson) // - .build(); - String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams); - verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono) - .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify(); + StepVerifier.create(returnedMono).expectErrorMatches(throwable -> { + return throwable instanceof WebClientResponseException; + }).verify(); } - @Test - void deletePolicy() { - whenPostReturnOkResponse(); + private void deleteAllPolicies(A1ProtocolType protocolType, String expUrl) { + clientUnderTest = new SdncOscA1Client(protocolType, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + String resp = createOkResponseWithBody(Arrays.asList("xxx")); + whenAsyncPostThenReturn(Mono.just(resp)); - String returned = clientUnderTest - .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) - .block(); + clientUnderTest.deleteAllPolicies().blockLast(); - assertEquals("OK", returned); - final String expUrl = policiesUrl() + "/" + POLICY_1_ID; - AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); - + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); } @Test - void getStatus() { - 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("OK", returnedStatus, "unexpected status"); - - final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status"; - AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); - - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); + void deleteAllPolicies_STD_V2() { + String expUrl1 = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies/xxx"; + deleteAllPolicies(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrl1); } @Test - void getVersion_STD() { - whenPostReturnOkResponse(); + void deleteAllPolicies_STD_V1() { + String expUrl1 = RIC_1_URL + "/A1-P/v1/policies/xxx"; + deleteAllPolicies(A1ProtocolType.SDNC_OSC_STD_V1_1, expUrl1); + } - A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); + @Test + void deleteAllPolicies_OSC() { + String expUrl1 = RIC_1_URL + "/a1-p/policytypes/xxx/policies/xxx"; + deleteAllPolicies(A1ProtocolType.SDNC_OSC_OSC_V1, expUrl1); + } - assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); + @Test + void getVersion_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // Version irrelevant here + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); - whenPostReturnOkResponseNoBody(); + whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); - returnedVersion = clientUnderTest.getProtocolVersion().block(); + A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); } @Test - void getVersion_OSC() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + void testGetStatus() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V2_0_0, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // controllerConfig(), asyncRestClientMock); + whenPostReturnOkResponse(); - whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); + Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); - A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); + String response = clientUnderTest.getPolicyStatus(policy).block(); + assertEquals("OK", response); - assertEquals(A1ProtocolType.SDNC_OSC_OSC_V1, returnedVersion); - } + String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1/status"; + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); - private String policiesUrl() { - return RIC_1_URL + "/A1-P/v1/policies"; } private Gson gson() { @@ -343,7 +384,7 @@ class SdncOscA1ClientTest { whenAsyncPostThenReturn(Mono.just(createOkResponseString(true))); } - private void whenPostReturnOkResponseNoBody() { + void whenPostReturnOkResponseNoBody() { whenAsyncPostThenReturn(Mono.just(createOkResponseString(false))); } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientV2Test.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientV2Test.java new file mode 100644 index 00000000..d9238d64 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientV2Test.java @@ -0,0 +1,180 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2020 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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.onap.ccsdk.oran.a1policymanagementservice.clients; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +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.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +@ExtendWith(MockitoExtension.class) +class StdA1ClientV2Test { + + private static final String RIC_URL = "RicUrl"; + + private static final String RIC_BASE_URL = "RicBaseUrl/A1-P/v2"; + + private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes"; + private static final String POLICIES = "/policies"; + private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/"; + 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 = "{\"policyId\":\"policy1\"}"; + + StdA1ClientVersion2 clientUnderTest; + + AsyncRestClient asyncRestClientMock; + + @BeforeEach + void init() { + RicConfig ricConfig = ImmutableRicConfig.builder() // + .ricId("name") // + .baseUrl("RicBaseUrl") // + .managedElementIds(new ArrayList<>()) // + .controllerName("") // + .build(); + asyncRestClientMock = mock(AsyncRestClient.class); + clientUnderTest = new StdA1ClientVersion2(ricConfig, asyncRestClientMock); + } + + @Test + void testGetPolicyTypeIdentities() { + List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID); + Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString()); + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp); + + Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities(); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify(); + } + + @Test + void testGetPolicyIdentities() { + Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString()); + Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString()); + Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString()); + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp) + .thenReturn(policyIdsType2Resp); + + List<String> returned = clientUnderTest.getPolicyIdentities().block(); + + assertEquals(2, returned.size(), ""); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES); + } + + @Test + void testGetValidPolicyType() { + String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + "}"; + Mono<String> policyTypeResp = Mono.just(policyType); + + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp); + + String response = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID).block(); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID); + assertThat(response).contains("title"); + } + + @Test + void testGetInValidPolicyTypeJson() { + String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + "}"; + Mono<String> policyTypeResp = Mono.just(policyType); + + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp); + + Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID); + StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); + } + + @Test + void testGetPolicyTypeWithoutCreateSchema() { + Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID); + + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp); + + Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID); + StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify(); + } + + @Test + void testPutPolicy() { + when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty()); + + clientUnderTest + .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) + .block(); + verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID, + POLICY_JSON_VALID); + } + + @Test + void testDeletePolicy() { + when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty()); + + Mono<String> returnedMono = clientUnderTest + .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID); + StepVerifier.create(returnedMono).expectComplete().verify(); + } + + @Test + void testDeleteAllPolicies() { + Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString()); + Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString()); + Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString()); + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp) + .thenReturn(policyIdsType2Resp); + when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty()); + + Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies(); + StepVerifier.create(returnedFlux).expectComplete().verify(); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID); + } +} diff --git a/a1-policy-management/src/test/resources/test_oran_get_schema_response.json b/a1-policy-management/src/test/resources/test_oran_get_schema_response.json new file mode 100644 index 00000000..d17f6163 --- /dev/null +++ b/a1-policy-management/src/test/resources/test_oran_get_schema_response.json @@ -0,0 +1,45 @@ +{ + "name": "pt1", + "description": "pt1 policy type", + "policy_type_id": 1, + "policySchema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "OSC_Type1_1.0.0", + "description": "Type 1 policy type", + "type": "object", + "properties": { + "scope": { + "type": "object", + "properties": { + "ueId": { + "type": "string" + }, + "qosId": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "ueId", + "qosId" + ] + }, + "qosObjective": { + "type": "object", + "properties": { + "priorityLevel": { + "type": "number" + } + }, + "additionalProperties": false, + "required": [ + "priorityLevel" + ] + } + }, + "additionalProperties": false, + "required": [ + "scope", "qosObjective" + ] + } +} |