2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.inventory.create;
23 import org.camunda.bpm.client.task.ExternalTask;
24 import org.camunda.bpm.client.task.ExternalTaskService;
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
26 import org.onap.so.adapters.audit.AAIObjectAuditList;
27 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.core.env.Environment;
33 import org.springframework.stereotype.Component;
36 public class CreateInventoryTask {
38 private static final String UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI = "Unable to write all inventory to A&AI";
40 private static final Logger logger = LoggerFactory.getLogger(CreateInventoryTask.class);
43 CreateAAIInventory createInventory;
46 public Environment env;
48 protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
49 setupMDC(externalTask);
50 boolean success = true;
51 String auditInventoryString = externalTask.getVariable("auditInventoryResult");
52 AAIObjectAuditList auditInventory = null;
54 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
55 auditInventory = objectMapper.getMapper().readValue(auditInventoryString, AAIObjectAuditList.class);
56 } catch (Exception e) {
57 logger.error("Error Parsing Audit Results", e);
59 if (auditInventory != null) {
61 logger.info("Executing External Task Create Inventory, Retry Number: {} \n {}", auditInventory,
62 externalTask.getRetries());
63 createInventory.createInventory(auditInventory);
64 } catch (Exception e) {
65 logger.error("Error during inventory of stack", e);
69 externalTaskService.complete(externalTask);
70 logger.debug("The External Task Id: {} Successful", externalTask.getId());
72 if (externalTask.getRetries() == null) {
73 logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}",
74 externalTask.getId(), getRetrySequence().length);
75 externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
76 UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000);
77 } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) {
78 logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTask.getId());
79 externalTaskService.handleBpmnError(externalTask, "AAIInventoryFailure");
81 logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ",
82 externalTask.getId(), externalTask.getRetries() - 1,
83 calculateRetryDelay(externalTask.getRetries()));
84 externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
85 UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, externalTask.getRetries() - 1,
86 calculateRetryDelay(externalTask.getRetries()));
88 logger.debug("The External Task Id: {} Failed", externalTask.getId());
91 logger.debug("The External Task Id: {} Failed, No Audit Results Written", externalTask.getId());
92 externalTaskService.handleBpmnError(externalTask, "AAIInventoryFailure");
96 private void setupMDC(ExternalTask externalTask) {
98 String msoRequestId = (String) externalTask.getVariable("mso-request-id");
99 if (msoRequestId != null && !msoRequestId.isEmpty())
100 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId);
101 } catch (Exception e) {
102 logger.error("Error in setting up MDC", e);
106 protected long calculateRetryDelay(int currentRetries) {
107 int retrySequence = getRetrySequence().length - currentRetries;
108 long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier", "6000"));
109 return Integer.parseInt(getRetrySequence()[retrySequence]) * retryMultiplier;
112 public String[] getRetrySequence() {
113 return env.getProperty("mso.workflow.topics.retrySequence", String[].class);