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.onap.so.utils.RetrySequenceLevel;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
42 public class AuditCreateStackService extends AbstractAuditService {
44 private static final Logger logger = LoggerFactory.getLogger(AuditCreateStackService.class);
47 public HeatStackAudit heatStackAudit;
50 private AuditMDCSetup mdcSetup;
52 public AuditCreateStackService() {
56 protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
57 mdcSetup.setupMDC(externalTask);
58 AuditInventory auditInventory = externalTask.getVariable("auditInventory");
59 Map<String, Object> variables = new HashMap<>();
60 boolean success = false;
62 Integer retryCount = externalTask.getRetries();
63 logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory, retryCount);
64 Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
65 auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
66 if (auditListOpt.isPresent()) {
67 auditListOpt.get().setAuditType("create");
68 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
69 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
70 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
71 success = !didCreateAuditFail(auditListOpt);
73 } catch (Exception e) {
74 logger.error("Error during audit of stack", e);
76 variables.put("auditIsSuccessful", success);
77 mdcSetup.setElapsedTime();
78 String externalTaskId = externalTask.getId();
80 externalTaskService.complete(externalTask, variables);
81 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
82 logger.debug("The External Task Id: {} Successful", externalTaskId);
83 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
84 mdcSetup.clearClientMDCs();
86 Integer retryCount = externalTask.getRetries();
87 if (retryCount == null) {
88 logger.debug("The External Task Id: {} Failed, Setting Retries to Default Start Value: {}",
89 externalTaskId, getRetrySequence().length);
90 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
91 UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000);
92 } else if (retryCount == 1) {
93 externalTaskService.complete(externalTask, variables);
94 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
95 logger.debug("The External Task Id: {} Failed, All Retries Exhausted", externalTaskId);
96 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
97 mdcSetup.clearClientMDCs();
99 logger.debug("The External Task Id: {} Failed, Decrementing Retries: {} , Retry Delay: ",
100 externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
101 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
102 UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1,
103 calculateRetryDelay(retryCount));
105 logger.debug("The External Task Id: {} Failed", externalTaskId);