08361bdbbfcd8d3282fbcf300d4477387414c8fb
[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.tasks.inventory;
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.stereotype.Component;
36
37
38 @Component
39 public class DeleteInventoryTask extends ExternalTaskUtils {
40
41     private static final String UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI = "Unable to write all inventory to A&AI";
42
43     private static final Logger logger = LoggerFactory.getLogger(DeleteInventoryTask.class);
44
45     private static final String AAI_INVENTORY_FAILURE = "AAIInventoryFailure";
46
47     @Autowired
48     private DeleteAAIInventory deleteInventory;
49
50     @Autowired
51     private AuditMDCSetup mdcSetup;
52
53     public DeleteInventoryTask() {
54         super(RetrySequenceLevel.SHORT);
55     }
56
57     public void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
58         mdcSetup.setupMDC(externalTask);
59         String externalTaskId = externalTask.getId();
60         CloudInformation cloudInformation = externalTask.getVariable("cloudInformation");
61         boolean success = true;
62         if (cloudInformation != null) {
63             Integer retryCount = externalTask.getRetries();
64             try {
65                 deleteInventory.heatbridge(cloudInformation);
66             } catch (Exception e) {
67                 logger.error("Error during inventory of stack", e);
68                 success = false;
69             }
70             mdcSetup.setElapsedTime();
71             if (success) {
72                 externalTaskService.complete(externalTask);
73                 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
74                 logger.debug("The External Task Id: {}  Successful", externalTaskId);
75                 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
76                 mdcSetup.clearClientMDCs();
77             } else {
78                 if (retryCount == null) {
79                     logger.error("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
80                             externalTaskId, getRetrySequence().length);
81                     externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
82                             UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000);
83                 } else if (retryCount != null && retryCount - 1 == 0) {
84                     externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE);
85                     mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
86                     logger.error("The External Task Id: {}  Failed, All Retries Exhausted", externalTaskId);
87                     logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
88                 } else {
89                     logger.error("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
90                             externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
91                     externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
92                             UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, retryCount - 1, calculateRetryDelay(retryCount));
93                 }
94                 logger.error("The External Task Id: {} Failed", externalTaskId);
95             }
96         } else {
97             logger.error("The External Task Id: {}  Failed, No Cloud Information Provided", externalTaskId);
98             externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE);
99         }
100     }
101 }