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.tasks.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.logging.tasks.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     public AuditMDCSetup mdcSetup;
 
  51     public AuditCreateStackService() {
 
  55     public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
 
  56         mdcSetup.setupMDC(externalTask);
 
  57         AuditInventory auditInventory = externalTask.getVariable("auditInventory");
 
  58         Map<String, Object> variables = new HashMap<>();
 
  59         boolean success = false;
 
  61             Integer retryCount = externalTask.getRetries();
 
  62             logger.info("Executing External Task Audit Inventory, Retry Number: {} \n {}", auditInventory, retryCount);
 
  63             Optional<AAIObjectAuditList> auditListOpt = heatStackAudit.auditHeatStack(auditInventory.getCloudRegion(),
 
  64                     auditInventory.getCloudOwner(), auditInventory.getTenantId(), auditInventory.getHeatStackName());
 
  65             if (auditListOpt.isPresent()) {
 
  66                 auditListOpt.get().setAuditType("create");
 
  67                 auditListOpt.get().setHeatStackName(auditInventory.getHeatStackName());
 
  68                 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
 
  69                 variables.put("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditListOpt.get()));
 
  70                 success = !didCreateAuditFail(auditListOpt);
 
  72         } catch (Exception e) {
 
  73             logger.error("Error during audit of stack", e);
 
  75         variables.put("auditIsSuccessful", success);
 
  76         mdcSetup.setElapsedTime();
 
  77         String externalTaskId = externalTask.getId();
 
  79             externalTaskService.complete(externalTask, variables);
 
  80             mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
 
  81             logger.debug("The External Task Id: {}  Successful", externalTaskId);
 
  82             logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
 
  83             mdcSetup.clearClientMDCs();
 
  85             Integer retryCount = externalTask.getRetries();
 
  86             if (retryCount == null) {
 
  87                 logger.debug("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
 
  88                         externalTaskId, getRetrySequence().length);
 
  89                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
 
  90                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, getRetrySequence().length, 10000);
 
  91             } else if (retryCount == 1) {
 
  92                 externalTaskService.complete(externalTask, variables);
 
  93                 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
 
  94                 logger.debug("The External Task Id: {}  Failed, All Retries Exhausted", externalTaskId);
 
  95                 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
 
  96                 mdcSetup.clearClientMDCs();
 
  98                 logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
 
  99                         externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
 
 100                 externalTaskService.handleFailure(externalTask, UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI,
 
 101                         UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI, retryCount - 1,
 
 102                         calculateRetryDelay(retryCount));
 
 104             logger.debug("The External Task Id: {} Failed", externalTaskId);