From 99dbc2588e004ad1c5a6bac78265e3875a669878 Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Thu, 26 Mar 2020 10:58:51 +0100 Subject: [PATCH] A1 controler access can be both OSC and ORAN versions of A1 Change-Id: I3c77a11f67cad6fceb3346ac13b62929def822a1 Issue-ID: NONRTRIC-164 Signed-off-by: PatrikBuhr --- .../org/oransc/policyagent/clients/A1Client.java | 7 +- .../policyagent/clients/A1ClientFactory.java | 9 +- .../oransc/policyagent/clients/A1UriBuilder.java | 10 +- .../oransc/policyagent/clients/OscA1Client.java | 98 +++++++++++++------- .../policyagent/clients/SdncOnapA1Client.java | 5 +- .../policyagent/clients/SdncOscA1Client.java | 102 +++++++++++++++++---- .../policyagent/clients/StdA1ClientVersion1.java | 48 ++++++++-- .../clients/StdA1UriBuilderVersion1.java | 66 ------------- .../policyagent/clients/A1ClientFactoryTest.java | 3 +- .../policyagent/clients/OscA1ClientTest.java | 47 ++++++---- .../policyagent/clients/SdncOscA1ClientTest.java | 45 +++++++-- 11 files changed, 270 insertions(+), 170 deletions(-) delete mode 100644 policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1UriBuilderVersion1.java diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java index 8715e88d..84c75381 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java @@ -31,9 +31,10 @@ public interface A1Client { public enum A1ProtocolType { UNKNOWN, // - STD_V1_1, // version 1.1 - OSC_V1, // - SDNC_OSC, // + STD_V1_1, // STD A1 version 1.1 + OSC_V1, // OSC 'A1' + SDNC_OSC_STD_V1_1, // SDNC_OSC with STD A1 version 1.1 southbound + SDNC_OSC_OSC_V1, // SDNC_OSC with OSC 'A1' southbound SDNC_ONAP } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java index 0be4b31b..be6d9b4c 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java @@ -70,8 +70,8 @@ public class A1ClientFactory { return new StdA1ClientVersion1(ric.getConfig()); } else if (version == A1ProtocolType.OSC_V1) { return new OscA1Client(ric.getConfig()); - } else if (version == A1ProtocolType.SDNC_OSC) { - return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(), + } else if (version == A1ProtocolType.SDNC_OSC_STD_V1_1 || version == A1ProtocolType.SDNC_OSC_OSC_V1) { + return new SdncOscA1Client(version, ric.getConfig(), appConfig.getA1ControllerBaseUrl(), appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword()); } else if (version == A1ProtocolType.SDNC_ONAP) { return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(), @@ -91,7 +91,7 @@ public class A1ClientFactory { if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) { return fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1)) // .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.OSC_V1))) // - .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_OSC))) // + .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_OSC_STD_V1_1))) // .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_ONAP))) // .doOnNext(ric::setProtocolVersion) .doOnNext(version -> logger.debug("Established protocol version:{} for Ric: {}", version, ric.name())) // @@ -102,7 +102,6 @@ public class A1ClientFactory { } private Mono fetchVersion(A1Client a1Client) { - return Mono.just(a1Client) // - .flatMap(client -> a1Client.getProtocolVersion()); + return a1Client.getProtocolVersion(); } } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1UriBuilder.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1UriBuilder.java index e32220dc..267a717e 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1UriBuilder.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1UriBuilder.java @@ -20,17 +20,13 @@ package org.oransc.policyagent.clients; -import org.oransc.policyagent.repository.Policy; - /** * Builder for A1 influenced REST APIs */ interface A1UriBuilder { - String createPutPolicyUri(Policy policy); - - String createGetPolicyIdsUri(); + String createPutPolicyUri(String type, String policyId); - String createDeleteUri(String policyId); + String createDeleteUri(String type, String policyId); - String createGetPolicyStatusUri(String policyId); + String createGetPolicyStatusUri(String type, String policyId); } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java index 1b7342f2..9384726d 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java @@ -28,46 +28,78 @@ import org.oransc.policyagent.configuration.RicConfig; import org.oransc.policyagent.repository.Policy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.web.util.UriComponentsBuilder; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally public class OscA1Client implements A1Client { - private static final String URL_PREFIX = "/a1-p"; - private static final String POLICY_TYPES = "/policytypes"; - private static final String CREATE_SCHEMA = "create_schema"; - private static final String TITLE = "title"; + public static class UriBuilder implements A1UriBuilder { + private final RicConfig ricConfig; + + public UriBuilder(RicConfig ricConfig) { + this.ricConfig = ricConfig; + } - private static final String HEALTHCHECK = "/healthcheck"; + @Override + public String createPutPolicyUri(String type, String policyId) { + return createPolicyUri(type, policyId); + } + + public String createGetPolicyIdsUri(String type) { + return createPolicyTypeUri(type) + "/policies"; + } - private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI = - UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}"); + @Override + public String createDeleteUri(String type, String policyId) { + return createPolicyUri(type, policyId); + } - private static final UriComponentsBuilder POLICY_URI = - UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}"); + @Override + public String createGetPolicyStatusUri(String type, String policyId) { + return createPolicyUri(type, policyId) + "/status"; + } - private static final UriComponentsBuilder POLICY_IDS_URI = - UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies"); + public String createHealtcheckUri() { + return baseUri() + "/healthcheck"; + } - private static final UriComponentsBuilder POLICY_STATUS_URI = - UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status"); + public String createGetSchemaUri(String type) { + return this.createPolicyTypeUri(type); + } - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + public String createPolicyTypesUri() { + return baseUri() + "/policytypes"; + } + private String createPolicyUri(String type, String id) { + return createPolicyTypeUri(type) + "/policies/" + id; + } + + private String createPolicyTypeUri(String type) { + return createPolicyTypesUri() + "/" + type; + } + + private String baseUri() { + return ricConfig.baseUrl() + "/a1-p"; + } + } + + private static final String TITLE = "title"; + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final AsyncRestClient restClient; + private final UriBuilder uri; public OscA1Client(RicConfig ricConfig) { - String baseUrl = ricConfig.baseUrl() + URL_PREFIX; - this.restClient = new AsyncRestClient(baseUrl); - if (logger.isDebugEnabled()) { - logger.debug("OscA1Client for ric: {}", ricConfig.name()); - } + this(ricConfig, new AsyncRestClient("")); } - public OscA1Client(AsyncRestClient restClient) { + public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) { this.restClient = restClient; + logger.debug("OscA1Client for ric: {}", ricConfig.name()); + + uri = new UriBuilder(ricConfig); } @Override @@ -85,15 +117,15 @@ public class OscA1Client implements A1Client { @Override public Mono getPolicyTypeSchema(String policyTypeId) { - String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString(); - return restClient.get(uri) // + String schemaUri = uri.createGetSchemaUri(policyTypeId); + return restClient.get(schemaUri) // .flatMap(response -> getCreateSchema(response, policyTypeId)); } @Override public Mono putPolicy(Policy policy) { - String uri = POLICY_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString(); - return restClient.put(uri, policy.json()); + String policyUri = this.uri.createPutPolicyUri(policy.type().name(), policy.id()); + return restClient.put(policyUri, policy.json()); } @Override @@ -103,7 +135,7 @@ public class OscA1Client implements A1Client { @Override public Mono getProtocolVersion() { - return restClient.get(HEALTHCHECK) // + return restClient.get(uri.createHealtcheckUri()) // .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1)); } @@ -115,25 +147,25 @@ public class OscA1Client implements A1Client { @Override public Mono getPolicyStatus(Policy policy) { - String uri = POLICY_STATUS_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString(); - return restClient.get(uri); + String statusUri = uri.createGetPolicyStatusUri(policy.type().name(), policy.id()); + return restClient.get(statusUri); } private Flux getPolicyTypeIds() { - return restClient.get(POLICY_TYPES) // + return restClient.get(uri.createPolicyTypesUri()) // .flatMapMany(JsonHelper::parseJsonArrayOfString); } private Flux getPolicyIdentitiesByType(String typeId) { - return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) // + return restClient.get(uri.createGetPolicyIdsUri(typeId)) // .flatMapMany(JsonHelper::parseJsonArrayOfString); } private Mono getCreateSchema(String policyTypeResponse, String policyTypeId) { try { JSONObject obj = new JSONObject(policyTypeResponse); - JSONObject schemaObj = obj.getJSONObject(CREATE_SCHEMA); + JSONObject schemaObj = obj.getJSONObject("create_schema"); schemaObj.put(TITLE, policyTypeId); return Mono.just(schemaObj.toString()); } catch (Exception e) { @@ -144,8 +176,8 @@ public class OscA1Client implements A1Client { } private Mono deletePolicyById(String typeId, String policyId) { - String uri = POLICY_URI.buildAndExpand(typeId, policyId).toUriString(); - return restClient.delete(uri); + String policyUri = uri.createDeleteUri(typeId, policyId); + return restClient.delete(policyUri); } private Flux deletePoliciesForType(String typeId) { diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java index 0568c360..af3c2a6d 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java @@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally public class SdncOnapA1Client implements A1Client { private static final String URL_PREFIX = "/A1-ADAPTER-API:"; @@ -44,9 +45,7 @@ public class SdncOnapA1Client implements A1Client { public SdncOnapA1Client(RicConfig ricConfig, String baseUrl, String username, String password) { this(ricConfig, username, password, new AsyncRestClient(baseUrl + "/restconf/operations")); - if (logger.isDebugEnabled()) { - logger.debug("SdncOnapA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), baseUrl); - } + logger.debug("SdncOnapA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), baseUrl); } public SdncOnapA1Client(RicConfig ricConfig, String username, String password, AsyncRestClient restClient) { 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 25fadbf5..d9aea591 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 @@ -63,27 +63,44 @@ public class SdncOscA1Client implements A1Client { .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) // .create(); // + private static final String GET_POLICY_RPC = "getA1Policy"; + private static final String UNHANDELED_PROTOCOL = "Bug, unhandeled protocoltype: "; private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final String a1ControllerUsername; private final String a1ControllerPassword; private final AsyncRestClient restClient; - private final A1UriBuilder uri; + private final RicConfig ricConfig; + private final A1ProtocolType protocolType; - public SdncOscA1Client(RicConfig ricConfig, String controllerBaseUrl, String username, String password) { - this(ricConfig, username, password, new AsyncRestClient(controllerBaseUrl + "/restconf/operations")); + public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, String controllerBaseUrl, String username, + String password) { + this(protocolType, 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) { + public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, String username, String password, + AsyncRestClient restClient) { this.a1ControllerUsername = username; this.a1ControllerPassword = password; this.restClient = restClient; - this.uri = new StdA1UriBuilderVersion1(ricConfig); + this.ricConfig = ricConfig; + this.protocolType = protocolType; + } @Override public Mono> getPolicyTypeIdentities() { - return Mono.just(Arrays.asList("")); + if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { + return Mono.just(Arrays.asList("")); + } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { + OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig); + final String ricUrl = uri.createPolicyTypesUri(); + return post(GET_POLICY_RPC, ricUrl, Optional.empty()) // + .flatMapMany(JsonHelper::parseJsonArrayOfString) // + .collectList(); + } + throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); } @Override @@ -94,46 +111,93 @@ public class SdncOscA1Client implements A1Client { @Override public Mono getPolicyTypeSchema(String policyTypeId) { - return Mono.just("{}"); + if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { + return Mono.just("{}"); + } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { + OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig); + final String ricUrl = uri.createGetSchemaUri(policyTypeId); + return post(GET_POLICY_RPC, ricUrl, Optional.empty()); + } + throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); } @Override public Mono putPolicy(Policy policy) { - final String ricUrl = uri.createPutPolicyUri(policy); + final String ricUrl = getUriBuilder().createPutPolicyUri(policy.type().name(), policy.id()); return post("putA1Policy", ricUrl, Optional.of(policy.json())); } @Override public Mono deletePolicy(Policy policy) { - return deletePolicyById(policy.id()); + return deletePolicyById(policy.type().name(), policy.id()); } @Override public Flux deleteAllPolicies() { - return getPolicyIds() // - .flatMap(this::deletePolicyById); // + if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { + return getPolicyIds() // + .flatMap(policyId -> deletePolicyById("", policyId)); // + } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { + OscA1Client.UriBuilder uriBuilder = new OscA1Client.UriBuilder(ricConfig); + return getPolicyTypeIdentities() // + .flatMapMany(Flux::fromIterable) + .flatMap(type -> post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty())) // + .flatMap(JsonHelper::parseJsonArrayOfString); + } + throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); } @Override public Mono getProtocolVersion() { - return getPolicyIdentities() // - .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC)); + return tryStdProtocolVersion() // + .onErrorResume(t -> tryOscProtocolVersion()); } @Override public Mono getPolicyStatus(Policy policy) { - final String ricUrl = uri.createGetPolicyStatusUri(policy.id()); + final String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.type().name(), policy.id()); return post("getA1PolicyStatus", ricUrl, Optional.empty()); } + private A1UriBuilder getUriBuilder() { + if (protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { + return new StdA1ClientVersion1.UriBuilder(ricConfig); + } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { + return new OscA1Client.UriBuilder(ricConfig); + } + throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); + } + + private Mono tryOscProtocolVersion() { + OscA1Client.UriBuilder oscApiuriBuilder = new OscA1Client.UriBuilder(ricConfig); + return post(GET_POLICY_RPC, oscApiuriBuilder.createHealtcheckUri(), Optional.empty()) // + .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC_OSC_V1)); + } + + private Mono tryStdProtocolVersion() { + StdA1ClientVersion1.UriBuilder uriBuilder = new StdA1ClientVersion1.UriBuilder(ricConfig); + return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(), Optional.empty()) // + .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC_STD_V1_1)); + } + private Flux getPolicyIds() { - final String ricUrl = uri.createGetPolicyIdsUri(); - return post("getA1Policy", ricUrl, Optional.empty()) // - .flatMapMany(JsonHelper::parseJsonArrayOfString); + if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { + StdA1ClientVersion1.UriBuilder uri = new StdA1ClientVersion1.UriBuilder(ricConfig); + final String ricUrl = uri.createGetPolicyIdsUri(); + return post(GET_POLICY_RPC, ricUrl, Optional.empty()) // + .flatMapMany(JsonHelper::parseJsonArrayOfString); + } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { + OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig); + return getPolicyTypeIdentities() // + .flatMapMany(Flux::fromIterable) + .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) // + .flatMap(JsonHelper::parseJsonArrayOfString); + } + throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); } - private Mono deletePolicyById(String policyId) { - final String ricUrl = uri.createDeleteUri(policyId); + private Mono deletePolicyById(String type, String policyId) { + final String ricUrl = getUriBuilder().createDeleteUri(type, policyId); return post("deleteA1Policy", ricUrl, Optional.empty()); } 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 6b9ef17c..7480b939 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 @@ -30,9 +30,44 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class StdA1ClientVersion1 implements A1Client { - private final AsyncRestClient restClient; - private final A1UriBuilder uri; + 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 policiesBaseUri() + policyId; + } + + public String createGetPolicyIdsUri() { + return baseUri() + "/policies"; + } + + @Override + public String createDeleteUri(String type, String policyId) { + return policiesBaseUri() + policyId; + } + + public String createGetPolicyStatusUri(String type, String policyId) { + return policiesBaseUri() + policyId + "/status"; + } + + private String baseUri() { + return ricConfig.baseUrl() + "/A1-P/v1"; + } + + private String policiesBaseUri() { + return createGetPolicyIdsUri() + "/"; + } + } + + private final AsyncRestClient restClient; + private final UriBuilder uri; public StdA1ClientVersion1(RicConfig ricConfig) { this(new AsyncRestClient(""), ricConfig); @@ -40,7 +75,7 @@ public class StdA1ClientVersion1 implements A1Client { public StdA1ClientVersion1(AsyncRestClient restClient, RicConfig ricConfig) { this.restClient = restClient; - this.uri = new StdA1UriBuilderVersion1(ricConfig); + this.uri = new UriBuilder(ricConfig); } @Override @@ -51,8 +86,7 @@ public class StdA1ClientVersion1 implements A1Client { @Override public Mono putPolicy(Policy policy) { - - return restClient.put(uri.createPutPolicyUri(policy), policy.json()) // + return restClient.put(uri.createPutPolicyUri(policy.type().name(), policy.id()), policy.json()) // .flatMap(JsonHelper::validateJson); } @@ -85,7 +119,7 @@ public class StdA1ClientVersion1 implements A1Client { @Override public Mono getPolicyStatus(Policy policy) { - return restClient.get(uri.createGetPolicyStatusUri(policy.id())); + return restClient.get(uri.createGetPolicyStatusUri(policy.type().name(), policy.id())); } private Flux getPolicyIds() { @@ -94,6 +128,6 @@ public class StdA1ClientVersion1 implements A1Client { } private Mono deletePolicyById(String policyId) { - return restClient.delete(uri.createDeleteUri(policyId)); + 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 deleted file mode 100644 index 6d836564..00000000 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1UriBuilderVersion1.java +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * ========================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/A1ClientFactoryTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java index df26d27d..20954701 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java @@ -119,7 +119,8 @@ public class A1ClientFactoryTest { assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1); assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client); assertTrue(createClient(A1ProtocolType.SDNC_ONAP) instanceof SdncOnapA1Client); - assertTrue(createClient(A1ProtocolType.SDNC_OSC) instanceof SdncOscA1Client); + assertTrue(createClient(A1ProtocolType.SDNC_OSC_STD_V1_1) instanceof SdncOscA1Client); + assertTrue(createClient(A1ProtocolType.SDNC_OSC_OSC_V1) instanceof SdncOscA1Client); assertTrue(createClient(A1ProtocolType.UNKNOWN) == null); } diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/OscA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/OscA1ClientTest.java index ceaffc83..5023d100 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/OscA1ClientTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/OscA1ClientTest.java @@ -20,11 +20,13 @@ package org.oransc.policyagent.clients; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.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; @@ -33,6 +35,8 @@ 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.oransc.policyagent.configuration.ImmutableRicConfig; +import org.oransc.policyagent.configuration.RicConfig; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -40,12 +44,14 @@ import reactor.test.StepVerifier; @ExtendWith(MockitoExtension.class) public class OscA1ClientTest { + private static final String RIC_URL = "RicUrl"; - private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes"; - private static final String POLICIES_IDENTITIES_URL = "/policies"; - private static final String POLICYTYPES_URL = "/policytypes/"; - private static final String POLICIES_URL = "/policies/"; + private static final String RIC_BASE_URL = "RicBaseUrl/a1-p"; + + 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\"}"; @@ -60,8 +66,13 @@ public class OscA1ClientTest { @BeforeEach public void init() { + RicConfig ricConfig = ImmutableRicConfig.builder() // + .name("name") // + .baseUrl("RicBaseUrl") // + .managedElementIds(new ArrayList<>()) // + .build(); asyncRestClientMock = mock(AsyncRestClient.class); - clientUnderTest = new OscA1Client(asyncRestClientMock); + clientUnderTest = new OscA1Client(ricConfig, asyncRestClientMock); } @Test @@ -83,11 +94,12 @@ public class OscA1ClientTest { when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp) .thenReturn(policyIdsType2Resp); - Mono> returnedMono = clientUnderTest.getPolicyIdentities(); - StepVerifier.create(returnedMono).expectNext(Arrays.asList(POLICY_1_ID, POLICY_2_ID)).expectComplete().verify(); + List returned = clientUnderTest.getPolicyIdentities().block(); + + assertEquals(2, returned.size(), ""); verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); - verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_IDENTITIES_URL); - verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES); } @Test @@ -130,11 +142,10 @@ public class OscA1ClientTest { public void testPutPolicy() { when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty()); - Mono returnedMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); - verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID, + 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); - StepVerifier.create(returnedMono).expectComplete().verify(); } @Test @@ -143,7 +154,7 @@ public class OscA1ClientTest { Mono 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_URL + POLICY_1_ID); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID); StepVerifier.create(returnedMono).expectComplete().verify(); } @@ -159,9 +170,9 @@ public class OscA1ClientTest { Flux returnedFlux = clientUnderTest.deleteAllPolicies(); StepVerifier.create(returnedFlux).expectComplete().verify(); verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); - verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_IDENTITIES_URL); - verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID); - verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL); - verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_URL + POLICY_2_ID); + 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/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java index 68c61ee6..e463cae9 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 @@ -36,9 +36,11 @@ 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.A1Client.A1ProtocolType; import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest; import org.oransc.policyagent.clients.SdncOscA1Client.AdapterResponse; import org.oransc.policyagent.repository.Policy; +import org.oransc.policyagent.repository.Ric; import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; @@ -65,17 +67,41 @@ public class SdncOscA1ClientTest { @BeforeEach public void init() { asyncRestClientMock = mock(AsyncRestClient.class); - clientUnderTest = new SdncOscA1Client(A1ClientHelper.createRic(RIC_1_URL).getConfig(), CONTROLLER_USERNAME, + Ric ric = A1ClientHelper.createRic(RIC_1_URL); + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), CONTROLLER_USERNAME, CONTROLLER_PASSWORD, asyncRestClientMock); } @Test - public void testGetPolicyTypeIdentities() { + public void testGetPolicyTypeIdentities_STD() { List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); assertEquals(1, policyTypeIds.size(), "should hardcoded to one"); assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); } + @Test + public void testGetPolicyTypeIdentities_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + CONTROLLER_USERNAME, CONTROLLER_PASSWORD, asyncRestClientMock); + + String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID)); + whenAsyncPostThenReturn(Mono.just(response)); + + List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); + assertEquals(1, policyTypeIds.size(), ""); + assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0), ""); + + String expUrl = RIC_1_URL + "/a1-p/policytypes"; + AdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = A1ClientHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + + } + private String policiesUrl() { return RIC_1_URL + "/A1-P/v1/policies"; } @@ -84,16 +110,19 @@ public class SdncOscA1ClientTest { return SdncOscA1Client.gson; } - @Test - public void testGetPolicyIdentities() { - - List policyIds = Arrays.asList(POLICY_1_ID, POLICY_2_ID); + private String createResponse(Object obj) { AdapterResponse output = ImmutableAdapterResponse.builder() // - .body(gson().toJson(policyIds)) // + .body(gson().toJson(obj)) // .httpStatus(200) // .build(); - String policyIdsResp = gson().toJson(output); + return gson().toJson(output); + } + + @Test + public void testGetPolicyIdentities() { + + String policyIdsResp = createResponse(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); whenAsyncPostThenReturn(Mono.just(policyIdsResp)); List returned = clientUnderTest.getPolicyIdentities().block(); -- 2.16.6