c5feee20898fe2da82e7ea489eeab362008ee55b
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  *
7  * Copyright (C) 2019 IBM
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.inventory.delete;
24
25 import org.camunda.bpm.client.task.ExternalTask;
26 import org.camunda.bpm.client.task.ExternalTaskService;
27 import org.onap.logging.ref.slf4j.ONAPLogConstants;
28 import org.onap.so.cloud.resource.beans.CloudInformation;
29 import org.onap.so.logging.tasks.AuditMDCSetup;
30 import org.onap.so.utils.ExternalTaskUtils;
31 import org.onap.so.utils.RetrySequenceLevel;
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
39 @Component
40 public class DeleteInventoryTask extends ExternalTaskUtils {
41
42     private static final String UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI = "Unable to write all inventory to A&AI";
43
44     private static final Logger logger = LoggerFactory.getLogger(DeleteInventoryTask.class);
45
46     private static final String AAI_INVENTORY_FAILURE = "AAIInventoryFailure";
47
48     @Autowired
49     private DeleteAAIInventory deleteInventory;
50
51     @Autowired
52     private Environment env;
53
54     @Autowired
55     private AuditMDCSetup mdcSetup;
56
57     public DeleteInventoryTask() {
58         super(RetrySequenceLevel.SHORT);
59     }
60
61     protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
62         mdcSetup.setupMDC(externalTask);
63         boolean inventoryException = false;
64         String externalTaskId = externalTask.getId();
65         CloudInformation cloudInformation = externalTask.getVariable("cloudInformation");
66         boolean success = true;
67         if (cloudInformation != null) {
68             Integer retryCount = externalTask.getRetries();
69             try {
70                 deleteInventory.heatbridge(cloudInformation, env.getProperty("heatBridgeDryrun", Boolean.class, true));
71             } catch (Exception e) {
72                 logger.error("Error during inventory of stack", e);
73                 success = false;
74             }
75             mdcSetup.setElapsedTime();
76             if (success) {
77                 externalTaskService.complete(externalTask);
78                 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
79                 logger.debug("The External Task Id: {}  Successful", externalTaskId);
80                 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
81                 mdcSetup.clearClientMDCs();
82             } else {
83                 if (retryCount == null) {
84                     logger.error("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
85                             externalTaskId, getRetrySequence().length);
86                     externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
87                             UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000);
88                 } else if (retryCount != null && retryCount - 1 == 0) {
89                     externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE);
90                     mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
91                     logger.error("The External Task Id: {}  Failed, All Retries Exhausted", externalTaskId);
92                     logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
93                 } else {
94                     logger.error("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
95                             externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
96                     externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
97                             UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, retryCount - 1, calculateRetryDelay(retryCount));
98                 }
99                 logger.error("The External Task Id: {} Failed", externalTaskId);
100             }
101         } else {
102             logger.error("The External Task Id: {}  Failed, No Cloud Information Provided", externalTaskId);
103             externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE);
104         }
105     }
106 }