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