Bugfix
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / SdncOscA1Client.java
index 03fa97d..6860173 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();
     }
@@ -69,8 +71,7 @@ public class SdncOscA1Client implements A1Client {
     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 +86,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
@@ -220,23 +218,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) {