[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / SDNCAdapter.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.camunda.bpm.engine.runtime.Execution
26 import org.openecomp.mso.bpmn.core.WorkflowException
27
28
29 // SDNC Adapter Request/Response processing
30
31 public class SDNCAdapter extends AbstractServiceTaskProcessor {
32
33         def Prefix="SDNCA_"
34         ExceptionUtil exceptionUtil = new ExceptionUtil()
35
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")
42                 try{
43
44                         utils.log("DEBUG", "=========== Begin PreProcess SDNCAdapterRequestScript  ===========", isDebugEnabled)
45                         utils.log("DEBUG", "Incoming sdncAdapterWorkflowRequest:\n" + execution.getVariable("sdncAdapterWorkflowRequest"), isDebugEnabled)
46
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)
53
54                         // Authorization Info
55                         String basicAuthValue = execution.getVariable("URN_mso_adapters_po_auth")
56                         utils.log("DEBUG", "Obtained BasicAuth userid password for sdnc adapter:" + basicAuthValue, isDebugEnabled)
57                         try {
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")
62                         }
63
64                         // TODO Use variables instead of passing xml request - Huh?
65
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)
71
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)
79                         }
80                         utils.log("DEBUG", "Callback URL to use:\n" + callbackUrlToUse, isDebugEnabled)
81                         requestHeader = requestHeader.replace(origCallbackUrl, callbackUrlToUse)
82
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")
99                         }
100                         utils.log("DEBUG", "service-type: " + sdnca_serviceType, isDebugEnabled)
101                         def serviceConfigActivate = false
102                         def source = ''
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')
107                                 }
108                         }
109                         execution.setVariable("serviceConfigActivate", serviceConfigActivate)
110                         utils.log("DEBUG", "serviceConfigActivate: " + serviceConfigActivate, isDebugEnabled)
111                         execution.setVariable("source", source)
112                         utils.log("DEBUG", "source: " + source, isDebugEnabled)
113
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)
117
118                         // Prepare SDNC Request to the SDNC Adapter
119                         String sdncAdapterRequest = """
120                         <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
121                         <SOAP-ENV:Body>
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>"""
125
126                         if (sdnca_svcInstanceId != null) {
127                                 sdncAdapterRequest += """
128                         <sdncadapter:SvcInstanceId>${sdnca_svcInstanceId}</sdncadapter:SvcInstanceId>"""
129                         }
130
131                         sdncAdapterRequest += """
132                         <sdncadapter:SvcAction>${sdnca_svcAction}</sdncadapter:SvcAction>
133                         <sdncadapter:SvcOperation>${sdnca_svcOperation}</sdncadapter:SvcOperation>
134                         <sdncadapter:CallbackUrl>${callbackUrlToUse}</sdncadapter:CallbackUrl>"""
135
136                         if (sdnca_msoAction != null) {
137                                 sdncAdapterRequest += """
138                         <sdncadapter:MsoAction>${sdnca_msoAction}</sdncadapter:MsoAction>"""
139                         }
140
141                         sdncAdapterRequest += """
142                         </sdncadapter:RequestHeader>
143                         <sdncadaptersc:RequestData>${sdncRequestData}</sdncadaptersc:RequestData></aetgt:SDNCAdapterRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>"""
144
145                         utils.logAudit("Outgoing SDNCAdapterRequest:\n" + sdncAdapterRequest)
146                         execution.setVariable("sdncAdapterRequest", sdncAdapterRequest)
147
148                         utils.log("DEBUG", execution.getVariable("sdncAdapterRequest"), isDebugEnabled)
149                         utils.log("DEBUG", execution.getVariable("URN_mso_adapters_sdnc_endpoint"), isDebugEnabled)
150                 }catch(Exception e){
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?
153                 }
154                 utils.log("DEBUG","=========== End pre Process SDNCRequestScript ===========", isDebugEnabled)
155         }
156
157         public void postProcessResponse (Execution execution) {
158
159                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
160                 try{
161                         utils.log("DEBUG","=========== Begin POSTProcess SDNCAdapter ===========", isDebugEnabled)
162                         utils.log("DEBUG","Incoming sdncAdapterCallbackRequest:\n" + execution.getVariable("sdncAdapterCallbackRequest"), isDebugEnabled)
163
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)
169                         
170                         if(sdnccallbackreq != null){
171                                 callbackHeader = utils.getNodeXml(sdnccallbackreq, "CallbackHeader")
172                                 callbackRequestData = utils.getNodeXml(sdnccallbackreq, "RequestData")
173
174                                 callbackHeader = callbackHeader.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "")
175                                 utils.log("DEBUG","SDNCCallbackHeader is:\n" + callbackHeader, isDebugEnabled)
176
177                                 callbackRequestData = callbackRequestData.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "")
178                                 utils.log("DEBUG","DECODED SDNCCallback RequestData is:\n" + callbackRequestData, isDebugEnabled)
179
180                                 String sdncAdapterWorkflowResponse ="""
181                                                 <sdncadapterworkflow:SDNCAdapterWorkflowResponse xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">
182                                                 <sdncadapterworkflow:response-data>
183                                                 ${callbackHeader}
184                                                 ${callbackRequestData}
185                                                 </sdncadapterworkflow:response-data>
186                                                 </sdncadapterworkflow:SDNCAdapterWorkflowResponse>"""
187
188
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)
195
196                                 // Check final indicator to determine if we are to continue listening or not
197                                 def String enhancedCallbackRequestData = callbackRequestData.replaceAll("&amp;", "&")
198                                 enhancedCallbackRequestData = enhancedCallbackRequestData.replaceAll("&lt;", "<")
199                                 enhancedCallbackRequestData = enhancedCallbackRequestData.replaceAll("&gt;", ">")
200                                 // replace the data with '&' (ex: subscriber-name= 'FOUR SEASONS HEATING & COOLING'
201                                 enhancedCallbackRequestData = enhancedCallbackRequestData.replace("&", "&amp;")
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
208                                         }
209                                 }
210                                 execution.setVariable("continueListening", continueListening)
211                                 utils.log("DEBUG", "Continue Listening: " + continueListening, isDebugEnabled)
212                         }else{
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)
217                         }
218                 }catch(Exception e){
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?
221                 }
222                 utils.log("DEBUG","=========== End POSTProcess SDNCAdapter ===========", isDebugEnabled)
223         }
224
225         public void callbackResponsecheck(Execution execution){
226
227                 def sdnccallbackreq=execution.getVariable("sdncAdapterCallbackRequest")
228                 utils.logAudit("sdncAdapterCallbackRequest :" + sdnccallbackreq)
229                 if (sdnccallbackreq==null){
230                         execution.setVariable("callbackResponseReceived",false);
231                 }else{
232                         execution.setVariable("callbackResponseReceived",true);
233                 }
234         }
235
236         public void resetCallbackRequest(Execution execution) {
237
238                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
239
240                 utils.log("DEBUG","=========== Begin Reset Callback Info SDNCAdapter ===========", isDebugEnabled)
241
242                 // Clear sdncAdapterCallbackRequest variable
243                 execution.removeVariable("sdncAdapterCallbackRequest")
244
245                 // Determine and set SDNC Timeout Value
246                 def enhancedCallbackRequestData = execution.getVariable("enhancedCallbackRequestData")
247                 utils.logAudit("sdncAdapter - enhancedCallbackRequestData :" + enhancedCallbackRequestData)
248                 def interim = false
249                 if (enhancedCallbackRequestData != null) {
250                         if (utils.nodeExists(enhancedCallbackRequestData, "ack-final-indicator")) {
251                                 if (utils.getNodeText(enhancedCallbackRequestData, "ack-final-indicator") == 'N') {
252                                         interim = true
253                                 }
254                         }
255                 }
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"
262                 }
263                 execution.setVariable("sdncTimeoutValue", timeoutValue)
264                 utils.log("DEBUG", "Setting SDNC Timeout Value to " + timeoutValue, isDebugEnabled)
265
266                 utils.log("DEBUG","=========== End Reset Callback Info SDNCAdapter ===========", isDebugEnabled)
267         }
268
269
270         public void prepareDBMessage(Execution execution) {
271
272                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
273
274                 utils.log("DEBUG","=========== Begin Prepare DB Message SDNCAdapter ===========", isDebugEnabled)
275
276                 // Create DB Message
277                 def dbRequestId = execution.getVariable("mso-request-id")
278                 String dbUpdateInterimStageCompletion = """
279                         <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
280                                 <SOAP-ENV:Body>
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>
286                                 </SOAP-ENV:Body>
287                         </SOAP-ENV:Envelope>
288                         """
289
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)
294         }
295
296
297
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());
302                 return utcTime;
303         }
304
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)
309         }
310
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")
315                 if(wf == null){
316                         exceptionUtil.buildWorkflowException(execution, 5000, "SDNCAdapter Encountered an Internal Error") // TODO: Not sure what message and error code we want here.....
317                 }else{
318                         execution.setVariable("WorkflowException", wf)
319                 }
320
321                 utils.log("DEBUG","Outgoing WorkflowException is: " + execution.getVariable("WorkflowException"), isDebugEnabled)
322                 utils.log("DEBUG","=========== End Assign Error ===========", isDebugEnabled)
323         }
324
325 }
326