2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.adapters.audit;
26 import org.camunda.bpm.client.task.ExternalTask;
27 import org.camunda.bpm.client.task.ExternalTaskService;
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.onap.so.audit.beans.AuditInventory;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.core.env.Environment;
35 import org.springframework.stereotype.Component;
38 public class AuditStackServiceData {
40 private static final String UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI = "Unable to find all VServers and L-Interaces in A&AI";
42 private static final int[] RETRY_SEQUENCE = new int[] { 1, 1, 2, 3, 5, 8, 13, 20};
45 private static final Logger logger = LoggerFactory.getLogger(AuditStackServiceData.class);
48 public HeatStackAudit heatStackAudit;
51 public Environment env;
53 protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService){
54 AuditInventory auditInventory = externalTask.getVariable("auditInventory");
55 setupMDC(externalTask);
56 boolean success = false;
58 logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,externalTask.getRetries());
59 success=heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(), auditInventory.getCloudOwner(),
60 auditInventory.getTenantId(), auditInventory.getHeatStackName());
61 } catch (Exception e) {
62 logger.error("Error during audit of stack", e);
66 externalTaskService.complete(externalTask);
67 logger.debug("The External Task Id: {} Successful", externalTask.getId());
69 if(externalTask.getRetries() == null){
70 logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}", externalTask.getId(),RETRY_SEQUENCE.length);
71 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, RETRY_SEQUENCE.length, 10000);
72 }else if(externalTask.getRetries() != null &&
73 externalTask.getRetries()-1 == 0){
74 logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTask.getId());
75 externalTaskService.handleBpmnError(externalTask, "AuditAAIInventoryFailure");
77 logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ", externalTask.getId(),externalTask.getRetries()-1, calculateRetryDelay(externalTask.getRetries()));
78 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, externalTask.getRetries()-1, calculateRetryDelay(externalTask.getRetries()));
80 logger.debug("The External Task Id: {} Failed", externalTask.getId());
85 private void setupMDC(ExternalTask externalTask) {
86 String msoRequestId = externalTask.getVariable("mso-request-id");
87 if(msoRequestId != null && !msoRequestId.isEmpty())
88 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId);
90 protected long calculateRetryDelay(int currentRetries){
91 int retrySequence = RETRY_SEQUENCE.length - currentRetries;
92 long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier","6000"));
93 return RETRY_SEQUENCE[retrySequence] * retryMultiplier;