* See the License for the specific language governing permissions and\r
* limitations under the License.\r
* ============LICENSE_END============================================\r
+ * Modifications copyright (c) 2018 Nokia\r
* ===================================================================\r
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * \r
*/\r
\r
package org.onap.clamp.clds.client;\r
protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);\r
protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();\r
protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
- public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
+ private static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
+ private static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval";\r
+ private static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit";\r
public static final String DCAE_TYPE_NAME = "typeName";\r
public static final String DCAE_TYPE_ID = "typeId";\r
+ private final ClampProperties refProp;\r
+ private final CldsDao cldsDao;\r
+ private final DcaeHttpConnectionManager dcaeHttpConnectionManager;\r
+\r
@Autowired\r
- private ClampProperties refProp;\r
- @Autowired\r
- private CldsDao cldsDao;\r
+ public DcaeInventoryServices(ClampProperties refProp, CldsDao cldsDao, DcaeHttpConnectionManager dcaeHttpConnectionManager) {\r
+ this.refProp = refProp;\r
+ this.cldsDao = cldsDao;\r
+ this.dcaeHttpConnectionManager = dcaeHttpConnectionManager;\r
+ }\r
+\r
\r
/**\r
* Set the event inventory.\r
* @throws ParseException\r
* In case of DCAE Json parse exception\r
*/\r
- public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException {\r
+ public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException, InterruptedException {\r
String artifactName = cldsModel.getControlName();\r
DcaeEvent dcaeEvent = new DcaeEvent();\r
DcaeInventoryResponse dcaeResponse = null;\r
}\r
\r
private void analyzeAndSaveDcaeResponse(DcaeInventoryResponse dcaeResponse, CldsModel cldsModel,\r
- DcaeEvent dcaeEvent, String userId) throws ParseException {\r
+ DcaeEvent dcaeEvent, String userId) {\r
if (dcaeResponse != null) {\r
logger.info("Dcae Response for query on inventory: " + dcaeResponse);\r
String oldTypeId = cldsModel.getTypeId();\r
- String newTypeId = "";\r
if (dcaeResponse.getTypeId() != null) {\r
- newTypeId = dcaeResponse.getTypeId();\r
cldsModel.setTypeId(dcaeResponse.getTypeId());\r
}\r
if (dcaeResponse.getTypeName() != null) {\r
cldsModel.setTypeName(dcaeResponse.getTypeName());\r
}\r
- if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)\r
+ if (oldTypeId == null || !cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_DISTRIBUTE)\r
|| cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE)) {\r
CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),\r
CldsEvent.ACTION_STATE_RECEIVED, null);\r
}\r
}\r
\r
+ private int getTotalCountFromDcaeInventoryResponse(String responseStr) throws ParseException {\r
+ JSONParser parser = new JSONParser();\r
+ Object obj0 = parser.parse(responseStr);\r
+ JSONObject jsonObj = (JSONObject) obj0;\r
+ Long totalCount = (Long) jsonObj.get("totalCount");\r
+ return totalCount.intValue();\r
+ }\r
+\r
+ private DcaeInventoryResponse getItemsFromDcaeInventoryResponse(String responseStr)\r
+ throws ParseException, IOException {\r
+ JSONParser parser = new JSONParser();\r
+ Object obj0 = parser.parse(responseStr);\r
+ JSONObject jsonObj = (JSONObject) obj0;\r
+ JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
+ JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
+ return JacksonUtils.getObjectMapperInstance().readValue(dcaeServiceType0.toString(),\r
+ DcaeInventoryResponse.class);\r
+ }\r
+\r
/**\r
* DO a query to DCAE to get some Information.\r
* \r
* In case of issues with the Json parsing\r
*/\r
public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
- throws IOException, ParseException {\r
+ throws IOException, ParseException, InterruptedException {\r
Date startTime = new Date();\r
LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="\r
+ artifactName;\r
String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString;\r
logger.info("Dcae Inventory Service full url - " + fullUrl);\r
- String dcaeInventoryResponse = null;\r
- String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
- JSONParser parser = new JSONParser();\r
- Object obj0 = parser.parse(responseStr);\r
- JSONObject jsonObj = (JSONObject) obj0;\r
- Long totalCount = (Long) jsonObj.get("totalCount");\r
- int numServices = totalCount.intValue();\r
- if (numServices > 0) {\r
- JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
- JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
- dcaeInventoryResponse = dcaeServiceType0.toString();\r
- logger.info("getDcaeInformation, answer from DCAE inventory:" + dcaeInventoryResponse);\r
- }\r
+ DcaeInventoryResponse response = queryDcaeInventory(fullUrl);\r
LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName());\r
LoggingUtils.setTimeContext(startTime, new Date());\r
- metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices);\r
- return JacksonUtils.getObjectMapperInstance().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class);\r
+ return response;\r
+ }\r
+\r
+ private DcaeInventoryResponse queryDcaeInventory(String fullUrl)\r
+ throws IOException, InterruptedException, ParseException {\r
+ int retryInterval = 0;\r
+ int retryLimit = 1;\r
+ if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) {\r
+ retryLimit = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT));\r
+ }\r
+ if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) {\r
+ retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL));\r
+ }\r
+ for (int i = 0; i < retryLimit; i++) {\r
+ metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");\r
+ String response = dcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
+ int totalCount = getTotalCountFromDcaeInventoryResponse(response);\r
+ metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount);\r
+ if (totalCount > 0) {\r
+ logger.info("getDcaeInformation, answer from DCAE inventory:" + response);\r
+ return getItemsFromDcaeInventoryResponse(response);\r
+ }\r
+ logger.info(\r
+ "Dcae inventory totalCount returned is 0, so waiting " + retryInterval + "ms before retrying ...");\r
+ // wait for a while and try to connect to DCAE again\r
+ Thread.sleep(retryInterval);\r
+ }\r
+ logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL");\r
+ return null;\r
}\r
\r
/**\r
String apiBodyString = dcaeServiceTypeRequest.toString();\r
logger.info("Dcae api Body String - " + apiBodyString);\r
String url = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types";\r
- String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString,\r
+ String responseStr = dcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString,\r
"application/json");\r
// If the DCAEServiceTypeRequest is created successfully it will\r
// return a json object responce containing a node for newly created\r
}\r
return typeId;\r
}\r
+\r
+ /**\r
+ * Method to delete blueprint from dcae inventory if it's exists.\r
+ * \r
+ * @param typeName\r
+ * @param serviceUuid\r
+ * @param resourceUuid\r
+ * @throws InterruptedException\r
+ */\r
+ public void deleteDCAEServiceType(String typeName, String serviceUuid, String resourceUuid)\r
+ throws InterruptedException {\r
+ Date startTime = new Date();\r
+ LoggingUtils.setTargetContext("DCAE", "deleteDCAEServiceType");\r
+ boolean result = false;\r
+ try {\r
+ DcaeInventoryResponse inventoryResponse = getDcaeInformation(typeName, serviceUuid, resourceUuid);\r
+ if (inventoryResponse != null && inventoryResponse.getTypeId() != null) {\r
+ String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types/"\r
+ + inventoryResponse.getTypeId();\r
+ dcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "DELETE", null, null);\r
+ }\r
+ result = true;\r
+ } catch (IOException | ParseException e) {\r
+ logger.error("Exception occurred during deleteDCAEServiceType Operation with DCAE", e);\r
+ throw new BadRequestException("Exception occurred during deleteDCAEServiceType Operation with DCAE", e);\r
+ } finally {\r
+ if (result) {\r
+ LoggingUtils.setResponseContext("0", "Delete DCAE ServiceType success", this.getClass().getName());\r
+ } else {\r
+ LoggingUtils.setResponseContext("900", "Delete DCAE ServiceType failed", this.getClass().getName());\r
+ LoggingUtils.setErrorContext("900", "Delete DCAE ServiceType error");\r
+ }\r
+ LoggingUtils.setTimeContext(startTime, new Date());\r
+ metricsLogger.info("deleteDCAEServiceType completed");\r
+ }\r
+ }\r
}\r