e02258c619a5cfa323222ec4209278a8dca82147
[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.create;
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.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
29 import org.onap.so.externaltasks.logging.AuditMDCSetup;
30 import org.onap.so.objects.audit.AAIObjectAuditList;
31 import org.onap.so.utils.ExternalTaskUtils;
32 import org.onap.so.utils.RetrySequenceLevel;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class CreateInventoryTask 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(CreateInventoryTask.class);
44
45     private static final String AAI_INVENTORY_FAILURE = "AAIInventoryFailure";
46
47     @Autowired
48     CreateAAIInventory createInventory;
49
50     @Autowired
51     private AuditMDCSetup mdcSetup;
52
53     public CreateInventoryTask() {
54         super(RetrySequenceLevel.SHORT);
55     }
56
57     protected void executeExternalTask(ExternalTask externalTask, ExternalTaskService externalTaskService) {
58         mdcSetup.setupMDC(externalTask);
59         boolean success = true;
60         boolean inventoryException = false;
61         String auditInventoryString = externalTask.getVariable("auditInventoryResult");
62         AAIObjectAuditList auditInventory = null;
63         String externalTaskId = externalTask.getId();
64         try {
65             GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
66             auditInventory = objectMapper.getMapper().readValue(auditInventoryString, AAIObjectAuditList.class);
67         } catch (Exception e) {
68             mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
69             logger.error("Error Parsing Audit Results", e);
70         }
71         mdcSetup.setElapsedTime();
72         if (auditInventory != null) {
73             Integer retryCount = externalTask.getRetries();
74             try {
75                 logger.info("Executing External Task Create Inventory, Retry Number: {} \n {}", auditInventory,
76                         retryCount);
77                 createInventory.createInventory(auditInventory);
78             } catch (InventoryException e) {
79                 logger.error("Error during inventory of stack", e);
80                 success = false;
81                 inventoryException = true;
82             } catch (Exception e) {
83                 logger.error("Error during inventory of stack", e);
84                 success = false;
85             }
86             mdcSetup.setElapsedTime();
87             if (success) {
88                 externalTaskService.complete(externalTask);
89                 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.COMPLETE.toString());
90                 logger.debug("The External Task Id: {}  Successful", externalTaskId);
91                 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
92                 mdcSetup.clearClientMDCs();
93             } else if (inventoryException) {
94                 mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
95                 logger.debug("The External Task Id: {}  Failed, Retry not needed", externalTaskId);
96                 externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE);
97             } else {
98                 if (retryCount == null) {
99                     logger.debug("The External Task Id: {}  Failed, Setting Retries to Default Start Value: {}",
100                             externalTaskId, getRetrySequence().length);
101                     externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
102                             UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, getRetrySequence().length, 10000);
103                 } else if (retryCount != null && retryCount - 1 == 0) {
104                     externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE);
105                     mdcSetup.setResponseCode(ONAPLogConstants.ResponseStatus.ERROR.toString());
106                     logger.debug("The External Task Id: {}  Failed, All Retries Exhausted", externalTaskId);
107                     logger.info(ONAPLogConstants.Markers.EXIT, "Exiting");
108                 } else {
109                     logger.debug("The External Task Id: {}  Failed, Decrementing Retries: {} , Retry Delay: ",
110                             externalTaskId, retryCount - 1, calculateRetryDelay(retryCount));
111                     externalTaskService.handleFailure(externalTask, UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI,
112                             UNABLE_TO_WRITE_ALL_INVENTORY_TO_A_AI, retryCount - 1, calculateRetryDelay(retryCount));
113                 }
114                 logger.debug("The External Task Id: {} Failed", externalTaskId);
115             }
116         } else {
117             logger.debug("The External Task Id: {}  Failed, No Audit Results Written", externalTaskId);
118             externalTaskService.handleBpmnError(externalTask, AAI_INVENTORY_FAILURE);
119         }
120     }
121 }