Rework the submit operation
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeDispatcherServices.java
index 0aca8fb..0f46595 100644 (file)
  * limitations under the License.\r
  * ============LICENSE_END============================================\r
  * ===================================================================\r
- * \r
+ *\r
  */\r
 \r
 package org.onap.clamp.clds.client;\r
 \r
 import com.att.eelf.configuration.EELFLogger;\r
 import com.att.eelf.configuration.EELFManager;\r
-import com.fasterxml.jackson.databind.JsonNode;\r
-import com.fasterxml.jackson.databind.node.ObjectNode;\r
 \r
+import com.google.gson.JsonObject;\r
 import java.io.IOException;\r
 import java.util.Date;\r
 \r
@@ -37,6 +36,7 @@ import org.json.simple.parser.ParseException;
 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
+import org.onap.clamp.util.HttpConnectionManager;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
 import org.springframework.stereotype.Component;\r
 \r
@@ -49,8 +49,8 @@ public class DcaeDispatcherServices {
 \r
     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);\r
     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
-    @Autowired\r
-    private ClampProperties refProp;\r
+    private final ClampProperties refProp;\r
+    private final HttpConnectionManager dcaeHttpConnectionManager;\r
     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
@@ -58,33 +58,19 @@ public class DcaeDispatcherServices {
     private static final String DCAE_LINK_FIELD = "links";\r
     private static final String DCAE_STATUS_FIELD = "status";\r
 \r
-    /**\r
-     * Delete the deployment on DCAE.\r
-     * \r
-     * @param deploymentId\r
-     *            The deployment ID\r
-     * @return Return the URL Status\r
-     */\r
-    public String deleteDeployment(String deploymentId) {\r
-        Date startTime = new Date();\r
-        LoggingUtils.setTargetContext("DCAE", "deleteDeployment");\r
-        try {\r
-            String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\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
-            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
-            throw new DcaeDeploymentException("Exception occurred during Delete Deployment Operation with DCAE", e);\r
-        } finally {\r
-            LoggingUtils.setTimeContext(startTime, new Date());\r
-            metricsLogger.info("deleteDeployment complete");\r
-        }\r
+    @Autowired\r
+    public DcaeDispatcherServices(ClampProperties refProp, HttpConnectionManager dcaeHttpConnectionManager) {\r
+        this.refProp = refProp;\r
+        this.dcaeHttpConnectionManager = dcaeHttpConnectionManager;\r
     }\r
 \r
+    /**\r
+     * Get the Operation Status from a specified URL with retry.\r
+     * @param operationStatusUrl\r
+     *        The URL of the DCAE\r
+     * @return The status\r
+     * @throws InterruptedException Exception during the retry\r
+     */\r
     public String getOperationStatusWithRetry(String operationStatusUrl) throws InterruptedException {\r
         String operationStatus = "";\r
         for (int i = 0; i < Integer.valueOf(refProp.getStringValue("dcae.dispatcher.retry.limit")); i++) {\r
@@ -103,9 +89,8 @@ public class DcaeDispatcherServices {
 \r
     /**\r
      * Get the Operation Status from a specified URL.\r
-     * \r
      * @param statusUrl\r
-     *            The URL provided by a previous DCAE Query\r
+     *        The URL provided by a previous DCAE Query\r
      * @return The status\r
      */\r
     public String getOperationStatus(String statusUrl) {\r
@@ -114,7 +99,7 @@ public class DcaeDispatcherServices {
         Date startTime = new Date();\r
         LoggingUtils.setTargetContext("DCAE", "getOperationStatus");\r
         try {\r
-            String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);\r
+            String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(statusUrl, "GET", null, null, "DCAE", null, null);\r
             JSONObject jsonObj = parseResponse(responseStr);\r
             String operationType = (String) jsonObj.get("operationType");\r
             String status = (String) jsonObj.get(DCAE_STATUS_FIELD);\r
@@ -132,52 +117,30 @@ public class DcaeDispatcherServices {
         return opStatus;\r
     }\r
 \r
-    /**\r
-     * This method send a getDeployments operation to DCAE.\r
-     */\r
-    public void getDeployments() {\r
-        Date startTime = new Date();\r
-        LoggingUtils.setTargetContext("DCAE", "getDeployments");\r
-        try {\r
-            String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX;\r
-            DcaeHttpConnectionManager.doDcaeHttpQuery(url, "GET", null, null);\r
-            LoggingUtils.setResponseContext("0", "Get deployments success", this.getClass().getName());\r
-        } catch (Exception e) {\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
-            throw new DcaeDeploymentException("Exception occurred during getDeployments Operation with DCAE", e);\r
-        } finally {\r
-            LoggingUtils.setTimeContext(startTime, new Date());\r
-            metricsLogger.info("getDeployments complete");\r
-        }\r
-    }\r
-\r
     /**\r
      * Returns status URL for createNewDeployment operation.\r
-     *\r
      * @param deploymentId\r
-     *            The deployment ID\r
+     *        The deployment ID\r
      * @param serviceTypeId\r
-     *            Service type ID\r
+     *        Service type ID\r
      * @param blueprintInputJson\r
-     *            The value for each blueprint parameters in a flat JSON\r
+     *        The value for each blueprint parameters in a flat JSON\r
      * @return The status URL\r
      */\r
-    public String createNewDeployment(String deploymentId, String serviceTypeId, JsonNode blueprintInputJson) {\r
+    public String createNewDeployment(String deploymentId, String serviceTypeId, JsonObject blueprintInputJson) {\r
         Date startTime = new Date();\r
         LoggingUtils.setTargetContext("DCAE", "createNewDeployment");\r
         try {\r
-            ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template");\r
-            rootNode.put("serviceTypeId", serviceTypeId);\r
+            JsonObject rootObject = refProp.getJsonTemplate("dcae.deployment.template").getAsJsonObject();\r
+            rootObject.addProperty("serviceTypeId", serviceTypeId);\r
             if (blueprintInputJson != null) {\r
-                rootNode.set("inputs", blueprintInputJson);\r
+                rootObject.add("inputs", blueprintInputJson);\r
             }\r
-            String apiBodyString = rootNode.toString();\r
+            String apiBodyString = rootObject.toString();\r
             logger.info("Dcae api Body String - " + apiBodyString);\r
             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
             String statusUrl = getDcaeResponse(url, "PUT", apiBodyString, "application/json", DCAE_LINK_FIELD,\r
-                    DCAE_STATUS_FIELD);\r
+                DCAE_STATUS_FIELD);\r
             LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());\r
             return statusUrl;\r
         } catch (Exception e) {\r
@@ -193,11 +156,10 @@ public class DcaeDispatcherServices {
 \r
     /***\r
      * Returns status URL for deleteExistingDeployment operation.\r
-     * \r
      * @param deploymentId\r
-     *            The deployment ID\r
+     *        The deployment ID\r
      * @param serviceTypeId\r
-     *            The service Type ID\r
+     *        The service Type ID\r
      * @return The status URL\r
      */\r
     public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {\r
@@ -208,7 +170,7 @@ public class DcaeDispatcherServices {
             logger.info("Dcae api Body String - " + apiBodyString);\r
             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
             String statusUrl = getDcaeResponse(url, "DELETE", apiBodyString, "application/json", DCAE_LINK_FIELD,\r
-                    DCAE_STATUS_FIELD);\r
+                DCAE_STATUS_FIELD);\r
             LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());\r
             return statusUrl;\r
 \r
@@ -217,7 +179,7 @@ public class DcaeDispatcherServices {
             LoggingUtils.setErrorContext("900", "Delete existing deployment error");\r
             logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);\r
             throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE",\r
-                    e);\r
+                e);\r
         } finally {\r
             LoggingUtils.setTimeContext(startTime, new Date());\r
             metricsLogger.info("deleteExistingDeployment complete");\r
@@ -225,10 +187,10 @@ public class DcaeDispatcherServices {
     }\r
 \r
     private String getDcaeResponse(String url, String requestMethod, String payload, String contentType, String node,\r
-            String nodeAttr) throws IOException, ParseException {\r
+        String nodeAttr) throws IOException, ParseException {\r
         Date startTime = new Date();\r
         try {\r
-            String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, requestMethod, payload, contentType);\r
+            String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(url, requestMethod, payload, contentType, "DCAE", null, null);\r
             JSONObject jsonObj = parseResponse(responseStr);\r
             JSONObject linksObj = (JSONObject) jsonObj.get(node);\r
             String statusUrl = (String) linksObj.get(nodeAttr);\r