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