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.camunda.bpm.engine.runtime.Execution
26 import org.openecomp.mso.bpmn.core.WorkflowException
29 // SDNC Adapter Request/Response processing
31 public class SDNCAdapter extends AbstractServiceTaskProcessor {
34 ExceptionUtil exceptionUtil = new ExceptionUtil()
36 // Script Task: Process SDNC Workflow Request
37 // Params: Workflow Execution
38 // Assume: Received SDNCAdapterWorkflowRequest is in variable 'sdncAdapterWorkflowRequest'
39 // Put created SDNCAdapterRequest in variable 'sdncAdapterRequest'
40 public void preProcessRequest (Execution execution) {
41 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
44 utils.log("DEBUG", "=========== Begin PreProcess SDNCAdapterRequestScript ===========", isDebugEnabled)
45 utils.log("DEBUG", "Incoming sdncAdapterWorkflowRequest:\n" + execution.getVariable("sdncAdapterWorkflowRequest"), isDebugEnabled)
47 // Initialize some variables used throughout the flow
48 execution.setVariable("prefix", Prefix)
49 execution.setVariable("sdncAdapterResponse", "")
50 execution.setVariable("asynchronousResponseTimeout", false)
51 execution.setVariable("continueListening", false)
52 execution.setVariable("SDNCA_SuccessIndicator", false)
55 String basicAuthValue = execution.getVariable("URN_mso_adapters_po_auth")
56 utils.log("DEBUG", "Obtained BasicAuth userid password for sdnc adapter:" + basicAuthValue, isDebugEnabled)
58 def encodedString = utils.getBasicAuth(basicAuthValue, execution.getVariable("URN_mso_msoKey"))
59 execution.setVariable("BasicAuthHeaderValue",encodedString)
60 } catch (IOException ex) {
61 utils.log("ERROR", "Unable to encode username password string")
64 // TODO Use variables instead of passing xml request - Huh?
66 // Get original RequestHeader
67 def sdncwfreq= execution.getVariable("sdncAdapterWorkflowRequest")
68 def requestHeader = utils.getNodeXml(sdncwfreq, "RequestHeader")
69 requestHeader = requestHeader.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "")
70 utils.log("DEBUG", "RequestHeader:\n" + requestHeader, isDebugEnabled)
72 // Set Callback URL to use from URN Mapping or jBoss Property
73 def origCallbackUrl = utils.getNodeText(requestHeader, "CallbackUrl")
74 def callbackUrlToUse = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
75 MsoUtils msoUtil = new MsoUtils()
76 def useQualifiedHostName = execution.getVariable("URN_mso_use_qualified_host")
77 if((useQualifiedHostName!=null) && (useQualifiedHostName.equals("true"))){
78 callbackUrlToUse = msoUtil.getQualifiedHostNameForCallback(callbackUrlToUse)
80 utils.log("DEBUG", "Callback URL to use:\n" + callbackUrlToUse, isDebugEnabled)
81 requestHeader = requestHeader.replace(origCallbackUrl, callbackUrlToUse)
83 // Get parameters from request header
84 def sdnca_svcInstanceId = utils.getNodeText1(requestHeader, "SvcInstanceId") // optional
85 utils.log("DEBUG", "SvcInstanceId: " + sdnca_svcInstanceId, isDebugEnabled)
86 def sdnca_msoAction = utils.getNodeText1(requestHeader, "MsoAction") // optional
87 utils.log("DEBUG", "MsoAction: " + sdnca_msoAction, isDebugEnabled)
88 def sdnca_svcAction = utils.getNodeText(requestHeader, "SvcAction")
89 utils.log("DEBUG", "SvcAction: " + sdnca_svcAction, isDebugEnabled)
90 def sdnca_svcOperation = utils.getNodeText(requestHeader, "SvcOperation")
91 utils.log("DEBUG", "SvcOperation: " + sdnca_svcOperation, isDebugEnabled)
92 def sdncRequestData = utils.getChildNodes(sdncwfreq, "SDNCRequestData")
93 sdncRequestData = sdncRequestData.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "")
94 sdncRequestData = sdncRequestData.replaceAll('tag0:', '').replaceAll(':tag0', '')
95 utils.log("DEBUG", "SDNCRequestData:\n" + sdncRequestData, isDebugEnabled)
96 def sdnca_serviceType = ""
97 if (utils.nodeExists(sdncwfreq, "service-type")) {
98 sdnca_serviceType = utils.getNodeText(sdncwfreq, "service-type")
100 utils.log("DEBUG", "service-type: " + sdnca_serviceType, isDebugEnabled)
101 def serviceConfigActivate = false
103 if ((sdnca_svcAction == 'activate') && (sdnca_svcOperation == 'service-configuration-operation') && (sdnca_serviceType == 'uCPE-VMS')) {
104 serviceConfigActivate = true
105 if (utils.nodeExists(sdncwfreq, 'source')) {
106 source = utils.getNodeText(sdncwfreq, 'source')
109 execution.setVariable("serviceConfigActivate", serviceConfigActivate)
110 utils.log("DEBUG", "serviceConfigActivate: " + serviceConfigActivate, isDebugEnabled)
111 execution.setVariable("source", source)
112 utils.log("DEBUG", "source: " + source, isDebugEnabled)
114 //calling process should pass a generated uuid if sending multiple sdnc requests
115 def requestId = utils.getNodeText(requestHeader, "RequestId")
116 execution.setVariable(Prefix + "requestId", requestId)
118 // Prepare SDNC Request to the SDNC Adapter
119 String sdncAdapterRequest = """
120 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
122 <aetgt:SDNCAdapterRequest xmlns:aetgt="http://org.openecomp/workflow/sdnc/adapter/schema/v1" xmlns:sdncadaptersc="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
123 <sdncadapter:RequestHeader xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
124 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>"""
126 if (sdnca_svcInstanceId != null) {
127 sdncAdapterRequest += """
128 <sdncadapter:SvcInstanceId>${sdnca_svcInstanceId}</sdncadapter:SvcInstanceId>"""
131 sdncAdapterRequest += """
132 <sdncadapter:SvcAction>${sdnca_svcAction}</sdncadapter:SvcAction>
133 <sdncadapter:SvcOperation>${sdnca_svcOperation}</sdncadapter:SvcOperation>
134 <sdncadapter:CallbackUrl>${callbackUrlToUse}</sdncadapter:CallbackUrl>"""
136 if (sdnca_msoAction != null) {
137 sdncAdapterRequest += """
138 <sdncadapter:MsoAction>${sdnca_msoAction}</sdncadapter:MsoAction>"""
141 sdncAdapterRequest += """
142 </sdncadapter:RequestHeader>
143 <sdncadaptersc:RequestData>${sdncRequestData}</sdncadaptersc:RequestData></aetgt:SDNCAdapterRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>"""
145 utils.logAudit("Outgoing SDNCAdapterRequest:\n" + sdncAdapterRequest)
146 execution.setVariable("sdncAdapterRequest", sdncAdapterRequest)
148 utils.log("DEBUG", execution.getVariable("sdncAdapterRequest"), isDebugEnabled)
149 utils.log("DEBUG", execution.getVariable("URN_mso_adapters_sdnc_endpoint"), isDebugEnabled)
151 utils.log("DEBUG", 'Internal Error occured during PreProcess Method: ' + e, isDebugEnabled)
152 exceptionUtil.buildAndThrowWorkflowException(execution, 9999, 'Internal Error occured during PreProcess Method') // TODO: what message and error code?
154 utils.log("DEBUG","=========== End pre Process SDNCRequestScript ===========", isDebugEnabled)
157 public void postProcessResponse (Execution execution) {
159 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
161 utils.log("DEBUG","=========== Begin POSTProcess SDNCAdapter ===========", isDebugEnabled)
162 utils.log("DEBUG","Incoming sdncAdapterCallbackRequest:\n" + execution.getVariable("sdncAdapterCallbackRequest"), isDebugEnabled)
164 // Check the sdnccallback request and get the responsecode
165 def sdnccallbackreq = execution.getVariable("sdncAdapterCallbackRequest")
166 def callbackRequestData = ""
167 def callbackHeader = ""
168 utils.logAudit("SDNCAdapterCallback Request :" + sdnccallbackreq)
170 if(sdnccallbackreq != null){
171 callbackHeader = utils.getNodeXml(sdnccallbackreq, "CallbackHeader")
172 callbackRequestData = utils.getNodeXml(sdnccallbackreq, "RequestData")
174 callbackHeader = callbackHeader.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "")
175 utils.log("DEBUG","SDNCCallbackHeader is:\n" + callbackHeader, isDebugEnabled)
177 callbackRequestData = callbackRequestData.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "")
178 utils.log("DEBUG","DECODED SDNCCallback RequestData is:\n" + callbackRequestData, isDebugEnabled)
180 String sdncAdapterWorkflowResponse ="""
181 <sdncadapterworkflow:SDNCAdapterWorkflowResponse xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">
182 <sdncadapterworkflow:response-data>
184 ${callbackRequestData}
185 </sdncadapterworkflow:response-data>
186 </sdncadapterworkflow:SDNCAdapterWorkflowResponse>"""
189 utils.log("DEBUG","Outgoing sdncAdapterWorkflowResponse:\n" + sdncAdapterWorkflowResponse, isDebugEnabled)
190 sdncAdapterWorkflowResponse = utils.formatXml(sdncAdapterWorkflowResponse)
191 utils.logAudit("sdncAdapterWorkflowResponse :" + sdncAdapterWorkflowResponse)
192 execution.setVariable("sdncAdapterResponse", sdncAdapterWorkflowResponse)
193 // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
194 execution.setVariable("WorkflowResponse", sdncAdapterWorkflowResponse)
196 // Check final indicator to determine if we are to continue listening or not
197 def String enhancedCallbackRequestData = callbackRequestData.replaceAll("&", "&")
198 enhancedCallbackRequestData = enhancedCallbackRequestData.replaceAll("<", "<")
199 enhancedCallbackRequestData = enhancedCallbackRequestData.replaceAll(">", ">")
200 // replace the data with '&' (ex: subscriber-name= 'FOUR SEASONS HEATING & COOLING'
201 enhancedCallbackRequestData = enhancedCallbackRequestData.replace("&", "&")
202 utils.log("DEBUG","EnhancedCallbackRequestData:\n" + enhancedCallbackRequestData, isDebugEnabled)
203 execution.setVariable("enhancedCallbackRequestData", enhancedCallbackRequestData)
204 def continueListening = false
205 if (utils.nodeExists(enhancedCallbackRequestData, "ack-final-indicator")) {
206 if (utils.getNodeText(enhancedCallbackRequestData, "ack-final-indicator") == 'N') {
207 continueListening = true
210 execution.setVariable("continueListening", continueListening)
211 utils.log("DEBUG", "Continue Listening: " + continueListening, isDebugEnabled)
213 // Timed out waiting for asynchronous message, build error response
214 exceptionUtil.buildWorkflowException(execution, 500, "SDNC Callback Timeout Error")
215 execution.setVariable("asynchronousResponseTimeout", true)
216 utils.log("DEBUG", "Timed out waiting for asynchronous message", isDebugEnabled)
219 utils.log("DEBUG", 'Internal Error occured during PostProcess Method: ' + e, isDebugEnabled)
220 exceptionUtil.buildAndThrowWorkflowException(execution, 9999, 'Internal Error occured during PostProcess Method') // TODO: what message and error code?
222 utils.log("DEBUG","=========== End POSTProcess SDNCAdapter ===========", isDebugEnabled)
225 public void callbackResponsecheck(Execution execution){
227 def sdnccallbackreq=execution.getVariable("sdncAdapterCallbackRequest")
228 utils.logAudit("sdncAdapterCallbackRequest :" + sdnccallbackreq)
229 if (sdnccallbackreq==null){
230 execution.setVariable("callbackResponseReceived",false);
232 execution.setVariable("callbackResponseReceived",true);
236 public void resetCallbackRequest(Execution execution) {
238 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
240 utils.log("DEBUG","=========== Begin Reset Callback Info SDNCAdapter ===========", isDebugEnabled)
242 // Clear sdncAdapterCallbackRequest variable
243 execution.removeVariable("sdncAdapterCallbackRequest")
245 // Determine and set SDNC Timeout Value
246 def enhancedCallbackRequestData = execution.getVariable("enhancedCallbackRequestData")
247 utils.logAudit("sdncAdapter - enhancedCallbackRequestData :" + enhancedCallbackRequestData)
249 if (enhancedCallbackRequestData != null) {
250 if (utils.nodeExists(enhancedCallbackRequestData, "ack-final-indicator")) {
251 if (utils.getNodeText(enhancedCallbackRequestData, "ack-final-indicator") == 'N') {
256 def timeoutValue = execution.getVariable("URN_mso_sdnc_timeout")
257 def sdncAdapterWorkflowRequest = execution.getVariable("sdncAdapterWorkflowRequest")
258 if (interim && utils.nodeExists(sdncAdapterWorkflowRequest, "InterimSDNCTimeOutValueInHours")) {
259 timeoutValue = "PT" + utils.getNodeText(sdncAdapterWorkflowRequest, "InterimSDNCTimeOutValueInHours") + "H"
260 } else if (utils.nodeExists(sdncAdapterWorkflowRequest, "SDNCTimeOutValueInMinutes")) {
261 timeoutValue = "PT" + utils.getNodeText(sdncAdapterWorkflowRequest, "SDNCTimeOutValueInMinutes") + "M"
263 execution.setVariable("sdncTimeoutValue", timeoutValue)
264 utils.log("DEBUG", "Setting SDNC Timeout Value to " + timeoutValue, isDebugEnabled)
266 utils.log("DEBUG","=========== End Reset Callback Info SDNCAdapter ===========", isDebugEnabled)
270 public void prepareDBMessage(Execution execution) {
272 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
274 utils.log("DEBUG","=========== Begin Prepare DB Message SDNCAdapter ===========", isDebugEnabled)
277 def dbRequestId = execution.getVariable("mso-request-id")
278 String dbUpdateInterimStageCompletion = """
279 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
281 <DBAdapter:updateInterimStageCompletion xmlns:DBAdapter="http://org.openecomp.mso/requestsdb">
282 <requestId>${dbRequestId}</requestId>
283 <interimStageCompletion>1</interimStageCompletion>
284 <lastModifiedBy>BPEL</lastModifiedBy>
285 </DBAdapter:updateInterimStageCompletion>
290 execution.setVariable("dbUpdateInterimStageCompletion", dbUpdateInterimStageCompletion)
291 utils.logAudit("sdncAdapter - dbUpdateInterimStageCompletion :" + dbUpdateInterimStageCompletion)
292 utils.log("DEBUG","DB UpdateInterimStageCompletion:\n" + dbUpdateInterimStageCompletion, isDebugEnabled)
293 utils.log("DEBUG","=========== End Prepare DB Message SDNCAdapter ===========", isDebugEnabled)
298 public String generateCurrentTimeInUtc(){
299 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
300 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
301 final String utcTime = sdf.format(new Date());
305 public void toggleSuccessIndicator(Execution execution){
306 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
307 execution.setVariable("SDNCA_SuccessIndicator", true)
308 utils.log("DEBUG","Setting SDNCA Success Indicator to True", isDebugEnabled)
311 public void assignError(Execution execution){
312 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
313 utils.log("DEBUG","=========== Started Assign Error ===========", isDebugEnabled)
314 WorkflowException wf = execution.getVariable("WorkflowException")
316 exceptionUtil.buildWorkflowException(execution, 5000, "SDNCAdapter Encountered an Internal Error") // TODO: Not sure what message and error code we want here.....
318 execution.setVariable("WorkflowException", wf)
321 utils.log("DEBUG","Outgoing WorkflowException is: " + execution.getVariable("WorkflowException"), isDebugEnabled)
322 utils.log("DEBUG","=========== End Assign Error ===========", isDebugEnabled)