d03c86ac3008d6b561ea195887abcbb40ede3544
[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.objects.audit.AAIObjectAuditList;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class AuditCreateStackService extends AbstractAuditService {
41
42     private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class);
43
44     @Autowired
45     public HeatStackAudit heatStackAudit;
46
47     protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
48         setupMDC(externalTask);
49         AuditInventory auditInventory = externalTask.getVariable("auditInventory");
50         Map<String, Object> variables = new HashMap<>();
51         boolean success = false;
52         try {
53             logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,
54                     externalTask.getRetries());
55             Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
56                     auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
57             if (auditListOpt.isPresent()) {
58                 auditListOpt.get().setAuditType("create");
59                 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
60                 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
61                 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
62                 success = !didCreateAuditFail(auditListOpt);
63             }
64         } catch (Exception e) {
65             logger.error("Error during audit of stack", e);
66         }
67         variables.put("auditIsSuccessful", success);
68         if (success) {
69             externalTaskService.complete(externalTask, variables);
70             logger.debug("The External Task Id: {}  Successful", externalTask.getId());
71             logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
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                 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
82             } else {
83                 logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
84                         externalTask.getId(), externalTask.getRetries() - 1,
85                         calculateRetryDelay(externalTask.getRetries()));
86                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
87                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, externalTask.getRetries() - 1,
88                         calculateRetryDelay(externalTask.getRetries()));
89             }
90             logger.debug("The External Task Id: {} Failed", externalTask.getId());
91         }
92     }
93
94
95
96 }