Removed MsoLogger class
[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
162                         TaskService taskService = execution.getProcessEngineServices().getTaskService()
163                         Task manualTask = taskService.newTask(taskId)
164                         taskService.saveTask(manualTask)
165                         taskService.setVariables(taskId, taskVariables)
166                         logger.debug("successfully created task: "+ taskId)
167                 } catch (BpmnError e) {
168                         logger.debug("BPMN exception: " + e.errorMessage)
169                         throw e;
170                 } catch (Exception ex){
171                         msg = "Exception in createManualTask " + ex.getMessage()
172                         logger.debug(msg)
173                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
174                 }
175                 logger.trace("Exit createManualTask of ManualHandling ")
176         }
177
178         public void setTaskVariables (DelegateTask task) {
179
180                 DelegateExecution execution = task.getExecution()
181
182                 String msg = ""
183                 logger.trace("setTaskVariables of ManualHandling ")
184                 String taskId = task.getId()
185                 logger.debug("taskId is: " + taskId)
186
187                 try {
188                         execution.setVariable('taskId', taskId)
189                         String type = "fallout"
190                         String nfRole = execution.getVariable("vnfType")
191                         String subscriptionServiceType = execution.getVariable("serviceType")
192                         String originalRequestId = execution.getVariable("msoRequestId")
193                         String originalRequestorId = execution.getVariable("requestorId")
194                         String errorSource = execution.getVariable("failedActivity")
195                         String errorCode = execution.getVariable("errorCode")
196                         String errorMessage = execution.getVariable("errorText")
197                         String buildingBlockName = execution.getVariable("currentActivity")
198                         String buildingBlockStep = execution.getVariable("workStep")
199                         String validResponses = execution.getVariable("validResponses")
200
201                         logger.debug("Before creating task")
202
203                         Map<String, String> taskVariables = new HashMap<String, String>()
204                         taskVariables.put("type", type)
205                         taskVariables.put("nfRole", nfRole)
206                         taskVariables.put("subscriptionServiceType", subscriptionServiceType)
207                         taskVariables.put("originalRequestId", originalRequestId)
208                         taskVariables.put("originalRequestorId", originalRequestorId)
209                         taskVariables.put("errorSource", errorSource)
210                         taskVariables.put("errorCode", errorCode)
211                         taskVariables.put("errorMessage", errorMessage)
212                         taskVariables.put("buildingBlockName", buildingBlockName)
213                         taskVariables.put("buildingBlockStep", buildingBlockStep)
214                         taskVariables.put("validResponses", validResponses)
215                         TaskService taskService = execution.getProcessEngineServices().getTaskService()
216
217
218                         taskService.setVariables(taskId, taskVariables)
219                         logger.debug("successfully created task: "+ taskId)
220                 } catch (BpmnError e) {
221                         logger.debug("BPMN exception: " + e.errorMessage)
222                         throw e;
223                 } catch (Exception ex){
224                         msg = "Exception in createManualTask " + ex.getMessage()
225                         logger.debug(msg)
226                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
227                 }
228                 logger.trace("Exit createManualTask of ManualHandling ")
229         }
230
231         public void completeTask (DelegateTask task) {
232
233                 DelegateExecution execution = task.getExecution()
234
235                 String msg = ""
236                 logger.trace("completeTask of ManualHandling ")
237                 String taskId = task.getId()
238                 logger.debug("taskId is: " + taskId)
239
240                 try {
241                         TaskService taskService = execution.getProcessEngineServices().getTaskService()
242
243                         Map<String, String> taskVariables = taskService.getVariables(taskId)
244                         String responseValue = taskVariables.get("responseValue")
245
246                         logger.debug("Received responseValue on completion: "+ responseValue)
247                         // Have to set the first letter of the response to upper case
248                         String responseValueForRainyDay = responseValue.substring(0, 1).toUpperCase() + responseValue.substring(1)
249                         logger.debug("ResponseValue to RainyDayHandler: "+ responseValueForRainyDay)
250                         execution.setVariable("responseValue", responseValueForRainyDay)
251
252                 } catch (BpmnError e) {
253                         logger.debug("BPMN exception: " + e.errorMessage)
254                         throw e;
255                 } catch (Exception ex){
256                         msg = "Exception in createManualTask " + ex.getMessage()
257                         logger.debug(msg)
258                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
259                 }
260                 logger.trace("Exit completeTask of ManualHandling ")
261         }
262
263         public void prepareRequestsDBStatusUpdate (DelegateExecution execution, String requestStatus){
264
265                 def method = getClass().getSimpleName() + '.prepareRequestsDBStatusUpdate(' +'execution=' + execution.getId() +')'
266                 logger.trace("prepareRequestsDBStatusUpdate of ManualHandling ")
267                 try {
268                         def requestId = execution.getVariable("msoRequestId")
269                         String payload = """
270                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
271                                                    <soapenv:Header/>
272                                                    <soapenv:Body>
273                                                       <req:updateInfraRequest>
274                                                          <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
275                                                          <lastModifiedBy>ManualHandling</lastModifiedBy>
276                                                          <requestStatus>${MsoUtils.xmlEscape(requestStatus)}</requestStatus>
277                                                       </req:updateInfraRequest>
278                                                    </soapenv:Body>
279                                                 </soapenv:Envelope>
280                                 """
281
282                         execution.setVariable("setUpdateDBstatusPayload", payload)
283                         logger.debug("Outgoing Update Mso Request Payload is: " + payload)
284                         logger.debug("setUpdateDBstatusPayload: " + payload)
285                 } catch (BpmnError e) {
286                         throw e;
287                 } catch (Exception e) {
288                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
289                                         'Caught exception in ' + method, "BPMN",
290                                         ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
291                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
292                 }
293
294                 logger.trace("Exit prepareRequestsDBStatusUpdate of ManualHandling ")
295         }
296
297         public void createAOTSTicket (DelegateExecution execution) {
298                 String msg = ""
299                 logger.trace("createAOTSTicket of ManualHandling ")
300                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
301                 // This method will not be throwing an exception, but rather log the error
302
303                 try {
304                         execution.setVariable("prefix", Prefix)
305                         setBasicDBAuthHeader(execution,isDebugLogEnabled)
306                         // check for required input
307                         String requestId = execution.getVariable("msoRequestId")
308                         logger.debug("requestId is: " + requestId)
309                         def currentActivity = execution.getVariable("currentActivity")
310                         logger.debug("currentActivity is: " + currentActivity)
311                         def workStep = execution.getVariable("workStep")
312                         logger.debug("workStep is: " + workStep)
313                         def failedActivity = execution.getVariable("failedActivity")
314                         logger.debug("failedActivity is: " + failedActivity)
315                         def errorCode = execution.getVariable("errorCode")
316                         logger.debug("errorCode is: " + errorCode)
317                         def errorText = execution.getVariable("errorText")
318                         logger.debug("errorText is: " + errorText)
319                         def vnfName = execution.getVariable("vnfName")
320                         logger.debug("vnfName is: " + vnfName)
321
322                         String rubyRequestId = UUID.randomUUID()
323                         logger.debug("rubyRequestId: " + rubyRequestId)
324                         String sourceName = vnfName
325                         logger.debug("sourceName: " + sourceName)
326                         String reason = "VID Workflow failed at " + failedActivity + " " + workStep + " call with error " + errorCode
327                         logger.debug("reason: " + reason)
328                         String workflowId = requestId
329                         logger.debug("workflowId: " + workflowId)
330                         String notification = "Request originated from VID | Workflow fallout on " + vnfName + " | Workflow step failure: " + workStep + " failed | VID workflow ID: " + workflowId
331                         logger.debug("notification: " + notification)
332
333                         logger.debug("Creating AOTS Ticket request")
334
335                         RubyClient rubyClient = new RubyClient()
336                         rubyClient.rubyCreateTicketCheckRequest(rubyRequestId, sourceName, reason, workflowId, notification)
337
338                 } catch (BpmnError e) {
339                         msg = "BPMN error in createAOTSTicket " + ex.getMessage()
340                         logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
341                                         ErrorCode.UnknownError.getValue());
342                 } catch (Exception ex){
343                         msg = "Exception in createAOTSTicket " + ex.getMessage()
344                         logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
345                                         ErrorCode.UnknownError.getValue());
346                 }
347                 logger.trace("Exit createAOTSTicket of ManualHandling ")
348         }
349
350
351
352 }