X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FDcaeInventoryServices.java;h=5f215968165e2f90e28237ca6fe010ffbce553c6;hb=refs%2Fchanges%2F59%2F50659%2F3;hp=c4abebdcd669ec4001bf34b73d310964b0e1fcfb;hpb=35c6bda8ced4143b4fc1731727ee9bbe76b34c96;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 c4abebdc..5f215968 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END============================================ * =================================================================== - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.clamp.clds.client; @@ -26,17 +26,14 @@ 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 com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import java.io.IOException; -import java.security.GeneralSecurityException; import java.util.Date; import java.util.List; import javax.ws.rs.BadRequestException; -import org.apache.commons.codec.DecoderException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -46,8 +43,10 @@ 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.dcae.DcaeInventoryResponse; import org.onap.clamp.clds.model.properties.Global; import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.util.JacksonUtils; import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -61,7 +60,11 @@ 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 metricsLogger = EELFManager.getInstance().getMetricsLogger(); - private static final String DCAE_INVENTORY_URL = "dcae.inventory.url"; + 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"; + public static final String DCAE_TYPE_NAME = "typeName"; + public static final String DCAE_TYPE_ID = "typeId"; @Autowired private ClampProperties refProp; @Autowired @@ -74,18 +77,13 @@ public class DcaeInventoryServices { * The CldsModel * @param userId * The user ID - * @throws GeneralSecurityException - * In case of issue when decryting the DCAE password * @throws ParseException * In case of DCAE Json parse exception - * @throws DecoderException - * In case of issues with HexString decoding */ - public void setEventInventory(CldsModel cldsModel, String userId) - throws GeneralSecurityException, ParseException, DecoderException { + public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException, 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) { @@ -103,7 +101,7 @@ public class DcaeInventoryServices { resourceUuid = resourceUuidList.get(0); } /* Inventory service url is called in this method */ - isDcaeInfoAvailable = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid); + dcaeResponse = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid); /* set dcae events */ dcaeEvent.setArtifactName(artifactName); dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION); @@ -120,24 +118,21 @@ public class DcaeInventoryServices { 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); @@ -148,6 +143,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. * @@ -157,39 +171,52 @@ public class DcaeInventoryServices { * The service UUID * @param resourceUuid * The resource UUID - * @return The DCAE inventory for the artifact + * @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 { + public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid) + throws IOException, ParseException, InterruptedException { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "getDcaeInformation"); String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName=" + 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()); 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 = 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); + } + logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL"); + return null; } /** @@ -217,8 +244,7 @@ public class DcaeInventoryServices { LoggingUtils.setTargetContext("DCAE", "createDCAEServiceType"); String typeId = null; try { - ObjectMapper mapper = new ObjectMapper(); - ObjectNode dcaeServiceTypeRequest = mapper.createObjectNode(); + ObjectNode dcaeServiceTypeRequest = JacksonUtils.getObjectMapperInstance().createObjectNode(); dcaeServiceTypeRequest.put("blueprintTemplate", blueprintTemplate); dcaeServiceTypeRequest.put("owner", owner); dcaeServiceTypeRequest.put("typeName", typeName); @@ -260,4 +286,40 @@ public class DcaeInventoryServices { } return typeId; } + + /** + * Method to delete blueprint from dcae inventory if it's exists. + * + * @param typeName + * @param serviceUuid + * @param resourceUuid + * @throws InterruptedException + */ + public void deleteDCAEServiceType(String typeName, String serviceUuid, String resourceUuid) + throws InterruptedException { + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "deleteDCAEServiceType"); + boolean result = false; + try { + DcaeInventoryResponse inventoryResponse = getDcaeInformation(typeName, serviceUuid, resourceUuid); + if (inventoryResponse != null && inventoryResponse.getTypeId() != null) { + String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types/" + + inventoryResponse.getTypeId(); + DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "DELETE", null, null); + } + result = true; + } catch (IOException | ParseException e) { + logger.error("Exception occurred during deleteDCAEServiceType Operation with DCAE", e); + throw new BadRequestException("Exception occurred during deleteDCAEServiceType Operation with DCAE", e); + } finally { + if (result) { + LoggingUtils.setResponseContext("0", "Delete DCAE ServiceType success", this.getClass().getName()); + } else { + LoggingUtils.setResponseContext("900", "Delete DCAE ServiceType failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Delete DCAE ServiceType error"); + } + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("deleteDCAEServiceType completed"); + } + } }