Minor Improvement
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeDispatcherServices.java
index 0ca2850..0aca8fb 100644 (file)
@@ -28,10 +28,12 @@ import com.att.eelf.configuration.EELFManager;
 import com.fasterxml.jackson.databind.JsonNode;\r
 import com.fasterxml.jackson.databind.node.ObjectNode;\r
 \r
+import java.io.IOException;\r
 import java.util.Date;\r
 \r
 import org.json.simple.JSONObject;\r
 import org.json.simple.parser.JSONParser;\r
+import org.json.simple.parser.ParseException;\r
 import org.onap.clamp.clds.config.ClampProperties;\r
 import org.onap.clamp.clds.exception.dcae.DcaeDeploymentException;\r
 import org.onap.clamp.clds.util.LoggingUtils;\r
@@ -52,7 +54,6 @@ public class DcaeDispatcherServices {
     private static final String STATUS_URL_LOG = "Status URL extracted: ";\r
     private static final String DCAE_URL_PREFIX = "/dcae-deployments/";\r
     private static final String DCAE_URL_PROPERTY_NAME = "dcae.dispatcher.url";\r
-    private static final String DCAE_REQUEST_FAILED_LOG = "RequestFailed - responseStr=";\r
     public static final String DCAE_REQUESTID_PROPERTY_NAME = "dcae.header.requestId";\r
     private static final String DCAE_LINK_FIELD = "links";\r
     private static final String DCAE_STATUS_FIELD = "status";\r
@@ -69,17 +70,11 @@ public class DcaeDispatcherServices {
         LoggingUtils.setTargetContext("DCAE", "deleteDeployment");\r
         try {\r
             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
-            String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", null, null);\r
-            JSONParser parser = new JSONParser();\r
-            Object obj0 = parser.parse(responseStr);\r
-            JSONObject jsonObj = (JSONObject) obj0;\r
-            JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
-            String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
+            String statusUrl = getDcaeResponse(url, "DELETE", null, null, DCAE_LINK_FIELD, DCAE_STATUS_FIELD);\r
             logger.info(STATUS_URL_LOG + statusUrl);\r
             LoggingUtils.setResponseContext("0", "Delete deployments success", this.getClass().getName());\r
             return statusUrl;\r
         } catch (Exception e) {\r
-            // Log StatusCode during exception in metrics log\r
             LoggingUtils.setResponseContext("900", "Delete deployments failed", this.getClass().getName());\r
             LoggingUtils.setErrorContext("900", "Delete deployments error");\r
             logger.error("Exception occurred during Delete Deployment Operation with DCAE", e);\r
@@ -120,16 +115,13 @@ public class DcaeDispatcherServices {
         LoggingUtils.setTargetContext("DCAE", "getOperationStatus");\r
         try {\r
             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);\r
-            JSONParser parser = new JSONParser();\r
-            Object obj0 = parser.parse(responseStr);\r
-            JSONObject jsonObj = (JSONObject) obj0;\r
+            JSONObject jsonObj = parseResponse(responseStr);\r
             String operationType = (String) jsonObj.get("operationType");\r
-            String status = (String) jsonObj.get("status");\r
+            String status = (String) jsonObj.get(DCAE_STATUS_FIELD);\r
             logger.info("Operation Type - " + operationType + ", Status " + status);\r
             LoggingUtils.setResponseContext("0", "Get operation status success", this.getClass().getName());\r
             opStatus = status;\r
         } catch (Exception e) {\r
-            // Log StatusCode during exception in metrics log\r
             LoggingUtils.setResponseContext("900", "Get operation status failed", this.getClass().getName());\r
             LoggingUtils.setErrorContext("900", "Get operation status error");\r
             logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);\r
@@ -151,7 +143,6 @@ public class DcaeDispatcherServices {
             DcaeHttpConnectionManager.doDcaeHttpQuery(url, "GET", null, null);\r
             LoggingUtils.setResponseContext("0", "Get deployments success", this.getClass().getName());\r
         } catch (Exception e) {\r
-            // Log StatusCode during exception in metrics log\r
             LoggingUtils.setResponseContext("900", "Get deployments failed", this.getClass().getName());\r
             LoggingUtils.setErrorContext("900", "Get deployments error");\r
             logger.error("Exception occurred during getDeployments Operation with DCAE", e);\r
@@ -180,23 +171,16 @@ public class DcaeDispatcherServices {
             ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template");\r
             rootNode.put("serviceTypeId", serviceTypeId);\r
             if (blueprintInputJson != null) {\r
-                rootNode.put("inputs", blueprintInputJson);\r
+                rootNode.set("inputs", blueprintInputJson);\r
             }\r
             String apiBodyString = rootNode.toString();\r
             logger.info("Dcae api Body String - " + apiBodyString);\r
             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
-            String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "PUT", apiBodyString,\r
-                    "application/json");\r
-            JSONParser parser = new JSONParser();\r
-            Object obj0 = parser.parse(responseStr);\r
-            JSONObject jsonObj = (JSONObject) obj0;\r
-            JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
-            String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
-            logger.info(STATUS_URL_LOG + statusUrl);\r
+            String statusUrl = getDcaeResponse(url, "PUT", apiBodyString, "application/json", DCAE_LINK_FIELD,\r
+                    DCAE_STATUS_FIELD);\r
             LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());\r
             return statusUrl;\r
         } catch (Exception e) {\r
-            // Log StatusCode during exception in metrics log\r
             LoggingUtils.setResponseContext("900", "Create new deployment failed", this.getClass().getName());\r
             LoggingUtils.setErrorContext("900", "Create new deployment error");\r
             logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);\r
@@ -207,7 +191,7 @@ public class DcaeDispatcherServices {
         }\r
     }\r
 \r
-    /**\r
+    /***\r
      * Returns status URL for deleteExistingDeployment operation.\r
      * \r
      * @param deploymentId\r
@@ -223,18 +207,12 @@ public class DcaeDispatcherServices {
             String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
             logger.info("Dcae api Body String - " + apiBodyString);\r
             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
-            String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", apiBodyString,\r
-                    "application/json");\r
-            JSONParser parser = new JSONParser();\r
-            Object obj0 = parser.parse(responseStr);\r
-            JSONObject jsonObj = (JSONObject) obj0;\r
-            JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
-            String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
-            logger.info(STATUS_URL_LOG + statusUrl);\r
+            String statusUrl = getDcaeResponse(url, "DELETE", apiBodyString, "application/json", DCAE_LINK_FIELD,\r
+                    DCAE_STATUS_FIELD);\r
             LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());\r
             return statusUrl;\r
+\r
         } catch (Exception e) {\r
-            // Log StatusCode during exception in metrics log\r
             LoggingUtils.setResponseContext("900", "Delete existing deployment failed", this.getClass().getName());\r
             LoggingUtils.setErrorContext("900", "Delete existing deployment error");\r
             logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);\r
@@ -245,4 +223,30 @@ public class DcaeDispatcherServices {
             metricsLogger.info("deleteExistingDeployment complete");\r
         }\r
     }\r
+\r
+    private String getDcaeResponse(String url, String requestMethod, String payload, String contentType, String node,\r
+            String nodeAttr) throws IOException, ParseException {\r
+        Date startTime = new Date();\r
+        try {\r
+            String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, requestMethod, payload, contentType);\r
+            JSONObject jsonObj = parseResponse(responseStr);\r
+            JSONObject linksObj = (JSONObject) jsonObj.get(node);\r
+            String statusUrl = (String) linksObj.get(nodeAttr);\r
+            logger.info(STATUS_URL_LOG + statusUrl);\r
+            return statusUrl;\r
+        } catch (IOException | ParseException e) {\r
+            logger.error("Exception occurred getting response from DCAE", e);\r
+            throw e;\r
+        } finally {\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("getDcaeResponse complete");\r
+        }\r
+    }\r
+\r
+    private JSONObject parseResponse(String responseStr) throws ParseException {\r
+        JSONParser parser = new JSONParser();\r
+        Object obj0 = parser.parse(responseStr);\r
+        JSONObject jsonObj = (JSONObject) obj0;\r
+        return jsonObj;\r
+    }\r
 }
\ No newline at end of file