X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FDcaeInventoryServices.java;h=b63bb646dd0baf1bcf55d68d7b7eee02398a543a;hb=ebcb4654ec66465fd63b35d5df2f6a3e5876fd22;hp=d5015040cbc2ab4014f3fe4dec1bc99c9960c0ae;hpb=378d05bded462b721cd04e7eb5ab06a944da452e;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 d5015040..b63bb646 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -17,8 +17,9 @@ * 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; @@ -26,7 +27,6 @@ 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; @@ -47,6 +47,7 @@ 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; @@ -60,13 +61,22 @@ 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(); - public static final String DCAE_INVENTORY_URL = "dcae.inventory.url"; + private static final String DCAE_INVENTORY_URL = "dcae.inventory.url"; + private static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval"; + private 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"; + private final ClampProperties refProp; + private final CldsDao cldsDao; + private final DcaeHttpConnectionManager dcaeHttpConnectionManager; + @Autowired - private ClampProperties refProp; - @Autowired - private CldsDao cldsDao; + public DcaeInventoryServices(ClampProperties refProp, CldsDao cldsDao, DcaeHttpConnectionManager dcaeHttpConnectionManager) { + this.refProp = refProp; + this.cldsDao = cldsDao; + this.dcaeHttpConnectionManager = dcaeHttpConnectionManager; + } + /** * Set the event inventory. @@ -78,7 +88,7 @@ public class DcaeInventoryServices { * @throws ParseException * In case of DCAE Json parse exception */ - public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException { + public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException, InterruptedException { String artifactName = cldsModel.getControlName(); DcaeEvent dcaeEvent = new DcaeEvent(); DcaeInventoryResponse dcaeResponse = null; @@ -120,19 +130,17 @@ public class DcaeInventoryServices { } private void analyzeAndSaveDcaeResponse(DcaeInventoryResponse dcaeResponse, CldsModel cldsModel, - DcaeEvent dcaeEvent, String userId) throws ParseException { + DcaeEvent dcaeEvent, String userId) { if (dcaeResponse != null) { logger.info("Dcae Response for query on inventory: " + dcaeResponse); String oldTypeId = cldsModel.getTypeId(); - String newTypeId = ""; if (dcaeResponse.getTypeId() != null) { - newTypeId = dcaeResponse.getTypeId(); cldsModel.setTypeId(dcaeResponse.getTypeId()); } 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); @@ -143,6 +151,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. * @@ -159,30 +186,45 @@ public class DcaeInventoryServices { * In case of issues with the Json parsing */ public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid) - throws IOException, ParseException { + 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 dcaeInventoryResponse = 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) { - 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 new ObjectMapper().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class); + 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; } /** @@ -210,8 +252,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); @@ -221,7 +262,7 @@ public class DcaeInventoryServices { String apiBodyString = dcaeServiceTypeRequest.toString(); logger.info("Dcae api Body String - " + apiBodyString); String url = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types"; - String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString, + String responseStr = dcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString, "application/json"); // If the DCAEServiceTypeRequest is created successfully it will // return a json object responce containing a node for newly created @@ -253,4 +294,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"); + } + } }