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