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