Code Enhancement (Sonar Fixes)
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / audit / AuditCreateStackService.java
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.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.core.env.Environment;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class AuditCreateStackService extends AbstractAuditService {
40
41     private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class);
42
43     @Autowired
44     public HeatStackAudit heatStackAudit;
45
46     @Autowired
47     public Environment environment;
48
49     protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
50         AuditInventory auditInventory = externalTask.getVariable("auditInventory");
51         Map<String, Object> variables = new HashMap<>();
52         setupMDC(externalTask);
53         boolean success = false;
54         try {
55             logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory,
56                     externalTask.getRetries());
57             Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
58                     auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
59             if (auditListOpt.isPresent()) {
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         } else {
72             if (externalTask.getRetries() == null) {
73                 logger.debug("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
74                         externalTask.getId(), getRetrySequence().length);
75                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
76                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000);
77             } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) {
78                 logger.debug("The External Task Id: {}  Failed, All Retries Exhausted", externalTask.getId());
79                 externalTaskService.complete(externalTask, variables);
80             } else {
81                 logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
82                         externalTask.getId(), externalTask.getRetries() - 1,
83                         calculateRetryDelay(externalTask.getRetries()));
84                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
85                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, externalTask.getRetries() - 1,
86                         calculateRetryDelay(externalTask.getRetries()));
87             }
88             logger.debug("The External Task Id: {} Failed", externalTask.getId());
89         }
90     }
91
92
93
94 }