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