3bf5139ac17717bf7b6f7bbd0998327979c99a13
[so.git] /
1 package org.onap.so.bpmn.infrastructure.manualhandling.tasks;
2
3 import java.util.Map;
4 import java.util.HashMap;
5 import org.onap.so.logger.LoggingAnchor;
6 import org.camunda.bpm.engine.TaskService;
7 import org.camunda.bpm.engine.delegate.BpmnError;
8 import org.camunda.bpm.engine.delegate.DelegateTask;
9 import org.camunda.bpm.engine.delegate.DelegateExecution;
10 import org.onap.so.client.exception.ExceptionBuilder;
11 import org.onap.so.client.ticket.ExternalTicket;
12 import org.onap.so.db.request.beans.InfraActiveRequests;
13 import org.onap.so.db.request.client.RequestsDbClient;
14 import org.onap.so.logger.ErrorCode;
15 import org.onap.so.logger.MessageEnum;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Component;
20
21 @Component
22 public class ManualHandlingTasks {
23     private static final Logger logger = LoggerFactory.getLogger(ManualHandlingTasks.class);
24
25     private static final String TASK_TYPE_PAUSE = "pause";
26     private static final String TASK_TYPE_FALLOUT = "fallout";
27     public static final String VNF_TYPE = "vnfType";
28     public static final String SERVICE_TYPE = "serviceType";
29     public static final String MSO_REQUEST_ID = "msoRequestId";
30     public static final String REQUESTOR_ID = "requestorId";
31     public static final String ERROR_CODE = "errorCode";
32     public static final String VALID_RESPONSES = "validResponses";
33     public static final String DESCRIPTION = "description";
34     public static final String BPMN_EXCEPTION = "BPMN exception: ";
35
36     @Autowired
37     private ExceptionBuilder exceptionUtil;
38
39     @Autowired
40     private RequestsDbClient requestDbclient;
41
42     public void setFalloutTaskVariables(DelegateTask task) {
43
44         DelegateExecution execution = task.getExecution();
45         try {
46             String taskId = task.getId();
47             logger.debug("taskId is: " + taskId);
48             String type = TASK_TYPE_FALLOUT;
49             String nfRole = (String) execution.getVariable(VNF_TYPE);
50             String subscriptionServiceType = (String) execution.getVariable(SERVICE_TYPE);
51             String originalRequestId = (String) execution.getVariable(MSO_REQUEST_ID);
52             String originalRequestorId = (String) execution.getVariable(REQUESTOR_ID);
53             String description = "";
54             String timeout = "";
55             String errorSource = (String) execution.getVariable("failedActivity");
56             String errorCode = (String) execution.getVariable(ERROR_CODE);
57             String errorMessage = (String) execution.getVariable("errorText");
58             String buildingBlockName = (String) execution.getVariable("currentActivity");
59             String buildingBlockStep = (String) execution.getVariable("workStep");
60             String validResponses = (String) execution.getVariable(VALID_RESPONSES);
61
62             Map<String, String> taskVariables = new HashMap<>();
63             taskVariables.put("type", type);
64             taskVariables.put("nfRole", nfRole);
65             taskVariables.put("subscriptionServiceType", subscriptionServiceType);
66             taskVariables.put("originalRequestId", originalRequestId);
67             taskVariables.put("originalRequestorId", originalRequestorId);
68             taskVariables.put("errorSource", errorSource);
69             taskVariables.put(ERROR_CODE, errorCode);
70             taskVariables.put("errorMessage", errorMessage);
71             taskVariables.put("buildingBlockName", buildingBlockName);
72             taskVariables.put("buildingBlockStep", buildingBlockStep);
73             taskVariables.put(VALID_RESPONSES, validResponses);
74             taskVariables.put("tmeout", timeout);
75             taskVariables.put(DESCRIPTION, description);
76             TaskService taskService = execution.getProcessEngineServices().getTaskService();
77
78             taskService.setVariables(taskId, taskVariables);
79             logger.debug("successfully created fallout task: " + taskId);
80         } catch (BpmnError e) {
81             logger.debug(BPMN_EXCEPTION + e.getMessage());
82             throw e;
83         } catch (Exception ex) {
84             String msg = "Exception in setFalloutTaskVariables " + ex.getMessage();
85             logger.debug(msg);
86             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
87         }
88     }
89
90     public void setPauseTaskVariables(DelegateTask task) {
91
92         DelegateExecution execution = task.getExecution();
93
94         try {
95             String taskId = task.getId();
96             logger.debug("taskId is: " + taskId);
97             String type = TASK_TYPE_PAUSE;
98             String nfRole = (String) execution.getVariable(VNF_TYPE);
99             String subscriptionServiceType = (String) execution.getVariable(SERVICE_TYPE);
100             String originalRequestId = (String) execution.getVariable(MSO_REQUEST_ID);
101             String originalRequestorId = (String) execution.getVariable(REQUESTOR_ID);
102             String description = (String) execution.getVariable(DESCRIPTION);
103             String timeout = (String) execution.getVariable("taskTimeout");
104             String errorSource = "";
105             String errorCode = "";
106             String errorMessage = "";
107             String buildingBlockName = "";
108             String buildingBlockStep = "";
109             String validResponses = (String) execution.getVariable(VALID_RESPONSES);
110
111             Map<String, String> taskVariables = new HashMap<>();
112             taskVariables.put("type", type);
113             taskVariables.put("nfRole", nfRole);
114             taskVariables.put(DESCRIPTION, description);
115             taskVariables.put("timeout", timeout);
116             taskVariables.put("subscriptionServiceType", subscriptionServiceType);
117             taskVariables.put("originalRequestId", originalRequestId);
118             taskVariables.put("originalRequestorId", originalRequestorId);
119             taskVariables.put("errorSource", errorSource);
120             taskVariables.put(ERROR_CODE, errorCode);
121             taskVariables.put("errorMessage", errorMessage);
122             taskVariables.put("buildingBlockName", buildingBlockName);
123             taskVariables.put("buildingBlockStep", buildingBlockStep);
124             taskVariables.put(VALID_RESPONSES, validResponses);
125             TaskService taskService = execution.getProcessEngineServices().getTaskService();
126
127             taskService.setVariables(taskId, taskVariables);
128             logger.debug("successfully created pause task: " + taskId);
129         } catch (BpmnError e) {
130             logger.debug(BPMN_EXCEPTION + e.getMessage());
131             throw e;
132         } catch (Exception ex) {
133             String msg = "Exception in setPauseTaskVariables " + ex.getMessage();
134             logger.debug(msg);
135             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
136         }
137
138     }
139
140     public void completeTask(DelegateTask task) {
141
142         DelegateExecution execution = task.getExecution();
143
144         try {
145
146             String taskId = task.getId();
147             logger.debug("taskId is: " + taskId);
148             TaskService taskService = execution.getProcessEngineServices().getTaskService();
149
150             Map<String, Object> taskVariables = taskService.getVariables(taskId);
151             String responseValue = (String) taskVariables.get("responseValue");
152
153             logger.debug("Received responseValue on completion: " + responseValue);
154             // Have to set the first letter of the response to upper case
155             String responseValueUppercaseStart =
156                     responseValue.substring(0, 1).toUpperCase() + responseValue.substring(1);
157             logger.debug("ResponseValue to taskListener: " + responseValueUppercaseStart);
158             execution.setVariable("responseValueTask", responseValueUppercaseStart);
159
160         } catch (BpmnError e) {
161             logger.debug(BPMN_EXCEPTION + e.getMessage());
162             throw e;
163         } catch (Exception ex) {
164             String msg = "Exception in completeManualTask " + ex.getMessage();
165             logger.debug(msg);
166             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
167         }
168
169     }
170
171     public void createExternalTicket(DelegateExecution execution) {
172
173         try {
174             ExternalTicket ticket = new ExternalTicket();
175
176             ticket.setRequestId((String) execution.getVariable(MSO_REQUEST_ID));
177             ticket.setCurrentActivity((String) execution.getVariable("currentActivity"));
178             ticket.setNfRole((String) execution.getVariable(VNF_TYPE));
179             ticket.setDescription((String) execution.getVariable(DESCRIPTION));
180             ticket.setSubscriptionServiceType((String) execution.getVariable(SERVICE_TYPE));
181             ticket.setRequestorId((String) execution.getVariable(REQUESTOR_ID));
182             ticket.setTimeout((String) execution.getVariable("taskTimeout"));
183             ticket.setErrorSource((String) execution.getVariable("failedActivity"));
184             ticket.setErrorCode((String) execution.getVariable(ERROR_CODE));
185             ticket.setErrorMessage((String) execution.getVariable("errorText"));
186             ticket.setWorkStep((String) execution.getVariable("workStep"));
187
188             ticket.createTicket();
189         } catch (BpmnError e) {
190             String msg = "BPMN error in createAOTSTicket " + e.getMessage();
191             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN",
192                     ErrorCode.UnknownError.getValue());
193         } catch (Exception ex) {
194             String msg = "Exception in createExternalTicket " + ex.getMessage();
195             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN",
196                     ErrorCode.UnknownError.getValue());
197         }
198
199     }
200
201     public void updateRequestDbStatus(DelegateExecution execution, String status) {
202         try {
203             String requestId = (String) execution.getVariable(MSO_REQUEST_ID);
204             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
205
206             request.setRequestStatus(status);
207             request.setLastModifiedBy("ManualHandling");
208
209             requestDbclient.updateInfraActiveRequests(request);
210         } catch (Exception e) {
211             logger.error("Unable to save the updated request status to the DB", e);
212         }
213     }
214
215 }