Fix DCAE connection issue
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.java
index 192dcdb..7662a9f 100644 (file)
@@ -27,16 +27,11 @@ import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;\r
 import com.fasterxml.jackson.core.JsonProcessingException;\r
 \r
-import java.io.BufferedReader;\r
 import java.io.IOException;\r
-import java.io.InputStreamReader;\r
-import java.net.HttpURLConnection;\r
-import java.net.URL;\r
+import java.security.GeneralSecurityException;\r
 import java.util.Date;\r
 import java.util.List;\r
 \r
-import javax.ws.rs.BadRequestException;\r
-\r
 import org.json.simple.JSONArray;\r
 import org.json.simple.JSONObject;\r
 import org.json.simple.parser.JSONParser;\r
@@ -52,34 +47,35 @@ import org.onap.clamp.clds.util.LoggingUtils;
 import org.springframework.beans.factory.annotation.Autowired;\r
 \r
 /**\r
+ * \r
  * This class implements the communication with DCAE for the service inventory.\r
  *\r
+ * \r
+ * \r
  */\r
 public class DcaeInventoryServices {\r
     protected static final EELFLogger logger        = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);\r
     protected static final EELFLogger auditLogger   = EELFManager.getInstance().getAuditLogger();\r
     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
-\r
     @Autowired\r
     private RefProp                   refProp;\r
-\r
     @Autowired\r
     private CldsDao                   cldsDao;\r
 \r
-    @Autowired\r
-    private SdcCatalogServices        sdcCatalogServices;\r
-\r
     /**\r
+     * \r
      * Set the event inventory.\r
      * \r
      * @param cldsModel\r
      *            The CldsModel\r
      * @param userId\r
      *            The user ID\r
+     * @throws GeneralSecurityException\r
+     *             In case of issue when decryting the DCAE password\r
      * @throws ParseException\r
-     *             In case of issues during the parsing of DCAE answer\r
+     *             In case of DCAE Json parse exception\r
      */\r
-    public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException {\r
+    public void setEventInventory(CldsModel cldsModel, String userId) throws GeneralSecurityException, ParseException {\r
         String artifactName = cldsModel.getControlName();\r
         DcaeEvent dcaeEvent = new DcaeEvent();\r
         String isDcaeInfoAvailable = null;\r
@@ -90,30 +86,30 @@ public class DcaeInventoryServices {
         }\r
         try {\r
             /*\r
+             * \r
              * Below are the properties required for calling the dcae inventory\r
+             * \r
              * url call\r
+             * \r
              */\r
             ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false,\r
                     "{}", cldsModel.getPropText());\r
             Global global = prop.getGlobal();\r
             String invariantServiceUuid = global.getService();\r
             List<String> resourceUuidList = global.getResourceVf();\r
-            String serviceUuid = sdcCatalogServices.getServiceUuidFromServiceInvariantId(invariantServiceUuid);\r
             String resourceUuid = "";\r
             if (resourceUuidList != null && !resourceUuidList.isEmpty()) {\r
-                resourceUuid = resourceUuidList.get(0).toString();\r
+                resourceUuid = resourceUuidList.get(0);\r
             }\r
             /* Invemtory service url is called in this method */\r
-            isDcaeInfoAvailable = getDcaeInformation(artifactName, serviceUuid, resourceUuid);\r
-\r
+            isDcaeInfoAvailable = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid);\r
             /* set dcae events */\r
             dcaeEvent.setArtifactName(artifactName);\r
             dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);\r
-\r
         } catch (JsonProcessingException e) {\r
             logger.error("Error during JSON decoding", e);\r
         } catch (IOException ex) {\r
-            logger.error("Error during JSON decoding", ex);\r
+            logger.error("Error during DCAE communication", ex);\r
         } finally {\r
             LoggingUtils.setTimeContext(startTime, new Date());\r
             metricsLogger.info("setEventInventory complete");\r
@@ -156,58 +152,23 @@ public class DcaeInventoryServices {
      *            The resource UUID\r
      * @return The DCAE inventory for the artifact\r
      * @throws IOException\r
-     *             In case of issues with the stream\r
      * @throws ParseException\r
-     *             In case of issues with the Json parsing\r
+     * \r
      */\r
     public String getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
             throws IOException, ParseException {\r
         Date startTime = new Date();\r
         LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
-        String queryString = "?sdcResourceId=" + resourceUuid + "&sdcServiceId=" + serviceUuid + "&typeName="\r
+        String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="\r
                 + artifactName;\r
         String fullUrl = refProp.getStringValue("DCAE_INVENTORY_URL") + "/dcae-service-types" + queryString;\r
-\r
         logger.info("Dcae Inventory Service full url - " + fullUrl);\r
         String daceInventoryResponse = null;\r
-        URL inventoryUrl = new URL(fullUrl);\r
-\r
-        HttpURLConnection conn = (HttpURLConnection) inventoryUrl.openConnection();\r
-        conn.setRequestMethod("GET");\r
-        String reqid = LoggingUtils.getRequestId();\r
-        logger.info("reqid set to " + reqid);\r
-        conn.setRequestProperty("X-ECOMP-RequestID", reqid);\r
-\r
-        boolean requestFailed = true;\r
-        int responseCode = conn.getResponseCode();\r
-        if (responseCode == 200) {\r
-            requestFailed = false;\r
-        }\r
-\r
-        StringBuilder response = new StringBuilder();\r
-\r
-        try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {\r
-            String inputLine = null;\r
-\r
-            while ((inputLine = in.readLine()) != null) {\r
-                response.append(inputLine);\r
-            }\r
-        }\r
-        String responseStr = response.toString();\r
-        if (responseStr != null) {\r
-            if (requestFailed) {\r
-                logger.error("requestFailed - responseStr=" + response);\r
-                throw new BadRequestException(responseStr);\r
-            }\r
-        }\r
-        String jsonResponseString = response.toString();\r
+        String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
         JSONParser parser = new JSONParser();\r
-        Object obj0 = parser.parse(jsonResponseString);\r
-\r
+        Object obj0 = parser.parse(responseStr);\r
         JSONObject jsonObj = (JSONObject) obj0;\r
-\r
         Long totalCount = (Long) jsonObj.get("totalCount");\r
-\r
         int numServices = totalCount.intValue();\r
         if (numServices == 0) {\r
             daceInventoryResponse = null;\r
@@ -215,11 +176,10 @@ public class DcaeInventoryServices {
             JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
             JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
             daceInventoryResponse = dcaeServiceType0.toString();\r
-            logger.info(daceInventoryResponse.toString());\r
+            logger.info(daceInventoryResponse);\r
         }\r
         LoggingUtils.setTimeContext(startTime, new Date());\r
         metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices);\r
         return daceInventoryResponse;\r
     }\r
-\r
 }\r