2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.common.scripts
23 import java.text.SimpleDateFormat
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.runtime.Execution
28 public class FalloutHandler extends AbstractServiceTaskProcessor {
30 ExceptionUtil exceptionUtil = new ExceptionUtil()
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)
38 execution.setVariable("prefix",Prefix)
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", "")
48 execution.setVariable("FH_notification-url-Ok", false)
49 execution.setVariable("FH_request_id-Ok", false)
51 //These variables are for Get Mso Aai Password Adapter
52 execution.setVariable("FH_deliveryStatus", true)
54 //update Response Status to pending ...Adapter variables
55 execution.setVariable("FH_updateResponseStatusPayload", null)
56 execution.setVariable("FH_updateResponseStatusResponse", null)
58 //update Request Gamma ...Adapter variables
59 execution.setVariable("FH_updateRequestGammaPayload", "")
60 execution.setVariable("FH_updateRequestGammaResponse", null)
61 execution.setVariable("FH_updateRequestGammaResponseCode", null)
63 //update Request Infra ...Adapter variables
64 execution.setVariable("FH_updateRequestInfraPayload", "")
65 execution.setVariable("FH_updateRequestInfraResponse", null)
66 execution.setVariable("FH_updateRequestInfraResponseCode", null)
68 //assign True to success variable
69 execution.setVariable("FH_success", true)
71 //Set notify status to Failed variable
72 execution.setVariable("FH_NOTIFY_STATUS", "SUCCESS")
74 //Set DB update variable
75 execution.setVariable("FH_updateRequestPayload", "")
76 execution.setVariable("FH_updateRequestResponse", null)
77 execution.setVariable("FH_updateRequestResponseCode", null)
80 execution.setVariable("BasicAuthHeaderValue","")
83 execution.setVariable("FH_parameterList", "")
86 execution.setVariable("FalloutHandlerResponse","")
87 execution.setVariable("FH_ErrorResponse", null)
88 execution.setVariable("FH_ResponseCode", "")
90 } catch (Exception e) {
91 logError('Caught exception in ' + method, e)
92 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
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)
101 // Initialize flow variables
102 initializeProcessVariables(execution)
103 setSuccessIndicator(execution, false)
106 String basicAuthValueDB = execution.getVariable("URN_mso_adapters_db_auth")
107 utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB, isDebugLogEnabled)
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)
118 def xml = execution.getVariable("FalloutHandlerRequest")
119 utils.log("DEBUG", " XML --> " + xml, isDebugLogEnabled)
120 utils.logAudit("FalloutHandler request: " + xml)
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
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)
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
145 if (utils.nodeExists(xml, "request-id")) {
146 execution.setVariable("FH_request_id",utils.getNodeText(xml,"request-id"))
148 utils.logAudit("FH_request_id: " + execution.getVariable("FH_request_id"))
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"))
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
163 if (utils.nodeExists(xml, "source")) {
164 execution.setVariable("FH_source",utils.getNodeText(xml,"source"))
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.
169 if (utils.nodeExists(xml, "ErrorCode")) {
170 errorCode = utils.getNodeText(xml,"ErrorCode")
171 if(errorCode != null && !errorCode.isEmpty()) {
172 execution.setVariable("FH_ErrorCode", errorCode)
175 utils.logAudit("FH_ErrorCode: " + errorCode)
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("&", "&").replace("<", "<").replace(">", ">")
183 execution.setVariable("FH_ErrorMessage", errorCode)
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)
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)
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")
210 execution.setVariable("FH_content_type", "text/xml")
212 } catch (Exception e) {
213 logError('Caught exception in ' + method, e)
214 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
217 utils.log("DEBUG","OUTOF --> Initialize Variables Fallout Handler #########",isDebugLogEnabled);
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)
227 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">
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>
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)
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)
258 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">
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>
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)
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)
288 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">
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>
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)
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)
318 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">
321 <req:updateResponseStatus>
322 <requestId>${execution.getVariable("FH_request_id")}</requestId>
323 <lastModifiedBy>BPEL</lastModifiedBy>
324 <responseStatus>SENDING_FINAL_NOTIFY</responseStatus>
325 </req:updateResponseStatus>
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)
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)
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)
359 * Used to create a workflow response in success and failure cases.
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)
367 Boolean success = (Boolean) execution.getVariable("FH_success")
368 String out = success ? "Fallout Handler Succeeded" : "Fallout Handler Failed";
370 def falloutHandlerResponse = """
371 <workflow:FalloutHandlerResponse xmlns:workflow="http://org.openecomp/mso/workflow/schema/v1">
372 <workflow:out>${out}</workflow:out>
373 </workflow:FalloutHandlerResponse>
376 falloutHandlerResponse = utils.formatXml(falloutHandlerResponse)
377 utils.logAudit("FalloutHandler Response: " + falloutHandlerResponse);
379 execution.setVariable("FalloutHandlerResponse", falloutHandlerResponse)
380 execution.setVariable("WorkflowResponse", falloutHandlerResponse)
381 execution.setVariable("FH_ResponseCode", success ? "200" : "500")
382 setSuccessIndicator(execution, success)
384 logDebug("FalloutHandlerResponse =\n" + falloutHandlerResponse, isDebugLogEnabled)
385 } catch (Exception e) {
386 // Do NOT throw WorkflowException!
387 logError('Caught exception in ' + method, e)