X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FSdncOscA1Client.java;h=a5735f7dbeee67444584c0ce7845a7b3612c8b2b;hb=ba50f8809edc7d49a74021e25b4094f4c3174b26;hp=68601735e79e30ed6749eec645c34c45deaacd66;hpb=0316038265be758ae8b1bce006b5fd017185c55d;p=nonrtric.git 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 68601735..a5735f7d 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 @@ -48,6 +48,8 @@ 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 + @Value.Immutable @org.immutables.gson.Gson.TypeAdapters public interface AdapterRequest { @@ -69,7 +71,6 @@ public class SdncOscA1Client implements A1Client { .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 ControllerConfig controllerConfig; private final AsyncRestClient restClient; @@ -77,14 +78,12 @@ public class SdncOscA1Client implements A1Client { private final A1ProtocolType protocolType; /** - * Constructor - * + * 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 - * @param ricConfig - * @param controllerBaseUrl the base URL of the SDNC controller - * @param username username to accesss the SDNC controller - * @param password password to accesss the SDNC controller + * @param ricConfig the configuration of the Ric to communicate with + * @param controllerConfig the configuration of the SDNC controller to use */ public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig) { this(protocolType, ricConfig, controllerConfig, @@ -92,6 +91,15 @@ public class SdncOscA1Client implements A1Client { logger.debug("SdncOscA1Client for ric: {}, a1Controller: {}", ricConfig.name(), 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 + * @param ricConfig the configuration of the Ric to communicate with + * @param controllerConfig the configuration of the SDNC controller to use + * @param restClient the REST client to use + */ public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig, AsyncRestClient restClient) { this.restClient = restClient; @@ -110,8 +118,14 @@ public class SdncOscA1Client implements A1Client { return post(GET_POLICY_RPC, ricUrl, Optional.empty()) // .flatMapMany(SdncJsonHelper::parseJsonArrayOfString) // .collectList(); + } else { + return Mono.error(createIllegalProtocolException()); } - throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); + + } + + private Exception createIllegalProtocolException() { + return new NullPointerException("Bug, unhandeled protocoltype: " + this.protocolType); } @Override @@ -127,15 +141,20 @@ public class SdncOscA1Client implements A1Client { } 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()); + return post(GET_POLICY_RPC, ricUrl, Optional.empty()) // + .flatMap(response -> OscA1Client.extractCreateSchema(response, policyTypeId)); + } else { + return Mono.error(createIllegalProtocolException()); } - throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); } @Override public Mono putPolicy(Policy policy) { - final String ricUrl = getUriBuilder().createPutPolicyUri(policy.type().name(), policy.id()); - return post("putA1Policy", ricUrl, Optional.of(policy.json())); + return getUriBuilder() // + .flatMap(builder -> { + String ricUrl = builder.createPutPolicyUri(policy.type().name(), policy.id()); + return post("putA1Policy", ricUrl, Optional.of(policy.json())); + }); } @Override @@ -147,15 +166,25 @@ public class SdncOscA1Client implements A1Client { public Flux deleteAllPolicies() { if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { return getPolicyIds() // - .flatMap(policyId -> deletePolicyById("", policyId)); // + .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); // } 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(SdncJsonHelper::parseJsonArrayOfString); + .flatMapMany(Flux::fromIterable) // + .flatMap(type -> oscDeleteInstancesForType(uriBuilder, type), CONCURRENCY_RIC); + } else { + return Flux.error(createIllegalProtocolException()); } - throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); + } + + private Flux oscGetInstancesForType(OscA1Client.UriBuilder uriBuilder, String type) { + return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) // + .flatMapMany(SdncJsonHelper::parseJsonArrayOfString); + } + + private Flux oscDeleteInstancesForType(OscA1Client.UriBuilder uriBuilder, String type) { + return oscGetInstancesForType(uriBuilder, type) // + .flatMap(instance -> deletePolicyById(type, instance), CONCURRENCY_RIC); } @Override @@ -166,17 +195,21 @@ public class SdncOscA1Client implements A1Client { @Override public Mono getPolicyStatus(Policy policy) { - final String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.type().name(), policy.id()); - return post("getA1PolicyStatus", ricUrl, Optional.empty()); + return getUriBuilder() // + .flatMap(builder -> { + String ricUrl = builder.createGetPolicyStatusUri(policy.type().name(), policy.id()); + return post("getA1PolicyStatus", ricUrl, Optional.empty()); + }); } - private A1UriBuilder getUriBuilder() { + private Mono getUriBuilder() { if (protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) { - return new StdA1ClientVersion1.UriBuilder(ricConfig); + return Mono.just(new StdA1ClientVersion1.UriBuilder(ricConfig)); } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) { - return new OscA1Client.UriBuilder(ricConfig); + return Mono.just(new OscA1Client.UriBuilder(ricConfig)); + } else { + return Mono.error(createIllegalProtocolException()); } - throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); } private Mono tryOscProtocolVersion() { @@ -203,13 +236,17 @@ public class SdncOscA1Client implements A1Client { .flatMapMany(Flux::fromIterable) .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) // .flatMap(SdncJsonHelper::parseJsonArrayOfString); + } else { + return Flux.error(createIllegalProtocolException()); } - throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType); } private Mono deletePolicyById(String type, String policyId) { - final String ricUrl = getUriBuilder().createDeleteUri(type, policyId); - return post("deleteA1Policy", ricUrl, Optional.empty()); + return getUriBuilder() // + .flatMap(builder -> { + String ricUrl = builder.createDeleteUri(type, policyId); + return post("deleteA1Policy", ricUrl, Optional.empty()); + }); } private Mono post(String rpcName, String ricUrl, Optional body) {