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 java.io.IOException;
24 import org.camunda.bpm.client.task.ExternalTask;
25 import org.camunda.bpm.client.task.ExternalTaskService;
26 import org.onap.logging.ref.slf4j.ONAPLogConstants;
27 import org.onap.so.adapters.audit.AAIObjectAuditList;
28 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.core.env.Environment;
35 import org.springframework.stereotype.Component;
38 public class CreateInventoryTask {
40 private static final String UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI = "Unable to write all inventory to A&AI";
42 private static final Logger logger = LoggerFactory.getLogger(CreateInventoryTask.class);
45 CreateAAIInventory createInventory;
48 public Environment env;
50 protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
51 boolean success = true;
52 String auditInventoryString = externalTask.getVariable("auditInventoryResult");
53 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
54 AAIObjectAuditList auditInventory = null;
56 auditInventory = objectMapper.getMapper().readValue(auditInventoryString, AAIObjectAuditList.class);
57 } catch (IOException e1) {
60 setupMDC(externalTask);
62 if (auditInventory != null) {
64 logger.info("Executing External Task Create Inventory, Retry Number: {} \n {}", auditInventory,
65 externalTask.getRetries());
66 createInventory.createInventory(auditInventory);
67 } catch (Exception e) {
68 logger.error("Error during inventory of stack", e);
72 externalTaskService.complete(externalTask);
73 logger.debug("The External Task Id: {} Successful", externalTask.getId());
75 if (externalTask.getRetries() == null) {
76 logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}",
77 externalTask.getId(), getRetrySequence().length);
78 externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
79 UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000);
80 } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) {
81 logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTask.getId());
82 externalTaskService.handleBpmnError(externalTask, "AAIInventoryFailure");
84 logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ",
85 externalTask.getId(), externalTask.getRetries() - 1,
86 calculateRetryDelay(externalTask.getRetries()));
87 externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
88 UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, externalTask.getRetries() - 1,
89 calculateRetryDelay(externalTask.getRetries()));
91 logger.debug("The External Task Id: {} Failed", externalTask.getId());
94 logger.debug("The External Task Id: {} Failed, No Audit Results Written", externalTask.getId());
95 externalTaskService.handleBpmnError(externalTask, "AAIInventoryFailure");
99 private void setupMDC(ExternalTask externalTask) {
100 String msoRequestId = (String) externalTask.getVariable("mso-request-id");
101 if (msoRequestId != null && !msoRequestId.isEmpty())
102 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId);
105 protected long calculateRetryDelay(int currentRetries) {
106 int retrySequence = getRetrySequence().length - currentRetries;
107 long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier", "6000"));
108 return Integer.parseInt(getRetrySequence()[retrySequence]) * retryMultiplier;
111 public String[] getRetrySequence() {
112 return env.getProperty("mso.workflow.topics.retrySequence", String[].class);