Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / OscA1Client.java
index efb1d52..a388267 100644 (file)
@@ -2,7 +2,7 @@
  * ========================LICENSE_START=================================
  * O-RAN-SC
  * %%
- * Copyright (C) 2019 Nordix Foundation
+ * 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.
@@ -22,50 +22,123 @@ package org.oransc.policyagent.clients;
 
 import java.lang.invoke.MethodHandles;
 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;
-import org.springframework.web.util.UriComponentsBuilder;
+
 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 {
-    private static final String URL_PREFIX = "/a1-p";
+    static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent to one NearRT RIC
 
-    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;
 
-    private static final String HEALTHCHECK = "/healthcheck";
+        public UriBuilder(RicConfig ricConfig) {
+            this.ricConfig = ricConfig;
+        }
 
-    private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
-        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
+        @Override
+        public String createPutPolicyUri(String type, String policyId) {
+            return createPolicyUri(type, policyId);
+        }
 
-    private static final UriComponentsBuilder POLICY_URI =
-        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}");
+        /**
+         * /a1-p/policytypes/{policy_type_id}/policies
+         */
+        public String createGetPolicyIdsUri(String type) {
+            return createPolicyTypeUri(type) + "/policies";
+        }
 
-    private static final UriComponentsBuilder POLICY_IDS_URI =
-        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies");
+        @Override
+        public String createDeleteUri(String type, String policyId) {
+            return createPolicyUri(type, policyId);
+        }
 
-    private static final UriComponentsBuilder POLICY_STATUS_URI =
-        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status");
+        /**
+         * ​/a1-p​/policytypes​/{policy_type_id}​/policies​/{policy_instance_id}​/status
+         */
+        @Override
+        public String createGetPolicyStatusUri(String type, String policyId) {
+            return createPolicyUri(type, policyId) + "/status";
+        }
 
-    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+        /**
+         * ​/a1-p​/healthcheck
+         */
+        public String createHealtcheckUri() {
+            return baseUri() + "/healthcheck";
+        }
 
-    private final AsyncRestClient restClient;
+        /**
+         * /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;
+        }
 
-    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());
+        /**
+         * /a1-p/policytypes/{policy_type_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, WebClientConfig clientConfig) {
+        this(ricConfig, new AsyncRestClient("", clientConfig));
     }
 
-    public OscA1Client(AsyncRestClient restClient) {
+    public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
         this.restClient = restClient;
+        logger.debug("OscA1Client for ric: {}", ricConfig.name());
+
+        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
@@ -83,15 +156,15 @@ public class OscA1Client implements A1Client {
 
     @Override
     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
-        String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
-        return restClient.get(uri) //
-            .flatMap(response -> getCreateSchema(response, policyTypeId));
+        String schemaUri = uri.createGetSchemaUri(policyTypeId);
+        return restClient.get(schemaUri) //
+            .flatMap(response -> extractCreateSchema(response, policyTypeId));
     }
 
     @Override
     public Mono<String> 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
@@ -101,52 +174,40 @@ public class OscA1Client implements A1Client {
 
     @Override
     public Mono<A1ProtocolType> getProtocolVersion() {
-        return restClient.get(HEALTHCHECK) //
+        return restClient.get(uri.createHealtcheckUri()) //
             .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
     }
 
     @Override
     public Flux<String> deleteAllPolicies() {
         return getPolicyTypeIds() //
-            .flatMap(this::deletePoliciesForType);
+            .flatMap(this::deletePoliciesForType, CONCURRENCY_RIC);
     }
 
     @Override
     public Mono<String> 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<String> getPolicyTypeIds() {
-        return restClient.get(POLICY_TYPES) //
-            .flatMapMany(JsonHelper::parseJsonArrayOfString);
+        return restClient.get(uri.createPolicyTypesUri()) //
+            .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
     }
 
     private Flux<String> getPolicyIdentitiesByType(String typeId) {
-        return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) //
-            .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) {
-            logger.error("Unexcpected response for policy type: {}", policyTypeResponse, e);
-            return Mono.error(e);
-        }
+        return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
+            .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
     }
 
     private Mono<String> 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<String> deletePoliciesForType(String typeId) {
         return getPolicyIdentitiesByType(typeId) //
-            .flatMap(policyId -> deletePolicyById(typeId, policyId));
+            .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);
     }
 }