59a1bcf797f83c3e9bb51a79685050a0ed66829c
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / ManualHandling.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.common.scripts;\r
21 \r
22 import static org.apache.commons.lang3.StringUtils.*;\r
23 \r
24 import java.time.chrono.AbstractChronology\r
25 import java.util.List\r
26 \r
27 import org.apache.commons.lang3.*\r
28 import org.camunda.bpm.engine.TaskService\r
29 import org.camunda.bpm.engine.task.Task\r
30 import org.camunda.bpm.engine.task.TaskQuery\r
31 import org.camunda.bpm.engine.delegate.BpmnError\r
32 import org.camunda.bpm.engine.delegate.DelegateTask\r
33 import org.camunda.bpm.engine.delegate.DelegateExecution\r
34 import org.camunda.bpm.engine.runtime.Execution\r
35 import org.json.JSONObject;\r
36 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
37 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
38 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition\r
39 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
40 \r
41 \r
42 \r
43 /**\r
44  * This groovy class supports the <class>ManualHandling.bpmn</class> process.\r
45  *\r
46  * @author\r
47  *\r
48  * Inputs:\r
49  * @param - msoRequestId\r
50  * @param - isDebugLogEnabled\r
51  * @param - serviceType\r
52  * @param - vnfType\r
53  * @param - requestorId\r
54  * @param - currentActivity\r
55  * @param - workStep\r
56  * @param - failedActivity\r
57  * @param - errorCode\r
58  * @param - errorText\r
59  * @param - validResponses\r
60  *\r
61  * Outputs:\r
62  * @param - WorkflowException\r
63  * @param - taskId\r
64  *\r
65  */\r
66 public class ManualHandling extends AbstractServiceTaskProcessor {\r
67 \r
68         String Prefix="MH_"\r
69         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
70         \r
71         JsonUtils jsonUtils = new JsonUtils()\r
72 \r
73         public void preProcessRequest (Execution execution) {\r
74                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
75                 String msg = ""\r
76                 utils.log("DEBUG"," ***** preProcessRequest of ManualHandling *****",  isDebugLogEnabled)\r
77 \r
78                 try {\r
79                         execution.setVariable("prefix", Prefix)\r
80                         // check for required input\r
81                         String requestId = execution.getVariable("msoRequestId")\r
82                         utils.log("DEBUG", "msoRequestId is: " + requestId, isDebugLogEnabled)          \r
83                         def serviceType = execution.getVariable("serviceType")\r
84                         utils.log("DEBUG", "serviceType is: " + serviceType, isDebugLogEnabled)\r
85                         def vnfType = execution.getVariable("vnfType")\r
86                         utils.log("DEBUG", "vnftype is: " + vnfType, isDebugLogEnabled)\r
87                         def currentActivity = execution.getVariable("currentActivity")\r
88                         utils.log("DEBUG", "currentActivity is: " + currentActivity, isDebugLogEnabled)\r
89                         def workStep = execution.getVariable("workStep")\r
90                         utils.log("DEBUG", "workStep is: " + workStep, isDebugLogEnabled)\r
91                         def failedActivity = execution.getVariable("failedActivity")\r
92                         utils.log("DEBUG", "failedActivity is: " + failedActivity, isDebugLogEnabled)\r
93                         def errorCode = execution.getVariable("errorCode")\r
94                         utils.log("DEBUG", "errorCode is: " + errorCode, isDebugLogEnabled)\r
95                         def errorText = execution.getVariable("errorText")\r
96                         utils.log("DEBUG", "errorText is: " + errorText, isDebugLogEnabled)\r
97                         def requestorId = execution.getVariable("requestorId")\r
98                         utils.log("DEBUG", "requestorId is: " + requestorId, isDebugLogEnabled)\r
99                         def validResponses = execution.getVariable("validResponses")\r
100                         utils.log("DEBUG", "validResponses is: " + validResponses, isDebugLogEnabled)\r
101                         \r
102                 } catch (BpmnError e) {\r
103                         throw e;\r
104                 } catch (Exception ex){\r
105                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
106                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
107                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
108                 }\r
109                 utils.log("DEBUG"," ***** Exit preProcessRequest of RainyDayHandler *****",  isDebugLogEnabled)\r
110         }\r
111 \r
112         public void createManualTask (Execution execution) {\r
113                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
114                 String msg = ""\r
115                 utils.log("DEBUG"," ***** createManualTask of ManualHandling *****",  isDebugLogEnabled)\r
116 \r
117                 try {\r
118                         String taskId = UUID.randomUUID()\r
119                         execution.setVariable('taskId', taskId)\r
120                         String type = "fallout"\r
121                         String nfRole = execution.getVariable("vnfType")\r
122                         String subscriptionServiceType = execution.getVariable("serviceType")\r
123                         String originalRequestId = execution.getVariable("msoRequestId")\r
124                         String originalRequestorId = execution.getVariable("requestorId")\r
125                         String errorSource = execution.getVariable("failedActivity")\r
126                         String errorCode = execution.getVariable("errorCode")\r
127                         String errorMessage = execution.getVariable("errorText")                        \r
128                         String buildingBlockName = execution.getVariable("currentActivity")\r
129                         String buildingBlockStep = execution.getVariable("workStep")\r
130                         String validResponses = execution.getVariable("validResponses")\r
131                         \r
132                         utils.log("DEBUG", "Before creating task", isDebugLogEnabled)                   \r
133                         \r
134                         Map<String, String> taskVariables = new HashMap<String, String>()\r
135                         taskVariables.put("type", type)\r
136                         taskVariables.put("nfRole", nfRole)\r
137                         taskVariables.put("subscriptionServiceType", subscriptionServiceType)\r
138                         taskVariables.put("originalRequestId", originalRequestId)\r
139                         taskVariables.put("originalRequestorId", originalRequestorId)\r
140                         taskVariables.put("errorSource", errorSource)\r
141                         taskVariables.put("errorCode", errorCode)\r
142                         taskVariables.put("errorMessage", errorMessage)\r
143                         taskVariables.put("buildingBlockName", buildingBlockName)\r
144                         taskVariables.put("buildingBlockStep", buildingBlockStep)\r
145                         taskVariables.put("validResponses", validResponses)                     \r
146                         \r
147                         TaskService taskService = execution.getProcessEngineServices().getTaskService()                 \r
148                         Task manualTask = taskService.newTask(taskId)                   \r
149                         taskService.saveTask(manualTask)                        \r
150                         taskService.setVariables(taskId, taskVariables)\r
151                         utils.log("DEBUG", "successfully created task: "+ taskId, isDebugLogEnabled)\r
152 \r
153                 } catch (BpmnError e) {\r
154                         utils.log("DEBUG", "BPMN exception: " + e.errorMessage, isDebugLogEnabled)\r
155                         throw e;\r
156                 } catch (Exception ex){\r
157                         msg = "Exception in createManualTask " + ex.getMessage()\r
158                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
159                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
160                 }\r
161                 utils.log("DEBUG"," ***** Exit createManualTask of ManualHandling *****",  isDebugLogEnabled)\r
162         }\r
163         \r
164         public void setTaskVariables (DelegateTask task) {\r
165                                 \r
166                 DelegateExecution execution = task.getExecution()\r
167                 \r
168                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
169                 String msg = ""\r
170                 utils.log("DEBUG"," ***** setTaskVariables of ManualHandling *****",  isDebugLogEnabled)\r
171                 String taskId = task.getId()\r
172                 utils.log("DEBUG", "taskId is: " + taskId, isDebugLogEnabled)\r
173 \r
174                 try {                   \r
175                         execution.setVariable('taskId', taskId)\r
176                         String type = "fallout"\r
177                         String nfRole = execution.getVariable("vnfType")\r
178                         String subscriptionServiceType = execution.getVariable("serviceType")\r
179                         String originalRequestId = execution.getVariable("msoRequestId")\r
180                         String originalRequestorId = execution.getVariable("requestorId")\r
181                         String errorSource = execution.getVariable("failedActivity")\r
182                         String errorCode = execution.getVariable("errorCode")\r
183                         String errorMessage = execution.getVariable("errorText")\r
184                         String buildingBlockName = execution.getVariable("currentActivity")\r
185                         String buildingBlockStep = execution.getVariable("workStep")\r
186                         String validResponses = execution.getVariable("validResponses")\r
187                         \r
188                         utils.log("DEBUG", "Before creating task", isDebugLogEnabled)\r
189                         \r
190                         Map<String, String> taskVariables = new HashMap<String, String>()\r
191                         taskVariables.put("type", type)\r
192                         taskVariables.put("nfRole", nfRole)\r
193                         taskVariables.put("subscriptionServiceType", subscriptionServiceType)\r
194                         taskVariables.put("originalRequestId", originalRequestId)\r
195                         taskVariables.put("originalRequestorId", originalRequestorId)\r
196                         taskVariables.put("errorSource", errorSource)\r
197                         taskVariables.put("errorCode", errorCode)\r
198                         taskVariables.put("errorMessage", errorMessage)\r
199                         taskVariables.put("buildingBlockName", buildingBlockName)\r
200                         taskVariables.put("buildingBlockStep", buildingBlockStep)\r
201                         taskVariables.put("validResponses", validResponses)\r
202                         TaskService taskService = execution.getProcessEngineServices().getTaskService()\r
203                         \r
204                                 \r
205                         taskService.setVariables(taskId, taskVariables)\r
206                         utils.log("DEBUG", "successfully created task: "+ taskId, isDebugLogEnabled)\r
207 \r
208                 } catch (BpmnError e) {\r
209                         utils.log("DEBUG", "BPMN exception: " + e.errorMessage, isDebugLogEnabled)\r
210                         throw e;\r
211                 } catch (Exception ex){\r
212                         msg = "Exception in createManualTask " + ex.getMessage()\r
213                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
214                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
215                 }\r
216                 utils.log("DEBUG"," ***** Exit createManualTask of ManualHandling *****",  isDebugLogEnabled)\r
217         }\r
218         \r
219         public void completeTask (DelegateTask task) {\r
220                 \r
221                 DelegateExecution execution = task.getExecution()\r
222 \r
223                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
224                 String msg = ""\r
225                 utils.log("DEBUG"," ***** completeTask of ManualHandling *****",  isDebugLogEnabled)\r
226                 String taskId = task.getId()    \r
227                 utils.log("DEBUG", "taskId is: " + taskId, isDebugLogEnabled)\r
228 \r
229                 try {                   \r
230                         TaskService taskService = execution.getProcessEngineServices().getTaskService() \r
231                 \r
232                         Map<String, String> taskVariables = taskService.getVariables(taskId)\r
233                         String responseValue = taskVariables.get("responseValue")\r
234                                                 \r
235                         utils.log("DEBUG", "Received responseValue on completion: "+ responseValue, isDebugLogEnabled)\r
236                         // Have to set the first letter of the response to upper case                   \r
237                         String responseValueForRainyDay = responseValue.substring(0, 1).toUpperCase() + responseValue.substring(1)\r
238                         utils.log("DEBUG", "ResponseValue to RainyDayHandler: "+ responseValueForRainyDay, isDebugLogEnabled)\r
239                         execution.setVariable("responseValue", responseValueForRainyDay)\r
240 \r
241                 } catch (BpmnError e) {\r
242                         utils.log("DEBUG", "BPMN exception: " + e.errorMessage, isDebugLogEnabled)\r
243                         throw e;\r
244                 } catch (Exception ex){\r
245                         msg = "Exception in createManualTask " + ex.getMessage()\r
246                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
247                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
248                 }\r
249                 utils.log("DEBUG"," ***** Exit completeTask of ManualHandling *****",  isDebugLogEnabled)\r
250         }       \r
251         \r
252         \r
253 }\r