2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.tasks.audit;
23 import java.util.HashMap;
25 import java.util.Optional;
26 import org.camunda.bpm.client.task.ExternalTask;
27 import org.camunda.bpm.client.task.ExternalTaskService;
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.onap.so.audit.beans.AuditInventory;
30 import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
31 import org.onap.so.logging.tasks.AuditMDCSetup;
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;
40 public class AuditDeleteStackService extends AbstractAuditService {
42 private static final Logger logger = LoggerFactory.getLogger(AuditDeleteStackService.class);
45 protected HeatStackAudit heatStackAudit;
48 protected AuditVServer auditVservers;
51 protected AuditDataService auditDataService;
54 protected Environment env;
57 public AuditMDCSetup mdcSetup;
59 public AuditDeleteStackService() {
63 public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
65 mdcSetup.setupMDC(externalTask);
66 AuditInventory auditInventory = externalTask.getVariable("auditInventory");
67 Map<String, Object> variables = new HashMap<>();
68 boolean success = false;
70 Integer retryCount = externalTask.getRetries();
71 logger.info("Executing External Task Delete Audit Inventory. Retry Number: {}", retryCount);
72 Optional<AAIObjectAuditList> auditListOpt = auditDataService.getStackDataFromRequestDb(auditInventory);
73 if (auditListOpt.isPresent()) {
74 auditVservers.auditVservers(auditListOpt.get());
76 logger.debug("Auditing Vservers based on vf module relationships");
77 auditListOpt = auditVservers.auditVserversThroughRelationships(auditInventory.getGenericVnfId(),
78 auditInventory.getHeatStackName());
80 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
81 auditListOpt.get().setAuditType("delete");
82 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
83 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
84 success = !didDeleteAuditFail(auditListOpt.get());
86 } catch (Exception e) {
87 logger.error("Error during audit of stack", e);
89 variables.put("auditIsSuccessful", success);
90 mdcSetup.setElapsedTime();
91 String externalTaskId = externalTask.getId();
93 externalTaskService.complete(externalTask, variables);
94 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
95 logger.debug("The External Task Id: {} Successful", externalTaskId);
96 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
97 mdcSetup.clearClientMDCs();
99 Integer retryCount = externalTask.getRetries();
100 if (retryCount == null) {
101 logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}",
102 externalTaskId, getRetrySequence().length);
103 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
104 UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000);
105 } else if (retryCount == 1) {
106 externalTaskService.complete(externalTask, variables);
107 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
108 logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId);
109 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
110 mdcSetup.clearClientMDCs();
112 logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ",
113 externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
114 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
115 UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1,
116 calculateRetryDelay(retryCount));
118 logger.debug("The External Task Id: {} Failed", externalTaskId);