54ffc597f4918848f61e617ccaf790218d242ca9
[so.git] /
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 java.util.HashMap;
26 import java.util.Map;
27 import java.util.Optional;
28 import org.camunda.bpm.client.task.ExternalTask;
29 import org.camunda.bpm.client.task.ExternalTaskService;
30 import org.onap.logging.ref.slf4j.ONAPLogConstants;
31 import org.onap.so.audit.beans.AuditInventory;
32 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
33 import org.onap.so.externaltasks.logging.AuditMDCSetup;
34 import org.onap.so.objects.audit.AAIObjectAuditList;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class AuditCreateStackService extends AbstractAuditService {
42
43     private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class);
44
45     @Autowired
46     public HeatStackAudit heatStackAudit;
47
48     @Autowired
49     private AuditMDCSetup mdcSetup;
50
51     protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
52         mdcSetup.setupMDC(externalTask);
53         AuditInventory auditInventory = externalTask.getVariable("auditInventory");
54         Map<String, Object> variables = new HashMap<>();
55         boolean success = false;
56         try {
57             Integer retryCount = externalTask.getRetries();
58             logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory, retryCount);
59             Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
60                     auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
61             if (auditListOpt.isPresent()) {
62                 auditListOpt.get().setAuditType("create");
63                 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
64                 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
65                 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
66                 success = !didCreateAuditFail(auditListOpt);
67             }
68         } catch (Exception e) {
69             logger.error("Error during audit of stack", e);
70         }
71         variables.put("auditIsSuccessful", success);
72         mdcSetup.setElapsedTime();
73         String externalTaskId = externalTask.getId();
74         if (success) {
75             externalTaskService.complete(externalTask, variables);
76             mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
77             logger.debug("The External Task Id: {}  Successful", externalTaskId);
78             logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
79             mdcSetup.clearClientMDCs();
80         } else {
81             Integer retryCount = externalTask.getRetries();
82             if (retryCount == null) {
83                 logger.debug("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
84                         externalTaskId, getRetrySequence().length);
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, getRetrySequence().length, 10000);
87             } else if (retryCount != null && retryCount - 1 == 0) {
88                 externalTaskService.complete(externalTask, variables);
89                 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
90                 logger.debug("The External Task Id: {}  Failed, All Retries Exhausted", externalTaskId);
91                 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
92                 mdcSetup.clearClientMDCs();
93             } else {
94                 logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
95                         externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
96                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
97                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1,
98                         calculateRetryDelay(retryCount));
99             }
100             logger.debug("The External Task Id: {} Failed", externalTaskId);
101         }
102     }
103
104
105
106 }