Add Request ID in Common Logging
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.java
index 3dfe9fe..c35eb0d 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.util.Date;\r
 import java.util.List;\r
 \r
 import javax.ws.rs.BadRequestException;\r
@@ -43,29 +48,43 @@ 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
+ * This class implements the communication with DCAE for the service inventory.\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 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
+    private RefProp                   refProp;\r
 \r
     @Autowired\r
-    private CldsDao                 cldsDao;\r
+    private CldsDao                   cldsDao;\r
 \r
     @Autowired\r
-    private SdcCatalogServices      sdcCatalogServices;\r
-\r
-    public void setEventInventory(CldsModel cldsModel, String userId) throws Exception {\r
+    private SdcCatalogServices        sdcCatalogServices;\r
+\r
+    /**\r
+     * Set the event inventory.\r
+     * \r
+     * @param cldsModel\r
+     *            The CldsModel\r
+     * @param userId\r
+     *            The user ID\r
+     * @throws ParseException\r
+     *             In case of issues during the parsing of DCAE answer\r
+     */\r
+    public void setEventInventory(CldsModel cldsModel, String userId) throws 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
@@ -74,8 +93,8 @@ public class DcaeInventoryServices {
              * Below are the properties required for calling the dcae inventory\r
              * url call\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
@@ -92,12 +111,12 @@ public class DcaeInventoryServices {
             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 +126,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 +145,55 @@ 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
+    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 = "?sdcResourceId=" + resourceUuid + "&sdcServiceId=" + 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
-        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
+\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
-        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
@@ -175,6 +217,8 @@ public class DcaeInventoryServices {
             daceInventoryResponse = dcaeServiceType0.toString();\r
             logger.info(daceInventoryResponse.toString());\r
         }\r
+        LoggingUtils.setTimeContext(startTime, new Date());\r
+        metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices);\r
         return daceInventoryResponse;\r
     }\r
 \r