Fix the ServiceTypeId null
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.java
index d501504..1e5deda 100644 (file)
@@ -18,7 +18,7 @@
  * limitations under the License.\r
  * ============LICENSE_END============================================\r
  * ===================================================================\r
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * \r
  */\r
 \r
 package org.onap.clamp.clds.client;\r
@@ -26,7 +26,6 @@ package org.onap.clamp.clds.client;
 import com.att.eelf.configuration.EELFLogger;\r
 import com.att.eelf.configuration.EELFManager;\r
 import com.fasterxml.jackson.core.JsonProcessingException;\r
-import com.fasterxml.jackson.databind.ObjectMapper;\r
 import com.fasterxml.jackson.databind.node.ObjectNode;\r
 \r
 import java.io.IOException;\r
@@ -47,6 +46,7 @@ import org.onap.clamp.clds.model.DcaeEvent;
 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.JacksonUtils;\r
 import org.onap.clamp.clds.util.LoggingUtils;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
 import org.springframework.stereotype.Component;\r
@@ -61,6 +61,8 @@ public class DcaeInventoryServices {
     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();\r
     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
     public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
+    public static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval";\r
+    public static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit";\r
     public static final String DCAE_TYPE_NAME = "typeName";\r
     public static final String DCAE_TYPE_ID = "typeId";\r
     @Autowired\r
@@ -78,7 +80,7 @@ public class DcaeInventoryServices {
      * @throws ParseException\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 ParseException, InterruptedException {\r
         String artifactName = cldsModel.getControlName();\r
         DcaeEvent dcaeEvent = new DcaeEvent();\r
         DcaeInventoryResponse dcaeResponse = null;\r
@@ -120,7 +122,7 @@ public class DcaeInventoryServices {
     }\r
 \r
     private void analyzeAndSaveDcaeResponse(DcaeInventoryResponse dcaeResponse, CldsModel cldsModel,\r
-            DcaeEvent dcaeEvent, String userId) throws ParseException {\r
+            DcaeEvent dcaeEvent, String userId) {\r
         if (dcaeResponse != null) {\r
             logger.info("Dcae Response for query on inventory: " + dcaeResponse);\r
             String oldTypeId = cldsModel.getTypeId();\r
@@ -143,6 +145,25 @@ public class DcaeInventoryServices {
         }\r
     }\r
 \r
+    private int getTotalCountFromDcaeInventoryResponse(String responseStr) throws ParseException {\r
+        JSONParser parser = new JSONParser();\r
+        Object obj0 = parser.parse(responseStr);\r
+        JSONObject jsonObj = (JSONObject) obj0;\r
+        Long totalCount = (Long) jsonObj.get("totalCount");\r
+        return totalCount.intValue();\r
+    }\r
+\r
+    private DcaeInventoryResponse getItemsFromDcaeInventoryResponse(String responseStr)\r
+            throws ParseException, IOException {\r
+        JSONParser parser = new JSONParser();\r
+        Object obj0 = parser.parse(responseStr);\r
+        JSONObject jsonObj = (JSONObject) obj0;\r
+        JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
+        JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
+        return JacksonUtils.getObjectMapperInstance().readValue(dcaeServiceType0.toString(),\r
+                DcaeInventoryResponse.class);\r
+    }\r
+\r
     /**\r
      * DO a query to DCAE to get some Information.\r
      * \r
@@ -159,30 +180,45 @@ public class DcaeInventoryServices {
      *             In case of issues with the Json parsing\r
      */\r
     public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
-            throws IOException, ParseException {\r
+            throws IOException, ParseException, InterruptedException {\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 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
-            JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
-            JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
-            dcaeInventoryResponse = dcaeServiceType0.toString();\r
-            logger.info("getDcaeInformation, answer from DCAE inventory:" + dcaeInventoryResponse);\r
-        }\r
+        DcaeInventoryResponse response = queryDcaeInventory(fullUrl);\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 new ObjectMapper().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class);\r
+        return response;\r
+    }\r
+\r
+    private DcaeInventoryResponse queryDcaeInventory(String fullUrl)\r
+            throws IOException, InterruptedException, ParseException {\r
+        int retryInterval = 0;\r
+        int retryLimit = 1;\r
+        if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) {\r
+            retryLimit = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT));\r
+        }\r
+        if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) {\r
+            retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL));\r
+        }\r
+        for (int i = 0; i < retryLimit; i++) {\r
+            metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");\r
+            String response = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
+            int totalCount = getTotalCountFromDcaeInventoryResponse(response);\r
+            metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount);\r
+            if (totalCount > 0) {\r
+                logger.info("getDcaeInformation, answer from DCAE inventory:" + response);\r
+                return getItemsFromDcaeInventoryResponse(response);\r
+            }\r
+            logger.info(\r
+                    "Dcae inventory totalCount returned is 0, so waiting " + retryInterval + "ms before retrying ...");\r
+            // wait for a while and try to connect to DCAE again\r
+            Thread.sleep(retryInterval);\r
+        }\r
+        logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL");\r
+        return null;\r
     }\r
 \r
     /**\r
@@ -210,8 +246,7 @@ public class DcaeInventoryServices {
         LoggingUtils.setTargetContext("DCAE", "createDCAEServiceType");\r
         String typeId = null;\r
         try {\r
-            ObjectMapper mapper = new ObjectMapper();\r
-            ObjectNode dcaeServiceTypeRequest = mapper.createObjectNode();\r
+            ObjectNode dcaeServiceTypeRequest = JacksonUtils.getObjectMapperInstance().createObjectNode();\r
             dcaeServiceTypeRequest.put("blueprintTemplate", blueprintTemplate);\r
             dcaeServiceTypeRequest.put("owner", owner);\r
             dcaeServiceTypeRequest.put("typeName", typeName);\r