Fix the ServiceTypeId null 39/46239/2
authorDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Fri, 4 May 2018 15:11:51 +0000 (17:11 +0200)
committerDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Fri, 4 May 2018 15:18:10 +0000 (17:18 +0200)
Fix the ServiceTypeId null sometimes when deploying SDC artifacts, this
was due to DCAE call returning empty result (Call too early)

Issue-ID: CLAMP-151
Change-Id: I2582fabb56815a2fe40936a1cd250ff9bf3f6862
Signed-off-by: Determe, Sebastien (sd378r) <sd378r@intl.att.com>
src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java
src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java

index 6bab230..1e5deda 100644 (file)
@@ -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,26 +187,14 @@ 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
-        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
@@ -196,24 +203,21 @@ public class DcaeInventoryServices {
         if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) {\r
             retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL));\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\r
-                    // 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\r
-        // 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
index c00d7de..9fff4ef 100644 (file)
@@ -64,6 +64,7 @@ import org.springframework.stereotype.Component;
 @Component
 public class PolicyClient {
 
+    protected PolicyEngine policyEngine;
     protected static final String LOG_POLICY_PREFIX = "Response is ";
     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyClient.class);
     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
@@ -352,10 +353,11 @@ public class PolicyClient {
      * 
      * @return A new policy engine
      */
-    private PolicyEngine getPolicyEngine() {
-        PolicyEngine policyEngine;
+    private synchronized PolicyEngine getPolicyEngine() {
         try {
-            policyEngine = new PolicyEngine(policyConfiguration.getProperties());
+            if (policyEngine == null) {
+                policyEngine = new PolicyEngine(policyConfiguration.getProperties());
+            }
         } catch (PolicyEngineException e) {
             throw new PolicyClientException("Exception when creating a new policy engine", e);
         }