Fix the ServiceTypeId null
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.java
index 289f31b..1e5deda 100644 (file)
@@ -122,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
@@ -145,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
@@ -168,54 +187,40 @@ 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 dcaeInventoryResponse = null;\r
-\r
-        String responseStr = queryDCAEInventory (fullUrl);\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 JacksonUtils.getObjectMapperInstance().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class);\r
+        return response;\r
     }\r
 \r
-    private String queryDCAEInventory (String fullUrl) throws IOException, InterruptedException {\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
+            retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL));\r
         }\r
-\r
-        int i = 0;\r
-        while (i < retryLimit) {\r
-            i++;\r
-            try {\r
-                return DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
-            } catch (BadRequestException e) {\r
-                if (i == retryLimit) {\r
-                    // reach the retry limit, but still failed to connect to DCAE\r
-                    throw e;\r
-                } else {\r
-                    // wait for a while and try to connect to DCAE again\r
-                    Thread.sleep(retryInterval);\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
-        // normally it should not go to this branch. It should either return the DCAE query result, or throw exception\r
+        logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL");\r
         return null;\r
     }\r
+\r
     /**\r
      * Inserts a new DCAEServiceType or updates an existing instance. If the\r
      * typeName is same second time(already exists) then the\r