Fix DCAE connection issue
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeDispatcherServices.java
index e5144bf..51e4abc 100644 (file)
 \r
 package org.onap.clamp.clds.client;\r
 \r
-import java.io.BufferedReader;\r
-import java.io.DataOutputStream;\r
-import java.io.InputStream;\r
-import java.io.InputStreamReader;\r
-import java.net.URL;\r
-import java.util.stream.Collectors;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+import com.fasterxml.jackson.databind.node.ObjectNode;\r
 \r
-import javax.net.ssl.HttpsURLConnection;\r
+import java.util.Date;\r
 \r
 import org.json.simple.JSONObject;\r
 import org.json.simple.parser.JSONParser;\r
+import org.onap.clamp.clds.exception.DcaeDeploymentException;\r
 import org.onap.clamp.clds.model.refprop.RefProp;\r
+import org.onap.clamp.clds.util.LoggingUtils;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
 \r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
-\r
 /**\r
- *\r
+ * This class implements the communication with DCAE for the service\r
+ * deployments.\r
  *\r
  */\r
 public class DcaeDispatcherServices {\r
-    protected static final EELFLogger logger        = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);\r
-    protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
-\r
+    protected static final EELFLogger logger                 = EELFManager.getInstance()\r
+            .getLogger(DcaeDispatcherServices.class);\r
+    protected static final EELFLogger metricsLogger          = EELFManager.getInstance().getMetricsLogger();\r
     @Autowired\r
-    private RefProp                 refProp;\r
+    private RefProp                   refProp;\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
+    private static final String       DCAE_LINK_FIELD        = "links";\r
+    private static final String       DCAE_STATUS_FIELD      = "status";\r
 \r
     /**\r
-     *\r
+     * Delete the deployment on DCAE.\r
+     * \r
      * @param deploymentId\r
-     * @return\r
-     * @throws Exception\r
+     *            The deployment ID\r
+     * @return Return the URL Status\r
      */\r
-    public String deleteDeployment(String deploymentId) throws Exception {\r
-\r
-        String statusUrl = null;\r
-        InputStream in = null;\r
+    public String deleteDeployment(String deploymentId) {\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "deleteDeployment");\r
         try {\r
-            String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;\r
-            logger.info("Dcae Dispatcher url - " + url);\r
-            URL obj = new URL(url);\r
-            HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
-            conn.setRequestMethod("DELETE");\r
-            int responseCode = conn.getResponseCode();\r
-\r
-            boolean requestFailed = true;\r
-            logger.info("responseCode=" + responseCode);\r
-            if (responseCode == 200 || responseCode == 202) {\r
-                requestFailed = false;\r
-            }\r
-\r
-            InputStream inStream = conn.getErrorStream();\r
-            if (inStream == null) {\r
-                inStream = conn.getInputStream();\r
-            }\r
-\r
-            String responseStr = null;\r
-            if (inStream != null) {\r
-                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inStream));\r
-                String inputLine = null;\r
-                StringBuffer response = new StringBuffer();\r
-                while ((inputLine = bufferedReader.readLine()) != null) {\r
-                    response.append(inputLine);\r
-                }\r
-                responseStr = response.toString();\r
-            }\r
-\r
-            if (responseStr != null) {\r
-                if (requestFailed) {\r
-                    logger.error("requestFailed - responseStr=" + responseStr);\r
-                    throw new Exception(responseStr);\r
-                }\r
-            }\r
-\r
-            logger.debug("response code " + responseCode);\r
-            in = conn.getInputStream();\r
-            logger.debug("res:" + responseStr);\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("links");\r
-            statusUrl = (String) linksObj.get("status");\r
-            logger.debug("Status URL: " + statusUrl);\r
-\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
+            return statusUrl;\r
         } catch (Exception e) {\r
-            logger.error(e.getClass().getName() + " " + e.getMessage());\r
-            throw e;\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
-            if (in != null) {\r
-                in.close();\r
-            }\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("deleteDeployment complete");\r
         }\r
-\r
-        return statusUrl;\r
-\r
     }\r
 \r
     /**\r
-     *\r
+     * Get the Operation Status from a specified URL.\r
+     * \r
      * @param statusUrl\r
-     * @return\r
-     * @throws Exception\r
+     *            The URL provided by a previous DCAE Query\r
+     * @return The status\r
+     * \r
      */\r
-    public String getOperationStatus(String statusUrl) throws Exception {\r
-\r
-        //Assigning processing status to monitor operation status further\r
+    public String getOperationStatus(String statusUrl) {\r
+        // Assigning processing status to monitor operation status further\r
         String opStatus = "processing";\r
-        InputStream in = null;\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "getOperationStatus");\r
         try {\r
-            URL obj = new URL(statusUrl);\r
-            HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
-            conn.setRequestMethod("GET");\r
-            int responseCode = conn.getResponseCode();\r
-            logger.debug("Deployment operation status response code - " + responseCode);\r
-            if(responseCode == 200){\r
-                in = conn.getInputStream();\r
-                String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));\r
-                JSONParser parser = new JSONParser();\r
-                Object obj0 = parser.parse(res);\r
-                JSONObject jsonObj = (JSONObject) obj0;\r
-                String operationType = (String) jsonObj.get("operationType");\r
-                String status = (String) jsonObj.get("status");\r
-                logger.debug("Operation Type - " + operationType + ", Status " + status);\r
-                opStatus = status;\r
-            }\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
+            String operationType = (String) jsonObj.get("operationType");\r
+            String status = (String) jsonObj.get("status");\r
+            logger.info("Operation Type - " + operationType + ", Status " + status);\r
+            opStatus = status;\r
         } catch (Exception e) {\r
-            logger.debug(e.getClass().getName() + " " + e.getMessage());\r
-            logger.debug(e.getMessage()\r
-                    + " : got exception while retrieving status, trying again until we get 200 response code");\r
+            logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);\r
         } finally {\r
-            if (in != null) {\r
-                in.close();\r
-            }\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("getOperationStatus complete");\r
         }\r
-\r
         return opStatus;\r
     }\r
 \r
     /**\r
-     *\r
-     * @throws Exception\r
+     * This method send a getDeployments operation to DCAE.\r
+     * \r
      */\r
-    public void getDeployments() throws Exception {\r
-        InputStream in = null;\r
+    public void getDeployments() {\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "getDeployments");\r
         try {\r
-            String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments";\r
-            logger.info("Dcae Dispatcher deployments url - " + url);\r
-            URL obj = new URL(url);\r
-            HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
-            conn.setRequestMethod("GET");\r
-            int responseCode = conn.getResponseCode();\r
-            logger.debug("response code " + responseCode);\r
-            in = conn.getInputStream();\r
-            String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));\r
-            logger.debug("res:" + res);\r
+            String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX;\r
+            DcaeHttpConnectionManager.doDcaeHttpQuery(url, "GET", null, null);\r
         } catch (Exception e) {\r
-            logger.error("Exception occurred during DCAE communication", e);\r
-            throw e;\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
-            if (in != null) {\r
-                in.close();\r
-            }\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("getDeployments complete");\r
         }\r
     }\r
 \r
     /**\r
-     * Returns status URL for deployment operation\r
+     * Returns status URL for createNewDeployment operation.\r
      *\r
      * @param deploymentId\r
+     *            The deployment ID\r
      * @param serviceTypeId\r
-     * @return\r
-     * @throws Exception\r
+     *            Service type ID\r
+     * @return The status URL\r
      */\r
-    public String createNewDeployment(String deploymentId, String serviceTypeId) throws Exception {\r
-\r
-        String statusUrl = null;\r
-        InputStream inStream = null;\r
-        BufferedReader in = null;\r
+    public String createNewDeployment(String deploymentId, String serviceTypeId) {\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "createNewDeployment");\r
         try {\r
-            String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
+            ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template");\r
+            rootNode.put("serviceTypeId", serviceTypeId);\r
+            String apiBodyString = rootNode.toString();\r
             logger.info("Dcae api Body String - " + apiBodyString);\r
-            String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;\r
-            logger.info("Dcae Dispatcher Service url - " + url);\r
-            URL obj = new URL(url);\r
-            HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
-            conn.setRequestMethod("PUT");\r
-            conn.setRequestProperty("Content-Type", "application/json");\r
-            conn.setDoOutput(true);\r
-            try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {\r
-                wr.writeBytes(apiBodyString);\r
-                wr.flush();\r
-            }\r
-\r
-            boolean requestFailed = true;\r
-            int responseCode = conn.getResponseCode();\r
-            logger.info("responseCode=" + responseCode);\r
-            if (responseCode == 200 || responseCode == 202) {\r
-                requestFailed = false;\r
-            }\r
-\r
-            inStream = conn.getErrorStream();\r
-            if (inStream == null) {\r
-                inStream = conn.getInputStream();\r
-            }\r
-\r
-            String responseStr = null;\r
-            if (inStream != null) {\r
-                in = new BufferedReader(new InputStreamReader(inStream));\r
-\r
-                String inputLine = null;\r
-\r
-                StringBuffer response = new StringBuffer();\r
-\r
-                while ((inputLine = in.readLine()) != null) {\r
-                    response.append(inputLine);\r
-                }\r
-\r
-                responseStr = response.toString();\r
-            }\r
-\r
-            if (responseStr != null) {\r
-                if (requestFailed) {\r
-                    logger.error("requestFailed - responseStr=" + responseStr);\r
-                    throw new Exception(responseStr);\r
-                }\r
-            }\r
-\r
-            logger.debug("response code " + responseCode);\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("links");\r
-            statusUrl = (String) linksObj.get("status");\r
-            logger.debug("Status URL: " + statusUrl);\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
+            return statusUrl;\r
         } catch (Exception e) {\r
-            logger.error("Exception occurred during the DCAE communication", e);\r
-            throw e;\r
+            logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);\r
+            throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e);\r
         } finally {\r
-            if (inStream != null) {\r
-                inStream.close();\r
-            }\r
-            if (in != null) {\r
-                in.close();\r
-            }\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("createNewDeployment complete");\r
         }\r
-        return statusUrl;\r
     }\r
 \r
     /**\r
-     *\r
+     * Returns status URL for deleteExistingDeployment operation.\r
+     * \r
      * @param deploymentId\r
+     *            The deployment ID\r
      * @param serviceTypeId\r
-     * @return\r
-     * @throws Exception\r
+     *            The service Type ID\r
+     * @return The status URL\r
      */\r
-    public String deleteExistingDeployment(String deploymentId, String serviceTypeId) throws Exception {\r
-\r
-        String statusUrl = null;\r
-        InputStream in = null;\r
+    public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment");\r
         try {\r
             String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
-            logger.debug(apiBodyString);\r
-            String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;\r
-            logger.info("Dcae Dispatcher deployments url - " + url);\r
-            URL obj = new URL(url);\r
-            HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
-            conn.setRequestMethod("DELETE");\r
-            conn.setRequestProperty("Content-Type", "application/json");\r
-            conn.setDoOutput(true);\r
-            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());\r
-            wr.writeBytes(apiBodyString);\r
-            wr.flush();\r
-\r
-            int responseCode = conn.getResponseCode();\r
-            logger.debug("response code " + responseCode);\r
-            in = conn.getInputStream();\r
-            String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));\r
-            logger.debug("res:" + res);\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(res);\r
+            Object obj0 = parser.parse(responseStr);\r
             JSONObject jsonObj = (JSONObject) obj0;\r
-            JSONObject linksObj = (JSONObject) jsonObj.get("links");\r
-            statusUrl = (String) linksObj.get("status");\r
-            logger.debug("Status URL: " + statusUrl);\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
+            return statusUrl;\r
         } catch (Exception e) {\r
-            logger.error("Exception occurred during DCAE communication", e);\r
-            throw e;\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
         } finally {\r
-            if (in != null) {\r
-                in.close();\r
-            }\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("deleteExistingDeployment complete");\r
         }\r
-        return statusUrl;\r
     }\r
-\r
 }
\ No newline at end of file