1710 Rebase - Second Attempt
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / FalloutHandler.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 \r
23 import java.text.SimpleDateFormat\r
24 \r
25 import org.apache.commons.lang3.*\r
26 import org.camunda.bpm.engine.runtime.Execution\r
27 \r
28 public class FalloutHandler extends AbstractServiceTaskProcessor {\r
29         String Prefix="FH_"\r
30         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
31         \r
32         public initializeProcessVariables(Execution execution){\r
33                 def method = getClass().getSimpleName() + '.initializeProcessVariables(' +'execution=' + execution.getId() +')'\r
34                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
35                 logDebug('Entered ' + method, isDebugLogEnabled)\r
36         \r
37                 try {\r
38                         execution.setVariable("prefix",Prefix)\r
39         \r
40                         //These variables are form the input Message to the BPMN\r
41                         execution.setVariable("FH_request_id","")\r
42                         execution.setVariable("FH_request_action","")\r
43                         execution.setVariable("FH_notification-url","")\r
44                         execution.setVariable("FH_mso-bpel-name","")\r
45                         execution.setVariable("FH_ErrorCode", "")\r
46                         execution.setVariable("FH_ErrorMessage", "")\r
47 \r
48                         execution.setVariable("FH_notification-url-Ok", false)\r
49                         execution.setVariable("FH_request_id-Ok", false)\r
50 \r
51                         //These variables are for Get Mso Aai Password Adapter\r
52                         execution.setVariable("FH_deliveryStatus", true)\r
53         \r
54                         //update Response Status to pending ...Adapter variables\r
55                         execution.setVariable("FH_updateResponseStatusPayload", null)\r
56                         execution.setVariable("FH_updateResponseStatusResponse", null)\r
57         \r
58                         //update Request Gamma ...Adapter variables\r
59                         execution.setVariable("FH_updateRequestGammaPayload", "")\r
60                         execution.setVariable("FH_updateRequestGammaResponse", null)\r
61                         execution.setVariable("FH_updateRequestGammaResponseCode", null)\r
62         \r
63                         //update Request Infra ...Adapter variables\r
64                         execution.setVariable("FH_updateRequestInfraPayload", "")\r
65                         execution.setVariable("FH_updateRequestInfraResponse", null)\r
66                         execution.setVariable("FH_updateRequestInfraResponseCode", null)\r
67         \r
68                         //assign True to success variable\r
69                         execution.setVariable("FH_success", true)\r
70         \r
71                         //Set notify status to Failed variable\r
72                         execution.setVariable("FH_NOTIFY_STATUS", "SUCCESS")\r
73         \r
74                         //Set DB update variable\r
75                         execution.setVariable("FH_updateRequestPayload", "")\r
76                         execution.setVariable("FH_updateRequestResponse", null)\r
77                         execution.setVariable("FH_updateRequestResponseCode", null)\r
78         \r
79                         //Auth variables\r
80                         execution.setVariable("BasicAuthHeaderValue","")\r
81         \r
82                         //Parameter list\r
83                         execution.setVariable("FH_parameterList",  "")\r
84         \r
85                         //Response variables\r
86                         execution.setVariable("FalloutHandlerResponse","")\r
87                         execution.setVariable("FH_ErrorResponse", null)\r
88                         execution.setVariable("FH_ResponseCode", "")\r
89         \r
90                 } catch (Exception e) {\r
91                         logError('Caught exception in ' + method, e)\r
92                 //      exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)\r
93                 }\r
94         }\r
95         \r
96         public void preProcessRequest (Execution execution) {\r
97                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'\r
98                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
99                 logDebug('Entered ' + method, isDebugLogEnabled)\r
100         \r
101                 // Initialize flow variables\r
102                 initializeProcessVariables(execution)\r
103                 setSuccessIndicator(execution, false)\r
104         \r
105                 setBasicDBAuthHeader(execution, isDebugLogEnabled)\r
106                 \r
107                 try {\r
108                         def xml = execution.getVariable("FalloutHandlerRequest")\r
109                         utils.log("DEBUG", " XML --> " + xml, isDebugLogEnabled)\r
110                         utils.logAudit("FalloutHandler request: " + xml)\r
111         \r
112                         //Check the incoming request type\r
113                         //Incoming request can be ACTIVE_REQUESTS (request-information node) or  INFRA_ACTIVE_REQUESTS (request-info node)\r
114                         if (utils.nodeExists(xml, "request-information")) {\r
115                                 execution.setVariable("FH_request_id-Ok", true) // Incoming request is for ACTIVE_REQUESTS\r
116                         }\r
117         \r
118                         //Check notification-url for the incoming request type\r
119                         //ACTIVE_REQUESTS may have notificationurl node\r
120                         //INFRA_ACTIVE_REQUESTS notificationurl node does not exist\r
121                         def notificationurl = ""\r
122                         if (utils.nodeExists(xml, "notification-url")) {\r
123                                 notificationurl = utils.getNodeText(xml,"notification-url")\r
124                                 if(notificationurl != null && !notificationurl.isEmpty()) {\r
125                                         utils.log("DEBUG", "********** Incoming notification Url is: " + notificationurl,isDebugLogEnabled);\r
126                                         execution.setVariable("FH_notification-url-Ok", true)\r
127                                         execution.setVariable("FH_notification-url",notificationurl)\r
128                                 }\r
129                         }\r
130         \r
131                         //Check request_id for the incoming request type\r
132                         //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)\r
133                         //For ACTIVE_REQUESTS payload request-id is NOT optional\r
134                         def request_id = ""\r
135                         if (utils.nodeExists(xml, "request-id")) {\r
136                                 execution.setVariable("FH_request_id",utils.getNodeText(xml,"request-id"))\r
137                         }\r
138                         utils.logAudit("FH_request_id: " + execution.getVariable("FH_request_id"))\r
139         \r
140                         // INFRA_ACTIVE_REQUESTS         have "action" element ... mandatory\r
141                         // ACTIVE_REQUEST have "request-action" ... mandatory\r
142                         if (utils.nodeExists(xml, "request-action")) {\r
143                                 execution.setVariable("FH_request_action",utils.getNodeText(xml,"request-action"))\r
144                         } else if (utils.nodeExists(xml, "action")) {\r
145                                 execution.setVariable("FH_request_action",utils.getNodeText(xml,"action"))\r
146                         }\r
147         \r
148         \r
149                         //Check source for the incoming request type\r
150                         //For INFRA_ACTIVE_REQUESTS payload source IS optional\r
151                         //For ACTIVE_REQUESTS payload source is NOT optional\r
152                         def source = ""\r
153                         if (utils.nodeExists(xml, "source")) {\r
154                                 execution.setVariable("FH_source",utils.getNodeText(xml,"source"))\r
155                         }\r
156         \r
157                         //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.\r
158                         def errorCode = ""\r
159                         if (utils.nodeExists(xml, "ErrorCode")) {\r
160                                 errorCode = utils.getNodeText(xml,"ErrorCode")\r
161                                 if(errorCode != null && !errorCode.isEmpty()) {\r
162                                         execution.setVariable("FH_ErrorCode", errorCode)\r
163                                 }\r
164                         }\r
165                         utils.logAudit("FH_ErrorCode: " + errorCode)\r
166         \r
167                         //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.\r
168                         def errorMessage = ""\r
169                         if (utils.nodeExists(xml, "ErrorMessage")) {\r
170                                 errorCode = utils.getNodeText(xml,"ErrorMessage")\r
171                                 if(errorCode != null && !errorCode.isEmpty()) {\r
172                                         errorCode = errorCode.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")\r
173                                         execution.setVariable("FH_ErrorMessage", errorCode)\r
174                                 }\r
175                         }\r
176         \r
177                         //Check for Parameter List\r
178                         if (utils.nodeExists(xml, "parameter-list")) {\r
179                                 def parameterList = utils.getNodeXml(xml, "parameter-list", false)\r
180                                 execution.setVariable("FH_parameterList", parameterList)\r
181                         }\r
182         \r
183                         utils.log("DEBUG","FH_notification-url-Ok --> " + execution.getVariable("FH_notification-url-Ok"),isDebugLogEnabled)\r
184                         utils.log("DEBUG","FH_request_id-OK --> " + execution.getVariable("FH_request_id-Ok"),isDebugLogEnabled)\r
185         \r
186                         // set the DHV/Service Instantiation values if specified in the request\r
187                         execution.setVariable("FH_is_srv_inst_req", String.valueOf("true".equals(utils.getNodeText1(xml, "is-srv-inst-req"))))\r
188                         utils.log("DEBUG", "FH_is_srv_inst_req --> " + execution.getVariable("FH_is_srv_inst_req"), isDebugLogEnabled)\r
189                         execution.setVariable("FH_is_json_content", String.valueOf("JSON".equals(utils.getNodeText1(xml, "resp-content-type"))))\r
190                         utils.log("DEBUG", "FH_is_json_content --> " + execution.getVariable("FH_is_json_content"), isDebugLogEnabled)\r
191                         execution.setVariable("FH_service_inst_id", utils.getNodeText1(xml, "service-instance-id"))\r
192                         utils.log("DEBUG", "FH_service_inst_id --> " + execution.getVariable("FH_service_inst_id"), isDebugLogEnabled)\r
193                         execution.setVariable("FH_start_time", utils.getNodeText1(xml, "start-time"))\r
194                         utils.log("DEBUG", "FH_start_time --> " + execution.getVariable("FH_start_time"), isDebugLogEnabled)\r
195                         // this variable is used by the camunda flow to set the Content-Type for the async response\r
196                         if (execution.getVariable("FH_is_srv_inst_req").equals("true") &&\r
197                                 execution.getVariable("FH_is_json_content").equals("true")) {\r
198                                 execution.setVariable("FH_content_type", "application/json")\r
199                         } else {\r
200                                 execution.setVariable("FH_content_type", "text/xml")\r
201                         }\r
202                 } catch (Exception e) {\r
203                         logError('Caught exception in ' + method, e)\r
204                 //      exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in" + method)\r
205                 }\r
206         \r
207                 utils.log("DEBUG","OUTOF --> Initialize Variables Fallout Handler #########",isDebugLogEnabled);\r
208         }\r
209         \r
210         public String updateRequestPayload (Execution execution){\r
211                 def method = getClass().getSimpleName() + '.updateRequestPayload(' +'execution=' + execution.getId() +')'\r
212                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
213                 logDebug('Entered ' + method, isDebugLogEnabled)\r
214         \r
215                 try {\r
216                         String payload = """\r
217                                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">\r
218                                         <soapenv:Header/>\r
219                                         <soapenv:Body>\r
220                                                 <req:updateRequest>\r
221                                                         <requestId>${execution.getVariable("FH_request_id")}</requestId>\r
222                                                         <lastModifiedBy>BPEL</lastModifiedBy>\r
223                                                         <finalErrorMessage>${execution.getVariable("FH_ErrorMessage")}</finalErrorMessage>\r
224                                                         <finalErrorCode>${execution.getVariable("FH_ErrorCode")}</finalErrorCode>\r
225                                                         <status>FAILED</status>\r
226                                                         <responseStatus>${execution.getVariable("FH_NOTIFY_STATUS")}</responseStatus>\r
227                                                 </req:updateRequest>\r
228                                         </soapenv:Body>\r
229                                         </soapenv:Envelope>\r
230                                 """\r
231         \r
232                         utils.logAudit("updateRequestPayload: " + payload)\r
233                         execution.setVariable("FH_updateRequestPayload", payload)\r
234                         return execution.getVariable("FH_updateRequestPayload")\r
235                 } catch (Exception e) {\r
236                         logError('Caught exception in ' + method, e)\r
237                 //      exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)\r
238                 }\r
239         }\r
240         \r
241         public String updateRequestInfraPayload (Execution execution){\r
242                 def method = getClass().getSimpleName() + '.updateRequestInfraPayload(' +'execution=' + execution.getId() +')'\r
243                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
244                 logDebug('Entered ' + method, isDebugLogEnabled)\r
245         \r
246                 try {\r
247                         String payload = """\r
248                                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">\r
249                                         <soapenv:Header/>\r
250                                         <soapenv:Body>\r
251                                                 <req:updateInfraRequest>\r
252                                                         <requestId>${execution.getVariable("FH_request_id")}</requestId>\r
253                                                         <lastModifiedBy>BPEL</lastModifiedBy>\r
254                                                         <statusMessage>${execution.getVariable("FH_ErrorMessage")}</statusMessage>\r
255                                                         <requestStatus>FAILED</requestStatus>\r
256                                                         <progress>100</progress>\r
257                                                 </req:updateInfraRequest>\r
258                                         </soapenv:Body>\r
259                                         </soapenv:Envelope>\r
260                                 """\r
261         \r
262                         execution.setVariable("FH_updateRequestInfraPayload", payload)\r
263                         utils.logAudit("updateRequestInfraPayload: " + payload)\r
264                         return execution.getVariable("FH_updateRequestInfraPayload")\r
265                 } catch (Exception e) {\r
266                         logError('Caught exception in ' + method, e)\r
267                 //      exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)\r
268                 }\r
269         }\r
270         \r
271         public String updateRequestGammaPayload (Execution execution){\r
272                 def method = getClass().getSimpleName() + '.updateRequestGammaPayload(' +'execution=' + execution.getId() +')'\r
273                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
274                 logDebug('Entered ' + method, isDebugLogEnabled)\r
275         \r
276                 try {\r
277                         String payload = """\r
278                                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="${execution.getVariable("URN_mso_default_adapter_namespace")}/requestsdb">\r
279                                         <soapenv:Header/>\r
280                                         <soapenv:Body>\r
281                                                 <req:updateRequest>\r
282                                                         <requestId>${execution.getVariable("FH_request_id")}</requestId>\r
283                                                         <lastModifiedBy>BPEL</lastModifiedBy>\r
284                                                         <finalErrorMessage>${execution.getVariable("FH_ErrorMessage")}</finalErrorMessage>\r
285                                                         <finalErrorCode>${execution.getVariable("FH_ErrorCode")}</finalErrorCode>\r
286                                                         <status>FAILED</status>\r
287                                                 </req:updateRequest>\r
288                                         </soapenv:Body>\r
289                                         </soapenv:Envelope>\r
290                                 """\r
291         \r
292                         execution.setVariable("FH_updateRequestGammaPayload", payload)\r
293                         utils.logAudit("updateRequestGammaPayload: " + payload)\r
294                         return execution.getVariable("FH_updateRequestGammaPayload")\r
295                 } catch (Exception e) {\r
296                         logError('Caught exception in ' + method, e)\r
297                 //      exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)\r
298                 }\r
299         }\r
300         \r
301         public String updateResponseStatusPayload (Execution execution){\r
302                 def method = getClass().getSimpleName() + '.updateResponseStatusPayload(' +'execution=' + execution.getId() +')'\r
303                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
304                 logDebug('Entered ' + method, isDebugLogEnabled)\r
305         \r
306                 try {\r
307                         String payload = """\r
308                                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">\r
309                                         <soapenv:Header/>\r
310                                         <soapenv:Body>\r
311                                                 <req:updateResponseStatus>\r
312                                                         <requestId>${execution.getVariable("FH_request_id")}</requestId>\r
313                                                         <lastModifiedBy>BPEL</lastModifiedBy>\r
314                                                         <responseStatus>SENDING_FINAL_NOTIFY</responseStatus>\r
315                                                 </req:updateResponseStatus>\r
316                                         </soapenv:Body>\r
317                                         </soapenv:Envelope>\r
318                                 """\r
319         \r
320                         execution.setVariable("FH_updateResponseStatusPayload", payload)\r
321                         utils.logAudit("updateResponseStatusPayload: " + payload)\r
322                         return execution.getVariable("FH_updateResponseStatusPayload")\r
323                 } catch (Exception e) {\r
324                         logError('Caught exception in ' + method, e)\r
325                 //      exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)\r
326                 }\r
327         }\r
328         \r
329         public void buildDBWorkflowException(Execution execution, String responseCodeVariable) {\r
330                 def method = getClass().getSimpleName() + '.buildDBWorkflowException(' +\r
331                         'execution=' + execution.getId() +\r
332                         ', responseCodeVariable=' + responseCodeVariable + ')'\r
333                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
334                 logDebug('Entered ' + method, isDebugLogEnabled)\r
335         \r
336                 try {\r
337                         def responseCode = execution.getVariable(responseCodeVariable)\r
338                         // If the HTTP response code was null, it means a connection fault occurred (a java exception)\r
339                         def errorMessage = responseCode == null ? "Could not connect to DB Adapter" : "DB Adapter returned ${responseCode} response"\r
340                         def errorCode = responseCode == null ? 7000 : 7020\r
341                 //      exceptionUtil.buildWorkflowException(execution, errorCode, errorMessage)\r
342                 } catch (Exception e) {\r
343                         logError('Caught exception in ' + method, e)\r
344                 //      exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)\r
345                 }\r
346         }\r
347         \r
348         /**\r
349          * Used to create a workflow response in success and failure cases.\r
350          */\r
351         public void postProcessResponse (Execution execution) {\r
352                 def method = getClass().getSimpleName() + '.postProcessResponse(' +'execution=' + execution.getId() +')'\r
353                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
354                 logDebug('Entered ' + method, isDebugLogEnabled)\r
355         \r
356                 try {\r
357                         Boolean success = (Boolean) execution.getVariable("FH_success")\r
358                         String out = success ? "Fallout Handler Succeeded" : "Fallout Handler Failed";\r
359         \r
360                         def falloutHandlerResponse = """\r
361                                         <workflow:FalloutHandlerResponse xmlns:workflow="http://org.openecomp/mso/workflow/schema/v1">\r
362                                            <workflow:out>${out}</workflow:out>\r
363                                         </workflow:FalloutHandlerResponse>\r
364                                 """\r
365         \r
366                         falloutHandlerResponse = utils.formatXml(falloutHandlerResponse)\r
367                         utils.logAudit("FalloutHandler Response: " + falloutHandlerResponse);\r
368         \r
369                         execution.setVariable("FalloutHandlerResponse", falloutHandlerResponse)\r
370                         execution.setVariable("WorkflowResponse", falloutHandlerResponse)\r
371                         execution.setVariable("FH_ResponseCode", success ? "200" : "500")\r
372                         setSuccessIndicator(execution, success)\r
373         \r
374                         logDebug("FalloutHandlerResponse =\n" + falloutHandlerResponse, isDebugLogEnabled)\r
375                 } catch (Exception e) {\r
376                         // Do NOT throw WorkflowException!\r
377                         logError('Caught exception in ' + method, e)\r
378                 }\r
379         }\r
380 }