Merge "Update Tasks API with additional fields"
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / ManualHandling.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
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.bpmn.common.scripts
24
25 import org.onap.so.logger.ErrorCode;
26
27 import static org.apache.commons.lang3.StringUtils.*;
28
29 import java.time.chrono.AbstractChronology
30 import java.util.List
31 import java.text.SimpleDateFormat
32 import java.util.Date
33
34 import org.apache.commons.lang3.*
35 import org.camunda.bpm.engine.TaskService
36 import org.camunda.bpm.engine.task.Task
37 import org.camunda.bpm.engine.task.TaskQuery
38 import org.camunda.bpm.engine.delegate.BpmnError
39 import org.camunda.bpm.engine.delegate.DelegateTask
40 import org.camunda.bpm.engine.delegate.DelegateExecution
41 import org.camunda.bpm.engine.delegate.DelegateExecution
42 import org.json.JSONObject;
43 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
44 import org.onap.so.bpmn.common.scripts.ExceptionUtil
45 import org.onap.so.bpmn.core.domain.ServiceDecomposition
46 import org.onap.so.bpmn.core.json.JsonUtils
47 import org.onap.so.client.ruby.*
48 import org.onap.so.logger.MessageEnum
49 import org.slf4j.Logger
50 import org.slf4j.LoggerFactory
51
52
53
54
55
56 /**
57  * This groovy class supports the <class>ManualHandling.bpmn</class> process.
58  *
59  * @author
60  *
61  * Inputs:
62  * @param - msoRequestId
63  * @param - isDebugLogEnabled
64  * @param - serviceType
65  * @param - vnfType
66  * @param - requestorId
67  * @param - currentActivity
68  * @param - workStep
69  * @param - failedActivity
70  * @param - errorCode
71  * @param - errorText
72  * @param - validResponses
73  * @param - vnfName
74  *
75  * Outputs:
76  * @param - WorkflowException
77  * @param - taskId
78  *
79  */
80 public class ManualHandling extends AbstractServiceTaskProcessor {
81     private static final Logger logger = LoggerFactory.getLogger( ManualHandling.class);
82
83
84         String Prefix="MH_"
85         ExceptionUtil exceptionUtil = new ExceptionUtil()
86
87         JsonUtils jsonUtils = new JsonUtils()
88
89         public void preProcessRequest (DelegateExecution execution) {
90                 String msg = ""
91                 logger.trace("preProcessRequest of ManualHandling ")
92
93                 try {
94                         execution.setVariable("prefix", Prefix)
95                         setBasicDBAuthHeader(execution, execution.getVariable('isDebugLogEnabled'))
96                         // check for required input
97                         String requestId = execution.getVariable("msoRequestId")
98                         logger.debug("msoRequestId is: " + requestId)
99                         def serviceType = execution.getVariable("serviceType")
100                         logger.debug("serviceType is: " + serviceType)
101                         def vnfType = execution.getVariable("vnfType")
102                         logger.debug("vnftype is: " + vnfType)
103                         def currentActivity = execution.getVariable("currentActivity")
104                         logger.debug("currentActivity is: " + currentActivity)
105                         def workStep = execution.getVariable("workStep")
106                         logger.debug("workStep is: " + workStep)
107                         def failedActivity = execution.getVariable("failedActivity")
108                         logger.debug("failedActivity is: " + failedActivity)
109                         def errorCode = execution.getVariable("errorCode")
110                         logger.debug("errorCode is: " + errorCode)
111                         def errorText = execution.getVariable("errorText")
112                         logger.debug("errorText is: " + errorText)
113                         def requestorId = execution.getVariable("requestorId")
114                         logger.debug("requestorId is: " + requestorId)
115                         def validResponses = execution.getVariable("validResponses")
116                         logger.debug("validResponses is: " + validResponses)
117
118                 } catch (BpmnError e) {
119                         throw e;
120                 } catch (Exception ex){
121                         msg = "Exception in preProcessRequest " + ex.getMessage()
122                         logger.debug(msg)
123                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
124                 }
125                 logger.trace("Exit preProcessRequest of RainyDayHandler ")
126         }
127
128         public void createManualTask (DelegateExecution execution) {
129                 String msg = ""
130                 logger.trace("createManualTask of ManualHandling ")
131
132                 try {
133                         String taskId = UUID.randomUUID()
134                         execution.setVariable('taskId', taskId)
135                         String type = "fallout"
136                         String nfRole = execution.getVariable("vnfType")
137                         String subscriptionServiceType = execution.getVariable("serviceType")
138                         String originalRequestId = execution.getVariable("msoRequestId")
139                         String originalRequestorId = execution.getVariable("requestorId")
140                         String errorSource = execution.getVariable("failedActivity")
141                         String errorCode = execution.getVariable("errorCode")
142                         String errorMessage = execution.getVariable("errorText")
143                         String buildingBlockName = execution.getVariable("currentActivity")
144                         String buildingBlockStep = execution.getVariable("workStep")
145                         String validResponses = execution.getVariable("validResponses")
146
147                         logger.debug("Before creating task")
148
149                         Map<String, String> taskVariables = new HashMap<String, String>()
150                         taskVariables.put("type", type)
151                         taskVariables.put("nfRole", nfRole)
152                         taskVariables.put("subscriptionServiceType", subscriptionServiceType)
153                         taskVariables.put("originalRequestId", originalRequestId)
154                         taskVariables.put("originalRequestorId", originalRequestorId)
155                         taskVariables.put("errorSource", errorSource)
156                         taskVariables.put("errorCode", errorCode)
157                         taskVariables.put("errorMessage", errorMessage)
158                         taskVariables.put("buildingBlockName", buildingBlockName)
159                         taskVariables.put("buildingBlockStep", buildingBlockStep)
160                         taskVariables.put("validResponses", validResponses)
161                         taskVariables.put("description", "")
162                         taskVariables.put("timeout", "")
163
164                         TaskService taskService = execution.getProcessEngineServices().getTaskService()
165                         Task manualTask = taskService.newTask(taskId)
166                         taskService.saveTask(manualTask)
167                         taskService.setVariables(taskId, taskVariables)
168                         logger.debug("successfully created task: "+ taskId)
169                 } catch (BpmnError e) {
170                         logger.debug("BPMN exception: " + e.errorMessage)
171                         throw e;
172                 } catch (Exception ex){
173                         msg = "Exception in createManualTask " + ex.getMessage()
174                         logger.debug(msg)
175                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
176                 }
177                 logger.trace("Exit createManualTask of ManualHandling ")
178         }
179
180         public void setTaskVariables (DelegateTask task) {
181
182                 DelegateExecution execution = task.getExecution()
183
184                 String msg = ""
185                 logger.trace("setTaskVariables of ManualHandling ")
186                 String taskId = task.getId()
187                 logger.debug("taskId is: " + taskId)
188
189                 try {
190                         execution.setVariable('taskId', taskId)
191                         String type = "fallout"
192                         String nfRole = execution.getVariable("vnfType")
193                         String subscriptionServiceType = execution.getVariable("serviceType")
194                         String originalRequestId = execution.getVariable("msoRequestId")
195                         String originalRequestorId = execution.getVariable("requestorId")
196                         String errorSource = execution.getVariable("failedActivity")
197                         String errorCode = execution.getVariable("errorCode")
198                         String errorMessage = execution.getVariable("errorText")
199                         String buildingBlockName = execution.getVariable("currentActivity")
200                         String buildingBlockStep = execution.getVariable("workStep")
201                         String validResponses = execution.getVariable("validResponses")
202
203                         logger.debug("Before creating task")
204
205                         Map<String, String> taskVariables = new HashMap<String, String>()
206                         taskVariables.put("type", type)
207                         taskVariables.put("nfRole", nfRole)
208                         taskVariables.put("subscriptionServiceType", subscriptionServiceType)
209                         taskVariables.put("originalRequestId", originalRequestId)
210                         taskVariables.put("originalRequestorId", originalRequestorId)
211                         taskVariables.put("errorSource", errorSource)
212                         taskVariables.put("errorCode", errorCode)
213                         taskVariables.put("errorMessage", errorMessage)
214                         taskVariables.put("buildingBlockName", buildingBlockName)
215                         taskVariables.put("buildingBlockStep", buildingBlockStep)
216                         taskVariables.put("validResponses", validResponses)
217                         taskVariables.put("description", "")
218                         taskVariables.put("timeout", "")
219                         TaskService taskService = execution.getProcessEngineServices().getTaskService()
220
221
222                         taskService.setVariables(taskId, taskVariables)
223                         logger.debug("successfully created task: "+ taskId)
224                 } catch (BpmnError e) {
225                         logger.debug("BPMN exception: " + e.errorMessage)
226                         throw e;
227                 } catch (Exception ex){
228                         msg = "Exception in createManualTask " + ex.getMessage()
229                         logger.debug(msg)
230                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
231                 }
232                 logger.trace("Exit createManualTask of ManualHandling ")
233         }
234
235         public void completeTask (DelegateTask task) {
236
237                 DelegateExecution execution = task.getExecution()
238
239                 String msg = ""
240                 logger.trace("completeTask of ManualHandling ")
241                 String taskId = task.getId()
242                 logger.debug("taskId is: " + taskId)
243
244                 try {
245                         TaskService taskService = execution.getProcessEngineServices().getTaskService()
246
247                         Map<String, String> taskVariables = taskService.getVariables(taskId)
248                         String responseValue = taskVariables.get("responseValue")
249
250                         logger.debug("Received responseValue on completion: "+ responseValue)
251                         // Have to set the first letter of the response to upper case
252                         String responseValueForRainyDay = responseValue.substring(0, 1).toUpperCase() + responseValue.substring(1)
253                         logger.debug("ResponseValue to RainyDayHandler: "+ responseValueForRainyDay)
254                         execution.setVariable("responseValue", responseValueForRainyDay)
255
256                 } catch (BpmnError e) {
257                         logger.debug("BPMN exception: " + e.errorMessage)
258                         throw e;
259                 } catch (Exception ex){
260                         msg = "Exception in createManualTask " + ex.getMessage()
261                         logger.debug(msg)
262                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
263                 }
264                 logger.trace("Exit completeTask of ManualHandling ")
265         }
266
267         public void prepareRequestsDBStatusUpdate (DelegateExecution execution, String requestStatus){
268
269                 def method = getClass().getSimpleName() + '.prepareRequestsDBStatusUpdate(' +'execution=' + execution.getId() +')'
270                 logger.trace("prepareRequestsDBStatusUpdate of ManualHandling ")
271                 try {
272                         def requestId = execution.getVariable("msoRequestId")
273                         String payload = """
274                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
275                                                    <soapenv:Header/>
276                                                    <soapenv:Body>
277                                                       <req:updateInfraRequest>
278                                                          <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
279                                                          <lastModifiedBy>ManualHandling</lastModifiedBy>
280                                                          <requestStatus>${MsoUtils.xmlEscape(requestStatus)}</requestStatus>
281                                                       </req:updateInfraRequest>
282                                                    </soapenv:Body>
283                                                 </soapenv:Envelope>
284                                 """
285
286                         execution.setVariable("setUpdateDBstatusPayload", payload)
287                         logger.debug("Outgoing Update Mso Request Payload is: " + payload)
288                         logger.debug("setUpdateDBstatusPayload: " + payload)
289                 } catch (BpmnError e) {
290                         throw e;
291                 } catch (Exception e) {
292                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
293                                         'Caught exception in ' + method, "BPMN",
294                                         ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
295                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
296                 }
297
298                 logger.trace("Exit prepareRequestsDBStatusUpdate of ManualHandling ")
299         }
300
301         public void createAOTSTicket (DelegateExecution execution) {
302                 String msg = ""
303                 logger.trace("createAOTSTicket of ManualHandling ")
304                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
305                 // This method will not be throwing an exception, but rather log the error
306
307                 try {
308                         execution.setVariable("prefix", Prefix)
309                         setBasicDBAuthHeader(execution,isDebugLogEnabled)
310                         // check for required input
311                         String requestId = execution.getVariable("msoRequestId")
312                         logger.debug("requestId is: " + requestId)
313                         def currentActivity = execution.getVariable("currentActivity")
314                         logger.debug("currentActivity is: " + currentActivity)
315                         def workStep = execution.getVariable("workStep")
316                         logger.debug("workStep is: " + workStep)
317                         def failedActivity = execution.getVariable("failedActivity")
318                         logger.debug("failedActivity is: " + failedActivity)
319                         def errorCode = execution.getVariable("errorCode")
320                         logger.debug("errorCode is: " + errorCode)
321                         def errorText = execution.getVariable("errorText")
322                         logger.debug("errorText is: " + errorText)
323                         def vnfName = execution.getVariable("vnfName")
324                         logger.debug("vnfName is: " + vnfName)
325
326                         String rubyRequestId = UUID.randomUUID()
327                         logger.debug("rubyRequestId: " + rubyRequestId)
328                         String sourceName = vnfName
329                         logger.debug("sourceName: " + sourceName)
330                         String reason = "VID Workflow failed at " + failedActivity + " " + workStep + " call with error " + errorCode
331                         logger.debug("reason: " + reason)
332                         String workflowId = requestId
333                         logger.debug("workflowId: " + workflowId)
334                         String notification = "Request originated from VID | Workflow fallout on " + vnfName + " | Workflow step failure: " + workStep + " failed | VID workflow ID: " + workflowId
335                         logger.debug("notification: " + notification)
336
337                         logger.debug("Creating AOTS Ticket request")
338
339                         RubyClient rubyClient = new RubyClient()
340                         rubyClient.rubyCreateTicketCheckRequest(rubyRequestId, sourceName, reason, workflowId, notification)
341
342                 } catch (BpmnError e) {
343                         msg = "BPMN error in createAOTSTicket " + ex.getMessage()
344                         logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
345                                         ErrorCode.UnknownError.getValue());
346                 } catch (Exception ex){
347                         msg = "Exception in createAOTSTicket " + ex.getMessage()
348                         logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
349                                         ErrorCode.UnknownError.getValue());
350                 }
351                 logger.trace("Exit createAOTSTicket of ManualHandling ")
352         }
353
354
355
356 }