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