Rework the SDC test
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.java
index c4abebd..d501504 100644 (file)
@@ -30,13 +30,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;\r
 \r
 import java.io.IOException;\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.apache.commons.codec.DecoderException;\r
 import org.json.simple.JSONArray;\r
 import org.json.simple.JSONObject;\r
 import org.json.simple.parser.JSONParser;\r
@@ -46,6 +44,7 @@ import org.onap.clamp.clds.dao.CldsDao;
 import org.onap.clamp.clds.model.CldsEvent;\r
 import org.onap.clamp.clds.model.CldsModel;\r
 import org.onap.clamp.clds.model.DcaeEvent;\r
+import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;\r
 import org.onap.clamp.clds.model.properties.Global;\r
 import org.onap.clamp.clds.model.properties.ModelProperties;\r
 import org.onap.clamp.clds.util.LoggingUtils;\r
@@ -61,7 +60,9 @@ public class DcaeInventoryServices {
     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
-    private static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
+    public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
+    public static final String DCAE_TYPE_NAME = "typeName";\r
+    public static final String DCAE_TYPE_ID = "typeId";\r
     @Autowired\r
     private ClampProperties refProp;\r
     @Autowired\r
@@ -74,18 +75,13 @@ public class DcaeInventoryServices {
      *            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
-     * @throws DecoderException\r
-     *             In case of issues with HexString decoding\r
      */\r
-    public void setEventInventory(CldsModel cldsModel, String userId)\r
-            throws GeneralSecurityException, ParseException, DecoderException {\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
+        DcaeInventoryResponse dcaeResponse = null;\r
         Date startTime = new Date();\r
         LoggingUtils.setTargetContext("DCAE", "setEventInventory");\r
         if (artifactName != null) {\r
@@ -103,7 +99,7 @@ public class DcaeInventoryServices {
                 resourceUuid = resourceUuidList.get(0);\r
             }\r
             /* Inventory service url is called in this method */\r
-            isDcaeInfoAvailable = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid);\r
+            dcaeResponse = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid);\r
             /* set dcae events */\r
             dcaeEvent.setArtifactName(artifactName);\r
             dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);\r
@@ -120,22 +116,21 @@ public class DcaeInventoryServices {
             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
-            /* Inserting Event in to DB */\r
-            logger.info(isDcaeInfoAvailable);\r
-            JSONParser parser = new JSONParser();\r
-            Object obj0 = parser.parse(isDcaeInfoAvailable);\r
-            JSONObject jsonObj = (JSONObject) obj0;\r
+        this.analyzeAndSaveDcaeResponse(dcaeResponse, cldsModel, dcaeEvent, userId);\r
+    }\r
+\r
+    private void analyzeAndSaveDcaeResponse(DcaeInventoryResponse dcaeResponse, CldsModel cldsModel,\r
+            DcaeEvent dcaeEvent, String userId) throws ParseException {\r
+        if (dcaeResponse != null) {\r
+            logger.info("Dcae Response for query on inventory: " + dcaeResponse);\r
             String oldTypeId = cldsModel.getTypeId();\r
             String newTypeId = "";\r
-            if (jsonObj.get("typeId") != null) {\r
-                newTypeId = jsonObj.get("typeId").toString();\r
-                cldsModel.setTypeId(jsonObj.get("typeId").toString());\r
+            if (dcaeResponse.getTypeId() != null) {\r
+                newTypeId = dcaeResponse.getTypeId();\r
+                cldsModel.setTypeId(dcaeResponse.getTypeId());\r
             }\r
-            // cldsModel.setTypeName(cldsModel.getControlName().toString()+".yml");\r
-            if (jsonObj.get("typeName") != null) {\r
-                cldsModel.setTypeName(jsonObj.get("typeName").toString());\r
+            if (dcaeResponse.getTypeName() != null) {\r
+                cldsModel.setTypeName(dcaeResponse.getTypeName());\r
             }\r
             if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)\r
                     || cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE)) {\r
@@ -157,13 +152,13 @@ public class DcaeInventoryServices {
      *            The service UUID\r
      * @param resourceUuid\r
      *            The resource UUID\r
-     * @return The DCAE inventory for the artifact\r
+     * @return The DCAE inventory for the artifact in DcaeInventoryResponse\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
+    public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
             throws IOException, ParseException {\r
         Date startTime = new Date();\r
         LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
@@ -171,25 +166,23 @@ public class DcaeInventoryServices {
                 + 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
+        String dcaeInventoryResponse = null;\r
         String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
         JSONParser parser = new JSONParser();\r
         Object obj0 = parser.parse(responseStr);\r
         JSONObject jsonObj = (JSONObject) obj0;\r
         Long totalCount = (Long) jsonObj.get("totalCount");\r
         int numServices = totalCount.intValue();\r
-        if (numServices == 0) {\r
-            daceInventoryResponse = null;\r
-        } else if (numServices > 0) {\r
+        if (numServices > 0) {\r
             JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
             JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
-            daceInventoryResponse = dcaeServiceType0.toString();\r
-            logger.info(daceInventoryResponse);\r
+            dcaeInventoryResponse = dcaeServiceType0.toString();\r
+            logger.info("getDcaeInformation, answer from DCAE inventory:" + dcaeInventoryResponse);\r
         }\r
         LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName());\r
         LoggingUtils.setTimeContext(startTime, new Date());\r
         metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices);\r
-        return daceInventoryResponse;\r
+        return new ObjectMapper().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class);\r
     }\r
 \r
     /**\r