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;
25 import org.camunda.bpm.client.task.ExternalTask;
26 import org.camunda.bpm.client.task.ExternalTaskService;
27 import org.onap.logging.ref.slf4j.ONAPLogConstants;
28 import org.onap.so.adapters.audit.AAIObjectAuditList;
29 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.core.env.Environment;
36 import org.springframework.stereotype.Component;
39 public class CreateInventoryTask {
41 private static final String UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI = "Unable to write all inventory to A&AI";
43 private static final Logger logger = LoggerFactory.getLogger(CreateInventoryTask.class);
46 CreateAAIInventory createInventory;
49 public Environment env;
51 protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
52 boolean success = true;
53 String auditInventoryString = externalTask.getVariable("auditInventoryResult");
54 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
55 AAIObjectAuditList auditInventory = null;
57 auditInventory = objectMapper.getMapper().readValue(auditInventoryString, AAIObjectAuditList.class);
58 } catch (IOException e1) {
61 setupMDC(externalTask);
63 if (auditInventory != null) {
65 logger.info("Executing External Task Create Inventory, Retry Number: {} \n {}", auditInventory,
66 externalTask.getRetries());
67 createInventory.createInventory(auditInventory);
68 } catch (Exception e) {
69 logger.error("Error during inventory of stack", e);
73 externalTaskService.complete(externalTask);
74 logger.debug("The External Task Id: {} Successful", externalTask.getId());
76 if (externalTask.getRetries() == null) {
77 logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}",
78 externalTask.getId(), getRetrySequence().length);
79 externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
80 UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000);
81 } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) {
82 logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTask.getId());
83 externalTaskService.handleBpmnError(externalTask, "AAIInventoryFailure");
85 logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ",
86 externalTask.getId(), externalTask.getRetries() - 1,
87 calculateRetryDelay(externalTask.getRetries()));
88 externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
89 UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, externalTask.getRetries() - 1,
90 calculateRetryDelay(externalTask.getRetries()));
92 logger.debug("The External Task Id: {} Failed", externalTask.getId());
95 logger.debug("The External Task Id: {} Failed, No Audit Results Written", externalTask.getId());
96 externalTaskService.handleBpmnError(externalTask, "AAIInventoryFailure");
100 private void setupMDC(ExternalTask externalTask) {
101 String msoRequestId = (String)externalTask.getVariable("mso-request-id");
102 if(msoRequestId != null && !msoRequestId.isEmpty())
103 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);