Merge "skip post instantiation configuration"
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / audit / AuditDeleteStackService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.adapters.audit;
22
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.audit.beans.AuditInventory;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.slf4j.MDC;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.core.env.Environment;
32 import org.springframework.stereotype.Component;
33
34 @Component
35 public class AuditDeleteStackService {
36         
37         private static final String UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI = "All VServers have not been deleted in A&AI";
38         
39         private static final int[] RETRY_SEQUENCE = new int[] { 1, 1, 2, 3, 5, 8, 13, 20};
40
41         
42         private static final Logger logger = LoggerFactory.getLogger(AuditDeleteStackService.class);
43         
44         @Autowired
45         public HeatStackAudit heatStackAudit; 
46         
47         @Autowired
48         public Environment env;
49
50         protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService){
51                 AuditInventory auditInventory = externalTask.getVariable("auditInventory");
52                 setupMDC(externalTask);
53                 boolean success = false;
54                 try {
55                         logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,externalTask.getRetries());
56                         success=heatStackAudit.auditHeatStackDeleted(auditInventory.getCloudRegion(), auditInventory.getCloudOwner(),
57                                         auditInventory.getTenantId(), auditInventory.getHeatStackName());
58                 } catch (Exception e) {
59                         logger.error("Error during audit of stack", e);
60                 }
61                 
62                 if (success) {
63                         externalTaskService.complete(externalTask);
64                         logger.debug("The External Task Id: {}  Successful", externalTask.getId());
65                 } else {
66                         if(externalTask.getRetries() == null){
67                                 logger.debug("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}", externalTask.getId(),RETRY_SEQUENCE.length);
68                                 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);                      
69                         }else if(externalTask.getRetries() != null &&
70                                         externalTask.getRetries()-1 == 0){
71                                 logger.debug("The External Task Id: {}  Failed, All Retries Exhausted", externalTask.getId());
72                                 externalTaskService.complete(externalTask);
73                         }else{
74                                 logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ", externalTask.getId(),externalTask.getRetries()-1, calculateRetryDelay(externalTask.getRetries()));
75                                 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()));
76                         }
77                         logger.debug("The External Task Id: {} Failed", externalTask.getId());
78                 }
79         }
80         private void setupMDC(ExternalTask externalTask) {
81                 String msoRequestId = (String)externalTask.getVariable("mso-request-id");
82                 if(msoRequestId != null && !msoRequestId.isEmpty())
83                         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId);
84         }
85         protected long calculateRetryDelay(int currentRetries){
86                 int retrySequence = RETRY_SEQUENCE.length - currentRetries;
87                 long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier","6000"));
88                 return RETRY_SEQUENCE[retrySequence] * retryMultiplier;
89         }
90 }