9b245ba6951b882caa62772e8d8a51e892a873e9
[so.git] /
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 java.util.HashMap;
24 import java.util.Map;
25 import java.util.Optional;
26 import org.camunda.bpm.client.task.ExternalTask;
27 import org.camunda.bpm.client.task.ExternalTaskService;
28 import org.onap.so.audit.beans.AuditInventory;
29 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
30 import org.onap.so.objects.audit.AAIObjectAuditList;
31 import org.slf4j.Logger;
32 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;
36
37 @Component
38 public class AuditDeleteStackService extends AbstractAuditService {
39
40     private static final Logger logger = LoggerFactory.getLogger(AuditDeleteStackService.class);
41
42     @Autowired
43     public HeatStackAudit heatStackAudit;
44
45     @Autowired
46     public Environment environment;
47
48     protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
49         AuditInventory auditInventory = externalTask.getVariable("auditInventory");
50         Map<String, Object> variables = new HashMap<>();
51         setupMDC(externalTask);
52         boolean success = false;
53         try {
54             logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,
55                     externalTask.getRetries());
56             Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
57                     auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
58             if (auditListOpt.isPresent()) {
59                 auditListOpt.get().setAuditType("delete");
60                 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
61                 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
62                 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
63                 success = !didDeleteAuditFail(auditListOpt);
64             }
65         } catch (Exception e) {
66             logger.error("Error during audit of stack", e);
67         }
68         variables.put("auditIsSuccessful", success);
69         if (success) {
70             externalTaskService.complete(externalTask, variables);
71             logger.debug("The External Task Id: {}  Successful", externalTask.getId());
72         } else {
73             if (externalTask.getRetries() == null) {
74                 logger.debug("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
75                         externalTask.getId(), getRetrySequence().length);
76                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
77                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000);
78             } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) {
79                 logger.debug("The External Task Id: {}  Failed, All Retries Exhausted", externalTask.getId());
80                 externalTaskService.complete(externalTask, variables);
81             } else {
82                 logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
83                         externalTask.getId(), externalTask.getRetries() - 1,
84                         calculateRetryDelay(externalTask.getRetries()));
85                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
86                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, externalTask.getRetries() - 1,
87                         calculateRetryDelay(externalTask.getRetries()));
88             }
89             logger.debug("The External Task Id: {} Failed", externalTask.getId());
90         }
91     }
92
93 }