X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FDcaeInventoryServices.java;h=d8bfeeaece8627e6e457db1a4b97212a9f34bd29;hb=refs%2Fchanges%2F68%2F83568%2F1;hp=7662a9fc1f4a0a6ad9d27fb14bcc554b4358a883;hpb=81305e6875977b25afd6320c6870c41fb65e040a;p=clamp.git 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 7662a9fc..d8bfeeae 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,18 +17,17 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * Modifications copyright (c) 2018 Nokia * =================================================================== - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.clamp.clds.client; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonProcessingException; import java.io.IOException; -import java.security.GeneralSecurityException; import java.util.Date; import java.util.List; @@ -36,64 +35,70 @@ import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.dao.CldsDao; import org.onap.clamp.clds.model.CldsEvent; import org.onap.clamp.clds.model.CldsModel; import org.onap.clamp.clds.model.DcaeEvent; -import org.onap.clamp.clds.model.prop.Global; -import org.onap.clamp.clds.model.prop.ModelProperties; -import org.onap.clamp.clds.model.refprop.RefProp; +import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse; +import org.onap.clamp.clds.model.properties.Global; +import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.LoggingUtils; +import org.onap.clamp.util.HttpConnectionManager; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; /** - * * This class implements the communication with DCAE for the service inventory. - * - * - * */ +@Component public class DcaeInventoryServices { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class); - protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); + + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class); + protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + public static final String DCAE_INVENTORY_URL = "dcae.inventory.url"; + public static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval"; + public static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit"; + private final ClampProperties refProp; + private final CldsDao cldsDao; + private final HttpConnectionManager httpConnectionManager; + + /** + * Constructor. + */ @Autowired - private RefProp refProp; - @Autowired - private CldsDao cldsDao; + public DcaeInventoryServices(ClampProperties refProp, CldsDao cldsDao, + HttpConnectionManager httpConnectionManager) { + this.refProp = refProp; + this.cldsDao = cldsDao; + this.httpConnectionManager = httpConnectionManager; + } /** - * * Set the event inventory. - * + * * @param cldsModel - * The CldsModel + * The CldsModel * @param userId - * The user ID - * @throws GeneralSecurityException - * In case of issue when decryting the DCAE password + * The user ID * @throws ParseException - * In case of DCAE Json parse exception + * In case of DCAE Json parse exception */ - public void setEventInventory(CldsModel cldsModel, String userId) throws GeneralSecurityException, ParseException { + public void setEventInventory(CldsModel cldsModel, String userId) throws InterruptedException { String artifactName = cldsModel.getControlName(); DcaeEvent dcaeEvent = new DcaeEvent(); - String isDcaeInfoAvailable = null; + DcaeInventoryResponse dcaeResponse = null; Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "setEventInventory"); if (artifactName != null) { artifactName = artifactName + ".yml"; } try { - /* - * - * Below are the properties required for calling the dcae inventory - * - * url call - * - */ + // Below are the properties required for calling the dcae inventory ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false, - "{}", cldsModel.getPropText()); + "{}", cldsModel.getPropText()); Global global = prop.getGlobal(); String invariantServiceUuid = global.getService(); List resourceUuidList = global.getResourceVf(); @@ -101,39 +106,42 @@ public class DcaeInventoryServices { if (resourceUuidList != null && !resourceUuidList.isEmpty()) { resourceUuid = resourceUuidList.get(0); } - /* Invemtory service url is called in this method */ - isDcaeInfoAvailable = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid); + /* Inventory service url is called in this method */ + dcaeResponse = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid); /* set dcae events */ dcaeEvent.setArtifactName(artifactName); dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION); - } catch (JsonProcessingException e) { + LoggingUtils.setResponseContext("0", "Set inventory success", this.getClass().getName()); + } catch (ParseException e) { + LoggingUtils.setResponseContext("900", "Set inventory failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Set inventory error"); logger.error("Error during JSON decoding", e); } catch (IOException ex) { + LoggingUtils.setResponseContext("900", "Set inventory failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Set inventory error"); logger.error("Error during DCAE communication", ex); } finally { LoggingUtils.setTimeContext(startTime, new Date()); metricsLogger.info("setEventInventory complete"); } - /* Null whether the DCAE has items lenght or not */ - if (isDcaeInfoAvailable != null) { - /* Inserting Event in to DB */ - logger.info(isDcaeInfoAvailable); - JSONParser parser = new JSONParser(); - Object obj0 = parser.parse(isDcaeInfoAvailable); - JSONObject jsonObj = (JSONObject) obj0; + this.analyzeAndSaveDcaeResponse(dcaeResponse, cldsModel, dcaeEvent, userId); + } + + private void analyzeAndSaveDcaeResponse(DcaeInventoryResponse dcaeResponse, CldsModel cldsModel, + DcaeEvent dcaeEvent, String userId) { + if (dcaeResponse != null) { + logger.info("Dcae Response for query on inventory: " + dcaeResponse); String oldTypeId = cldsModel.getTypeId(); - String newTypeId = ""; - if (jsonObj.get("typeId") != null) { - newTypeId = jsonObj.get("typeId").toString(); - cldsModel.setTypeId(jsonObj.get("typeId").toString()); + if (dcaeResponse.getTypeId() != null) { + cldsModel.setTypeId(dcaeResponse.getTypeId()); } - // cldsModel.setTypeName(cldsModel.getControlName().toString()+".yml"); - if (jsonObj.get("typeName") != null) { - cldsModel.setTypeName(jsonObj.get("typeName").toString()); + if (dcaeResponse.getTypeName() != null) { + cldsModel.setTypeName(dcaeResponse.getTypeName()); } - if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)) { + if (oldTypeId == null || !cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_DISTRIBUTE) + || cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE)) { CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(), - CldsEvent.ACTION_STATE_RECEIVED, null); + CldsEvent.ACTION_STATE_RECEIVED, null); } cldsModel.save(cldsDao, userId); } else { @@ -141,45 +149,77 @@ 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 { + 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 JsonUtils.GSON.fromJson(dcaeServiceType0.toString(), DcaeInventoryResponse.class); + } + /** * DO a query to DCAE to get some Information. - * + * * @param artifactName - * The artifact Name + * The artifact Name * @param serviceUuid - * The service UUID + * The service UUID * @param resourceUuid - * The resource UUID - * @return The DCAE inventory for the artifact + * The resource UUID + * @return The DCAE inventory for the artifact in DcaeInventoryResponse * @throws IOException + * In case of issues with the stream * @throws ParseException - * + * In case of issues with the Json parsing */ - public String getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid) - throws IOException, ParseException { - Date startTime = new Date(); + public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid) + throws IOException, ParseException, InterruptedException { LoggingUtils.setTargetContext("DCAE", "getDcaeInformation"); String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName=" - + artifactName; - String fullUrl = refProp.getStringValue("DCAE_INVENTORY_URL") + "/dcae-service-types" + queryString; + + artifactName; + String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString; logger.info("Dcae Inventory Service full url - " + fullUrl); - String daceInventoryResponse = null; - String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null); - 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) { - daceInventoryResponse = null; - } else if (numServices > 0) { - JSONArray itemsArray = (JSONArray) jsonObj.get("items"); - JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0); - daceInventoryResponse = dcaeServiceType0.toString(); - logger.info(daceInventoryResponse); - } + DcaeInventoryResponse response = queryDcaeInventory(fullUrl); + LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName()); + Date startTime = new Date(); LoggingUtils.setTimeContext(startTime, new Date()); - metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices); - return daceInventoryResponse; + return response; + } + + private DcaeInventoryResponse queryDcaeInventory(String fullUrl) + throws IOException, InterruptedException, ParseException { + int retryInterval = 0; + int retryLimit = 1; + if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) { + retryLimit = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT)); + } + if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) { + retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL)); + } + for (int i = 0; i < retryLimit; i++) { + metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory"); + String response = httpConnectionManager.doGeneralHttpQuery(fullUrl, "GET", null, null, "DCAE", 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); + } + logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL"); + return null; } }