7feafcc421a2ab54a60de0387981c842c2b9b7a9
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoUpdateNetworkInstance.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 package org.openecomp.mso.bpmn.infrastructure.scripts;
21
22 import groovy.xml.XmlUtil
23 import groovy.json.*
24 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
25 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
26 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
27 import org.openecomp.mso.bpmn.common.scripts.NetworkUtils
28 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
29 import org.openecomp.mso.bpmn.common.scripts.VidUtils
30 import org.openecomp.mso.bpmn.core.WorkflowException
31 import org.openecomp.mso.bpmn.core.json.JsonUtils
32 import org.openecomp.mso.rest.APIResponse
33
34 import java.util.UUID;
35
36 import org.camunda.bpm.engine.delegate.BpmnError
37 import org.camunda.bpm.engine.runtime.Execution
38 import org.apache.commons.lang3.*
39 import org.apache.commons.codec.binary.Base64;
40 import org.springframework.web.util.UriUtils
41
42 /**
43  * This groovy class supports the <class>DoUpdateNetworkInstance.bpmn</class> process.
44  *
45  */
46 public class DoUpdateNetworkInstance extends AbstractServiceTaskProcessor {
47         String Prefix="UPDNETI_"
48         ExceptionUtil exceptionUtil = new ExceptionUtil()
49         JsonUtils jsonUtil = new JsonUtils()
50         VidUtils vidUtils = new VidUtils(this)
51         NetworkUtils networkUtils = new NetworkUtils()
52         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
53
54         /**
55          * This method is executed during the preProcessRequest task of the <class>DoUpdateNetworkInstance.bpmn</class> process.
56          * @param execution
57          */
58         public InitializeProcessVariables(Execution execution){
59                 /* Initialize all the process variables in this block */
60
61                 execution.setVariable(Prefix + "messageId", "")
62                 execution.setVariable("BasicAuthHeaderValuePO", "")
63                 execution.setVariable("BasicAuthHeaderValueSDNC", "")
64                 execution.setVariable(Prefix + "networkRequest", "")
65                 execution.setVariable(Prefix + "networkInputs", "")
66                 execution.setVariable(Prefix + "networkOutputs", "")
67                 execution.setVariable(Prefix + "requestId", "")
68                 execution.setVariable(Prefix + "source", "")
69                 execution.setVariable(Prefix + "networkId", "")
70
71                 execution.setVariable(Prefix + "isPONR", false)    // Point-of-no-return, means, rollback is not needed
72
73                 // AAI query Cloud Region
74                 execution.setVariable(Prefix + "queryCloudRegionRequest","")
75                 execution.setVariable(Prefix + "queryCloudRegionReturnCode","")
76                 execution.setVariable(Prefix + "queryCloudRegionResponse","")
77                 execution.setVariable(Prefix + "cloudRegionPo","")
78                 execution.setVariable(Prefix + "cloudRegionSdnc","")
79                 execution.setVariable(Prefix + "isCloudRegionGood", false)
80
81                 // AAI query Id
82                 execution.setVariable(Prefix + "queryIdAAIRequest","")
83                 execution.setVariable(Prefix + "queryIdAAIResponse", "")
84                 execution.setVariable(Prefix + "aaiIdReturnCode", "")
85
86                 // AAI query vpn binding
87                 execution.setVariable(Prefix + "queryVpnBindingAAIRequest","")
88                 execution.setVariable(Prefix + "queryVpnBindingAAIResponse", "")
89                 execution.setVariable(Prefix + "aaiQqueryVpnBindingReturnCode", "")
90                 execution.setVariable(Prefix + "vpnBindings", null)
91                 execution.setVariable(Prefix + "vpnCount", 0)
92                 execution.setVariable(Prefix + "routeCollection", "")
93
94                 // AAI query network policy
95                 execution.setVariable(Prefix + "queryNetworkPolicyAAIRequest","")
96                 execution.setVariable(Prefix + "queryNetworkPolicyAAIResponse", "")
97                 execution.setVariable(Prefix + "aaiQqueryNetworkPolicyReturnCode", "")
98                 execution.setVariable(Prefix + "networkPolicyUriList", null)
99                 execution.setVariable(Prefix + "networkPolicyCount", 0)
100                 execution.setVariable(Prefix + "networkCollection", "")
101
102                 // AAI query route table reference
103                 execution.setVariable(Prefix + "queryNetworkTableRefAAIRequest","")
104                 execution.setVariable(Prefix + "queryNetworkTableRefAAIResponse", "")
105                 execution.setVariable(Prefix + "aaiQqueryNetworkTableRefReturnCode", "")
106                 execution.setVariable(Prefix + "networkTableRefUriList", null)
107                 execution.setVariable(Prefix + "networkTableRefCount", 0)
108                 execution.setVariable(Prefix + "tableRefCollection", "")
109                 
110                 // AAI requery Id
111                 execution.setVariable(Prefix + "requeryIdAAIRequest","")
112                 execution.setVariable(Prefix + "requeryIdAAIResponse", "")
113                 execution.setVariable(Prefix + "aaiRequeryIdReturnCode", "")
114
115                 // AAI update contrail
116                 execution.setVariable(Prefix + "updateContrailAAIUrlRequest","")
117                 execution.setVariable(Prefix + "updateContrailAAIPayloadRequest","")
118                 execution.setVariable(Prefix + "updateContrailAAIResponse", "")
119                 execution.setVariable(Prefix + "aaiUpdateContrailReturnCode", "")
120
121                 execution.setVariable(Prefix + "updateNetworkRequest", "")
122                 execution.setVariable(Prefix + "updateNetworkResponse", "")
123                 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
124                 execution.setVariable(Prefix + "networkReturnCode", "")
125                 execution.setVariable(Prefix + "isNetworkRollbackNeeded", false)
126
127                 execution.setVariable(Prefix + "changeAssignSDNCRequest", "")
128                 execution.setVariable(Prefix + "changeAssignSDNCResponse", "")
129                 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
130                 execution.setVariable(Prefix + "sdncReturnCode", "")
131                 execution.setVariable(Prefix + "isSdncRollbackNeeded", false)
132                 execution.setVariable(Prefix + "sdncResponseSuccess", false)
133
134                 execution.setVariable(Prefix + "isVnfBindingPresent", false)
135                 execution.setVariable(Prefix + "Success", false)
136                 execution.setVariable(Prefix + "serviceInstanceId", "")
137                 
138                 execution.setVariable(Prefix + "isException", false)
139                 
140         }
141
142         // **************************************************
143         //     Pre or Prepare Request Section
144         // **************************************************
145         /**
146          * This method is executed during the preProcessRequest task of the <class>DoUpdateNetworkInstance.bpmn</class> process.
147          * @param execution
148          */
149         public void preProcessRequest (Execution execution) {
150                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
151                 execution.setVariable("prefix",Prefix)
152
153                 utils.log("DEBUG", " ***** Inside preProcessRequest DoUpdateNetworkInstance Request ***** ", isDebugEnabled)
154
155                 try {
156                         // initialize flow variables
157                         InitializeProcessVariables(execution)
158         
159                         // GET Incoming request & validate 3 kinds of format.
160                         execution.setVariable("action", "UPDATE")
161                         String networkRequest = execution.getVariable("bpmnRequest")
162                         if (networkRequest != null) {
163                                 if (networkRequest.contains("requestDetails")) {
164                                         // JSON format request is sent, create xml
165                                         try {
166                                                 def prettyJson = JsonOutput.prettyPrint(networkRequest.toString())
167                                                 utils.log("DEBUG", " Incoming message formatted . . . : " + '\n' + prettyJson, isDebugEnabled)
168                                                 networkRequest =  vidUtils.createXmlNetworkRequestInfra(execution, networkRequest)
169                 
170                                         } catch (Exception ex) {
171                                                 String dataErrorMessage = " Invalid json format Request - " + ex.getMessage()
172                                                 utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
173                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
174                                         }
175                                 } else {
176                                          // XML format request is sent
177                                    
178                                 }
179                         } else {
180                                 // vIPR format request is sent, create xml from individual variables
181                                 networkRequest = vidUtils.createXmlNetworkRequestInstance(execution)
182                         }
183                         
184                         networkRequest = utils.formatXml(networkRequest)
185                         utils.logAudit(networkRequest)
186                         execution.setVariable(Prefix + "networkRequest", networkRequest)
187                         utils.log("DEBUG", " network-request - " + '\n' + networkRequest, isDebugEnabled)
188                 
189                         // validate 'disableRollback'  (aka, 'suppressRollback')
190                         boolean rollbackEnabled = networkUtils.isRollbackEnabled(execution, networkRequest)
191                         execution.setVariable(Prefix + "rollbackEnabled", rollbackEnabled)
192                         utils.log("DEBUG", Prefix + "rollbackEnabled - " + rollbackEnabled, isDebugEnabled)
193                                                                                 
194                         String networkInputs = utils.getNodeXml(networkRequest, "network-inputs", false).replace("tag0:","").replace(":tag0","")
195                         execution.setVariable(Prefix + "networkInputs", networkInputs)
196                         utils.log("DEBUG", Prefix + "networkInputs - " + '\n' + networkInputs, isDebugEnabled)
197                         
198                         // prepare messageId
199                         String messageId = execution.getVariable(Prefix + "messageId")  // for testing
200                         if (messageId == null || messageId == "") {
201                                 messageId = UUID.randomUUID()
202                                 utils.log("DEBUG", " UPDNETI_messageId, random generated: " + messageId, isDebugEnabled)
203                         } else {
204                                 utils.log("DEBUG", " UPDNETI_messageId, pre-assigned: " + messageId, isDebugEnabled)
205                         }
206                         execution.setVariable(Prefix + "messageId", messageId)
207                         
208                         String source = utils.getNodeText1(networkRequest, "source")
209                         execution.setVariable(Prefix + "source", source)
210                         utils.log("DEBUG", Prefix + "source - " + source, isDebugEnabled)
211                         
212                         String networkId = ""
213                         if (utils.nodeExists(networkRequest, "network-id")) {
214                                 networkId = utils.getNodeText1(networkRequest, "network-id")
215                                 if (networkId == 'null' || networkId == "") {
216                                         sendSyncError(execution)
217                                         // missing value of networkId
218                                         String dataErrorMessage = "Variable 'network-id' value/element is missing."
219                                         utils.log("DEBUG", " Invalid Request - " + dataErrorMessage, isDebugEnabled)
220                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
221         
222                                 }
223                         }
224         
225                         String lcpCloudRegion = ""
226                         if (utils.nodeExists(networkRequest, "aic-cloud-region")) {
227                                 lcpCloudRegion = utils.getNodeText1(networkRequest, "aic-cloud-region")
228                                 if ((lcpCloudRegion == 'null') || (lcpCloudRegion == "")) {
229                                         sendSyncError(execution)
230                                         String dataErrorMessage = "requestDetails has missing 'aic-cloud-region' value/element."
231                                         utils.log("DEBUG", " Invalid Request - " + dataErrorMessage, isDebugEnabled)
232                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
233                                 }
234                         }
235         
236                         String serviceInstanceId = ""
237                         if (utils.nodeExists(networkRequest, "service-instance-id")) {
238                                 serviceInstanceId = utils.getNodeText1(networkRequest, "service-instance-id")
239                                 if ((serviceInstanceId == 'null') || (lcpCloudRegion == "")) {
240                                         sendSyncError(execution)
241                                         String dataErrorMessage = "Variable 'serviceInstanceId' value/element is missing."
242                                         utils.log("DEBUG", " Invalid Request - " + dataErrorMessage, isDebugEnabled)
243                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
244                                 }
245                         }
246                         
247                         // PO Authorization Info / headers Authorization=
248                         String basicAuthValuePO = execution.getVariable("URN_mso_adapters_po_auth")
249                         utils.log("DEBUG", " Obtained BasicAuth userid password for PO/SDNC adapter: " + basicAuthValuePO, isDebugEnabled)
250                         try {
251                                 def encodedString = utils.getBasicAuth(basicAuthValuePO, execution.getVariable("URN_mso_msoKey"))
252                                 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
253                                 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
254         
255                         } catch (IOException ex) {
256                                 String exceptionMessage = "Exception Encountered in DoUpdateNetworkInstance, PreProcessRequest() - "
257                                 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
258                                 utils.log("DEBUG", dataErrorMessage, , isDebugEnabled)
259                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
260                         }
261                         
262                         // Set variables for Generic Get Sub Flow use
263                         execution.setVariable(Prefix + "serviceInstanceId", serviceInstanceId)
264                         utils.log("DEBUG", Prefix + "serviceInstanceId - " + serviceInstanceId, isDebugEnabled)
265         
266                         execution.setVariable("GENGS_type", "service-instance")
267                         utils.log("DEBUG", "GENGS_type - " + "service-instance", isDebugEnabled)
268                         utils.log("DEBUG", " Url for SDNC adapter: " + execution.getVariable("URN_mso_adapters_sdnc_endpoint"), isDebugEnabled)
269                         
270                         String sdncVersion = execution.getVariable("sdncVersion")
271                         utils.log("DEBUG", "sdncVersion? : " + sdncVersion, isDebugEnabled)
272                         
273                         // build 'networkOutputs'                       
274                         networkId = utils.getNodeText1(networkRequest, "network-id")
275                         if ((networkId == null) || (networkId == "null")) {
276                                 networkId = ""
277                         }
278                         String networkName = utils.getNodeText1(networkRequest, "network-name")
279                         if ((networkName == null) || (networkName == "null")) {
280                                 networkName = ""
281                         }
282                         String networkOutputs =
283                            """<network-outputs>
284                            <network-id>${networkId}</network-id>
285                            <network-name>${networkName}</network-name>
286                          </network-outputs>"""
287                         execution.setVariable(Prefix + "networkOutputs", networkOutputs)
288                         utils.log("DEBUG", Prefix + "networkOutputs - " + '\n' + networkOutputs, isDebugEnabled)
289                         execution.setVariable(Prefix + "networkId", networkId)
290                         execution.setVariable(Prefix + "networkName", networkName)
291                 
292
293                 } catch (BpmnError e) {
294                         throw e;
295
296                 } catch (Exception ex){
297                         sendSyncError(execution)
298                          // caught exception
299                         String exceptionMessage = "Exception Encountered in DoUpdateNetworkInstance, PreProcessRequest() - " + ex.getMessage()
300                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
301                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
302
303                 }
304         }
305
306         public void callRESTQueryAAICloudRegion (Execution execution) {
307                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
308                 execution.setVariable("prefix", Prefix)
309
310                 utils.log("DEBUG", " ***** Inside callRESTQueryAAICloudRegion of DoUpdateNetworkInstance ***** " , isDebugEnabled)
311
312                 try {
313                         String networkInputs  = execution.getVariable(Prefix + "networkInputs")
314                         String cloudRegion = utils.getNodeText1(networkInputs, "aic-cloud-region")
315                         cloudRegion = UriUtils.encode(cloudRegion,"UTF-8")
316
317                         // Prepare AA&I url
318                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
319                         AaiUtil aaiUtil = new AaiUtil(this)
320                         String aai_uri = aaiUtil.getCloudInfrastructureCloudRegionUri(execution)
321                         String queryCloudRegionRequest = "${aai_endpoint}${aai_uri}/" + cloudRegion
322                         utils.logAudit(queryCloudRegionRequest)
323                         execution.setVariable(Prefix + "queryCloudRegionRequest", queryCloudRegionRequest)
324                         utils.log("DEBUG", " UPDNETI_queryCloudRegionRequest - " + "\n" + queryCloudRegionRequest, isDebugEnabled)
325
326                         String cloudRegionPo = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
327                         String cloudRegionSdnc = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "SDNC", cloudRegion)
328
329                         if ((cloudRegionPo != "ERROR") && (cloudRegionSdnc != "ERROR")) {
330                                 execution.setVariable(Prefix + "cloudRegionPo", cloudRegionPo)
331                                 execution.setVariable(Prefix + "cloudRegionSdnc", cloudRegionSdnc)
332                                 execution.setVariable(Prefix + "isCloudRegionGood", true)
333
334                         } else {
335                             String dataErrorMessage = "QueryAAICloudRegion Unsuccessful. Return Code: " + execution.getVariable(Prefix + "queryCloudRegionReturnCode")
336                             utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
337                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
338
339                         }
340
341                         utils.log("DEBUG", " is Cloud Region Good: " + execution.getVariable(Prefix + "isCloudRegionGood"), isDebugEnabled)
342
343                 } catch (BpmnError e) {
344                         throw e;
345
346                 } catch (Exception ex) {
347                         // try error
348                         String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow - callRESTQueryAAICloudRegion() - " + ex.getMessage()
349                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
350                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
351
352                 }
353
354         }
355
356         public void callRESTQueryAAINetworkId(Execution execution) {
357                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
358                 execution.setVariable("prefix", Prefix)
359
360                 utils.log("DEBUG", " ***** Inside callRESTQueryAAINetworkId of DoUpdateNetworkInstance ***** " , isDebugEnabled)
361
362                 try {
363                         // get variables
364                         String networkRequest = execution.getVariable(Prefix + "networkRequest")
365                         String networkId   = utils.getNodeText1(networkRequest, "network-id")
366                         networkId = UriUtils.encode(networkId,"UTF-8")
367                         execution.setVariable(Prefix + "networkId", networkId)
368
369                         // Prepare AA&I url
370                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
371                         AaiUtil aaiUriUtil = new AaiUtil(this)
372                         String aai_uri = aaiUriUtil.getNetworkL3NetworkUri(execution)
373                         String queryIdAAIRequest = "${aai_endpoint}${aai_uri}/" + networkId + "?depth=1"
374                         utils.logAudit(queryIdAAIRequest)
375                         execution.setVariable(Prefix + "queryIdAAIRequest", queryIdAAIRequest)
376                         utils.log("DEBUG", Prefix + "queryIdAAIRequest - " + "\n" + queryIdAAIRequest, isDebugEnabled)
377
378                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, queryIdAAIRequest)
379                         String returnCode = response.getStatusCode()
380                         execution.setVariable(Prefix + "aaiIdReturnCode", returnCode)
381                         utils.log("DEBUG", " ***** AAI Response Code  : " + returnCode, isDebugEnabled)
382                         
383                         String aaiResponseAsString = response.getResponseBodyAsString()
384
385                         if (returnCode=='200') {
386                                 utils.logAudit(aaiResponseAsString)
387                                 execution.setVariable(Prefix + "queryIdAAIResponse", aaiResponseAsString)
388                                 utils.log("DEBUG", " QueryAAINetworkId Success REST Response - " + "\n" + aaiResponseAsString, isDebugEnabled)
389
390                         } else {
391                                 if (returnCode=='404') {
392                                         String dataErrorMessage = "Response Error from QueryAAINetworkId is 404 (Not Found)."
393                                         utils.log("DEBUG", " AAI Query Failed. " + dataErrorMessage, isDebugEnabled)
394                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
395
396                                 } else {
397                                    if (aaiResponseAsString.contains("RESTFault")) {
398                                            WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
399                                            execution.setVariable("WorkflowException", exceptionObject)
400                                            throw new BpmnError("MSOWorkflowException")
401
402                                    } else {
403                                                         // aai all errors
404                                                         String dataErrorMessage = "Unexpected Response from QueryAAINetworkId - " + returnCode
405                                                         utils.log("DEBUG", "Unexpected Response from QueryAAINetworkId - " + dataErrorMessage, isDebugEnabled)
406                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
407
408                                   }
409                                 }
410                         }
411
412                 } catch (BpmnError e) {
413                         throw e;
414
415                 } catch (Exception ex) {
416                         String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow. callRESTQueryAAINetworkId() - " + ex.getMessage()
417                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
418                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
419
420                 }
421
422         }
423
424         public void callRESTReQueryAAINetworkId(Execution execution) {
425                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
426                 execution.setVariable("prefix", Prefix)
427
428                 utils.log("DEBUG", " ***** Inside callRESTReQueryAAINetworkId of DoUpdateNetworkInstance ***** " , isDebugEnabled)
429
430                 try {
431                         // get variables
432                         String networkRequest = execution.getVariable(Prefix + "networkRequest")
433                         String networkId   = utils.getNodeText1(networkRequest, "network-id")
434                         networkId = UriUtils.encode(networkId,"UTF-8")
435
436                         // Prepare AA&I url
437                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
438                         AaiUtil aaiUriUtil = new AaiUtil(this)
439                         String aai_uri = aaiUriUtil.getNetworkL3NetworkUri(execution)
440                         String requeryIdAAIRequest = "${aai_endpoint}${aai_uri}/" + networkId + "?depth=1"
441                         utils.logAudit(requeryIdAAIRequest)
442                         execution.setVariable(Prefix + "requeryIdAAIRequest", requeryIdAAIRequest)
443                         utils.log("DEBUG", " UPDNETI_requeryIdAAIRequest - " + "\n" + requeryIdAAIRequest, isDebugEnabled)
444
445                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, requeryIdAAIRequest)
446                         String returnCode = response.getStatusCode()
447                         execution.setVariable(Prefix + "aaiRequeryIdReturnCode", returnCode)
448                         utils.log("DEBUG", " ***** AAI ReQuery Response Code  : " + returnCode, isDebugEnabled)
449
450                         String aaiResponseAsString = response.getResponseBodyAsString()
451
452                         if (returnCode=='200') {
453                                 utils.logAudit(aaiResponseAsString)
454                                 execution.setVariable(Prefix + "requeryIdAAIResponse", aaiResponseAsString)
455                                 utils.log("DEBUG", " ReQueryAAINetworkId Success REST Response - " + "\n" + aaiResponseAsString, isDebugEnabled)
456
457                                 String netId = utils.getNodeText1(aaiResponseAsString, "network-id")
458                                 String netName = utils.getNodeText1(aaiResponseAsString, "network-name")
459                                 String networkOutputs =
460                                    """<network-outputs>
461                    <network-id>${netId}</network-id>                    
462                    <network-name>${netName}</network-name>
463                  </network-outputs>"""
464                                 execution.setVariable(Prefix + "networkOutputs", networkOutputs)
465                                 utils.log("DEBUG", " networkOutputs - " + '\n' + networkOutputs, isDebugEnabled)
466                                 
467                         } else {
468                                 if (returnCode=='404') {
469                                         String dataErrorMessage = "Response Error from ReQueryAAINetworkId is 404 (Not Found)."
470                                         utils.log("DEBUG", " AAI ReQuery Failed. - " + dataErrorMessage, isDebugEnabled)
471                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
472
473                                 } else {
474                                    if (aaiResponseAsString.contains("RESTFault")) {
475                                            WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
476                                            execution.setVariable("WorkflowException", exceptionObject)
477                                            throw new BpmnError("MSOWorkflowException")
478
479                                            } else {
480                                                         // aai all errors
481                                                         String dataErrorMessage = "Unexpected Response from ReQueryAAINetworkId - " + returnCode
482                                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
483                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
484
485                                         }
486                                 }
487                         }
488
489                 } catch (BpmnError e) {
490                         throw e;
491
492                 } catch (Exception ex) {
493                         String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow. callRESTReQueryAAINetworkId() - " + ex.getMessage()
494                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
495                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
496
497                 }
498
499         }
500
501         public void callRESTQueryAAINetworkVpnBinding(Execution execution) {
502                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
503                 execution.setVariable("prefix", Prefix)
504
505                 utils.log("DEBUG", " ***** Inside callRESTQueryAAINetworkVpnBinding of DoUpdateNetworkInstance ***** " , isDebugEnabled)
506
507                 try {
508
509                         // get variables
510                         String queryIdAAIResponse   = execution.getVariable(Prefix + "requeryIdAAIResponse").replace('<?xml version="1.0" encoding="UTF-8"?>', "")
511                         String relationship = networkUtils.getFirstNodeXml(queryIdAAIResponse, "relationship-list").trim().replace("tag0:","").replace(":tag0","")
512                         utils.log("DEBUG", " relationship - " + relationship, isDebugEnabled)
513
514                         // Check if Vnf Binding is present, then build a List of vnfBinding
515                         List vpnBindingUri = networkUtils.getVnfBindingObject(relationship)
516                         int vpnCount = vpnBindingUri.size()
517                         execution.setVariable(Prefix + "vpnCount", vpnCount)
518                         utils.log("DEBUG", " UPDNETI_vpnCount - " + vpnCount, isDebugEnabled)
519
520                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
521                         AaiUtil aaiUriUtil = new AaiUtil(this)
522
523                         if (vpnCount > 0) {
524                                 execution.setVariable(Prefix + "vpnBindings", vpnBindingUri)
525                                 utils.log("DEBUG", " vpnBindingUri List - " + vpnBindingUri, isDebugEnabled)
526
527                                 String routeTargets = ""
528                                 // AII loop call using list vpnBindings
529                                 for (i in 0..vpnBindingUri.size()-1) {
530
531                                         int counting = i+1
532
533                                         // prepare url using vpnBinding
534                                         String queryVpnBindingAAIRequest = ""
535                                         String aai_uri = aaiUriUtil.getNetworkVpnBindingUri(execution)
536
537                                         // Note: By default, the vpnBinding url is found in 'related-link' of the response,
538                                         //       so, the default in URN mappings for this is set to "" (ie, space), unless forced to use the URN mapping.
539                                         if (aai_uri == null || aai_uri == "") {
540                                                 // using value of 'related-link' from response
541                                                 if (vpnBindingUri[i].charAt(vpnBindingUri[i].length()-1) == '/') {
542                                                     queryVpnBindingAAIRequest = "${aai_endpoint}" + vpnBindingUri[i].substring(0, vpnBindingUri[i].length()-1) + "?depth=all"
543                                                 } else {
544                                                     queryVpnBindingAAIRequest = "${aai_endpoint}" + vpnBindingUri[i] + "?depth=all"
545                                                 }
546
547                                         } else {
548                                             // using uri value in URN mapping
549                                                 String vpnBindingId = vpnBindingUri[i].substring(vpnBindingUri[i].indexOf("/vpn-binding/")+13, vpnBindingUri[i].length())
550                                                 if (vpnBindingId.charAt(vpnBindingId.length()-1) == '/') {
551                                                         vpnBindingId = vpnBindingId.substring(0, vpnBindingId.length()-1)
552                                                 }
553                                             queryVpnBindingAAIRequest = "${aai_endpoint}${aai_uri}/" + vpnBindingId + "?depth=all"
554                                         }
555
556                                         utils.logAudit(queryVpnBindingAAIRequest)
557                                         execution.setVariable(Prefix + "queryVpnBindingAAIRequest", queryVpnBindingAAIRequest)
558                                         utils.log("DEBUG", " UPDNETI_queryVpnBindingAAIRequest, , vpnBinding #" + counting + " : " + "\n" + queryVpnBindingAAIRequest, isDebugEnabled)
559
560                                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, queryVpnBindingAAIRequest)
561                                         String returnCode = response.getStatusCode()
562                                         execution.setVariable(Prefix + "aaiQqueryVpnBindingReturnCode", returnCode)
563                                         utils.log("DEBUG", " ***** AAI query vpn binding Response Code, vpnBinding #" + counting + " : " + returnCode, isDebugEnabled)
564
565                                         String aaiResponseAsString = response.getResponseBodyAsString()
566
567                                         if (returnCode=='200') {
568                                                 utils.logAudit(aaiResponseAsString)
569                                                 execution.setVariable(Prefix + "queryVpnBindingAAIResponse", aaiResponseAsString)
570                                                 utils.log("DEBUG", " AAI Query Vpn Binding Success REST Response, , vpnBinding #" + counting + " : " + "\n" + aaiResponseAsString, isDebugEnabled)
571
572                                                 String routeTarget = ""
573                                                 if (utils.nodeExists(aaiResponseAsString, "global-route-target")) {
574                                                         routeTarget  = utils.getNodeText1(aaiResponseAsString, "global-route-target")
575                                                         routeTargets += "<routeTargets>" + routeTarget + "</routeTargets>" + '\n'
576                                                 }
577
578                                         } else {
579                                                 if (returnCode=='404') {
580                                                         String dataErrorMessage = "Response Error from AAINetworkVpnBinding is 404 (Not Found)."
581                                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
582                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
583
584                                                 } else {
585                                                    if (aaiResponseAsString.contains("RESTFault")) {
586                                                            WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
587                                                            execution.setVariable("WorkflowException", exceptionObject)
588                                                            throw new BpmnError("MSOWorkflowException")
589
590                                                            } else {
591                                                                         // aai all errors
592                                                                         String dataErrorMessage = " Unexpected Response from AAINetworkVpnBinding - " + returnCode
593                                                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
594                                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
595
596                                                           }
597                                                 }
598                                         }
599
600                                 } // end loop
601
602                                 execution.setVariable(Prefix + "routeCollection", routeTargets)
603                                 utils.log("DEBUG", " UPDNETI_routeCollection - " + '\n' + routeTargets, isDebugEnabled)
604
605                         } else {
606                                 // reset return code to success
607                                 execution.setVariable(Prefix + "aaiQqueryVpnBindingReturnCode", "200")
608                                 String aai_uri = aaiUriUtil.getNetworkL3NetworkUri(execution)
609                                 String schemaVersion = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
610                             String aaiStubResponse =
611                                         """     <rest:payload contentType="text/xml" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd">
612                                                         <vpn-binding xmlns="${schemaVersion}">
613                                                       <global-route-target/>
614                                                         </vpn-binding>
615                                                 </rest:payload>"""
616                                 String aaiStubResponseAsXml = utils.formatXml(aaiStubResponse)
617                                 execution.setVariable(Prefix + "queryVpnBindingAAIResponse", aaiStubResponseAsXml)
618                                 execution.setVariable(Prefix + "routeCollection", "<routeTargets/>")
619                                 utils.log("DEBUG", " No vpnBinding, using this stub as response - " + '\n' + aaiStubResponseAsXml, isDebugEnabled)
620
621                         }
622
623                 } catch (BpmnError e) {
624                         throw e;
625
626                 } catch (Exception ex) {
627                         String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow. callRESTQueryAAINetworkVpnBinding() - " + ex.getMessage()
628                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
629                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
630
631                 }
632
633         }
634
635         public void callRESTQueryAAINetworkPolicy(Execution execution) {
636                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
637                 execution.setVariable("prefix", Prefix)
638
639                 utils.log("DEBUG", " ***** Inside callRESTQueryAAINetworkPolicy of DoUpdateNetworkInstance ***** " , isDebugEnabled)
640
641                 try {
642                         // get variables
643                         String queryIdAAIResponse   = execution.getVariable(Prefix + "requeryIdAAIResponse").replace('<?xml version="1.0" encoding="UTF-8"?>', "")
644                         String relationship = networkUtils.getFirstNodeXml(queryIdAAIResponse, "relationship-list").trim().replace("tag0:","").replace(":tag0","")
645                         utils.log("DEBUG", " relationship - " + relationship, isDebugEnabled)
646
647                         // Check if Network Policy is present, then build a List of network policy
648                         List networkPolicyUriList = networkUtils.getNetworkPolicyObject(relationship)
649                         int networkPolicyCount = networkPolicyUriList.size()
650                         execution.setVariable(Prefix + "networkPolicyCount", networkPolicyCount)
651                         utils.log("DEBUG", " UPDNETI_networkPolicyCount - " + networkPolicyCount, isDebugEnabled)
652
653                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
654                         AaiUtil aaiUriUtil = new AaiUtil(this)
655
656                         if (networkPolicyCount > 0) {
657                                 execution.setVariable(Prefix + "networkPolicyUriList", networkPolicyUriList)
658                                 utils.log("DEBUG", " networkPolicyUri List - " + networkPolicyUriList, isDebugEnabled)
659
660                                 String networkPolicies = ""
661                                 // AII loop call using list vpnBindings
662                                 for (i in 0..networkPolicyUriList.size()-1) {
663
664                                         int counting = i+1
665
666                                         // prepare url using vpnBinding
667                                         String queryNetworkPolicyAAIRequest = ""
668
669                                         String aai_uri = aaiUriUtil.getNetworkPolicyUri(execution)
670
671                                         // Note: By default, the network policy url is found in 'related-link' of the response,
672                                         //       so, the default in URN mappings for this is set to "" (ie, space), unless forced to use the URN mapping.
673                                         if (aai_uri == null || aai_uri == "") {
674                                                 // using value of 'related-link' from response
675                                                 if (networkPolicyUriList[i].charAt(networkPolicyUriList[i].length()-1) == '/') {
676                                                         queryNetworkPolicyAAIRequest = "${aai_endpoint}" + networkPolicyUriList[i].substring(0, networkPolicyUriList[i].length()-1) + "?depth=all"
677                                                 } else {
678                                                         queryNetworkPolicyAAIRequest = "${aai_endpoint}" + networkPolicyUriList[i] + "?depth=all"
679                                                 }
680                                         } else {
681                                                 // using uri value in URN mapping
682                                                 String networkPolicyId = networkPolicyUriList[i].substring(networkPolicyUriList[i].indexOf("/network-policy/")+16, networkPolicyUriList[i].length())
683                                                 println " networkPolicyId - " + networkPolicyId
684                                                 if (networkPolicyId.charAt(networkPolicyId.length()-1) == '/') {
685                                                         networkPolicyId = networkPolicyId.substring(0, networkPolicyId.length()-1)
686                                                 }
687                                                 queryNetworkPolicyAAIRequest = "${aai_endpoint}${aai_uri}/" + networkPolicyId + "?depth=all"
688
689                                         }
690
691
692                                         utils.logAudit(queryNetworkPolicyAAIRequest)
693                                         execution.setVariable(Prefix + "queryNetworkPolicyAAIRequest", queryNetworkPolicyAAIRequest)
694                                         utils.log("DEBUG", " UPDNETI_queryNetworkPolicyAAIRequest, , NetworkPolicy #" + counting + " : " + "\n" + queryNetworkPolicyAAIRequest, isDebugEnabled)
695
696                                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, queryNetworkPolicyAAIRequest)
697                                         String returnCode = response.getStatusCode()
698                                         execution.setVariable(Prefix + "aaiQqueryNetworkPolicyReturnCode", returnCode)
699                                         utils.log("DEBUG", " ***** AAI query network policy Response Code, NetworkPolicy #" + counting + " : " + returnCode, isDebugEnabled)
700
701                                         String aaiResponseAsString = response.getResponseBodyAsString()
702
703                                         if (returnCode=='200') {
704                                                 utils.logAudit(aaiResponseAsString)
705                                                 execution.setVariable(Prefix + "queryNetworkPolicyAAIResponse", aaiResponseAsString)
706                                                 utils.log("DEBUG", " QueryAAINetworkPolicy Success REST Response, , NetworkPolicy #" + counting + " : " + "\n" + aaiResponseAsString, isDebugEnabled)
707
708                                                 String networkPolicy = ""
709                                                 if (utils.nodeExists(aaiResponseAsString, "network-policy-fqdn")) {
710                                                         networkPolicy  = utils.getNodeText1(aaiResponseAsString, "network-policy-fqdn")
711                                                         networkPolicies += "<policyFqdns>" + networkPolicy + "</policyFqdns>" + '\n'
712                                                 }
713
714                                         } else {
715                                                 if (returnCode=='404') {
716                                                         String dataErrorMessage = "Response Error from QueryAAINetworkPolicy is 404 (Not Found)."
717                                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
718                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
719
720                                                 } else {
721                                                    if (aaiResponseAsString.contains("RESTFault")) {
722                                                            WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
723                                                            execution.setVariable("WorkflowException", exceptionObject)
724                                                            throw new BpmnError("MSOWorkflowException")
725
726                                                            } else {
727                                                                         // aai all errors
728                                                                         String dataErrorMessage = "Unexpected Response from QueryAAINetworkPolicy - " + returnCode
729                                                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
730                                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
731
732                                                           }
733                                                 }
734                                         }
735
736                                 } // end loop
737
738                                 execution.setVariable(Prefix + "networkCollection", networkPolicies)
739                                 utils.log("DEBUG", " UPDNETI_networkCollection - " + '\n' + networkPolicies, isDebugEnabled)
740
741                         } else {
742                                 // reset return code to success
743                                 execution.setVariable(Prefix + "aaiQqueryNetworkPolicyReturnCode", "200")
744                                 String aai_uri = aaiUriUtil.getNetworkL3NetworkUri(execution)
745                                 String schemaVersion = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
746                                 String aaiStubResponse =
747                                         """     <rest:payload contentType="text/xml" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd">
748                                                         <network-policy xmlns="${schemaVersion}">
749                                                           <network-policy-fqdn/>
750                             </network-policy>
751                                                 </rest:payload>"""
752                                 String aaiStubResponseAsXml = utils.formatXml(aaiStubResponse)
753                                 execution.setVariable(Prefix + "queryNetworkPolicyAAIResponse", aaiStubResponseAsXml)
754                                 execution.setVariable(Prefix + "networkCollection", "<policyFqdns/>")
755                                 utils.log("DEBUG", " No net policies, using this stub as response - " + '\n' + aaiStubResponseAsXml, isDebugEnabled)
756
757                         }
758
759                 } catch (BpmnError e) {
760                         throw e;
761
762                 } catch (Exception ex) {
763                         String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow. callRESTQueryAAINetworkPolicy() - " + ex.getMessage()
764                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
765                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
766
767                 }
768
769         }
770
771         public void callRESTQueryAAINetworkTableRef(Execution execution) {
772                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
773                 execution.setVariable("prefix", Prefix)
774
775                 utils.log("DEBUG", " ***** Inside callRESTQueryAAINetworkTableRef of DoUpdateNetworkInstance ***** " , isDebugEnabled)
776
777                 try {
778                         // get variables
779                         String queryIdAAIResponse   = execution.getVariable(Prefix + "requeryIdAAIResponse").replace('<?xml version="1.0" encoding="UTF-8"?>', "")
780                         String relationship = networkUtils.getFirstNodeXml(queryIdAAIResponse, "relationship-list").trim().replace("tag0:","").replace(":tag0","")
781                         utils.log("DEBUG", " relationship - " + relationship, isDebugEnabled)
782
783                         // Check if Network TableREf is present, then build a List of network policy
784                         List networkTableRefUriList = networkUtils.getNetworkTableRefObject(relationship)
785                         int networkTableRefCount = networkTableRefUriList.size()
786                         execution.setVariable(Prefix + "networkTableRefCount", networkTableRefCount)
787                         utils.log("DEBUG", " UPDNETI_networkTableRefCount - " + networkTableRefCount, isDebugEnabled)
788
789                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
790                         AaiUtil aaiUriUtil = new AaiUtil(this)
791
792                         if (networkTableRefCount > 0) {
793                                 execution.setVariable(Prefix + "networkTableRefUriList", networkTableRefUriList)
794                                 utils.log("DEBUG", " networkTableRefUri List - " + networkTableRefUriList, isDebugEnabled)
795
796                                 // AII loop call using list vpnBindings
797                                 String networkTableRefs = ""
798                                 for (i in 0..networkTableRefUriList.size()-1) {
799
800                                         int counting = i+1
801
802                                         // prepare url using tableRef
803                                         String queryNetworkTableRefAAIRequest = ""
804
805                                         String aai_uri = aaiUriUtil.getNetworkTableReferencesUri(execution)
806
807                                         // Note: By default, the network policy url is found in 'related-link' of the response,
808                                         //       so, the default in URN mappings for this is set to "" (ie, space), unless forced to use the URN mapping.
809                                         if (aai_uri == null || aai_uri == "") {
810                                                 // using value of 'related-link' from response
811                                                 if (networkTableRefUriList[i].charAt(networkTableRefUriList[i].length()-1) == '/') {
812                                                         queryNetworkTableRefAAIRequest = "${aai_endpoint}" + networkTableRefUriList[i].substring(0, networkTableRefUriList[i].length()-1) + "?depth=all"
813                                                 } else {
814                                                         queryNetworkTableRefAAIRequest = "${aai_endpoint}" + networkTableRefUriList[i] + "?depth=all"
815                                                 }
816                                         } else {
817                                                 // using uri value in URN mapping
818                                                 String networkTableRefId = networkTableRefUriList[i].substring(networkTableRefUriList[i].indexOf("/route-table-reference/")+23, networkTableRefUriList[i].length())
819
820                                                 if (networkTableRefId.charAt(networkTableRefId.length()-1) == '/') {
821                                                         networkTableRefId = networkTableRefId.substring(0, networkTableRefId.length()-1)
822                                                 }
823                                                 queryNetworkTableRefAAIRequest = "${aai_endpoint}${aai_uri}/" + networkTableRefId + "?depth=all"
824
825                                         }
826
827
828                                         utils.logAudit(queryNetworkTableRefAAIRequest)
829                                         execution.setVariable(Prefix + "queryNetworkTableRefAAIRequest", queryNetworkTableRefAAIRequest)
830                                         utils.log("DEBUG", " UPDNETI_queryNetworkTableRefAAIRequest, , NetworkTableRef #" + counting + " : " + "\n" + queryNetworkTableRefAAIRequest, isDebugEnabled)
831
832                                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, queryNetworkTableRefAAIRequest)
833                                         String returnCode = response.getStatusCode()
834                                         execution.setVariable(Prefix + "aaiQqueryNetworkTableRefReturnCode", returnCode)
835                                         utils.log("DEBUG", " ***** AAI query network Table Reference Response Code, NetworkTableRef #" + counting + " : " + returnCode, isDebugEnabled)
836
837                                         String aaiResponseAsString = response.getResponseBodyAsString()
838
839                                         if (returnCode=='200') {
840                                                 utils.logAudit(aaiResponseAsString)
841                                                 execution.setVariable(Prefix + "queryNetworkTableRefAAIResponse", aaiResponseAsString)
842                                                 utils.log("DEBUG", " QueryAAINetworkTableRef Success REST Response, , NetworkTableRef #" + counting + " : " + "\n" + aaiResponseAsString, isDebugEnabled)
843
844                                                 String networkTableRef = ""
845                                                 if (utils.nodeExists(aaiResponseAsString, "route-table-reference-fqdn")) {
846                                                         networkTableRef  = utils.getNodeText1(aaiResponseAsString, "route-table-reference-fqdn")
847                                                         networkTableRefs += "<routeTableFqdns>" + networkTableRef + "</routeTableFqdns>" + '\n'
848                                                 }
849
850                                         } else {
851                                                 if (returnCode=='404') {
852                                                         String dataErrorMessage = "Response Error from QueryAAINetworkTableRef is 404 (Not Found)."
853                                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
854                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
855                                                         
856                                                 } else {
857                                                    if (aaiResponseAsString.contains("RESTFault")) {
858                                                            WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
859                                                            execution.setVariable("WorkflowException", exceptionObject)
860                                                            throw new BpmnError("MSOWorkflowException")
861                 
862                                                            } else {
863                                                                         // aai all errors
864                                                                         String dataErrorMessage = "Unexpected Response from QueryAAINetworkTableRef - " + returnCode
865                                                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
866                                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
867
868                                                           }
869                                                 }
870                                         }
871
872                                 } // end loop
873
874                                 execution.setVariable(Prefix + "tableRefCollection", networkTableRefs)
875                                 utils.log("DEBUG", " UPDNETI_tableRefCollection - " + '\n' + networkTableRefs, isDebugEnabled)
876
877                         } else {
878                                 // reset return code to success
879                                 execution.setVariable(Prefix + "aaiQqueryNetworkTableRefReturnCode", "200")
880                                 String aai_uri = aaiUriUtil.getNetworkL3NetworkUri(execution)
881                                 String schemaVersion = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
882                                 String aaiStubResponse =
883                                         """     <rest:payload contentType="text/xml" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd">
884                                                         <route-table-references xmlns="${schemaVersion}">
885                                                           <route-table-reference-fqdn/>
886                             </route-table-references>
887                                                 </rest:payload>"""
888                                 String aaiStubResponseAsXml = utils.formatXml(aaiStubResponse)
889                                 execution.setVariable(Prefix + "queryNetworkTableRefAAIResponse", aaiStubResponseAsXml)
890                                 execution.setVariable(Prefix + "tableRefCollection", "<routeTableFqdns/>")
891                                 utils.log("DEBUG", " No net table references, using this stub as response - " + '\n' + aaiStubResponseAsXml, isDebugEnabled)
892
893                         }
894
895                 } catch (BpmnError e) {
896                         throw e;
897                         
898                 } catch (Exception ex) {
899                         String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow. callRESTQueryAAINetworkTableRef() - " + ex.getMessage()
900                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
901                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
902
903                 }
904
905         }
906         
907         public void callRESTUpdateContrailAAINetwork(Execution execution) {
908                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
909                 execution.setVariable("prefix", Prefix)
910
911                 utils.log("DEBUG", " ***** Inside callRESTUpdateContrailAAINetwork of DoUpdateNetworkInstance ***** " , isDebugEnabled)
912
913                 try {
914                         // get variables
915                         String networkRequest = execution.getVariable(Prefix + "networkRequest")
916                         String networkId   = utils.getNodeText1(networkRequest, "network-id")
917                         networkId = UriUtils.encode(networkId,"UTF-8")
918                         String requeryIdAAIResponse   = execution.getVariable(Prefix + "requeryIdAAIResponse")
919                         String updateNetworkResponse   = execution.getVariable(Prefix + "updateNetworkResponse")
920
921                         // Prepare url
922                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
923                         AaiUtil aaiUriUtil = new AaiUtil(this)
924                         String aai_uri = aaiUriUtil.getNetworkL3NetworkUri(execution)
925                         String updateContrailAAIUrlRequest = "${aai_endpoint}${aai_uri}/" + networkId + "?depth=1"
926
927                         utils.logAudit(updateContrailAAIUrlRequest)
928                         execution.setVariable(Prefix + "updateContrailAAIUrlRequest", updateContrailAAIUrlRequest)
929                         utils.log("DEBUG", " UPDNETI_updateContrailAAIUrlRequest - " + "\n" + updateContrailAAIUrlRequest, isDebugEnabled)
930
931                         //Prepare payload (PUT)
932                         String schemaVersion = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
933                         String payload = networkUtils.ContrailNetworkCreatedUpdate(requeryIdAAIResponse, updateNetworkResponse, schemaVersion)
934                         String payloadXml = utils.formatXml(payload)
935                         utils.logAudit(payloadXml)
936                         execution.setVariable(Prefix + "updateContrailAAIPayloadRequest", payloadXml)
937                         utils.log("DEBUG", " 'payload' to Update Contrail - " + "\n" + payloadXml, isDebugEnabled)
938
939                         APIResponse response = aaiUriUtil.executeAAIPutCall(execution, updateContrailAAIUrlRequest, payload)
940                         String returnCode = response.getStatusCode()
941                         String aaiUpdateContrailResponseAsString = response.getResponseBodyAsString()
942                         
943                         execution.setVariable(Prefix + "aaiUpdateContrailReturnCode", returnCode)
944                         utils.log("DEBUG", " ***** AAI Update Contrail Response Code  : " + returnCode, isDebugEnabled)
945
946
947                         if (returnCode=='200') {
948                                 utils.logAudit(aaiUpdateContrailResponseAsString)
949                                 execution.setVariable(Prefix + "updateContrailAAIResponse", aaiUpdateContrailResponseAsString)
950                                 utils.log("DEBUG", " AAI Update Contrail Success REST Response - " + "\n" + aaiUpdateContrailResponseAsString, isDebugEnabled)
951                                 // Point-of-no-return is set to false, rollback not needed.
952                                 execution.setVariable(Prefix + "isPONR", true)
953
954                         } else {
955                                 if (returnCode=='404') {
956                                         String dataErrorMessage = " Response Error from UpdateContrailAAINetwork is 404 (Not Found)."
957                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
958                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
959
960                                 } else {
961                                    if (aaiUpdateContrailResponseAsString.contains("RESTFault")) {
962                                            WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiUpdateContrailResponseAsString, execution)
963                                            execution.setVariable("WorkflowException", exceptionObject)
964                                            throw new BpmnError("MSOWorkflowException")
965
966                                            } else {
967                                                         // aai all errors
968                                                         String errorMessage = "Unexpected Response from UpdateContrailAAINetwork - " + returnCode
969                                                         utils.log("DEBUG", errorMessage, isDebugEnabled)
970                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, errorMessage)
971                                           }
972                                 }
973                         }
974
975                 } catch (BpmnError e) {
976                         throw e;
977
978                 } catch (Exception ex) {
979                         String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow. callRESTUpdateContrailAAINetwork() - " + ex.getMessage()
980                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
981                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
982
983                 }
984
985         }
986
987         public void prepareUpdateNetworkRequest (Execution execution) {
988                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
989                 execution.setVariable("prefix", Prefix)
990
991                 utils.log("DEBUG", " ***** Inside prepareUpdateNetworkRequest of DoUpdateNetworkInstance ***** ", isDebugEnabled)
992
993                 try {
994
995                         // get variables
996                         String requestId = execution.getVariable(Prefix + "requestId")
997                         String messageId = execution.getVariable(Prefix + "messageId")
998                         String source    = execution.getVariable(Prefix + "source")
999
1000                         String requestInput = execution.getVariable(Prefix + "networkRequest")
1001                         String queryIdResponse = execution.getVariable(Prefix + "requeryIdAAIResponse")
1002                         String cloudRegionId = execution.getVariable(Prefix + "cloudRegionPo")
1003                         String backoutOnFailure = execution.getVariable(Prefix + "rollbackEnabled")
1004                         
1005                         // Prepare Network request
1006                         String routeCollection = execution.getVariable(Prefix + "routeCollection")
1007                         String policyCollection = execution.getVariable(Prefix + "networkCollection")
1008                         String tableCollection = execution.getVariable(Prefix + "tableRefCollection")
1009                         String updateNetworkRequest = networkUtils.UpdateNetworkRequestV2(execution, requestId, messageId, requestInput, queryIdResponse, routeCollection, policyCollection, tableCollection, cloudRegionId, backoutOnFailure, source )
1010                         // Format Response
1011                         String buildUpdateNetworkRequestAsString = utils.formatXml(updateNetworkRequest)
1012                         buildUpdateNetworkRequestAsString = buildUpdateNetworkRequestAsString.replace(":w1aac13n0", "").replace("w1aac13n0:", "")
1013                         utils.logAudit(buildUpdateNetworkRequestAsString)
1014
1015                         execution.setVariable(Prefix + "updateNetworkRequest", buildUpdateNetworkRequestAsString)
1016                         utils.log("DEBUG", " UPDNETI_updateNetworkRequest - " + "\n" +  buildUpdateNetworkRequestAsString, isDebugEnabled)
1017
1018                 } catch (Exception ex) {
1019                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstance flow. prepareUpdateNetworkRequest() - " + ex.getMessage()
1020                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1021                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
1022
1023                 }
1024
1025         }
1026
1027         public void prepareSDNCRequest (Execution execution) {
1028                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1029                 execution.setVariable("prefix", Prefix)
1030
1031                 utils.log("DEBUG", " ***** Inside prepareSDNCRequest of DoUpdateNetworkInstance ***** ", isDebugEnabled)
1032
1033                 try {
1034                         // get variables
1035                         String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
1036                         String updateNetworkInput = execution.getVariable(Prefix + "networkRequest")
1037                         String cloudRegionId = execution.getVariable(Prefix + "cloudRegionSdnc")
1038
1039                         String networkId = ""
1040                         if (utils.nodeExists(updateNetworkInput, "network-id")) {
1041                            networkId = utils.getNodeText1(updateNetworkInput, "network-id")
1042                         }
1043                         if (networkId == null) {networkId = ""}
1044
1045                         String serviceInstanceId = utils.getNodeText1(updateNetworkInput, "service-instance-id")
1046
1047                         // 1. prepare assign topology via SDNC Adapter SUBFLOW call
1048                         String sndcTopologyCreateRequest = sdncAdapterUtils.sdncTopologyRequestV2(execution, updateNetworkInput, serviceInstanceId, sdncCallback, "changeassign", "NetworkActivateRequest", cloudRegionId, networkId, null, null)
1049
1050                         String sndcTopologyUpdateRequesAsString = utils.formatXml(sndcTopologyCreateRequest)
1051                         utils.logAudit(sndcTopologyUpdateRequesAsString)
1052                         execution.setVariable(Prefix + "changeAssignSDNCRequest", sndcTopologyUpdateRequesAsString)
1053                         utils.log("DEBUG", " UPDNETI_changeAssignSDNCRequest - " + "\n" +  sndcTopologyUpdateRequesAsString, isDebugEnabled)
1054
1055
1056                 } catch (Exception ex) {
1057                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstance flow. prepareSDNCRequest() - " + ex.getMessage()
1058                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1059                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
1060
1061                 }
1062
1063         }
1064
1065         
1066
1067
1068         // **************************************************
1069         //     Post or Validate Response Section
1070         // **************************************************
1071
1072         public void validateUpdateNetworkResponse (Execution execution) {
1073                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1074                 execution.setVariable("prefix", Prefix)
1075
1076                 utils.log("DEBUG", " ***** Inside validateUpdateNetworkResponse of DoUpdateNetworkInstance *****", isDebugEnabled)
1077
1078                 try {
1079                         String returnCode = execution.getVariable(Prefix + "networkReturnCode")
1080                         String networkResponse = execution.getVariable(Prefix + "updateNetworkResponse")
1081                         if (networkResponse==null)      {
1082                                 networkResponse="" // reset
1083                         }
1084
1085                         utils.log("DEBUG", " Network Adapter update responseCode: " + returnCode, isDebugEnabled)
1086
1087                         String errorMessage = ""
1088                         if (returnCode == "200") {
1089                                 execution.setVariable(Prefix + "isNetworkRollbackNeeded", true)
1090                                 utils.logAudit(networkResponse)
1091                                 execution.setVariable(Prefix + "updateNetworkResponse", networkResponse)
1092                                 utils.log("DEBUG", " Network Adapter update Success Response - " + "\n" + networkResponse, isDebugEnabled)
1093
1094                                 // prepare rollback data
1095                                 String rollbackData = utils.getNodeXml(networkResponse, "rollback", false).replace("tag0:","").replace(":tag0","")
1096                                 rollbackData = rollbackData.replace("rollback>", "networkRollback>")
1097                                 String rollbackNetwork =
1098                                         """<rollbackNetworkRequest>
1099                                                         ${rollbackData}
1100                                                 </rollbackNetworkRequest>"""
1101                                 String rollbackNetworkXml = utils.formatXml(rollbackNetwork)
1102                                 execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkXml)
1103                                 utils.log("DEBUG", " Network Adapter rollback data - " + "\n" + rollbackNetworkXml, isDebugEnabled)
1104
1105                         } else { // network error
1106                            if (returnCode.toInteger() > 399 && returnCode.toInteger() < 600) {   //4xx, 5xx
1107                                    if (networkResponse.contains("updateNetworkError")) {
1108                                            networkResponse = networkResponse.replace('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', '')
1109                                            errorMessage = utils.getNodeText1(networkResponse, "message")
1110                                            errorMessage  = "Received error from Network Adapter: " + errorMessage
1111                                            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, errorMessage)
1112
1113                                    } else { // CatchAll exception
1114                                            if (returnCode == "500") {
1115                                                    errorMessage = "JBWEB000065: HTTP Status 500."
1116                                        } else {
1117                                                errorMessage = "Return code is " + returnCode
1118                                        }
1119                                            errorMessage  = "Received error from Network Adapter: " + errorMessage
1120                                            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, errorMessage)
1121
1122                                    }
1123
1124                            } else { // CatchAll exception
1125                                    String dataErrorMessage  = "Received error from Network Adapter. Return code is: " + returnCode
1126                                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
1127
1128                            }
1129
1130                         }
1131
1132                 } catch (BpmnError e) {
1133                         throw e;
1134
1135                 } catch (Exception ex) {
1136                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstance flow. validateUpdateNetworkResponse() - " + ex.getMessage()
1137                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1138                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
1139
1140                 }
1141
1142
1143         }
1144
1145         public void validateSDNCResponse (Execution execution) {
1146                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1147                 execution.setVariable("prefix", Prefix)
1148
1149                 utils.log("DEBUG", " ***** Inside validateSDNCResponse of DoUpdateNetworkInstance ***** ", isDebugEnabled)
1150
1151                 String response = execution.getVariable(Prefix + "changeAssignSDNCResponse")
1152                 WorkflowException workflowException = null
1153                 try {
1154                         workflowException = execution.getVariable(Prefix + "WorkflowException")
1155                         //execution.setVariable("WorkflowException", workflowException)
1156                 } catch (Exception ex) {
1157                         utils.log("DEBUG", " Sdnc 'WorkflowException' object is empty or null. ", isDebugEnabled)
1158                 }
1159
1160                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
1161
1162                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
1163                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
1164                 // reset variable
1165                 String changeAssignSDNCResponseDecodeXml = sdncAdapterUtils.decodeXML(execution.getVariable(Prefix + "changeAssignSDNCResponse"))
1166                 changeAssignSDNCResponseDecodeXml = changeAssignSDNCResponseDecodeXml.replace("&", "&amp;").replace('<?xml version="1.0" encoding="UTF-8"?>', "")
1167                 execution.setVariable(Prefix + "changeAssignSDNCResponse", changeAssignSDNCResponseDecodeXml)
1168
1169                 if (execution.getVariable(Prefix + "sdncResponseSuccess") == true) {  // from sdnc util, prefix+'sdncResponseSuccess'
1170                         execution.setVariable(Prefix + "isSdncRollbackNeeded", true)
1171                         utils.log("DEBUG", "Successfully Validated SDNC Response", isDebugEnabled)
1172
1173                 } else {
1174                         utils.log("DEBUG", "Did NOT Successfully Validated SDNC Response", isDebugEnabled)
1175                         throw new BpmnError("MSOWorkflowException")
1176                 }
1177
1178         }
1179
1180
1181         public void postProcessResponse (Execution execution) {
1182                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1183                 execution.setVariable("prefix", Prefix)
1184
1185                 utils.log("DEBUG", " ***** Inside postProcessResponse of DoUpdateNetworkInstance ***** ", isDebugEnabled)
1186
1187                 try {
1188                         utils.log("DEBUG", " ***** Is Exception Encountered (isException)? : " + execution.getVariable(Prefix + "isException"), isDebugEnabled)
1189                         if (execution.getVariable(Prefix + "isException") == false) {
1190                                 // set rollback data
1191                                 execution.setVariable("orchestrationStatus", "")
1192                                 execution.setVariable("networkId", execution.getVariable(Prefix + "networkId"))
1193                                 execution.setVariable("networkName", execution.getVariable(Prefix + "networkName"))
1194                                 prepareSuccessRollbackData(execution) // populate rollbackData
1195                                 execution.setVariable("WorkflowException", null)
1196                                 execution.setVariable(Prefix + "Success", true)
1197                                 utils.log("DEBUG", " ***** postProcessResponse(), GOOD !!!", isDebugEnabled)
1198                         } else {
1199                                 execution.setVariable(Prefix + "Success", false) 
1200                                 execution.setVariable("rollbackData", null)
1201                                 String exceptionMessage = " Exception encountered in MSO Bpmn. "
1202                                 if (execution.getVariable("workflowException") != null) {  // Output of Rollback flow.
1203                                    utils.log("DEBUG", " ***** workflowException: " + execution.getVariable("workflowException"), isDebugEnabled)
1204                                    WorkflowException wfex = execution.getVariable("workflowException")
1205                                    exceptionMessage = wfex.getErrorMessage()
1206                                 } else {
1207                                if (execution.getVariable(Prefix + "WorkflowException") != null) {
1208                                       WorkflowException pwfex = execution.getVariable(Prefix + "WorkflowException")
1209                                       exceptionMessage = pwfex.getErrorMessage()
1210                                }   
1211                                 }
1212                             // going to the Main flow: a-la-carte or macro
1213                             utils.log("DEBUG", " ***** postProcessResponse(), BAD !!!", isDebugEnabled)
1214                             exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
1215                                 throw new BpmnError("MSOWorkflowException")
1216                         }
1217                                                         
1218                 } catch(BpmnError b){
1219                      utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
1220                      throw b
1221                         
1222
1223                 } catch (Exception ex) {
1224                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstance flow. postProcessResponse() - " + ex.getMessage()
1225                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1226                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
1227
1228         }
1229
1230
1231         }
1232
1233         public void prepareSDNCRollbackRequest (Execution execution) {
1234                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1235                 execution.setVariable("prefix", Prefix)
1236
1237                 utils.log("DEBUG", " ***** Inside prepareSDNCRollbackRequest of DoUpdateNetworkInstance ***** ", isDebugEnabled)
1238
1239                 try {
1240                         // for some reason the WorkflowException object is null after the sdnc rollback call task, need to save WorkflowException. 
1241                         execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
1242                         // get variables
1243                         String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
1244                         String updateNetworkInput = execution.getVariable(Prefix + "networkRequest")
1245                         String cloudRegionId = execution.getVariable(Prefix + "cloudRegionSdnc")
1246                         String changeAssignSDNCResponse = execution.getVariable(Prefix + "changeAssignSDNCResponse")
1247                         String networkId = utils.getNodeText1(changeAssignSDNCResponse, "network-id")
1248
1249                         String serviceInstanceId = utils.getNodeText1(updateNetworkInput, "service-instance-id")
1250
1251                         // 2. prepare rollback topology via SDNC Adapter SUBFLOW call
1252                         String sndcTopologyRollbackRequest = sdncAdapterUtils.sdncTopologyRequestV2(execution, updateNetworkInput, serviceInstanceId, sdncCallback, "rollback", "NetworkActivateRequest", cloudRegionId, networkId, null, null)
1253                         String sndcTopologyRollbackRequestAsString = utils.formatXml(sndcTopologyRollbackRequest)
1254                         execution.setVariable(Prefix + "rollbackSDNCRequest", sndcTopologyRollbackRequestAsString)
1255                         utils.log("DEBUG", " Preparing request for SDNC Topology assign's rollback/compensation . . . - " + "\n" +  sndcTopologyRollbackRequestAsString, isDebugEnabled)
1256
1257
1258                 } catch (Exception ex) {
1259                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstance flow. prepareSDNCRollbackRequest() - " + ex.getMessage()
1260                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1261                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
1262
1263                 }
1264
1265         }
1266
1267         public void prepareRollbackData(Execution execution) {
1268                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1269                 execution.setVariable("prefix",Prefix)
1270                 
1271                 utils.log("DEBUG", " ***** Inside prepareRollbackData() of DoUpdateNetworkInstance ***** ", isDebugEnabled)
1272                 
1273                 try {
1274                         
1275                         Map<String, String> rollbackData = new HashMap<String, String>();
1276                         String rollbackSDNCRequest = execution.getVariable(Prefix + "rollbackSDNCRequest")
1277                         if (rollbackSDNCRequest != null) {
1278                                 if (rollbackSDNCRequest != "") {
1279                                         rollbackData.put("rollbackSDNCRequest", execution.getVariable(Prefix + "rollbackSDNCRequest"))
1280                                 }
1281                         }
1282                         String rollbackNetworkRequest = execution.getVariable(Prefix + "rollbackNetworkRequest")
1283                         if (rollbackNetworkRequest != null) {
1284                                 if (rollbackNetworkRequest != "") {
1285                                         rollbackData.put("rollbackNetworkRequest", execution.getVariable(Prefix + "rollbackNetworkRequest"))
1286                                 }
1287                         }
1288                         execution.setVariable("rollbackData", rollbackData)
1289                         utils.log("DEBUG", "** rollbackData : " + rollbackData, isDebugEnabled)
1290                         
1291                         execution.setVariable("WorkflowException", execution.getVariable(Prefix + "WorkflowException"))
1292                         utils.log("DEBUG", "** WorkflowException : " + execution.getVariable("WorkflowException"), isDebugEnabled)
1293                         
1294                 } catch (Exception ex) {
1295                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstance flow. prepareRollbackData() - " + ex.getMessage()
1296                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1297                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
1298                 
1299                 }
1300                 
1301         }
1302         
1303         public void prepareSuccessRollbackData(Execution execution) {
1304                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1305                 execution.setVariable("prefix",Prefix)
1306                 
1307                 utils.log("DEBUG", " ***** Inside prepareSuccessRollbackData() of DoUpdateNetworkInstance ***** ", isDebugEnabled)
1308                 
1309                 try {
1310                         
1311                         if (execution.getVariable("sdncVersion") != '1610') {
1312                                 // skip: 1702 for 'changeassign' or equivalent not yet defined in SNDC, so no rollback.
1313                         } else {
1314                                 prepareSDNCRollbackRequest(execution)
1315                         }
1316                         
1317                         Map<String, String> rollbackData = new HashMap<String, String>();
1318                         String rollbackSDNCRequest = execution.getVariable(Prefix + "rollbackSDNCRequest")
1319                         if (rollbackSDNCRequest != null) {
1320                                 if (rollbackSDNCRequest != "") {
1321                                         rollbackData.put("rollbackSDNCRequest", rollbackSDNCRequest)
1322                                 }
1323                         }
1324                         String rollbackNetworkRequest = execution.getVariable(Prefix + "rollbackNetworkRequest")
1325                         if (rollbackNetworkRequest != null) {
1326                                 if (rollbackNetworkRequest != "") {
1327                                         rollbackData.put("rollbackNetworkRequest", rollbackNetworkRequest)
1328                                 }
1329                         }
1330                         execution.setVariable("rollbackData", rollbackData)
1331                         
1332                         utils.log("DEBUG", "** 'rollbackData' for Full Rollback : " + rollbackData, isDebugEnabled)
1333                         execution.setVariable("WorkflowException", null)
1334
1335                         
1336                 } catch (Exception ex) {
1337                         String exceptionMessage = " Bpmn error encountered in DoUpdateNetworkInstance flow. prepareSuccessRollbackData() - " + ex.getMessage()
1338                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1339                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
1340                 
1341                 }
1342                 
1343         }
1344         
1345         public void setExceptionFlag(Execution execution){
1346                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1347                 execution.setVariable("prefix",Prefix)
1348                 
1349                 utils.log("DEBUG", " ***** Inside setExceptionFlag() of DoUpdateNetworkInstance ***** ", isDebugEnabled)
1350                 
1351                 try {
1352                         
1353                         execution.setVariable(Prefix + "isException", true)
1354                         
1355                         if (execution.getVariable("SavedWorkflowException1") != null) {
1356                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
1357                         } else {
1358                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
1359                         }
1360                         utils.log("DEBUG", Prefix + "WorkflowException - " +execution.getVariable(Prefix + "WorkflowException"), isDebugEnabled)
1361                         
1362                 } catch(Exception ex){
1363                           String exceptionMessage = "Bpmn error encountered in DoUpdateNetworkInstance flow. setExceptionFlag(): " + ex.getMessage()
1364                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
1365                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
1366                 }
1367                 
1368         }
1369
1370
1371         // *******************************
1372         //     Build Error Section
1373         // *******************************
1374
1375         public void processJavaException(Execution execution){
1376                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1377                 execution.setVariable("prefix",Prefix)
1378                 try{
1379                         utils.log("DEBUG", "Caught a Java Exception", isDebugEnabled)
1380                         utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)
1381                         utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)
1382                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - "  + Prefix)  // Adding this line temporarily until this flows error handling gets updated
1383                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
1384                         
1385                 }catch(Exception e){
1386                         utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)
1387                         execution.setVariable("UnexpectedError", "Exception in processJavaException method")  // Adding this line temporarily until this flows error handling gets updated
1388                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method")
1389                 }
1390                 utils.log("DEBUG", "Completed processJavaException Method", isDebugEnabled)
1391         }
1392
1393 }