Update the policyName send to Policy
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.java
index 3dfe9fe..a53ea64 100644 (file)
 \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.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
@@ -43,61 +49,78 @@ import org.onap.clamp.clds.model.DcaeEvent;
 import org.onap.clamp.clds.model.prop.Global;\r
 import org.onap.clamp.clds.model.prop.ModelProperties;\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
-import com.fasterxml.jackson.core.JsonProcessingException;\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
-\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
     @Autowired\r
-    private RefProp                 refProp;\r
-\r
+    private RefProp                   refProp;\r
     @Autowired\r
-    private CldsDao                 cldsDao;\r
-\r
+    private CldsDao                   cldsDao;\r
     @Autowired\r
-    private SdcCatalogServices      sdcCatalogServices;\r
-\r
-    public void setEventInventory(CldsModel cldsModel, String userId) throws Exception {\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 DCAE Json parse exception\r
+     */\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
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "setEventInventory");\r
         if (artifactName != null) {\r
             artifactName = artifactName + ".yml";\r
         }\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
+            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.size() > 0) {\r
-                resourceUuid = resourceUuidList.get(0).toString();\r
+            if (resourceUuidList != null && !resourceUuidList.isEmpty()) {\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
-            // exception\r
-            logger.error("JsonProcessingException" + e);\r
-        } catch (IOException e) {\r
-\r
-            // exception\r
-            logger.error("IOException :" + e);\r
+            logger.error("Error during JSON decoding", e);\r
+        } catch (IOException ex) {\r
+            logger.error("Error during JSON decoding", ex);\r
+        } finally {\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("setEventInventory complete");\r
         }\r
         /* Null whether the DCAE has items lenght or not */\r
         if (isDcaeInfoAvailable != null) {\r
@@ -107,18 +130,18 @@ public class DcaeInventoryServices {
             Object obj0 = parser.parse(isDcaeInfoAvailable);\r
             JSONObject jsonObj = (JSONObject) obj0;\r
             String oldTypeId = cldsModel.getTypeId();\r
-                   String newTypeId = "";\r
+            String newTypeId = "";\r
             if (jsonObj.get("typeId") != null) {\r
-               newTypeId = jsonObj.get("typeId").toString();\r
+                newTypeId = jsonObj.get("typeId").toString();\r
                 cldsModel.setTypeId(jsonObj.get("typeId").toString());\r
             }\r
             // cldsModel.setTypeName(cldsModel.getControlName().toString()+".yml");\r
             if (jsonObj.get("typeName") != null) {\r
                 cldsModel.setTypeName(jsonObj.get("typeName").toString());\r
             }\r
-            if(oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)){\r
-               CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),\r
-                    CldsEvent.ACTION_STATE_RECEIVED, null);\r
+            if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)) {\r
+                CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),\r
+                        CldsEvent.ACTION_STATE_RECEIVED, null);\r
             }\r
             cldsModel.save(cldsDao, userId);\r
         } else {\r
@@ -126,32 +149,50 @@ public class DcaeInventoryServices {
         }\r
     }\r
 \r
-    public String getDcaeInformation(String artifactName, String serviceUUID, String resourceUUID)\r
+    /**\r
+     * DO a query to DCAE to get some Information.\r
+     * \r
+     * @param artifactName\r
+     *            The artifact Name\r
+     * @param serviceUuid\r
+     *            The service UUID\r
+     * @param resourceUuid\r
+     *            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
-        String queryString = "?sdcResourceId=" + resourceUUID + "&sdcServiceId=" + serviceUUID + "&typeName="\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
+        String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="\r
                 + artifactName;\r
         String fullUrl = refProp.getStringValue("DCAE_INVENTORY_URL") + "/dcae-service-types" + queryString;\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
         boolean requestFailed = true;\r
         int responseCode = conn.getResponseCode();\r
         if (responseCode == 200) {\r
             requestFailed = false;\r
         }\r
-\r
-        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));\r
-        String inputLine = null;\r
-        StringBuffer response = new StringBuffer();\r
-        String responseStr = null;\r
-        while ((inputLine = in.readLine()) != null) {\r
-            response.append(inputLine);\r
+        StringBuilder response = new StringBuilder();\r
+        try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {\r
+            String inputLine = null;\r
+            while ((inputLine = in.readLine()) != null) {\r
+                response.append(inputLine);\r
+            }\r
         }\r
-        in.close();\r
-        responseStr = response.toString();\r
+        String responseStr = response.toString();\r
         if (responseStr != null) {\r
             if (requestFailed) {\r
                 logger.error("requestFailed - responseStr=" + response);\r
@@ -161,11 +202,8 @@ public class DcaeInventoryServices {
         String jsonResponseString = response.toString();\r
         JSONParser parser = new JSONParser();\r
         Object obj0 = parser.parse(jsonResponseString);\r
-\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
@@ -173,9 +211,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