Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / OscA1Client.java
index 9384726..a388267 100644 (file)
@@ -25,6 +25,7 @@ import java.util.List;
 
 import org.json.JSONObject;
 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;
@@ -32,8 +33,12 @@ import org.slf4j.LoggerFactory;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
+/**
+ * Client for accessing OSC A1 REST API
+ */
 @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
 
     public static class UriBuilder implements A1UriBuilder {
         private final RicConfig ricConfig;
@@ -47,6 +52,9 @@ public class OscA1Client implements A1Client {
             return createPolicyUri(type, policyId);
         }
 
+        /**
+         * /a1-p/policytypes/{policy_type_id}/policies
+         */
         public String createGetPolicyIdsUri(String type) {
             return createPolicyTypeUri(type) + "/policies";
         }
@@ -56,27 +64,45 @@ public class OscA1Client implements A1Client {
             return createPolicyUri(type, policyId);
         }
 
+        /**
+         * ​/a1-p​/policytypes​/{policy_type_id}​/policies​/{policy_instance_id}​/status
+         */
         @Override
         public String createGetPolicyStatusUri(String type, String policyId) {
             return createPolicyUri(type, policyId) + "/status";
         }
 
+        /**
+         * ​/a1-p​/healthcheck
+         */
         public String createHealtcheckUri() {
             return baseUri() + "/healthcheck";
         }
 
+        /**
+         * /a1-p/policytypes/{policy_type_id}
+         */
         public String createGetSchemaUri(String type) {
             return this.createPolicyTypeUri(type);
         }
 
+        /**
+         * ​/a1-p​/policytypes​/{policy_type_id}
+         */
         public String createPolicyTypesUri() {
             return baseUri() + "/policytypes";
         }
 
+        /**
+         * ​/a1-p​/policytypes​/{policy_type_id}​/policies​/{policy_instance_id}
+         */
         private String createPolicyUri(String type, String id) {
             return createPolicyTypeUri(type) + "/policies/" + id;
         }
 
+        /**
+         * /a1-p/policytypes/{policy_type_id}
+         */
         private String createPolicyTypeUri(String type) {
             return createPolicyTypesUri() + "/" + type;
         }
@@ -91,8 +117,8 @@ public class OscA1Client implements A1Client {
     private final AsyncRestClient restClient;
     private final UriBuilder uri;
 
-    public OscA1Client(RicConfig ricConfig) {
-        this(ricConfig, new AsyncRestClient(""));
+    public OscA1Client(RicConfig ricConfig, WebClientConfig clientConfig) {
+        this(ricConfig, new AsyncRestClient("", clientConfig));
     }
 
     public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
@@ -102,6 +128,19 @@ public class OscA1Client implements A1Client {
         uri = new UriBuilder(ricConfig);
     }
 
+    public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) {
+        try {
+            JSONObject obj = new JSONObject(policyTypeResponse);
+            JSONObject schemaObj = obj.getJSONObject("create_schema");
+            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() //
@@ -119,7 +158,7 @@ public class OscA1Client implements A1Client {
     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
         String schemaUri = uri.createGetSchemaUri(policyTypeId);
         return restClient.get(schemaUri) //
-            .flatMap(response -> getCreateSchema(response, policyTypeId));
+            .flatMap(response -> extractCreateSchema(response, policyTypeId));
     }
 
     @Override
@@ -142,7 +181,7 @@ public class OscA1Client implements A1Client {
     @Override
     public Flux<String> deleteAllPolicies() {
         return getPolicyTypeIds() //
-            .flatMap(this::deletePoliciesForType);
+            .flatMap(this::deletePoliciesForType, CONCURRENCY_RIC);
     }
 
     @Override
@@ -154,25 +193,12 @@ public class OscA1Client implements A1Client {
 
     private Flux<String> getPolicyTypeIds() {
         return restClient.get(uri.createPolicyTypesUri()) //
-            .flatMapMany(JsonHelper::parseJsonArrayOfString);
+            .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
     }
 
     private Flux<String> getPolicyIdentitiesByType(String typeId) {
         return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
-            .flatMapMany(JsonHelper::parseJsonArrayOfString);
-    }
-
-    private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
-        try {
-            JSONObject obj = new JSONObject(policyTypeResponse);
-            JSONObject schemaObj = obj.getJSONObject("create_schema");
-            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);
-        }
+            .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
     }
 
     private Mono<String> deletePolicyById(String typeId, String policyId) {
@@ -182,6 +208,6 @@ public class OscA1Client implements A1Client {
 
     private Flux<String> deletePoliciesForType(String typeId) {
         return getPolicyIdentitiesByType(typeId) //
-            .flatMap(policyId -> deletePolicyById(typeId, policyId));
+            .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);
     }
 }