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=f1cfd18f32b9c3e4a98e6c88ba4659f69310ef51;hpb=d16052ac1ed8cf545637d5c78de87cd17d6db0bf;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 f1cfd18f..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; @@ -60,15 +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"; - 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 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. @@ -122,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); @@ -145,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. * @@ -168,54 +193,40 @@ 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) { 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)); + 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; } + /** * Inserts a new DCAEServiceType or updates an existing instance. If the * typeName is same second time(already exists) then the @@ -251,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 @@ -283,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"); + } + } }