2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.adapters.audit;
25 import java.util.HashMap;
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.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
41 public class AuditCreateStackService extends AbstractAuditService {
43 private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class);
46 public HeatStackAudit heatStackAudit;
49 private AuditMDCSetup mdcSetup;
51 protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
52 mdcSetup.setupMDC(externalTask);
53 AuditInventory auditInventory = externalTask.getVariable("auditInventory");
54 Map<String, Object> variables = new HashMap<>();
55 boolean success = false;
57 Integer retryCount = externalTask.getRetries();
58 logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory, retryCount);
59 Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
60 auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
61 if (auditListOpt.isPresent()) {
62 auditListOpt.get().setAuditType("create");
63 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
64 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
65 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
66 success = !didCreateAuditFail(auditListOpt);
68 } catch (Exception e) {
69 logger.error("Error during audit of stack", e);
71 variables.put("auditIsSuccessful", success);
72 mdcSetup.setElapsedTime();
73 String externalTaskId = externalTask.getId();
75 externalTaskService.complete(externalTask, variables);
76 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
77 logger.debug("The External Task Id: {} Successful", externalTaskId);
78 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
79 mdcSetup.clearClientMDCs();
81 Integer retryCount = externalTask.getRetries();
82 if (retryCount == null) {
83 logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}",
84 externalTaskId, getRetrySequence().length);
85 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
86 UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000);
87 } else if (retryCount == 1) {
88 externalTaskService.complete(externalTask, variables);
89 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
90 logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId);
91 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
92 mdcSetup.clearClientMDCs();
94 logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ",
95 externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
96 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
97 UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1,
98 calculateRetryDelay(retryCount));
100 logger.debug("The External Task Id: {} Failed", externalTaskId);