Initial OpenECOMP MSO commit
[so.git] / bpmn / MSOGammaBPMN / src / main / groovy / com / att / bpm / scripts / FalloutHandler.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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 com.att.bpm.scripts
22
23 import java.text.SimpleDateFormat
24
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.runtime.Execution
27
28 public class FalloutHandler extends AbstractServiceTaskProcessor {
29
30         String Prefix="FH_"
31         ExceptionUtil exceptionUtil = new ExceptionUtil()
32
33         public initializeProcessVariables(Execution execution){
34                 def method = getClass().getSimpleName() + '.initializeProcessVariables(' +'execution=' + execution.getId() +')'
35                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
36                 logDebug('Entered ' + method, isDebugLogEnabled)
37
38                 try {
39                         execution.setVariable("prefix",Prefix)
40
41                         //These variables are form the input Message to the BPMN
42                         execution.setVariable("FH_request_id","")
43                         execution.setVariable("FH_request_action","")
44                         execution.setVariable("FH_notification-url","")
45                         execution.setVariable("FH_mso-bpel-name","")
46                         execution.setVariable("FH_ErrorCode", "")
47                         execution.setVariable("FH_ErrorMessage", "")
48
49                         execution.setVariable("FH_notification-url-Ok", false)
50                         execution.setVariable("FH_request_id-Ok", false)
51
52                         //These variables are for Get Mso Aai Password Adapter
53                         execution.setVariable("FH_deliveryStatus", true)
54
55                         //Notify CSI Adapter variables
56                         execution.setVariable("FH_notifyCSIPayload", "")
57                         execution.setVariable("FH_notifyCSIResponseCode", null)
58                         execution.setVariable("FH_notifyCSIResponse", "")
59
60                         //update Response Status to pending ...Adapter variables
61                         execution.setVariable("FH_updateResponseStatusPayload", null)
62                         execution.setVariable("FH_updateResponseStatusResponse", null)
63
64                         //update Request Gamma ...Adapter variables
65                         execution.setVariable("FH_updateRequestGammaPayload", "")
66                         execution.setVariable("FH_updateRequestGammaResponse", null)
67                         execution.setVariable("FH_updateRequestGammaResponseCode", null)
68
69                         //update Request Infra ...Adapter variables
70                         execution.setVariable("FH_updateRequestInfraPayload", "")
71                         execution.setVariable("FH_updateRequestInfraResponse", null)
72                         execution.setVariable("FH_updateRequestInfraResponseCode", null)
73
74                         //assign True to success variable
75                         execution.setVariable("FH_success", true)
76
77                         //Set notify status to Failed variable
78                         execution.setVariable("FH_NOTIFY_STATUS", "SUCCESS")
79
80                         //Set DB update variable
81                         execution.setVariable("FH_updateRequestPayload", "")
82                         execution.setVariable("FH_updateRequestResponse", null)
83                         execution.setVariable("FH_updateRequestResponseCode", null)
84
85                         //Auth variables
86                         execution.setVariable("BasicAuthHeaderValue","")
87
88                         //Parameter list
89                         execution.setVariable("FH_parameterList",  "")
90
91                         //Response variables
92                         execution.setVariable("FalloutHandlerResponse","")
93                         execution.setVariable("FH_ErrorResponse", null)
94                         execution.setVariable("FH_ResponseCode", "")
95
96                 } catch (Exception e) {
97                         logError('Caught exception in ' + method, e)
98                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
99                 }
100         }
101
102         public void preProcessRequest (Execution execution) {
103                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
104                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
105                 logDebug('Entered ' + method, isDebugLogEnabled)
106
107                 // Initialize flow variables
108                 initializeProcessVariables(execution)
109                 setSuccessIndicator(execution, false)
110
111                 try {
112                         def xml = execution.getVariable("FalloutHandlerRequest")
113                         utils.log("DEBUG", " XML --> " + xml, isDebugLogEnabled)
114                         utils.logAudit("FalloutHandler request: " + xml)
115
116                         //Check the incoming request type
117                         //Incoming request can be ACTIVE_REQUESTS (request-information node) or  INFRA_ACTIVE_REQUESTS (request-info node)
118                         if (utils.nodeExists(xml, "request-information")) {
119                                 execution.setVariable("FH_request_id-Ok", true) // Incoming request is for ACTIVE_REQUESTS
120                         }
121
122                         //Check notification-url for the incoming request type
123                         //ACTIVE_REQUESTS may have notificationurl node
124                         //INFRA_ACTIVE_REQUESTS notificationurl node does not exist
125                         def notificationurl = ""
126                         if (utils.nodeExists(xml, "notification-url")) {
127                                 notificationurl = utils.getNodeText(xml,"notification-url")
128                                 if(notificationurl != null && !notificationurl.isEmpty()) {
129                                         execution.setVariable("FH_notification-url-Ok", true)
130                                         execution.setVariable("FH_notification-url",notificationurl)
131                                 }
132                         }
133
134                         //Check request_id for the incoming request type
135                         //For INFRA_ACTIVE_REQUESTS payload request-id IS optional (Not sure why this is option since req id is primary key ... also tried exe through SOAP UI to check if MSO code handles null like auto generated seq not it does not)
136                         //For ACTIVE_REQUESTS payload request-id is NOT optional
137                         def request_id = ""
138                         if (utils.nodeExists(xml, "request-id")) {
139                                 execution.setVariable("FH_request_id",utils.getNodeText(xml,"request-id"))
140                         }
141                         utils.logAudit("FH_request_id: " + execution.getVariable("FH_request_id"))
142
143                         // INFRA_ACTIVE_REQUESTS         have "action" element ... mandatory
144                         // ACTIVE_REQUEST have "request-action" ... mandatory
145                         if (utils.nodeExists(xml, "request-action")) {
146                                 execution.setVariable("FH_request_action",utils.getNodeText(xml,"request-action"))
147                         } else if (utils.nodeExists(xml, "action")) {
148                                 execution.setVariable("FH_request_action",utils.getNodeText(xml,"action"))
149                         }
150
151
152                         //Check source for the incoming request type
153                         //For INFRA_ACTIVE_REQUESTS payload source IS optional
154                         //For ACTIVE_REQUESTS payload source is NOT optional
155                         def source = ""
156                         if (utils.nodeExists(xml, "source")) {
157                                 execution.setVariable("FH_source",utils.getNodeText(xml,"source"))
158                         }
159
160                         //Check if ErrorCode node exists. If yes, initialize it from request xml, if no, it will stay with defaulf value already set in initializeProcessVariables() method above.
161                         def errorCode = ""
162                         if (utils.nodeExists(xml, "ErrorCode")) {
163                                 errorCode = utils.getNodeText(xml,"ErrorCode")
164                                 if(errorCode != null && !errorCode.isEmpty()) {
165                                         execution.setVariable("FH_ErrorCode", errorCode)
166                                 }
167                         }
168                         utils.logAudit("FH_ErrorCode: " + errorCode)
169
170                         //Check if ErrorMessage node exists. If yes, initialize it from request xml, if no, it will stay with defaulf value already set in initializeProcessVariables() method above.
171                         def errorMessage = ""
172                         if (utils.nodeExists(xml, "ErrorMessage")) {
173                                 errorCode = utils.getNodeText(xml,"ErrorMessage")
174                                 if(errorCode != null && !errorCode.isEmpty()) {
175                                         errorCode = errorCode.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
176                                         execution.setVariable("FH_ErrorMessage", errorCode)
177                                 }
178                         }
179
180                         //Check for Parameter List
181                         if (utils.nodeExists(xml, "parameter-list")) {
182                                 def parameterList = utils.getNodeXml(xml, "parameter-list", false)
183                                 execution.setVariable("FH_parameterList", parameterList)
184                         }
185
186                         utils.log("DEBUG","FH_notification-url-Ok --> " + execution.getVariable("FH_notification-url-Ok"),isDebugLogEnabled)
187                         utils.log("DEBUG","FH_request_id-OK --> " + execution.getVariable("FH_request_id-Ok"),isDebugLogEnabled)
188
189                 } catch (Exception e) {
190                         logError('Caught exception in ' + method, e)
191                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
192                 }
193
194                 utils.log("DEBUG","OUTOF --> Initialize Variables Fallout Handler #########",isDebugLogEnabled);
195         }
196
197         public String updateRequestPayload (Execution execution){
198                 def method = getClass().getSimpleName() + '.updateRequestPayload(' +'execution=' + execution.getId() +')'
199                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
200                 logDebug('Entered ' + method, isDebugLogEnabled)
201
202                 try {
203                         String payload = """
204                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://com.att.mso/requestsdb">
205                                 <soapenv:Header/>
206                                 <soapenv:Body>
207                                         <req:updateRequest>
208                                                 <requestId>${execution.getVariable("FH_request_id")}</requestId>
209                                                 <lastModifiedBy>BPEL</lastModifiedBy>
210                                                 <finalErrorMessage>${execution.getVariable("FH_ErrorMessage")}</finalErrorMessage>
211                                                 <finalErrorCode>${execution.getVariable("FH_ErrorCode")}</finalErrorCode>
212                                                 <status>FAILED</status>
213                                                 <responseStatus>${execution.getVariable("FH_NOTIFY_STATUS")}</responseStatus>
214                                         </req:updateRequest>
215                                 </soapenv:Body>
216                                 </soapenv:Envelope>
217                         """
218
219                         utils.logAudit("updateRequestPayload: " + payload)
220                         execution.setVariable("FH_updateRequestPayload", payload)
221                         return execution.getVariable("FH_updateRequestPayload")
222                 } catch (Exception e) {
223                         logError('Caught exception in ' + method, e)
224                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
225                 }
226         }
227
228         public String updateRequestInfraPayload (Execution execution){
229                 def method = getClass().getSimpleName() + '.updateRequestInfraPayload(' +'execution=' + execution.getId() +')'
230                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
231                 logDebug('Entered ' + method, isDebugLogEnabled)
232
233                 try {
234                         String payload = """
235                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://com.att.mso/requestsdb">
236                                 <soapenv:Header/>
237                                 <soapenv:Body>
238                                         <req:updateInfraRequest>
239                                                 <requestId>${execution.getVariable("FH_request_id")}</requestId>
240                                                 <lastModifiedBy>BPEL</lastModifiedBy>
241                                                 <statusMessage>${execution.getVariable("FH_ErrorMessage")}</statusMessage>
242                                                 <requestStatus>FAILED</requestStatus>
243                                                 <progress>100</progress>
244                                         </req:updateInfraRequest>
245                                 </soapenv:Body>
246                                 </soapenv:Envelope>
247                         """
248
249                         execution.setVariable("FH_updateRequestInfraPayload", payload)
250                         utils.logAudit("updateRequestInfraPayload: " + payload)
251                         return execution.getVariable("FH_updateRequestInfraPayload")
252                 } catch (Exception e) {
253                         logError('Caught exception in ' + method, e)
254                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
255                 }
256         }
257
258         public String updateRequestGammaPayload (Execution execution){
259                 def method = getClass().getSimpleName() + '.updateRequestGammaPayload(' +'execution=' + execution.getId() +')'
260                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
261                 logDebug('Entered ' + method, isDebugLogEnabled)
262
263                 try {
264                         String payload = """
265                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://com.att.mso/requestsdb">
266                                 <soapenv:Header/>
267                                 <soapenv:Body>
268                                         <req:updateRequest>
269                                                 <requestId>${execution.getVariable("FH_request_id")}</requestId>
270                                                 <lastModifiedBy>BPEL</lastModifiedBy>
271                                                 <finalErrorMessage>${execution.getVariable("FH_ErrorMessage")}</finalErrorMessage>
272                                                 <finalErrorCode>${execution.getVariable("FH_ErrorCode")}</finalErrorCode>
273                                                 <status>FAILED</status>
274                                         </req:updateRequest>
275                                 </soapenv:Body>
276                                 </soapenv:Envelope>
277                         """
278
279                         execution.setVariable("FH_updateRequestGammaPayload", payload)
280                         utils.logAudit("updateRequestGammaPayload: " + payload)
281                         return execution.getVariable("FH_updateRequestGammaPayload")
282                 } catch (Exception e) {
283                         logError('Caught exception in ' + method, e)
284                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
285                 }
286         }
287
288         public String updateResponseStatusPayload (Execution execution){
289                 def method = getClass().getSimpleName() + '.updateResponseStatusPayload(' +'execution=' + execution.getId() +')'
290                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
291                 logDebug('Entered ' + method, isDebugLogEnabled)
292
293                 try {
294                         String payload = """
295                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://com.att.mso/requestsdb">
296                                 <soapenv:Header/>
297                                 <soapenv:Body>
298                                         <req:updateResponseStatus>
299                                                 <requestId>${execution.getVariable("FH_request_id")}</requestId>
300                                                 <lastModifiedBy>BPEL</lastModifiedBy>
301                                                 <responseStatus>SENDING_FINAL_NOTIFY</responseStatus>
302                                         </req:updateResponseStatus>
303                                 </soapenv:Body>
304                                 </soapenv:Envelope>
305                         """
306
307                         execution.setVariable("FH_updateResponseStatusPayload", payload)
308                         utils.logAudit("updateResponseStatusPayload: " + payload)
309                         return execution.getVariable("FH_updateResponseStatusPayload")
310                 } catch (Exception e) {
311                         logError('Caught exception in ' + method, e)
312                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
313                 }
314         }
315
316         public String notifyCSIPayload (Execution execution) {
317                 return "";
318         }
319
320         public String notifyCCDFailurePayload (Execution execution) {
321                 def method = getClass().getSimpleName() + '.notifyCCDFailurePayload(' +'execution=' + execution.getId() +')'
322                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
323                 logDebug('Entered ' + method, isDebugLogEnabled)
324
325                 try {
326                         def parameterList = execution.getVariable('FH_parameterList')
327                         String payload ="""
328                                 <ns:status-notification xmlns:ns="http://ecomp.att.com/mso/statusnotification/schema/v1" xmlns:msoservtypes="http://ecomp.att.com/mso/request/types/v1">
329                                         <msoservtypes:request-id>${execution.getVariable("FH_request_id")}</msoservtypes:request-id>
330                                         <msoservtypes:request-action>${execution.getVariable("FH_request_action")}</msoservtypes:request-action>
331                                         <msoservtypes:source>${execution.getVariable("FH_source")}</msoservtypes:source>
332                                         <msoservtypes:error-code>${execution.getVariable("FH_ErrorCode")}</msoservtypes:error-code>
333                                         <msoservtypes:error-message>${execution.getVariable("FH_ErrorMessage")}</msoservtypes:error-message>
334                                         <msoservtypes:ack-final-indicator>Y</msoservtypes:ack-final-indicator>
335                                         ${parameterList}
336                                 </ns:status-notification>
337                         """
338
339                         payload = utils.formatXml(payload)
340                         utils.logAudit("notifyCCDFailurePayload: " + payload)
341
342                         execution.setVariable("FH_notifyCCDFailurePayload", payload)
343                         utils.log("DEBUG", "FH_notifyCCDFailurePayload --> " + "\n" + execution.getVariable("FH_notifyCCDFailurePayload"), isDebugLogEnabled)
344                         return execution.getVariable("FH_notifyCCDFailurePayload")
345                 } catch (Exception e) {
346                         logError('Caught exception in ' + method, e)
347                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
348                 }
349         }
350
351         public void buildDBWorkflowException(Execution execution, String responseCodeVariable) {
352                 def method = getClass().getSimpleName() + '.buildDBWorkflowException(' +
353                         'execution=' + execution.getId() +
354                         ', responseCodeVariable=' + responseCodeVariable + ')'
355                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
356                 logDebug('Entered ' + method, isDebugLogEnabled)
357
358                 try {
359                         def responseCode = execution.getVariable(responseCodeVariable)
360                         // If the HTTP response code was null, it means a connection fault occurred (a java exception)
361                         def errorMessage = responseCode == null ? "Could not connect to DB Adapter" : "DB Adapter returned ${responseCode} response"
362                         def errorCode = responseCode == null ? 7000 : 7020
363                         exceptionUtil.buildWorkflowException(execution, errorCode, errorMessage)
364                 } catch (Exception e) {
365                         logError('Caught exception in ' + method, e)
366                         exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
367                 }
368         }
369
370         /**
371          * Used to create a workflow response in success and failure cases.
372          */
373         public void postProcessResponse (Execution execution) {
374                 def method = getClass().getSimpleName() + '.postProcessResponse(' +'execution=' + execution.getId() +')'
375                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
376                 logDebug('Entered ' + method, isDebugLogEnabled)
377
378                 try {
379                         Boolean success = (Boolean) execution.getVariable("FH_success")
380                         String out = success ? "Fallout Handler Succeeded" : "Fallout Handler Failed";
381
382                         def falloutHandlerResponse = """
383                                 <workflow:FalloutHandlerResponse xmlns:workflow="http://ecomp.att.com/mso/workflow/schema/v1">
384                                    <workflow:out>${out}</workflow:out>
385                                 </workflow:FalloutHandlerResponse>
386                         """
387
388                         falloutHandlerResponse = utils.formatXml(falloutHandlerResponse)
389                         utils.logAudit("FalloutHandler Response: " + falloutHandlerResponse);
390
391                         execution.setVariable("FalloutHandlerResponse", falloutHandlerResponse)
392                         execution.setVariable("WorkflowResponse", falloutHandlerResponse)
393                         execution.setVariable("FH_ResponseCode", success ? "200" : "500")
394                         setSuccessIndicator(execution, success)
395
396                         logDebug("FalloutHandlerResponse =\n" + falloutHandlerResponse, isDebugLogEnabled)
397                 } catch (Exception e) {
398                         // Do NOT throw WorkflowException!
399                         logError('Caught exception in ' + method, e)
400                 }
401         }
402 }