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