Fix bug in A1 clients
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / SdncOscA1Client.java
index fcb3236..79a2a5e 100644 (file)
@@ -33,6 +33,7 @@ import org.immutables.value.Value;
 import org.json.JSONObject;
 import org.oransc.policyagent.configuration.ControllerConfig;
 import org.oransc.policyagent.configuration.RicConfig;
+import org.oransc.policyagent.configuration.WebClientConfig;
 import org.oransc.policyagent.repository.Policy;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -48,6 +49,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 {
@@ -76,23 +79,40 @@ public class SdncOscA1Client implements A1Client {
     private final A1ProtocolType protocolType;
 
     /**
-     * Constructor
-     * 
-     * @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
+     * 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 the configuration of the Ric to communicate with
+     * @param controllerConfig the configuration of the SDNC controller to use
+     *
+     * @throws IllegalArgumentException when the protocolType is wrong.
      */
-    public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig) {
+    public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
+        WebClientConfig clientConfig) {
         this(protocolType, ricConfig, controllerConfig,
-            new AsyncRestClient(controllerConfig.baseUrl() + "/restconf/operations"));
+            new AsyncRestClient(controllerConfig.baseUrl() + "/restconf/operations", clientConfig));
         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
+     *
+     * @throws IllegalArgumentException when the protocolType is wrong.
+     */
     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);
+        }
         this.restClient = restClient;
         this.ricConfig = ricConfig;
         this.protocolType = protocolType;
@@ -103,22 +123,16 @@ public class SdncOscA1Client implements A1Client {
     public Mono<List<String>> getPolicyTypeIdentities() {
         if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
             return Mono.just(Arrays.asList(""));
-        } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) {
+        } else {
             OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig);
             final String ricUrl = uri.createPolicyTypesUri();
             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
                 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString) //
                 .collectList();
-        } else {
-            return Mono.error(createIllegalProtocolException());
         }
 
     }
 
-    private Exception createIllegalProtocolException() {
-        return new NullPointerException("Bug, unhandeled protocoltype: " + this.protocolType);
-    }
-
     @Override
     public Mono<List<String>> getPolicyIdentities() {
         return getPolicyIds() //
@@ -129,13 +143,11 @@ public class SdncOscA1Client implements A1Client {
     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
         if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
             return Mono.just("{}");
-        } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) {
+        } else {
             OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig);
             final String ricUrl = uri.createGetSchemaUri(policyTypeId);
             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
                 .flatMap(response -> OscA1Client.extractCreateSchema(response, policyTypeId));
-        } else {
-            return Mono.error(createIllegalProtocolException());
         }
     }
 
@@ -157,18 +169,25 @@ public class SdncOscA1Client implements A1Client {
     public Flux<String> deleteAllPolicies() {
         if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
             return getPolicyIds() //
-                .flatMap(policyId -> deletePolicyById("", policyId)); //
-        } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) {
+                .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); //
+        } else {
             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);
-        } else {
-            return Flux.error(createIllegalProtocolException());
+                .flatMapMany(Flux::fromIterable) //
+                .flatMap(type -> oscDeleteInstancesForType(uriBuilder, type), CONCURRENCY_RIC);
         }
     }
 
+    private Flux<String> oscGetInstancesForType(OscA1Client.UriBuilder 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) //
+            .flatMap(instance -> deletePolicyById(type, instance), CONCURRENCY_RIC);
+    }
+
     @Override
     public Mono<A1ProtocolType> getProtocolVersion() {
         return tryStdProtocolVersion() //
@@ -187,10 +206,8 @@ public class SdncOscA1Client implements A1Client {
     private Mono<A1UriBuilder> getUriBuilder() {
         if (protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
             return Mono.just(new StdA1ClientVersion1.UriBuilder(ricConfig));
-        } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) {
-            return Mono.just(new OscA1Client.UriBuilder(ricConfig));
         } else {
-            return Mono.error(createIllegalProtocolException());
+            return Mono.just(new OscA1Client.UriBuilder(ricConfig));
         }
     }
 
@@ -212,14 +229,12 @@ public class SdncOscA1Client implements A1Client {
             final String ricUrl = uri.createGetPolicyIdsUri();
             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
                 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
-        } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) {
+        } else {
             OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig);
             return getPolicyTypeIdentities() //
                 .flatMapMany(Flux::fromIterable)
                 .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) //
                 .flatMap(SdncJsonHelper::parseJsonArrayOfString);
-        } else {
-            return Flux.error(createIllegalProtocolException());
         }
     }
 
@@ -254,10 +269,10 @@ public class SdncOscA1Client implements A1Client {
         } else {
             logger.debug("Error response: {} {}", output.httpStatus(), body);
             byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8);
-            WebClientResponseException e = new WebClientResponseException(output.httpStatus(), "statusText", null,
-                responseBodyBytes, StandardCharsets.UTF_8, null);
+            WebClientResponseException responseException = new WebClientResponseException(output.httpStatus(),
+                "statusText", null, responseBodyBytes, StandardCharsets.UTF_8, null);
 
-            return Mono.error(e);
+            return Mono.error(responseException);
         }
     }