Remove code smells from policyagent/clients 73/2473/10
authorelinuxhenrik <henrik.b.andersson@est.tech>
Tue, 11 Feb 2020 15:55:33 +0000 (16:55 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 28 Feb 2020 11:20:40 +0000 (12:20 +0100)
Change-Id: I7d5ffdb75573f153102f240352820fd5fcd9e795
Issue-ID: NONRTRIC-142
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
13 files changed:
policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java
policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java
policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java
policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java
policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java [moved from policy-agent/src/main/java/org/oransc/policyagent/clients/SdnrOnapA1Client.java with 84% similarity]
policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapAdapterInput.java [moved from policy-agent/src/main/java/org/oransc/policyagent/clients/SdnrOnapAdapterInput.java with 96% similarity]
policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java
policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1Client.java
policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java
policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java
policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOnapA1ClientTest.java [moved from policy-agent/src/test/java/org/oransc/policyagent/clients/SdnrOnapA1ClientTest.java with 92% similarity]
policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java
policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java

index 0b18104..a3c7448 100644 (file)
@@ -29,8 +29,12 @@ import reactor.core.publisher.Mono;
 
 public interface A1Client {
 
-    public static enum A1ProtocolType {
-        UNKNOWN, STD_V1, OSC_V1, SDNC_OSC, SDNR_ONAP
+    public enum A1ProtocolType {
+        UNKNOWN, //
+        STD_V1, //
+        OSC_V1, //
+        SDNC_OSC, //
+        SDNC_ONAP
     }
 
     public Mono<A1ProtocolType> getProtocolVersion();
index 6598c27..0e7fee2 100644 (file)
@@ -102,7 +102,7 @@ public class A1ClientFactory {
     }
 
     protected A1Client createSdnrOnapA1Client(Ric ric) {
-        return new SdnrOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
+        return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
             appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
     }
 
index cea7060..0a7c3a1 100644 (file)
@@ -17,6 +17,7 @@
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
+
 package org.oransc.policyagent.clients;
 
 import java.lang.invoke.MethodHandles;
index f7fd232..d03dbb4 100644 (file)
@@ -31,19 +31,41 @@ 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;
 
 public class OscA1Client implements A1Client {
+    private static final String URL_PREFIX = "/a1-p";
+
+    private static final String POLICYTYPES = "/policytypes";
+    private static final String CREATE_SCHEMA = "create_schema";
+    private static final String TITLE = "title";
+
+    private static final String HEALTHCHECK = "/healthcheck";
+
+    private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
+        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
+
+    private static final UriComponentsBuilder POLICY_URI =
+        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}");
+
+    private static final UriComponentsBuilder POLICY_IDS_URI =
+        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies");
+
+    private static final UriComponentsBuilder POLICY_STATUS_URI =
+        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status");
+
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
     private final AsyncRestClient restClient;
 
     public OscA1Client(RicConfig ricConfig) {
-        String baseUrl = ricConfig.baseUrl() + "/a1-p";
+        String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
         this.restClient = new AsyncRestClient(baseUrl);
-        logger.debug("OscA1Client for ric: {}", ricConfig.name());
+        if (logger.isDebugEnabled()) {
+            logger.debug("OscA1Client for ric: {}", ricConfig.name());
+        }
     }
 
     public OscA1Client(AsyncRestClient restClient) {
@@ -52,7 +74,7 @@ public class OscA1Client implements A1Client {
 
     @Override
     public Mono<List<String>> getPolicyTypeIdentities() {
-        return restClient.get("/policytypes") //
+        return restClient.get(POLICYTYPES) //
             .flatMap(this::parseJsonArrayOfString);
     }
 
@@ -60,51 +82,32 @@ public class OscA1Client implements A1Client {
     public Mono<List<String>> getPolicyIdentities() {
         return getPolicyTypeIdentities() //
             .flatMapMany(types -> Flux.fromIterable(types)) //
-            .flatMap(type -> getPolicyIdentities(type)) //
+            .flatMap(type -> getPolicyIdentitiesById(type)) //
             .flatMap(policyIds -> Flux.fromIterable(policyIds)) //
             .collectList();
     }
 
-    private Mono<List<String>> getPolicyIdentities(String typeId) {
-        return restClient.get("/policytypes/" + typeId + "/policies") //
-            .flatMap(this::parseJsonArrayOfString);
-    }
-
     @Override
     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
-        return restClient.get("/policytypes/" + policyTypeId) //
+        String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
+        return restClient.get(uri) //
             .flatMap(response -> getCreateSchema(response, policyTypeId));
     }
 
-    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);
-        }
-    }
-
     @Override
     public Mono<String> putPolicy(Policy policy) {
-        return restClient.put("/policytypes/" + policy.type().name() + "/policies/" + policy.id(), policy.json());
+        String uri = POLICY_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
+        return restClient.put(uri, policy.json());
     }
 
     @Override
     public Mono<String> deletePolicy(Policy policy) {
-        return deletePolicy(policy.type().name(), policy.id());
-    }
-
-    private Mono<String> deletePolicy(String typeId, String policyId) {
-        return restClient.delete("/policytypes/" + typeId + "/policies/" + policyId);
+        return deletePolicyById(policy.type().name(), policy.id());
     }
 
     @Override
     public Mono<A1ProtocolType> getProtocolVersion() {
-        return restClient.get("/healthcheck") //
+        return restClient.get(HEALTHCHECK) //
             .flatMap(resp -> Mono.just(A1ProtocolType.OSC_V1));
     }
 
@@ -115,10 +118,40 @@ public class OscA1Client implements A1Client {
             .flatMap(typeId -> deletePoliciesForType(typeId)); //
     }
 
+    @Override
+    public Mono<String> getPolicyStatus(Policy policy) {
+        String uri = POLICY_STATUS_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
+        return restClient.get(uri);
+
+    }
+
+    private Mono<List<String>> getPolicyIdentitiesById(String typeId) {
+        String uri = POLICY_IDS_URI.buildAndExpand(typeId).toUriString();
+        return restClient.get(uri) //
+            .flatMap(this::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);
+        }
+    }
+
+    private Mono<String> deletePolicyById(String typeId, String policyId) {
+        String uri = POLICY_URI.buildAndExpand(typeId, policyId).toUriString();
+        return restClient.delete(uri);
+    }
+
     private Flux<String> deletePoliciesForType(String typeId) {
-        return getPolicyIdentities(typeId) //
+        return getPolicyIdentitiesById(typeId) //
             .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
-            .flatMap(policyId -> deletePolicy(typeId, policyId)); //
+            .flatMap(policyId -> deletePolicyById(typeId, policyId)); //
     }
 
     private Mono<List<String>> parseJsonArrayOfString(String inputString) {
@@ -134,11 +167,4 @@ public class OscA1Client implements A1Client {
             return Mono.error(ex);
         }
     }
-
-    @Override
-    public Mono<String> getPolicyStatus(Policy policy) {
-        // /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
-        return restClient.get("/policytypes/" + policy.type().name() + "/policies/" + policy.id() + "/status");
-
-    }
 }
@@ -39,7 +39,9 @@ import org.slf4j.LoggerFactory;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
-public class SdnrOnapA1Client implements A1Client {
+public class SdncOnapA1Client implements A1Client {
+    private static final String URL_PREFIX = "/A1-ADAPTER-API:";
+
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
     private final String a1ControllerUsername;
@@ -51,14 +53,14 @@ public class SdnrOnapA1Client implements A1Client {
         .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
         .create(); //
 
-    public SdnrOnapA1Client(RicConfig ricConfig, String baseUrl, String username, String password) {
+    public SdncOnapA1Client(RicConfig ricConfig, String baseUrl, String username, String password) {
         this(ricConfig, username, password, new AsyncRestClient(baseUrl + "/restconf/operations"));
         if (logger.isDebugEnabled()) {
             logger.debug("SdnrOnapA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), baseUrl);
         }
     }
 
-    public SdnrOnapA1Client(RicConfig ricConfig, String username, String password, AsyncRestClient restClient) {
+    public SdncOnapA1Client(RicConfig ricConfig, String username, String password, AsyncRestClient restClient) {
         this.ricConfig = ricConfig;
         this.a1ControllerUsername = username;
         this.a1ControllerPassword = password;
@@ -67,14 +69,14 @@ public class SdnrOnapA1Client implements A1Client {
 
     @Override
     public Mono<List<String>> getPolicyTypeIdentities() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(ricConfig.baseUrl()) //
             .build();
         String inputJsonString = createInputJsonString(inputParams);
         logger.debug("POST getPolicyTypeIdentities inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader("/A1-ADAPTER-API:getPolicyTypes", inputJsonString, a1ControllerUsername,
+            .postWithAuthHeader(URL_PREFIX + "getPolicyTypes", inputJsonString, a1ControllerUsername,
                 a1ControllerPassword) //
             .flatMap(response -> getValueFromResponse(response, "policy-type-id-list")) //
             .flatMap(this::parseJsonArrayOfString);
@@ -90,7 +92,7 @@ public class SdnrOnapA1Client implements A1Client {
     }
 
     public Mono<List<String>> getPolicyIdentities(String policyTypeId) {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(ricConfig.baseUrl()) //
             .policyTypeId(policyTypeId) //
             .build();
@@ -98,7 +100,7 @@ public class SdnrOnapA1Client implements A1Client {
         logger.debug("POST getPolicyIdentities inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader("/A1-ADAPTER-API:getPolicyInstances", inputJsonString, a1ControllerUsername,
+            .postWithAuthHeader(URL_PREFIX + "getPolicyInstances", inputJsonString, a1ControllerUsername,
                 a1ControllerPassword) //
             .flatMap(response -> getValueFromResponse(response, "policy-instance-id-list")) //
             .flatMap(this::parseJsonArrayOfString);
@@ -106,7 +108,7 @@ public class SdnrOnapA1Client implements A1Client {
 
     @Override
     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(ricConfig.baseUrl()) //
             .policyTypeId(policyTypeId) //
             .build();
@@ -114,7 +116,7 @@ public class SdnrOnapA1Client implements A1Client {
         logger.debug("POST getPolicyType inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader("/A1-ADAPTER-API:getPolicyType", inputJsonString, a1ControllerUsername,
+            .postWithAuthHeader(URL_PREFIX + "getPolicyType", inputJsonString, a1ControllerUsername,
                 a1ControllerPassword) //
             .flatMap(response -> getValueFromResponse(response, "policy-type")) //
             .flatMap(this::extractPolicySchema);
@@ -122,7 +124,7 @@ public class SdnrOnapA1Client implements A1Client {
 
     @Override
     public Mono<String> putPolicy(Policy policy) {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(ricConfig.baseUrl()) //
             .policyTypeId(policy.type().name()) //
             .policyInstanceId(policy.id()) //
@@ -132,12 +134,12 @@ public class SdnrOnapA1Client implements A1Client {
         String inputJsonString = createInputJsonString(inputParams);
         logger.debug("POST putPolicy inputJsonString = {}", inputJsonString);
 
-        return restClient.postWithAuthHeader("/A1-ADAPTER-API:createPolicyInstance", inputJsonString,
+        return restClient.postWithAuthHeader(URL_PREFIX + "createPolicyInstance", inputJsonString,
             a1ControllerUsername, a1ControllerPassword);
     }
 
-    public Mono<String> deletePolicy(String policyTypeId, String policyId) {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+    public Mono<String> deletePolicyByIds(String policyTypeId, String policyId) {
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(ricConfig.baseUrl()) //
             .policyTypeId(policyTypeId) //
             .policyInstanceId(policyId) //
@@ -145,13 +147,13 @@ public class SdnrOnapA1Client implements A1Client {
         String inputJsonString = createInputJsonString(inputParams);
         logger.debug("POST deletePolicy inputJsonString = {}", inputJsonString);
 
-        return restClient.postWithAuthHeader("/A1-ADAPTER-API:deletePolicyInstance", inputJsonString,
+        return restClient.postWithAuthHeader(URL_PREFIX + "deletePolicyInstance", inputJsonString,
             a1ControllerUsername, a1ControllerPassword);
     }
 
     @Override
     public Mono<String> deletePolicy(Policy policy) {
-        return deletePolicy(policy.type().name(), policy.id());
+        return deletePolicyByIds(policy.type().name(), policy.id());
     }
 
     @Override
@@ -161,22 +163,15 @@ public class SdnrOnapA1Client implements A1Client {
             .flatMap(typeId -> deletePoliciesForType(typeId)); //
     }
 
-    private Flux<String> deletePoliciesForType(String typeId) {
-        return getPolicyIdentities(typeId) //
-            .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
-            .flatMap(policyId -> deletePolicy(typeId, policyId)); //
-    }
-
     @Override
     public Mono<A1ProtocolType> getProtocolVersion() {
         return getPolicyTypeIdentities() //
-            .flatMap(x -> Mono.just(A1ProtocolType.SDNR_ONAP));
+            .flatMap(x -> Mono.just(A1ProtocolType.SDNC_ONAP));
     }
 
-    private String createInputJsonString(SdnrOnapAdapterInput inputParams) {
-        JSONObject inputJson = new JSONObject();
-        inputJson.put("input", new JSONObject(gson.toJson(inputParams)));
-        return inputJson.toString();
+    @Override
+    public Mono<String> getPolicyStatus(Policy policy) {
+        return Mono.error(new Exception("Status not implemented in the controller"));
     }
 
     private Mono<String> getValueFromResponse(String response, String key) {
@@ -222,8 +217,15 @@ public class SdnrOnapA1Client implements A1Client {
         }
     }
 
-    @Override
-    public Mono<String> getPolicyStatus(Policy policy) {
-        return Mono.error(new Exception("Status not implemented in the controller"));
+    private Flux<String> deletePoliciesForType(String typeId) {
+        return getPolicyIdentities(typeId) //
+            .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
+            .flatMap(policyId -> deletePolicyByIds(typeId, policyId)); //
+    }
+
+    private String createInputJsonString(SdncOnapAdapterInput inputParams) {
+        JSONObject inputJson = new JSONObject();
+        inputJson.put("input", new JSONObject(gson.toJson(inputParams)));
+        return inputJson.toString();
     }
 }
@@ -28,7 +28,7 @@ import org.immutables.value.Value;
 
 @Value.Immutable
 @Gson.TypeAdapters
-public interface SdnrOnapAdapterInput {
+public interface SdncOnapAdapterInput {
     public String nearRtRicId();
 
     public Optional<String> policyTypeId();
index 67d42d7..4000b36 100644 (file)
@@ -23,7 +23,6 @@ package org.oransc.policyagent.clients;
 import com.google.gson.FieldNamingPolicy;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
-
 import java.lang.invoke.MethodHandles;
 import java.util.ArrayList;
 import java.util.List;
@@ -40,6 +39,8 @@ import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 public class SdncOscA1Client implements A1Client {
+    private static final String URL_PREFIX = "/A1-ADAPTER-API:";
+
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
     private final String a1ControllerUsername;
@@ -74,7 +75,7 @@ public class SdncOscA1Client implements A1Client {
         logger.debug("POST getPolicyTypeIdentities inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader("/A1-ADAPTER-API:getPolicyTypeIdentities", inputJsonString, a1ControllerUsername,
+            .postWithAuthHeader(URL_PREFIX + "getPolicyTypeIdentities", inputJsonString, a1ControllerUsername,
                 a1ControllerPassword) //
             .flatMap(response -> getValueFromResponse(response, "policy-type-id-list")) //
             .flatMap(this::parseJsonArrayOfString);
@@ -89,7 +90,7 @@ public class SdncOscA1Client implements A1Client {
         logger.debug("POST getPolicyIdentities inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader("/A1-ADAPTER-API:getPolicyIdentities", inputJsonString, a1ControllerUsername,
+            .postWithAuthHeader(URL_PREFIX + "getPolicyIdentities", inputJsonString, a1ControllerUsername,
                 a1ControllerPassword) //
             .flatMap(response -> getValueFromResponse(response, "policy-id-list")) //
             .flatMap(this::parseJsonArrayOfString);
@@ -105,7 +106,7 @@ public class SdncOscA1Client implements A1Client {
         logger.debug("POST getPolicyType inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader("/A1-ADAPTER-API:getPolicyType", inputJsonString, a1ControllerUsername,
+            .postWithAuthHeader(URL_PREFIX + "getPolicyType", inputJsonString, a1ControllerUsername,
                 a1ControllerPassword) //
             .flatMap(response -> getValueFromResponse(response, "policy-type")) //
             .flatMap(this::extractPolicySchema);
@@ -123,34 +124,21 @@ public class SdncOscA1Client implements A1Client {
         logger.debug("POST putPolicy inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader("/A1-ADAPTER-API:putPolicy", inputJsonString, a1ControllerUsername,
-                a1ControllerPassword) //
+            .postWithAuthHeader(URL_PREFIX + "putPolicy", inputJsonString, a1ControllerUsername, a1ControllerPassword)
             .flatMap(response -> getValueFromResponse(response, "returned-policy")) //
             .flatMap(this::validateJson);
     }
 
     @Override
     public Mono<String> deletePolicy(Policy policy) {
-        return deletePolicy(policy.id());
+        return deletePolicyById(policy.id());
     }
 
     @Override
     public Flux<String> deleteAllPolicies() {
         return getPolicyIdentities() //
             .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) // )
-            .flatMap(policyId -> deletePolicy(policyId)); //
-    }
-
-    public Mono<String> deletePolicy(String policyId) {
-        SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() //
-            .nearRtRicUrl(ricConfig.baseUrl()) //
-            .policyId(policyId) //
-            .build();
-        String inputJsonString = createInputJsonString(inputParams);
-        logger.debug("POST deletePolicy inputJsonString = {}", inputJsonString);
-
-        return restClient.postWithAuthHeader("/A1-ADAPTER-API:deletePolicy", inputJsonString, a1ControllerUsername,
-            a1ControllerPassword);
+            .flatMap(policyId -> deletePolicyById(policyId)); //
     }
 
     @Override
@@ -159,10 +147,9 @@ public class SdncOscA1Client implements A1Client {
             .flatMap(x -> Mono.just(A1ProtocolType.SDNC_OSC));
     }
 
-    private String createInputJsonString(SdncOscAdapterInput inputParams) {
-        JSONObject inputJson = new JSONObject();
-        inputJson.put("input", new JSONObject(gson.toJson(inputParams)));
-        return inputJson.toString();
+    @Override
+    public Mono<String> getPolicyStatus(Policy policy) {
+        return Mono.error(new Exception("Status not implemented in the SDNC controller"));
     }
 
     private Mono<String> getValueFromResponse(String response, String key) {
@@ -217,8 +204,22 @@ public class SdncOscA1Client implements A1Client {
         }
     }
 
-    @Override
-    public Mono<String> getPolicyStatus(Policy policy) {
-        return Mono.error(new Exception("Status not implemented in the SDNC controller"));
+    private Mono<String> deletePolicyById(String policyId) {
+        SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() //
+            .nearRtRicUrl(ricConfig.baseUrl()) //
+            .policyId(policyId) //
+            .build();
+
+        String inputJsonString = createInputJsonString(inputParams);
+        logger.debug("POST deletePolicy inputJsonString = {}", inputJsonString);
+
+        return restClient.postWithAuthHeader(URL_PREFIX + "deletePolicy", inputJsonString, a1ControllerUsername,
+            a1ControllerPassword);
+    }
+
+    private String createInputJsonString(SdncOscAdapterInput params) {
+        JSONObject inputJson = new JSONObject();
+        inputJson.put("input", new JSONObject(gson.toJson(params)));
+        return inputJson.toString();
     }
 }
index 702658e..7a43f3c 100644 (file)
@@ -23,7 +23,6 @@ package org.oransc.policyagent.clients;
 import java.lang.invoke.MethodHandles;
 import java.util.ArrayList;
 import java.util.List;
-
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -31,59 +30,80 @@ 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;
 
 public class StdA1Client implements A1Client {
+    private static final String URL_PREFIX = "/A1-P/v1";
+
+    private static final String POLICYTYPES_URI = "/policytypes";
+    private static final String POLICY_TYPE_ID = "policyTypeId";
+
+    private static final String POLICIES_URI = "/policies";
+    private static final String POLICY_SCHEMA = "policySchema";
+
+    private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
+        UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
+
+    private static final UriComponentsBuilder POLICY_URI =
+        UriComponentsBuilder.fromPath("/policies/{policy-id}").queryParam(POLICY_TYPE_ID, "{policy-type-name}");
+
+    private static final UriComponentsBuilder POLICY_DELETE_URI =
+        UriComponentsBuilder.fromPath("/policies/{policy-id}");
+
+    private static final UriComponentsBuilder POLICY_STATUS_URI =
+        UriComponentsBuilder.fromPath("/policies/{policy-id}/status");
+
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
     private final AsyncRestClient restClient;
 
     public StdA1Client(RicConfig ricConfig) {
-        String baseUrl = ricConfig.baseUrl() + "/A1-P/v1";
+        String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
         this.restClient = new AsyncRestClient(baseUrl);
     }
 
-    public StdA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
+    public StdA1Client(AsyncRestClient restClient) {
         this.restClient = restClient;
     }
 
     @Override
     public Mono<List<String>> getPolicyIdentities() {
-        return restClient.get("/policies") //
+        return restClient.get(POLICIES_URI) //
             .flatMap(this::parseJsonArrayOfString);
     }
 
     @Override
     public Mono<String> putPolicy(Policy policy) {
-        String url = "/policies/" + policy.id() + "?policyTypeId=" + policy.type().name();
-        return restClient.put(url, policy.json()) //
+        String uri = POLICY_URI.buildAndExpand(policy.id(), policy.type().name()).toUriString();
+        return restClient.put(uri, policy.json()) //
             .flatMap(this::validateJson);
     }
 
     @Override
     public Mono<List<String>> getPolicyTypeIdentities() {
-        return restClient.get("/policytypes") //
+        return restClient.get(POLICYTYPES_URI) //
             .flatMap(this::parseJsonArrayOfString);
     }
 
     @Override
     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
-        return restClient.get("/policytypes/" + policyTypeId) //
+        String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
+        return restClient.get(uri) //
             .flatMap(this::extractPolicySchema);
     }
 
     @Override
     public Mono<String> deletePolicy(Policy policy) {
-        return deletePolicy(policy.id());
+        return deletePolicyById(policy.id());
     }
 
     @Override
     public Flux<String> deleteAllPolicies() {
         return getPolicyIdentities() //
             .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) // )
-            .flatMap(policyId -> deletePolicy(policyId)); //
+            .flatMap(policyId -> deletePolicyById(policyId)); //
     }
 
     @Override
@@ -92,8 +112,15 @@ public class StdA1Client implements A1Client {
             .flatMap(x -> Mono.just(A1ProtocolType.STD_V1));
     }
 
-    private Mono<String> deletePolicy(String policyId) {
-        return restClient.delete("/policies/" + policyId);
+    @Override
+    public Mono<String> getPolicyStatus(Policy policy) {
+        String uri = POLICY_STATUS_URI.buildAndExpand(policy.id()).toUriString();
+        return restClient.get(uri);
+    }
+
+    private Mono<String> deletePolicyById(String policyId) {
+        String uri = POLICY_DELETE_URI.buildAndExpand(policyId).toUriString();
+        return restClient.delete(uri);
     }
 
     private Mono<List<String>> parseJsonArrayOfString(String inputString) {
@@ -113,7 +140,7 @@ public class StdA1Client implements A1Client {
     private Mono<String> extractPolicySchema(String inputString) {
         try {
             JSONObject jsonObject = new JSONObject(inputString);
-            JSONObject schemaObject = jsonObject.getJSONObject("policySchema");
+            JSONObject schemaObject = jsonObject.getJSONObject(POLICY_SCHEMA);
             String schemaString = schemaObject.toString();
             return Mono.just(schemaString);
         } catch (JSONException ex) { // invalid json
@@ -130,9 +157,4 @@ public class StdA1Client implements A1Client {
         }
     }
 
-    @Override
-    public Mono<String> getPolicyStatus(Policy policy) {
-        return restClient.get("/policies/" + policy.id() + "/status");
-    }
-
 }
index 1ea677c..4f4a9be 100644 (file)
@@ -22,12 +22,10 @@ package org.oransc.policyagent;
 
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
-
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.nio.file.Files;
-
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.oransc.policyagent.configuration.ApplicationConfig;
index b42cebf..7162478 100644 (file)
@@ -130,7 +130,7 @@ public class A1ClientFactoryTest {
             .expectNext(sdnrOnapA1ClientMock) //
             .verifyComplete();
 
-        assertEquals(A1ProtocolType.SDNR_ONAP, ric.getProtocolVersion(), "Not correct protocol");
+        assertEquals(A1ProtocolType.SDNC_ONAP, ric.getProtocolVersion(), "Not correct protocol");
     }
 
     @Test
@@ -180,7 +180,7 @@ public class A1ClientFactoryTest {
 
     private void whenGetProtocolVersionSdnrOnapA1ClientReturnCorrectProtocol() {
         doReturn(sdnrOnapA1ClientMock).when(factoryUnderTest).createSdnrOnapA1Client(any(Ric.class));
-        when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNR_ONAP));
+        when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNC_ONAP));
     }
 
     private void whenGetProtocolVersionSdncOscA1ClientThrowException() {
@@ -41,7 +41,7 @@ import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
 @ExtendWith(MockitoExtension.class)
-public class SdnrOnapA1ClientTest {
+public class SdncOnapA1ClientTest {
     private static final String CONTROLLER_USERNAME = "username";
     private static final String CONTROLLER_PASSWORD = "password";
     private static final String RIC_1_URL = "RicUrl";
@@ -59,20 +59,20 @@ public class SdnrOnapA1ClientTest {
     private static final String POLICY_2_ID = "policy2";
     private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
 
-    SdnrOnapA1Client clientUnderTest;
+    SdncOnapA1Client clientUnderTest;
 
     AsyncRestClient asyncRestClientMock;
 
     @BeforeEach
     public void init() {
         asyncRestClientMock = mock(AsyncRestClient.class);
-        clientUnderTest = new SdnrOnapA1Client(A1ClientHelper.createRic(RIC_1_URL).getConfig(), CONTROLLER_USERNAME,
+        clientUnderTest = new SdncOnapA1Client(A1ClientHelper.createRic(RIC_1_URL).getConfig(), CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD, asyncRestClientMock);
     }
 
     @Test
     public void testGetPolicyTypeIdentities() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .build();
         String inputJsonString = A1ClientHelper.createInputJsonString(inputParams);
@@ -90,16 +90,16 @@ public class SdnrOnapA1ClientTest {
 
     @Test
     public void testGetPolicyIdentities() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .build();
         String inputJsonStringGetTypeIds = A1ClientHelper.createInputJsonString(inputParams);
-        inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_1_ID) //
             .build();
         String inputJsonStringGetPolicyIdsType1 = A1ClientHelper.createInputJsonString(inputParams);
-        inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_2_ID) //
             .build();
@@ -128,7 +128,7 @@ public class SdnrOnapA1ClientTest {
 
     @Test
     public void testGetValidPolicyType() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_1_ID) //
             .build();
@@ -146,7 +146,7 @@ public class SdnrOnapA1ClientTest {
 
     @Test
     public void testGetInvalidPolicyType() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_1_ID) //
             .build();
@@ -164,7 +164,7 @@ public class SdnrOnapA1ClientTest {
 
     @Test
     public void testPutPolicy() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_1_ID) //
             .policyInstanceId(POLICY_1_ID) //
@@ -184,7 +184,7 @@ public class SdnrOnapA1ClientTest {
 
     @Test
     public void testDeletePolicy() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_1_ID) //
             .policyInstanceId(POLICY_1_ID) //
@@ -202,27 +202,27 @@ public class SdnrOnapA1ClientTest {
 
     @Test
     public void testDeleteAllPolicies() {
-        SdnrOnapAdapterInput inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .build();
         String inputJsonStringGetTypeIds = A1ClientHelper.createInputJsonString(inputParams);
-        inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_1_ID) //
             .build();
         String inputJsonStringGetPolicyIdsType1 = A1ClientHelper.createInputJsonString(inputParams);
-        inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_2_ID) //
             .build();
         String inputJsonStringGetPolicyIdsType2 = A1ClientHelper.createInputJsonString(inputParams);
-        inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_1_ID) //
             .policyInstanceId(POLICY_1_ID) //
             .build();
         String inputJsonStringDeletePolicy1 = A1ClientHelper.createInputJsonString(inputParams);
-        inputParams = ImmutableSdnrOnapAdapterInput.builder() //
+        inputParams = ImmutableSdncOnapAdapterInput.builder() //
             .nearRtRicId(RIC_1_URL) //
             .policyTypeId(POLICY_TYPE_2_ID) //
             .policyInstanceId(POLICY_2_ID) //
index 57d809e..b451a0a 100644 (file)
@@ -83,6 +83,7 @@ public class SdncOscA1ClientTest {
         whenAsyncPostThenReturn(policyTypeIdsResp);
 
         Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
+
         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD);
         StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
@@ -100,6 +101,7 @@ public class SdncOscA1ClientTest {
         whenAsyncPostThenReturn(policyIdsResp);
 
         Mono<List<String>> returnedMono = clientUnderTest.getPolicyIdentities();
+
         verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD);
         StepVerifier.create(returnedMono).expectNext(policyIds).expectComplete().verify();
@@ -118,6 +120,7 @@ public class SdncOscA1ClientTest {
         whenAsyncPostThenReturn(policyTypeResp);
 
         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
+
         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD);
         StepVerifier.create(returnedMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
@@ -136,6 +139,7 @@ public class SdncOscA1ClientTest {
         whenAsyncPostThenReturn(policyTypeResp);
 
         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
+
         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD);
         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
@@ -156,6 +160,7 @@ public class SdncOscA1ClientTest {
 
         Mono<String> returnedMono = clientUnderTest
             .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
+
         verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD);
         StepVerifier.create(returnedMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
@@ -176,6 +181,7 @@ public class SdncOscA1ClientTest {
 
         Mono<String> returnedMono = clientUnderTest
             .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
+
         verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD);
         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
@@ -193,6 +199,7 @@ public class SdncOscA1ClientTest {
 
         Mono<String> returnedMono = clientUnderTest
             .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
+
         verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonString, CONTROLLER_USERNAME,
             CONTROLLER_PASSWORD);
         StepVerifier.create(returnedMono).expectComplete().verify();
@@ -220,6 +227,7 @@ public class SdncOscA1ClientTest {
         whenAsyncPostThenReturn(policyIdsResp).thenReturn(Mono.empty());
 
         Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
+
         StepVerifier.create(returnedFlux).expectComplete().verify();
         verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetIds,
             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
index 3efbebb..40f09b9 100644 (file)
@@ -22,7 +22,6 @@ package org.oransc.policyagent.clients;
 
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -32,9 +31,9 @@ import org.json.JSONException;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.oransc.policyagent.repository.Policy;
-
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
@@ -59,12 +58,12 @@ public class StdA1ClientTest {
 
     StdA1Client clientUnderTest;
 
+    @Mock
     AsyncRestClient asyncRestClientMock;
 
     @BeforeEach
     public void init() {
-        asyncRestClientMock = mock(AsyncRestClient.class);
-        clientUnderTest = new StdA1Client(A1ClientHelper.createRic(RIC_URL).getConfig(), asyncRestClientMock);
+        clientUnderTest = new StdA1Client(asyncRestClientMock);
     }
 
     @Test