From 96a55871dabf4fa374de16091e165f0dc77bd9f7 Mon Sep 17 00:00:00 2001 From: "Determe, Sebastien (sd378r)" Date: Fri, 4 May 2018 17:11:51 +0200 Subject: Fix the ServiceTypeId null 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) --- .../clamp/clds/client/DcaeInventoryServices.java | 68 ++++++++++++---------- .../clamp/clds/client/req/policy/PolicyClient.java | 8 ++- 2 files changed, 41 insertions(+), 35 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java index 6bab2303f..1e5dedaa6 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -145,6 +145,25 @@ public class DcaeInventoryServices { } } + private int getTotalCountFromDcaeInventoryResponse(String responseStr) throws ParseException { + JSONParser parser = new JSONParser(); + Object obj0 = parser.parse(responseStr); + JSONObject jsonObj = (JSONObject) obj0; + Long totalCount = (Long) jsonObj.get("totalCount"); + return totalCount.intValue(); + } + + private DcaeInventoryResponse getItemsFromDcaeInventoryResponse(String responseStr) + throws ParseException, IOException { + JSONParser parser = new JSONParser(); + Object obj0 = parser.parse(responseStr); + JSONObject jsonObj = (JSONObject) obj0; + JSONArray itemsArray = (JSONArray) jsonObj.get("items"); + JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0); + return JacksonUtils.getObjectMapperInstance().readValue(dcaeServiceType0.toString(), + DcaeInventoryResponse.class); + } + /** * DO a query to DCAE to get some Information. * @@ -168,26 +187,14 @@ public class DcaeInventoryServices { + artifactName; String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString; logger.info("Dcae Inventory Service full url - " + fullUrl); - String dcaeInventoryResponse = null; - String responseStr = queryDCAEInventory(fullUrl); - JSONParser parser = new JSONParser(); - Object obj0 = parser.parse(responseStr); - JSONObject jsonObj = (JSONObject) obj0; - Long totalCount = (Long) jsonObj.get("totalCount"); - int numServices = totalCount.intValue(); - if (numServices > 0) { - JSONArray itemsArray = (JSONArray) jsonObj.get("items"); - JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0); - dcaeInventoryResponse = dcaeServiceType0.toString(); - logger.info("getDcaeInformation, answer from DCAE inventory:" + dcaeInventoryResponse); - } + DcaeInventoryResponse response = queryDcaeInventory(fullUrl); LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName()); LoggingUtils.setTimeContext(startTime, new Date()); - metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices); - return JacksonUtils.getObjectMapperInstance().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class); + return response; } - private String queryDCAEInventory(String fullUrl) throws IOException, InterruptedException { + private DcaeInventoryResponse queryDcaeInventory(String fullUrl) + throws IOException, InterruptedException, ParseException { int retryInterval = 0; int retryLimit = 1; if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) { @@ -196,24 +203,21 @@ public class DcaeInventoryServices { if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) { retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL)); } - int i = 0; - while (i < retryLimit) { - i++; - try { - return DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null); - } catch (BadRequestException e) { - if (i == retryLimit) { - // reach the retry limit, but still failed to connect to - // DCAE - throw e; - } else { - // wait for a while and try to connect to DCAE again - Thread.sleep(retryInterval); - } + for (int i = 0; i < retryLimit; i++) { + metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory"); + String response = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null); + int totalCount = getTotalCountFromDcaeInventoryResponse(response); + metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount); + if (totalCount > 0) { + logger.info("getDcaeInformation, answer from DCAE inventory:" + response); + return getItemsFromDcaeInventoryResponse(response); } + logger.info( + "Dcae inventory totalCount returned is 0, so waiting " + retryInterval + "ms before retrying ..."); + // wait for a while and try to connect to DCAE again + Thread.sleep(retryInterval); } - // normally it should not go to this branch. It should either return the - // DCAE query result, or throw exception + logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL"); return null; } diff --git a/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java b/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java index c00d7de8c..9fff4efc5 100644 --- a/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java +++ b/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java @@ -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); } -- cgit 1.2.3-korg