77229919cd2f4321541534ceea34d82d17fc5d1e
[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.so.audit.beans.AuditInventory;
31 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
32 import org.onap.so.objects.audit.AAIObjectAuditList;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.core.env.Environment;
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     @Autowired
48     public Environment environment;
49
50     protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
51         AuditInventory auditInventory = externalTask.getVariable("auditInventory");
52         Map<String, Object> variables = new HashMap<>();
53         setupMDC(externalTask);
54         boolean success = false;
55         try {
56             logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,
57                     externalTask.getRetries());
58             Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
59                     auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
60             if (auditListOpt.isPresent()) {
61                 auditListOpt.get().setAuditType("create");
62                 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
63                 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
64                 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
65                 success = !didCreateAuditFail(auditListOpt);
66             }
67         } catch (Exception e) {
68             logger.error("Error during audit of stack", e);
69         }
70         variables.put("auditIsSuccessful", success);
71         if (success) {
72             externalTaskService.complete(externalTask, variables);
73             logger.debug("The External Task Id: {}  Successful", externalTask.getId());
74         } else {
75             if (externalTask.getRetries() == null) {
76                 logger.debug("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
77                         externalTask.getId(), getRetrySequence().length);
78                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
79                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000);
80             } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) {
81                 logger.debug("The External Task Id: {}  Failed, All Retries Exhausted", externalTask.getId());
82                 externalTaskService.complete(externalTask, variables);
83             } else {
84                 logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
85                         externalTask.getId(), externalTask.getRetries() - 1,
86                         calculateRetryDelay(externalTask.getRetries()));
87                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
88                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, externalTask.getRetries() - 1,
89                         calculateRetryDelay(externalTask.getRetries()));
90             }
91             logger.debug("The External Task Id: {} Failed", externalTask.getId());
92         }
93     }
94
95
96
97 }