Merge "Change formatting of API documentation"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / SdncOscA1Client.java
index 03fa97d..d9536a5 100644 (file)
@@ -30,6 +30,8 @@ import java.util.List;
 import java.util.Optional;
 
 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.repository.Policy;
 import org.slf4j.Logger;
@@ -56,8 +58,8 @@ public class SdncOscA1Client implements A1Client {
 
     @Value.Immutable
     @org.immutables.gson.Gson.TypeAdapters
-    public interface AdapterResponse {
-        public String body();
+    public interface AdapterOutput {
+        public Optional<String> body();
 
         public int httpStatus();
     }
@@ -67,10 +69,8 @@ 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 String a1ControllerUsername;
-    private final String a1ControllerPassword;
+    private final ControllerConfig controllerConfig;
     private final AsyncRestClient restClient;
     private final RicConfig ricConfig;
     private final A1ProtocolType protocolType;
@@ -85,21 +85,18 @@ public class SdncOscA1Client implements A1Client {
      * @param username username to accesss the SDNC controller
      * @param password password to accesss the SDNC controller
      */
-    public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, String controllerBaseUrl, String username,
-        String password) {
-        this(protocolType, ricConfig, username, password,
-            new AsyncRestClient(controllerBaseUrl + "/restconf/operations"));
-        logger.debug("SdncOscA1Client for ric: {}, a1ControllerBaseUrl: {}", ricConfig.name(), controllerBaseUrl);
+    public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig) {
+        this(protocolType, ricConfig, controllerConfig,
+            new AsyncRestClient(controllerConfig.baseUrl() + "/restconf/operations"));
+        logger.debug("SdncOscA1Client for ric: {}, a1Controller: {}", ricConfig.name(), controllerConfig);
     }
 
-    public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, String username, String password,
+    public SdncOscA1Client(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
         AsyncRestClient restClient) {
-        this.a1ControllerUsername = username;
-        this.a1ControllerPassword = password;
         this.restClient = restClient;
         this.ricConfig = ricConfig;
         this.protocolType = protocolType;
-
+        this.controllerConfig = controllerConfig;
     }
 
     @Override
@@ -112,8 +109,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
@@ -129,15 +132,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 -> SdncJsonHelper.getCreateSchema(response, policyTypeId));
+        } else {
+            return Mono.error(createIllegalProtocolException());
         }
-        throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType);
     }
 
     @Override
     public Mono<String> 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
@@ -156,8 +164,9 @@ public class SdncOscA1Client implements A1Client {
                 .flatMapMany(Flux::fromIterable)
                 .flatMap(type -> post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty())) //
                 .flatMap(SdncJsonHelper::parseJsonArrayOfString);
+        } else {
+            return Flux.error(createIllegalProtocolException());
         }
-        throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType);
     }
 
     @Override
@@ -168,17 +177,21 @@ public class SdncOscA1Client implements A1Client {
 
     @Override
     public Mono<String> 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<A1UriBuilder> 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<A1ProtocolType> tryOscProtocolVersion() {
@@ -205,13 +218,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<String> 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<String> post(String rpcName, String ricUrl, Optional<String> body) {
@@ -220,23 +237,33 @@ public class SdncOscA1Client implements A1Client {
             .body(body) //
             .build();
         final String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams);
+        logger.debug("POST inputJsonString = {}", inputJsonString);
 
         return restClient
-            .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, a1ControllerUsername, a1ControllerPassword)
+            .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, this.controllerConfig.userName(),
+                this.controllerConfig.password()) //
             .flatMap(this::extractResponseBody);
     }
 
-    private Mono<String> extractResponseBody(String response) {
-        AdapterResponse output = gson.fromJson(response, ImmutableAdapterResponse.class);
-        String body = output.body();
+    private Mono<String> extractResponse(JSONObject responseOutput) {
+        AdapterOutput output = gson.fromJson(responseOutput.toString(), ImmutableAdapterOutput.class);
+        Optional<String> optionalBody = output.body();
+        String body = optionalBody.isPresent() ? optionalBody.get() : "";
         if (HttpStatus.valueOf(output.httpStatus()).is2xxSuccessful()) {
             return Mono.just(body);
+        } 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);
+
+            return Mono.error(e);
         }
-        byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8);
-        WebClientResponseException e = new WebClientResponseException(output.httpStatus(), "statusText", null,
-            responseBodyBytes, StandardCharsets.UTF_8, null);
+    }
 
-        return Mono.error(e);
+    private Mono<String> extractResponseBody(String responseStr) {
+        return SdncJsonHelper.getOutput(responseStr) //
+            .flatMap(this::extractResponse);
     }
 
     private String controllerUrl(String rpcName) {