1fd5ab0abccc9b2b4590f1dd1c9a9e1ed2e0d380
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateVfModule.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.infrastructure.scripts;
22
23 import javax.xml.parsers.DocumentBuilder
24 import javax.xml.parsers.DocumentBuilderFactory
25
26 import org.apache.commons.lang3.*
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.runtime.Execution
29 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
30 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
31 import org.openecomp.mso.bpmn.common.scripts.NetworkUtils
32 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
33 import org.openecomp.mso.bpmn.common.scripts.VfModuleBase
34 import org.openecomp.mso.bpmn.core.RollbackData
35 import org.openecomp.mso.bpmn.core.WorkflowException
36 import org.openecomp.mso.bpmn.core.json.JsonUtils
37 import org.openecomp.mso.rest.APIResponse
38 import org.openecomp.mso.rest.RESTClient
39 import org.openecomp.mso.rest.RESTConfig
40 import org.springframework.web.util.UriUtils
41 import org.w3c.dom.Document
42 import org.w3c.dom.Element
43 import org.w3c.dom.NamedNodeMap
44 import org.w3c.dom.Node
45 import org.w3c.dom.NodeList
46 import org.xml.sax.InputSource
47
48
49
50 public class DoCreateVfModule extends VfModuleBase {
51
52         String Prefix="DCVFM_"
53         ExceptionUtil exceptionUtil = new ExceptionUtil()
54         JsonUtils jsonUtil = new JsonUtils()
55         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
56
57         /**
58          * Validates the request message and sets up the workflow.
59          * @param execution the execution
60          */
61         public void preProcessRequest(Execution execution) {
62                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
63                         'execution=' + execution.getId() +
64                         ')'
65                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
66                 logDebug('Entered ' + method, isDebugLogEnabled)
67
68                 execution.setVariable('prefix', Prefix)
69                 try{
70                         def rollbackData = execution.getVariable("RollbackData")
71                         if (rollbackData == null) {
72                                 rollbackData = new RollbackData()
73                         }
74
75                         execution.setVariable("DCVFM_vnfParamsExistFlag", false)
76                         execution.setVariable("DCVFM_oamManagementV4Address", "")
77                         execution.setVariable("DCVFM_oamManagementV6Address", "")
78
79                         String request = execution.getVariable("DoCreateVfModuleRequest")
80
81                         if (request == null || request.isEmpty()) {
82                                 // Building Block-type request
83
84                                 String vfModuleModelInfo = execution.getVariable("vfModuleModelInfo")           
85                                 
86                                 def serviceModelInfo = execution.getVariable("serviceModelInfo")
87                                 logDebug("serviceModelInfo: " + serviceModelInfo, isDebugLogEnabled)
88                                 def vnfModelInfo = execution.getVariable("vnfModelInfo")
89                                 
90
91                                 //tenantId
92                                 def tenantId = execution.getVariable("tenantId")
93                                 execution.setVariable("DCVFM_tenantId", tenantId)
94                                 rollbackData.put("VFMODULE", "tenantid", tenantId)
95                                 //volumeGroupId
96                                 def volumeGroupId = execution.getVariable("volumeGroupId")
97                                 execution.setVariable("DCVFM_volumeGroupId", volumeGroupId)
98                                 //volumeGroupName
99                                 def volumeGroupName = execution.getVariable("volumeGroupName")
100                                 execution.setVariable("DCVFM_volumeGroupName", volumeGroupName)
101                                 //cloudSiteId
102                                 def cloudSiteId = execution.getVariable("lcpCloudRegionId")
103                                 execution.setVariable("DCVFM_cloudSiteId", cloudSiteId)
104                                 rollbackData.put("VFMODULE", "aiccloudregion", cloudSiteId)
105                                 logDebug("cloudSiteId: " + cloudSiteId, isDebugLogEnabled)
106                                 //vnfType
107                                 def vnfType = execution.getVariable("vnfType")
108                                 execution.setVariable("DCVFM_vnfType", vnfType)
109                                 rollbackData.put("VFMODULE", "vnftype", vnfType)
110                                 logDebug("vnfType: " + vnfType, isDebugLogEnabled)
111                                 //vnfName
112                                 def vnfName = execution.getVariable("vnfName")
113                                 execution.setVariable("DCVFM_vnfName", vnfName)
114                                 rollbackData.put("VFMODULE", "vnfname", vnfName)
115                                 logDebug("vnfName: " + vnfName, isDebugLogEnabled)
116                                 //vnfId
117                                 def vnfId = execution.getVariable("vnfId")
118                                 execution.setVariable("DCVFM_vnfId", vnfId)
119                                 rollbackData.put("VFMODULE", "vnfid", vnfId)
120                                 logDebug("vnfId: " + vnfId, isDebugLogEnabled)
121                                 //vfModuleName
122                                 def vfModuleName = execution.getVariable("vfModuleName")
123                                 execution.setVariable("DCVFM_vfModuleName", vfModuleName)
124                                 rollbackData.put("VFMODULE", "vfmodulename", vfModuleName)
125                                 logDebug("vfModuleName: " + vfModuleName, isDebugLogEnabled)
126                                 //vfModuleModelName
127                                 def vfModuleModelName = jsonUtil.getJsonValue(vfModuleModelInfo, "modelName")
128                                 execution.setVariable("DCVFM_vfModuleModelName", vfModuleModelName)
129                                 rollbackData.put("VFMODULE", "vfmodulemodelname", vfModuleModelName)
130                                 logDebug("vfModuleModelName: " + vfModuleModelName, isDebugLogEnabled)
131                                 //modelCustomizationUuid
132                                 def modelCustomizationUuid = jsonUtil.getJsonValue(vfModuleModelInfo, "modelCustomizationUuid")
133                                 execution.setVariable("DCVFM_modelCustomizationUuid", modelCustomizationUuid)
134                                 rollbackData.put("VFMODULE", "modelcustomizationuuid", modelCustomizationUuid)
135                                 logDebug("modelCustomizationUuid: " + modelCustomizationUuid, isDebugLogEnabled)
136                                 //vfModuleId
137                                 def vfModuleId = execution.getVariable("vfModuleId")
138                                 execution.setVariable("DCVFM_vfModuleId", vfModuleId)
139                                 logDebug("vfModuleId: " + vfModuleId, isDebugLogEnabled)
140                                 def requestId = execution.getVariable("msoRequestId")
141                                 execution.setVariable("DCVFM_requestId", requestId)
142                                 logDebug("requestId: " + requestId, isDebugLogEnabled)
143                                 // Set mso-request-id to request-id for VNF Adapter interface
144                                 execution.setVariable("mso-request-id", requestId)
145                                 //serviceId
146                                 def serviceId = execution.getVariable("serviceId")
147                                 execution.setVariable("DCVFM_serviceId", serviceId)
148                                 logDebug("serviceId: " + serviceId, isDebugLogEnabled)
149                                 //serviceInstanceId
150                                 def serviceInstanceId = execution.getVariable("serviceInstanceId")
151                                 execution.setVariable("DCVFM_serviceInstanceId", serviceInstanceId)
152                                 rollbackData.put("VFMODULE", "serviceInstanceId", serviceInstanceId)
153                                 logDebug("serviceInstanceId: " + serviceInstanceId, isDebugLogEnabled)
154                                 //source - HARDCODED
155                                 def source = "VID"
156                                 execution.setVariable("DCVFM_source", source)
157                                 rollbackData.put("VFMODULE", "source", source)
158                                 logDebug("source: " + source, isDebugLogEnabled)
159                                 //backoutOnFailure
160                                 def disableRollback = execution.getVariable("disableRollback")
161                                 def backoutOnFailure = true
162                                 if (disableRollback != null && disableRollback.equals("true")) {
163                                         backoutOnFailure = false
164                                 }
165                                 execution.setVariable("DCVFM_backoutOnFailure", backoutOnFailure)
166                                 logDebug("backoutOnFailure: " + backoutOnFailure, isDebugLogEnabled)
167                                 //isBaseVfModule
168                                 def isBaseVfModule = execution.getVariable("isBaseVfModule")
169                                 execution.setVariable("DCVFM_isBaseVfModule", isBaseVfModule)
170                                 logDebug("isBaseVfModule: " + isBaseVfModule, isDebugLogEnabled)
171                                 //asdcServiceModelVersion
172                                 def asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
173                                 execution.setVariable("DCVFM_asdcServiceModelVersion", asdcServiceModelVersion)
174                                 logDebug("asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugLogEnabled)
175                                 //personaModelId
176                                 execution.setVariable("DCVFM_personaModelId", jsonUtil.getJsonValue(vfModuleModelInfo, "modelInvariantId"))
177                                 //personaModelVersion
178                                 execution.setVariable("DCVFM_personaModelVersion", jsonUtil.getJsonValue(vfModuleModelInfo, "modelVersionId"))
179                                 //vfModuleLabel
180                                 def vfModuleLabel = execution.getVariable("vfModuleLabel")
181                                 if (vfModuleLabel != null) {
182                                         execution.setVariable("DCVFM_vfModuleLabel", vfModuleLabel)
183                                         logDebug("vfModuleLabel: " + vfModuleLabel, isDebugLogEnabled)
184                                 }
185                                 //Get or Generate UUID
186                                 String uuid = execution.getVariable("DCVFM_uuid")
187                                 if(uuid == null){
188                                         uuid = UUID.randomUUID()
189                                         logDebug("Generated messageId (UUID) is: " + uuid, isDebugLogEnabled)
190                                 }else{
191                                         logDebug("Found messageId (UUID) is: " + uuid, isDebugLogEnabled)
192                                 }
193                                 //isVidRequest
194                                 String isVidRequest = execution.getVariable("isVidRequest")
195                                 // default to true
196                                 if (isVidRequest == null || isVidRequest.isEmpty()) {
197                                         execution.setVariable("isVidRequest", "true")
198                                 }
199                                 //globalSubscriberId
200                                 String globalSubscriberId = execution.getVariable("globalSubscriberId")
201                                 execution.setVariable("DCVFM_globalSubscriberId", globalSubscriberId)
202                                 logDebug("globalSubsrciberId: " + globalSubscriberId, isDebugLogEnabled)                                
203                                 Map<String,String> vfModuleInputParams = execution.getVariable("vfModuleInputParams")
204                                 if (vfModuleInputParams != null) {
205                                         execution.setVariable("DCVFM_vnfParamsMap", vfModuleInputParams)
206                                         execution.setVariable("DCVFM_vnfParamsExistFlag", true)
207                                 }
208                                 //usePreload
209                                 def usePreload = execution.getVariable("usePreload")
210                                 execution.setVariable("DCVFM_usePreload", usePreload)
211                                 logDebug("usePreload: " + usePreload, isDebugLogEnabled)
212
213                         }
214                         else {
215                                 // The info is inside the request - DEAD CODE
216                                 utils.logAudit("DoCreateVfModule request: " + request)
217
218                                 //tenantId
219                                 def tenantId = ""
220                                 if (utils.nodeExists(request, "tenant-id")) {
221                                         tenantId = utils.getNodeText(request, "tenant-id")
222                                 }
223                                 execution.setVariable("DCVFM_tenantId", tenantId)
224                                 rollbackData.put("VFMODULE", "tenantid", tenantId)
225                                 //volumeGroupId
226                                 def volumeGroupId = ""
227                                 if (utils.nodeExists(request, "volume-group-id")) {
228                                         volumeGroupId = utils.getNodeText(request, "volume-group-id")
229                                 }
230                                 execution.setVariable("DCVFM_volumeGroupId", volumeGroupId)
231                                 //volumeGroupId
232                                 def volumeGroupName = ""
233                                 if (utils.nodeExists(request, "volume-group-name")) {
234                                         volumeGroupName = utils.getNodeText(request, "volume-group-name")
235                                 }
236                                 execution.setVariable("DCVFM_volumeGroupName", volumeGroupName)
237                                 //cloudSiteId
238                                 def cloudSiteId = ""
239                                 if (utils.nodeExists(request, "aic-cloud-region")) {
240                                         cloudSiteId = utils.getNodeText(request, "aic-cloud-region")
241                                 }
242                                 execution.setVariable("DCVFM_cloudSiteId", cloudSiteId)
243                                 rollbackData.put("VFMODULE", "aiccloudregion", cloudSiteId)
244                                 logDebug("cloudSiteId: " + cloudSiteId, isDebugLogEnabled)
245                                 //vnfType
246                                 def vnfType = ""
247                                 if (utils.nodeExists(request, "vnf-type")) {
248                                         vnfType = utils.getNodeText(request, "vnf-type")
249                                 }
250                                 execution.setVariable("DCVFM_vnfType", vnfType)
251                                 rollbackData.put("VFMODULE", "vnftype", vnfType)
252                                 logDebug("vnfType: " + vnfType, isDebugLogEnabled)
253                                 //vnfName
254                                 def vnfName = ""
255                                 if (utils.nodeExists(request, "vnf-name")) {
256                                         vnfName = utils.getNodeText(request, "vnf-name")
257                                 }
258                                 execution.setVariable("DCVFM_vnfName", vnfName)
259                                 rollbackData.put("VFMODULE", "vnfname", vnfName)
260                                 logDebug("vnfName: " + vnfName, isDebugLogEnabled)
261                                 //vnfId
262                                 def vnfId = ""
263                                 if (utils.nodeExists(request, "vnf-id")) {
264                                         vnfId = utils.getNodeText(request, "vnf-id")
265                                 }
266                                 execution.setVariable("DCVFM_vnfId", vnfId)
267                                 rollbackData.put("VFMODULE", "vnfid", vnfId)
268                                 logDebug("vnfId: " + vnfId, isDebugLogEnabled)
269                                 //vfModuleName
270                                 def vfModuleName = ""
271                                 if (utils.nodeExists(request, "vf-module-name")) {
272                                         vfModuleName = utils.getNodeText(request, "vf-module-name")
273                                 }
274                                 execution.setVariable("DCVFM_vfModuleName", vfModuleName)
275                                 rollbackData.put("VFMODULE", "vfmodulename", vfModuleName)
276                                 logDebug("vfModuleName: " + vfModuleName, isDebugLogEnabled)
277                                 //vfModuleModelName
278                                 def vfModuleModelName = ""
279                                 if (utils.nodeExists(request, "vf-module-model-name")) {
280                                         vfModuleModelName = utils.getNodeText(request, "vf-module-model-name")
281                                 }
282                                 execution.setVariable("DCVFM_vfModuleModelName", vfModuleModelName)
283                                 rollbackData.put("VFMODULE", "vfmodulemodelname", vfModuleModelName)
284                                 logDebug("vfModuleModelName: " + vfModuleModelName, isDebugLogEnabled)
285                                 //modelCustomizationUuid
286                                 def modelCustomizationUuid = ""
287                                 if (utils.nodeExists(request, "model-customization-id")) {
288                                         modelCustomizationUuid = utils.getNodeText(request, "model-customization-id")
289                                 }
290                                 execution.setVariable("DCVFM_modelCustomizationUuid", modelCustomizationUuid)
291                                 rollbackData.put("VFMODULE", "modelcustomizationuuid", modelCustomizationUuid)
292                                 logDebug("modelCustomizationUuid: " + modelCustomizationUuid, isDebugLogEnabled)
293                                 //vfModuleId
294                                 def vfModuleId = ""
295                                 if (utils.nodeExists(request, "vf-module-id")) {
296                                         vfModuleId = utils.getNodeText(request, "vf-module-id")
297                                 }
298                                 execution.setVariable("DCVFM_vfModuleId", vfModuleId)
299                                 logDebug("vfModuleId: " + vfModuleId, isDebugLogEnabled)
300                                 def requestId = ""
301                                 if (utils.nodeExists(request, "request-id")) {
302                                         requestId = utils.getNodeText(request, "request-id")
303                                 }
304                                 execution.setVariable("DCVFM_requestId", requestId)
305                                 logDebug("requestId: " + requestId, isDebugLogEnabled)
306                                 //serviceId
307                                 def serviceId = ""
308                                 if (utils.nodeExists(request, "service-id")) {
309                                         serviceId = utils.getNodeText(request, "service-id")
310                                 }
311                                 execution.setVariable("DCVFM_serviceId", serviceId)
312                                 logDebug("serviceId: " + serviceId, isDebugLogEnabled)
313                                 //serviceInstanceId
314                                 def serviceInstanceId = ""
315                                 if (utils.nodeExists(request, "service-instance-id")) {
316                                         serviceInstanceId = utils.getNodeText(request, "service-instance-id")
317                                 }
318                                 execution.setVariable("DCVFM_serviceInstanceId", serviceInstanceId)
319                                 rollbackData.put("VFMODULE", "serviceInstanceId", serviceInstanceId)
320                                 logDebug("serviceInstanceId: " + serviceInstanceId, isDebugLogEnabled)
321                                 //source
322                                 def source = ""
323                                 if (utils.nodeExists(request, "source")) {
324                                         source = utils.getNodeText(request, "source")
325                                 }
326                                 execution.setVariable("DCVFM_source", source)
327                                 rollbackData.put("VFMODULE", "source", source)
328                                 logDebug("source: " + source, isDebugLogEnabled)
329                                 //backoutOnFailure
330                                 NetworkUtils networkUtils = new NetworkUtils()
331                                 def backoutOnFailure = networkUtils.isRollbackEnabled(execution,request)
332                                 execution.setVariable("DCVFM_backoutOnFailure", backoutOnFailure)
333                                 logDebug("backoutOnFailure: " + backoutOnFailure, isDebugLogEnabled)
334                                 //isBaseVfModule
335                                 def isBaseVfModule = "false"
336                                 if (utils.nodeExists(request, "is-base-vf-module")) {
337                                         isBaseVfModule = utils.getNodeText(request, "is-base-vf-module")
338                                 }
339                                 execution.setVariable("DCVFM_isBaseVfModule", isBaseVfModule)
340                                 logDebug("isBaseVfModule: " + isBaseVfModule, isDebugLogEnabled)
341
342                                 //asdcServiceModelVersion
343                                 def asdcServiceModelVersion = ""
344                                 if (utils.nodeExists(request, "asdc-service-model-version")) {
345                                         asdcServiceModelVersion = utils.getNodeText(request, "asdc-service-model-version")
346                                 }
347                                 execution.setVariable("DCVFM_asdcServiceModelVersion", asdcServiceModelVersion)
348                                 logDebug("asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugLogEnabled)
349
350                                 //personaModelId
351                                 def personaModelId = ""
352                                 if (utils.nodeExists(request, "persona-model-id")) {
353                                         personaModelId = utils.getNodeText(request, "persona-model-id")
354                                 }
355                                 execution.setVariable("DCVFM_personaModelId", personaModelId)
356                                 logDebug("personaModelId: " + personaModelId, isDebugLogEnabled)
357
358                                 //personaModelVersion
359                                 def personaModelVersion = ""
360                                 if (utils.nodeExists(request, "persona-model-version")) {
361                                         personaModelVersion = utils.getNodeText(request, "persona-model-version")
362                                 }
363                                 execution.setVariable("DCVFM_personaModelVersion", personaModelVersion)
364                                 logDebug("personaModelVersion: " + personaModelVersion, isDebugLogEnabled)
365
366                                 // Process the parameters
367
368                                                 String vnfParamsChildNodes = utils.getChildNodes(request, "vnf-params")
369                                                 if(vnfParamsChildNodes == null || vnfParamsChildNodes.length() < 1){
370                                                                 utils.log("DEBUG", "Request contains NO VNF Params", isDebugLogEnabled)
371                                                 }else{
372                                                                 utils.log("DEBUG", "Request does contain VNF Params", isDebugLogEnabled)
373                                                                 execution.setVariable("DCVFM_vnfParamsExistFlag", true)
374
375                                                                 InputSource xmlSource = new InputSource(new StringReader(request));
376                                                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
377                                                                 docFactory.setNamespaceAware(true)
378                                                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
379                                                                 Document xml = docBuilder.parse(xmlSource)
380                                                                 //Get params, build map
381                                                                 Map<String, String> paramsMap = new HashMap<String, String>()
382                                                                 NodeList paramsList = xml.getElementsByTagNameNS("*", "param")
383
384                                                                 for (int z = 0; z < paramsList.getLength(); z++) {
385                                                                         Node node = paramsList.item(z)
386                                                                         String paramValue = node.getTextContent()
387                                                                         NamedNodeMap e = node.getAttributes()
388                                                                         String paramName = e.getNamedItem("name").getTextContent()
389                                                                         paramsMap.put(paramName, paramValue)
390                                                                 }
391                                                                 execution.setVariable("DCVFM_vnfParamsMap", paramsMap)
392                                                         }
393                         }
394
395                         //Get or Generate UUID
396                         String uuid = execution.getVariable("DCVFM_uuid")
397                         if(uuid == null){
398                                 uuid = UUID.randomUUID()
399                                 logDebug("Generated messageId (UUID) is: " + uuid, isDebugLogEnabled)
400                         }else{
401                                 logDebug("Found messageId (UUID) is: " + uuid, isDebugLogEnabled)
402                         }
403                         // Get sdncVersion, default to empty
404                         String sdncVersion = execution.getVariable("sdncVersion")
405                         if (sdncVersion == null) {
406                                 sdncVersion = ""
407                         }
408                         logDebug("sdncVersion: " + sdncVersion, isDebugLogEnabled)
409                         execution.setVariable("DCVFM_sdncVersion", sdncVersion)
410                         
411                         execution.setVariable("DCVFM_uuid", uuid)
412                         execution.setVariable("DCVFM_baseVfModuleId", "")
413                         execution.setVariable("DCVFM_baseVfModuleHeatStackId", "")
414                         execution.setVariable("DCVFM_heatStackId", "")
415                         execution.setVariable("DCVFM_contrailServiceInstanceFqdn", "")
416                         execution.setVariable("DCVFM_volumeGroupStackId", "")
417                         execution.setVariable("DCVFM_cloudRegionForVolume", "")
418                         execution.setVariable("DCVFM_contrailNetworkPolicyFqdnList", "")
419                         execution.setVariable("DCVFM_vnfTypeToQuery", "generic-vnf")
420                         rollbackData.put("VFMODULE", "rollbackPrepareUpdateVfModule", "false")
421                         rollbackData.put("VFMODULE", "rollbackUpdateAAIVfModule", "false")
422                         rollbackData.put("VFMODULE", "rollbackVnfAdapterCreate", "false")
423                         rollbackData.put("VFMODULE", "rollbackSDNCRequestActivate", "false")
424                         rollbackData.put("VFMODULE", "rollbackSDNCRequestAssign", "false")
425                         rollbackData.put("VFMODULE", "rollbackCreateAAIVfModule", "false")
426                         rollbackData.put("VFMODULE", "rollbackCreateNetworkPoliciesAAI", "false")
427                         rollbackData.put("VFMODULE", "rollbackUpdateVnfAAI", "false")
428
429                         String sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
430                                 if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
431                                         def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'
432                                         logError(msg)
433                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
434                                 }
435                                 execution.setVariable("DCVFM_sdncCallbackUrl", sdncCallbackUrl)
436                                 utils.logAudit("SDNC Callback URL: " + sdncCallbackUrl)
437                             logDebug("SDNC Callback URL is: " + sdncCallbackUrl, isDebugLogEnabled)
438
439
440                         execution.setVariable("RollbackData", rollbackData)
441                 }catch(BpmnError b){
442                         throw b
443                 }catch(Exception e){
444                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in PreProcess method!")
445                 }
446
447                 logDebug('Exited ' + method, isDebugLogEnabled)
448         }
449
450         /**
451          * Validates a workflow response.
452          * @param execution the execution
453          * @param responseVar the execution variable in which the response is stored
454          * @param responseCodeVar the execution variable in which the response code is stored
455          * @param errorResponseVar the execution variable in which the error response is stored
456          */
457         public void validateWorkflowResponse(Execution execution, String responseVar,
458                         String responseCodeVar, String errorResponseVar) {
459                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
460                 sdncAdapterUtils.validateSDNCResponse(execution, responseVar, responseCodeVar, errorResponseVar)
461         }
462
463
464         /**
465          * Sends the empty, synchronous response back to the API Handler.
466          * @param execution the execution
467          */
468         public void sendResponse(Execution execution) {
469                 def method = getClass().getSimpleName() + '.sendResponse(' +
470                         'execution=' + execution.getId() +
471                         ')'
472                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
473                 logDebug('Entered ' + method, isDebugLogEnabled)
474
475                 try {
476                         sendWorkflowResponse(execution, 200, "")
477                         logDebug('Exited ' + method, isDebugLogEnabled)
478                 } catch (BpmnError e) {
479                         throw e;
480                 } catch (Exception e) {
481                         logError('Caught exception in ' + method, e)
482                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Internal Error')
483                 }
484         }
485
486         /**
487          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VNF info.
488          * A 200 response is expected with the VNF info in the response body. Will find out the base module info
489          * and existing VNF's name for add-on modules
490          *
491          * @param execution The flow's execution instance.
492          */
493         public void postProcessCreateAAIVfModule(Execution execution) {
494                 def method = getClass().getSimpleName() + '.getVfModule(' +
495                         'execution=' + execution.getId() +
496                         ')'
497                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
498                 logDebug('Entered ' + method, isDebugLogEnabled)
499
500                 try {
501                         def createResponse = execution.getVariable('DCVFM_createVfModuleResponse')
502                         utils.logAudit("createVfModule Response: " + createResponse)
503
504                         def rollbackData = execution.getVariable("RollbackData")
505                         String vnfName = utils.getNodeText1(createResponse, 'vnf-name')
506                         if (vnfName != null) {
507                                 execution.setVariable('DCVFM_vnfName', vnfName)
508                                 logDebug("vnfName retrieved from AAI is: " + vnfName, isDebugLogEnabled)
509                                 rollbackData.put("VFMODULE", "vnfname", vnfName)
510                         }
511                         String vnfId = utils.getNodeText1(createResponse, 'vnf-id')
512                         execution.setVariable('DCVFM_vnfId', vnfId)
513                         logDebug("vnfId is: " + vnfId, isDebugLogEnabled)
514                         String vfModuleId = utils.getNodeText1(createResponse, 'vf-module-id')
515                         execution.setVariable('DCVFM_vfModuleId', vfModuleId)
516                         logDebug("vfModuleId is: " + vfModuleId, isDebugLogEnabled)
517                         String vfModuleIndex= utils.getNodeText1(createResponse, 'vf-module-index')
518                         execution.setVariable('DCVFM_vfModuleIndex', vfModuleIndex)
519                         logDebug("vfModuleIndex is: " + vfModuleIndex, isDebugLogEnabled)
520                         rollbackData.put("VFMODULE", "vnfid", vnfId)
521                         rollbackData.put("VFMODULE", "vfmoduleid", vfModuleId)
522                         rollbackData.put("VFMODULE", "rollbackCreateAAIVfModule", "true")
523                         rollbackData.put("VFMODULE", "rollbackPrepareUpdateVfModule", "true")
524                         execution.setVariable("RollbackData", rollbackData)
525                 } catch (Exception ex) {
526                                 ex.printStackTrace()
527                                 logDebug('Exception occurred while postProcessing CreateAAIVfModule request:' + ex.getMessage(),isDebugLogEnabled)
528                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Bad response from CreateAAIVfModule' + ex.getMessage())
529                 }
530                 logDebug('Exited ' + method, isDebugLogEnabled)
531         }
532
533
534         /**
535          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VNF info.
536          * A 200 response is expected with the VNF info in the response body. Will find out the base module info.
537          *
538          * @param execution The flow's execution instance.
539          */
540         public void queryAAIVfModule(Execution execution) {
541                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
542                 def method = getClass().getSimpleName() + '.getVfModule(' +
543                         'execution=' + execution.getId() +
544                         ')'
545                 logDebug('Entered ' + method, isDebugLogEnabled)
546
547                 try {
548                         def vnfId = execution.getVariable('DCVFM_vnfId')
549                         def vfModuleId = execution.getVariable('DCVFM_vfModuleId')
550
551                         AaiUtil aaiUriUtil = new AaiUtil(this)
552                         String  aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
553                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
554
555                         String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
556                         utils.logAudit("AAI endPoint: " + endPoint)
557
558                         try {
559                                 RESTConfig config = new RESTConfig(endPoint);
560                                 def responseData = ''
561                                 def aaiRequestId = UUID.randomUUID().toString()
562                                 RESTClient client = new RESTClient(config).
563                                         addHeader('X-TransactionId', aaiRequestId).
564                                         addHeader('X-FromAppId', 'MSO').
565                                         addHeader('Content-Type', 'application/xml').
566                                         addHeader('Accept','application/xml');
567                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
568                                 APIResponse response = client.httpGet()
569                                 utils.logAudit("createVfModule - invoking httpGet() to AAI")
570
571                                 responseData = response.getResponseBodyAsString()
572                                 if (responseData != null) {
573                                         logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)
574
575                                 }
576
577                                 utils.logAudit("createVfModule - queryAAIVfModule Response: " + responseData)
578                                 utils.logAudit("createVfModule - queryAAIVfModule ResponseCode: " + response.getStatusCode())
579
580                                 execution.setVariable('DCVFM_queryAAIVfModuleResponseCode', response.getStatusCode())
581                                 execution.setVariable('DCVFM_queryAAIVfModuleResponse', responseData)
582                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
583                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
584                                 if (response.getStatusCode() == 200) {
585                                         // Parse the VNF record from A&AI to find base module info
586                                         logDebug('Parsing the VNF data to find base module info', isDebugLogEnabled)
587                                         if (responseData != null) {
588                                                 def vfModulesText = utils.getNodeXml(responseData, "vf-modules")
589                                                 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
590                                                 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
591                                                 int vfModulesSize = 0
592                                                 for (i in 0..vfModules.size()-1) {
593                                                         def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
594                                                         def isBaseVfModule = utils.getNodeText(vfModuleXml, "is-base-vf-module")
595
596                                                         if (isBaseVfModule == "true") {
597                                                             String baseModuleId = utils.getNodeText1(vfModuleXml, "vf-module-id")
598                                                             execution.setVariable("DCVFM_baseVfModuleId", baseModuleId)
599                                                             logDebug('Received baseVfModuleId: ' + baseModuleId, isDebugLogEnabled)
600                                                             String baseModuleHeatStackId = utils.getNodeText1(vfModuleXml, "heat-stack-id")
601                                                             execution.setVariable("DCVFM_baseVfModuleHeatStackId", baseModuleHeatStackId)
602                                                             logDebug('Received baseVfModuleHeatStackId: ' + baseModuleHeatStackId, isDebugLogEnabled)
603                                                         }
604                                                 }
605                                         }
606                                 }
607                         } catch (Exception ex) {
608                                 ex.printStackTrace()
609                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
610                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
611                         }
612                         logDebug('Exited ' + method, isDebugLogEnabled)
613                 } catch (BpmnError e) {
614                         throw e;
615                 } catch (Exception e) {
616                         logError('Caught exception in ' + method, e)
617                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())
618                 }
619         }
620
621
622         public void preProcessSDNCAssignRequest(Execution execution){
623                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
624                 execution.setVariable("prefix", Prefix)
625                 logDebug(" ======== STARTED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
626                 def vnfId = execution.getVariable("DCVFM_vnfId")
627                 def vfModuleId = execution.getVariable("DCVFM_vfModuleId")
628                 def serviceInstanceId = execution.getVariable("DCVFM_serviceInstanceId")
629                 logDebug("NEW VNF ID: " + vnfId, isDebugLogEnabled)
630                 utils.logAudit("NEW VNF ID: " + vnfId)
631
632                 try{
633
634                         //Build SDNC Request
635
636                         def svcInstId = ""
637                         if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {
638                                 svcInstId = vfModuleId
639                         }
640                         else {
641                                 svcInstId = serviceInstanceId
642                         }
643
644                         String assignSDNCRequest = buildSDNCRequest(execution, svcInstId, "assign")
645
646                         assignSDNCRequest = utils.formatXml(assignSDNCRequest)
647                         execution.setVariable("DCVFM_assignSDNCRequest", assignSDNCRequest)
648                         logDebug("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest, isDebugLogEnabled)
649                         utils.logAudit("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest)
650
651                 }catch(Exception e){
652                         utils.log("ERROR", "Exception Occured Processing preProcessSDNCAssignRequest. Exception is:\n" + e, isDebugLogEnabled)
653                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareProvision Method:\n" + e.getMessage())
654                 }
655                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
656         }
657
658         public void preProcessSDNCGetRequest(Execution execution, String element){
659                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
660                 String sdncVersion = execution.getVariable("DCVFM_sdncVersion")
661                 execution.setVariable("prefix", Prefix)
662                 utils.log("DEBUG", " ======== STARTED preProcessSDNCGetRequest Process ======== ", isDebugLogEnabled)
663                 try{
664                         def serviceInstanceId = execution.getVariable('DCVFM_serviceInstanceId')
665                         
666                         String uuid = execution.getVariable('testReqId') // for junits
667                         if(uuid==null){
668                                 uuid = execution.getVariable("mso-request-id") + "-" +  System.currentTimeMillis()
669                         }
670                                         
671                         def callbackUrl = execution.getVariable("DCVFM_sdncCallbackUrl")
672                         utils.logAudit("callbackUrl:" + callbackUrl)
673                         
674                         def vfModuleId = execution.getVariable('DCVFM_vfModuleId')
675                         
676                         def svcInstId = ""
677                         if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {
678                                 svcInstId = vfModuleId
679                         }
680                         else {
681                                 svcInstId = serviceInstanceId
682                         }
683                         
684                         def msoAction = ""
685                         if (!sdncVersion.equals("1707")) {
686                                 msoAction = "mobility"
687                         }
688                         else {
689                                 msoAction = "vfmodule"
690                         }
691                         // For VNF, serviceOperation (URI for topology GET) will be retrieved from "selflink" element 
692                         // in the response from GenericGetVnf
693                         // For VF Module, in 1707 serviceOperation will be retrieved from "object-path" element
694                         // in SDNC Assign Response
695                         // For VF Module for older versions, serviceOperation is constructed using vfModuleId
696                         
697                         String serviceOperation = ""
698                         if (element.equals("vnf")) {
699                                 def vnfQueryResponse = execution.getVariable("DCVFM_vnfQueryResponse")
700                                 serviceOperation = utils.getNodeText1(vnfQueryResponse, "selflink")
701                                 utils.log("DEBUG", "VNF - service operation: " + serviceOperation, isDebugLogEnabled)
702                         }
703                         else if (element.equals("vfmodule")) {
704                                 String response = execution.getVariable("DCVFM_assignSDNCAdapterResponse")
705                                 utils.logAudit("DCVFM_assignSDNCAdapterResponse is: \n" + response)                                                     
706                         
707                                 if (!sdncVersion.equals("1707")) {
708                                         serviceOperation = "/VNF-API:vnfs/vnf-list/" + vfModuleId
709                                         utils.log("DEBUG", "VF Module with sdncVersion before 1707 - service operation: " + serviceOperation, isDebugLogEnabled)
710                                 }
711                                 else {                          
712                                         String data = utils.getNodeXml(response, "response-data")                                       
713                                         data = data.replaceAll("&lt;", "<")
714                                         data = data.replaceAll("&gt;", ">")
715                                         utils.log("DEBUG", "responseData: " + data, isDebugLogEnabled)
716                                         serviceOperation = utils.getNodeText1(data, "object-path")
717                                         utils.log("DEBUG", "VF Module with sdncVersion of 1707 - service operation: " + serviceOperation, isDebugLogEnabled)
718                                 }                               
719                         }               
720
721                         //!!!! TEMPORARY WORKAROUND FOR SDNC REPLICATION ISSUE
722                         sleep(5000)
723
724                         String SDNCGetRequest =
725                                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
726                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
727                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
728                                         <sdncadapter:RequestHeader>
729                                         <sdncadapter:RequestId>${uuid}</sdncadapter:RequestId>
730                                         <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
731                                         <sdncadapter:SvcAction>query</sdncadapter:SvcAction>
732                                         <sdncadapter:SvcOperation>${serviceOperation}</sdncadapter:SvcOperation>
733                                         <sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
734                                         <sdncadapter:MsoAction>${msoAction}</sdncadapter:MsoAction>
735                                 </sdncadapter:RequestHeader>
736                                         <sdncadapterworkflow:SDNCRequestData></sdncadapterworkflow:SDNCRequestData>
737                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
738
739                         utils.logAudit("SDNCGetRequest: \n" + SDNCGetRequest)
740                         execution.setVariable("DCVFM_getSDNCRequest", SDNCGetRequest)
741                         utils.log("DEBUG", "Outgoing GetSDNCRequest is: \n" + SDNCGetRequest, isDebugLogEnabled)
742
743                 }catch(Exception e){
744                         utils.log("ERROR", "Exception Occurred Processing preProcessSDNCGetRequest. Exception is:\n" + e, isDebugLogEnabled)
745                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during prepareProvision Method:\n" + e.getMessage())
746                 }
747                 utils.log("DEBUG", "======== COMPLETED preProcessSDNCGetRequest Process ======== ", isDebugLogEnabled)
748         }
749
750
751         public void preProcessVNFAdapterRequest(Execution execution) {
752                 def method = getClass().getSimpleName() + '.VNFAdapterCreateVfModule(' +
753                         'execution=' + execution.getId() +
754                         ')'
755                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
756                 logDebug('Entered ' + method, isDebugLogEnabled)
757
758                 //def xml = execution.getVariable("DoCreateVfModuleRequest")
759                 //logDebug('VNF REQUEST is: ' + xml, isDebugLogEnabled)
760
761                 //Get variables
762                 //cloudSiteId
763                 def cloudSiteId = execution.getVariable("DCVFM_cloudSiteId")
764                 //tenantId
765                 def tenantId = execution.getVariable("DCVFM_tenantId")
766                 //vnfType
767                 def vnfType = execution.getVariable("DCVFM_vnfType")
768                 //vnfName
769                 def vnfName = execution.getVariable("DCVFM_vnfName")
770                 //vnfId
771                 def vnfId = execution.getVariable("DCVFM_vnfId")
772                 //vfModuleName
773                 def vfModuleName = execution.getVariable("DCVFM_vfModuleName")
774                 //vfModuleModelName
775                 def vfModuleModelName = execution.getVariable("DCVFM_vfModuleModelName")
776                 //vfModuleId
777                 def vfModuleId = execution.getVariable("DCVFM_vfModuleId")
778                 //vfModuleIndex
779                 def vfModuleIndex = execution.getVariable("DCVFM_vfModuleIndex")
780                 //requestId
781                 def requestId = execution.getVariable("DCVFM_requestId")
782                 //serviceId
783                 def serviceId = execution.getVariable("DCVFM_serviceId")
784                 //serviceInstanceId
785                 def serviceInstanceId = execution.getVariable("DCVFM_serviceInstanceId")
786                 //backoutOnFailure
787                 def backoutOnFailure = execution.getVariable("DCVFM_backoutOnFailure")
788                 //volumeGroupId
789                 def volumeGroupId = execution.getVariable("DCVFM_volumeGroupId")
790                 // baseVfModuleId
791                 def baseVfModuleId = execution.getVariable("DCVFM_baseVfModuleId")
792                 // baseVfModuleStackId
793                 def baseVfModuleStackId = execution.getVariable("DCVFM_baseVfModuleHeatStackId")
794                 // asdcServiceModelVersion
795                 def asdcServiceModelVersion = execution.getVariable("DCVFM_asdcServiceModelVersion")
796                 //volumeGroupStackId
797                 def volumeGroupStackId = execution.getVariable("DCVFM_volumeGroupStackId")
798                 //modelCustomizationUuid
799                 def modelCustomizationUuid = execution.getVariable("DCVFM_modelCustomizationUuid")
800
801                 def messageId = execution.getVariable('mso-request-id') + '-' +
802                                 System.currentTimeMillis()
803
804                 def notificationUrl = createCallbackURL(execution, "VNFAResponse", messageId)
805                 def useQualifiedHostName = execution.getVariable("URN_mso_use_qualified_host")
806
807                 utils.logAudit("notificationUrl: " + notificationUrl)
808                 utils.logAudit("QualifiedHostName: " + useQualifiedHostName)
809
810                 if ('true'.equals(useQualifiedHostName)) {
811                         notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
812                 }
813
814                 Map<String, String> vnfParamsMap = execution.getVariable("DCVFM_vnfParamsMap")
815                 
816                 //Get SDNC Response Data for VnfSubCreate Request
817                 String sdncGetResponse = execution.getVariable('DCVFM_getSDNCAdapterResponse')
818                 utils.logAudit("sdncGetResponse: " + sdncGetResponse)
819
820                 String vfModuleParams = buildVfModuleParams(vnfParamsMap, sdncGetResponse, vnfId, vnfName,
821                                 vfModuleId, vfModuleName, vfModuleIndex)
822
823                 def svcInstId = ""
824                 if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {
825                                 svcInstId = serviceId
826                 }
827                 else {
828                         svcInstId = serviceInstanceId
829                 }
830
831                 def createVnfARequest = """
832                 <createVfModuleRequest>
833                 <cloudSiteId>${cloudSiteId}</cloudSiteId>
834                 <tenantId>${tenantId}</tenantId>
835                 <vnfId>${vnfId}</vnfId>
836                 <vnfName>${vnfName}</vnfName>
837                 <vfModuleName>${vfModuleName}</vfModuleName>
838                 <vfModuleId>${vfModuleId}</vfModuleId>
839                 <vnfType>${vnfType}</vnfType>
840                 <vfModuleType>${vfModuleModelName}</vfModuleType>
841                 <vnfVersion>${asdcServiceModelVersion}</vnfVersion>
842                 <modelCustomizationUuid>${modelCustomizationUuid}</modelCustomizationUuid>
843                 <requestType></requestType>
844                 <volumeGroupId>${volumeGroupId}</volumeGroupId>
845         <volumeGroupStackId>${volumeGroupStackId}</volumeGroupStackId>
846         <baseVfModuleId>${baseVfModuleId}</baseVfModuleId>
847         <baseVfModuleStackId>${baseVfModuleStackId}</baseVfModuleStackId>
848         <skipAAI>true</skipAAI>
849         <backout>${backoutOnFailure}</backout>
850         <failIfExists>true</failIfExists>
851                 <vfModuleParams>
852                 ${vfModuleParams}
853                 </vfModuleParams>
854                 <msoRequest>
855                         <requestId>${requestId}</requestId>
856                         <serviceInstanceId>${svcInstId}</serviceInstanceId>
857                 </msoRequest>
858                 <messageId>${messageId}</messageId>
859                 <notificationUrl>${notificationUrl}</notificationUrl>
860                 </createVfModuleRequest>"""
861
862                 utils.logAudit("Create VfModule Request to VNF Adapter : " + createVnfARequest)
863                 logDebug("Create VfModule Request to VNF Adapter: " + createVnfARequest, isDebugLogEnabled)
864                 execution.setVariable("DCVFM_createVnfARequest", createVnfARequest)
865         }
866
867         /**
868          * Validates the request, request id and service instance id.  If a problem is found,
869          * a WorkflowException is generated and an MSOWorkflowException event is thrown. This
870          * method also sets up the log context for the workflow.
871          * @param execution the execution
872          * @return the validated request
873          */
874         public String validateInfraRequest(Execution execution) {
875                 def method = getClass().getSimpleName() + '.validateInfraRequest(' +
876                         'execution=' + execution.getId() +
877                         ')'
878                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
879                 logDebug('Entered ' + method, isDebugLogEnabled)
880
881                 String processKey = getProcessKey(execution);
882                 def prefix = execution.getVariable("prefix")
883
884                 if (prefix == null) {
885                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " prefix is null")
886                 }
887
888                 try {
889                         def request = execution.getVariable(prefix + 'Request')
890
891                         if (request == null) {
892                                 request = execution.getVariable(processKey + 'Request')
893
894                                 if (request == null) {
895                                         request = execution.getVariable('bpmnRequest')
896                                 }
897
898                                 setVariable(execution, processKey + 'Request', null);
899                                 setVariable(execution, 'bpmnRequest', null);
900                                 setVariable(execution, prefix + 'Request', request);
901                         }
902
903                         if (request == null) {
904                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request is null")
905                         }
906                         utils.logAudit("DoCreateVfModule Request: " + request)
907
908                         /*
909
910                         def requestId = execution.getVariable("mso-request-id")
911
912                         if (requestId == null) {
913                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request has no mso-request-id")
914                         }
915
916                         def serviceInstanceId = execution.getVariable("mso-service-instance-id")
917
918                         if (serviceInstanceId == null) {
919                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request message has no mso-service-instance-id")
920                         }
921
922                         utils.logContext(requestId, serviceInstanceId)
923                         */
924                         logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
925                         logDebug('Exited ' + method, isDebugLogEnabled)
926                         return request
927                 } catch (BpmnError e) {
928                         throw e;
929                 } catch (Exception e) {
930                         logError('Caught exception in ' + method, e)
931                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")
932                 }
933         }
934
935         public boolean isVolumeGroupIdPresent(Execution execution) {
936
937                 def method = getClass().getSimpleName() + '.isVolumeGroupIdPresent(' +
938                         'execution=' + execution.getId() +
939                         ')'
940                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
941                 logDebug('Entered ' + method, isDebugLogEnabled)
942
943                 def request = execution.getVariable('DoCreateVfModuleRequest')
944                 String volumeGroupId = utils.getNodeText1(request, "volume-group-id")
945                 if (volumeGroupId == null || volumeGroupId.isEmpty()) {
946                         logDebug('No volume group id is present', isDebugLogEnabled)
947                         return false
948                 }
949                 else {
950                         logDebug('Volume group id is present', isDebugLogEnabled)
951                         return true
952                 }
953
954         }
955
956         public boolean isVolumeGroupNamePresent(Execution execution) {
957
958                 def method = getClass().getSimpleName() + '.isVolumeGroupNamePresent(' +
959                         'execution=' + execution.getId() +
960                         ')'
961                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
962                 logDebug('Entered ' + method, isDebugLogEnabled)
963
964                 def request = execution.getVariable('DoCreateVfModuleRequest')
965                 String volumeGroupName = utils.getNodeText1(request, "volume-group-name")
966                 if (volumeGroupName == null || volumeGroupName.isEmpty()) {
967                         logDebug('No volume group name is present', isDebugLogEnabled)
968                         return false
969                 }
970                 else {
971                         logDebug('Volume group name is present', isDebugLogEnabled)
972                         return true
973                 }
974
975         }
976
977         public String buildSDNCRequest(Execution execution, String svcInstId, String action){
978
979                 String uuid = execution.getVariable('testReqId') // for junits
980                 if(uuid==null){
981                         uuid = execution.getVariable("mso-request-id") + "-" +          System.currentTimeMillis()
982                 }
983                 def callbackURL = execution.getVariable("DCVFM_sdncCallbackUrl")
984                 def requestId = execution.getVariable("DCVFM_requestId")
985                 def serviceId = execution.getVariable("DCVFM_serviceId")
986                 def vnfType = execution.getVariable("DCVFM_vnfType")
987                 def vnfName = execution.getVariable("DCVFM_vnfName")
988                 def tenantId = execution.getVariable("DCVFM_tenantId")
989                 def source = execution.getVariable("DCVFM_source")
990                 def backoutOnFailure = execution.getVariable("DCVFM_backoutOnFailure")
991                 def vfModuleId = execution.getVariable("DCVFM_vfModuleId")
992                 def vfModuleName = execution.getVariable("DCVFM_vfModuleName")
993                 def vfModuleModelName = execution.getVariable("DCVFM_vfModuleModelName")
994                 def vnfId = execution.getVariable("DCVFM_vnfId")
995                 def cloudSiteId = execution.getVariable("DCVFM_cloudSiteId")
996                 def sdncVersion = execution.getVariable("DCVFM_sdncVersion")
997                 def serviceModelInfo = execution.getVariable("serviceModelInfo")
998                 def vnfModelInfo = execution.getVariable("vnfModelInfo")
999                 def vfModuleModelInfo = execution.getVariable("vfModuleModelInfo")
1000                 String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
1001                 String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
1002                 String vfModuleEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vfModuleModelInfo)
1003                 def globalSubscriberId = execution.getVariable("DCVFM_globalSubscriberId")
1004                 boolean usePreload = execution.getVariable("DCVFM_usePreload")
1005                 String usePreloadToSDNC = usePreload ? "Y" : "N"
1006                 def modelCustomizationUuid = execution.getVariable("DCVFM_modelCustomizationUuid")
1007                 def modelCustomizationUuidString = ""
1008                 if (!usePreload) {
1009                         modelCustomizationUuidString = "<modelCustomizationUuid>" + modelCustomizationUuid + "</modelCustomizationUuid>"
1010                 }
1011
1012                 String sdncVNFParamsXml = ""
1013
1014                 if(execution.getVariable("DCVFM_vnfParamsExistFlag") == true){
1015                         sdncVNFParamsXml = buildSDNCParamsXml(execution)
1016                 }else{
1017                         sdncVNFParamsXml = ""
1018                 }
1019                 
1020                 String sdncRequest = ""
1021                 
1022                 if (!sdncVersion.equals("1707")) {
1023
1024                         sdncRequest =
1025                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
1026                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
1027                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
1028            <sdncadapter:RequestHeader>
1029                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
1030                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
1031                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
1032                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
1033                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
1034                 </sdncadapter:RequestHeader>
1035         <sdncadapterworkflow:SDNCRequestData>
1036                 <request-information>
1037                         <request-id>${requestId}</request-id>
1038                         <request-action>VNFActivateRequest</request-action>
1039                         <source>${source}</source>
1040                         <notification-url/>
1041                 </request-information>
1042                 <service-information>
1043                         <service-id>${serviceId}</service-id>
1044                         <service-type>${serviceId}</service-type>
1045                         <service-instance-id>${svcInstId}</service-instance-id>
1046                         <subscriber-name>notsurewecare</subscriber-name>
1047                 </service-information>
1048                 <vnf-request-information>
1049                         <vnf-id>${vfModuleId}</vnf-id>
1050                         <vnf-type>${vfModuleModelName}</vnf-type>
1051                         <vnf-name>${vfModuleName}</vnf-name>
1052                         <generic-vnf-id>${vnfId}</generic-vnf-id>
1053                         <generic-vnf-name>${vnfName}</generic-vnf-name>
1054                         <generic-vnf-type>${vnfType}</generic-vnf-type>
1055                         <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
1056                         <tenant>${tenantId}</tenant>
1057                         ${modelCustomizationUuidString}
1058                         <use-preload>${usePreloadToSDNC}</use-preload>
1059                 ${sdncVNFParamsXml}
1060                 </vnf-request-information>
1061         </sdncadapterworkflow:SDNCRequestData>
1062         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
1063                 
1064                 }
1065                 else {  
1066                         
1067                         sdncRequest =
1068                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
1069                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
1070                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
1071            <sdncadapter:RequestHeader>
1072                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
1073                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
1074                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
1075                                 <sdncadapter:SvcOperation>vf-module-topology-operation</sdncadapter:SvcOperation>
1076                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
1077                                 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
1078                 </sdncadapter:RequestHeader>
1079         <sdncadapterworkflow:SDNCRequestData>
1080                 <request-information>
1081                         <request-id>${requestId}</request-id>
1082                         <request-action>CreateVfModuleInstance</request-action>
1083                         <source>${source}</source>
1084                         <notification-url/>
1085                 </request-information>
1086                 <service-information>
1087                         <service-id>${serviceId}</service-id>
1088                         <subscription-service-type>${serviceId}</subscription-service-type>
1089                         ${serviceEcompModelInformation}
1090                         <service-instance-id>${svcInstId}</service-instance-id>
1091                         <global-customer-id>${globalSubscriberId}</global-customer-id>                  
1092                 </service-information>          
1093                 <vnf-information>
1094                         <vnf-id>${vnfId}</vnf-id>
1095                         <vnf-type>${vnfType}</vnf-type>
1096                         ${vnfEcompModelInformation}                     
1097                 </vnf-information>
1098                 <vf-module-information>
1099                         <vf-module-id>${vfModuleId}</vf-module-id>
1100                         <vf-module-type>${vfModuleModelName}</vf-module-type>
1101                         ${vfModuleEcompModelInformation}                        
1102                 </vf-module-information>
1103                 <vf-module-request-input>                       
1104                         <vf-module-name>${vfModuleName}</vf-module-name>
1105                         <tenant>${tenantId}</tenant>
1106                         <aic-cloud-region>${cloudSiteId}</aic-cloud-region>                     
1107                 ${sdncVNFParamsXml}
1108                 </vf-module-request-input>
1109           </sdncadapterworkflow:SDNCRequestData>
1110         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
1111                         
1112                         
1113                         /*
1114                         sdncRequest =
1115                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
1116                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
1117                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
1118            <sdncadapter:RequestHeader>
1119                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
1120                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
1121                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
1122                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
1123                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
1124                 </sdncadapter:RequestHeader>
1125         <sdncadapterworkflow:SDNCRequestData>
1126                 <request-information>
1127                         <request-id>${requestId}</request-id>
1128                         <request-action>CreateVfModuleInstance</request-action>
1129                         <source>${source}</source>
1130                         <notification-url/>
1131                 </request-information>
1132                 <service-information>
1133                         <service-id>${serviceId}</service-id>
1134                         <service-type>${serviceId}</service-type>
1135                         ${serviceEcompModelInformation}                 
1136                         <service-instance-id>${svcInstId}</service-instance-id>
1137                         <global-customer-id>${globalSubscriberId}</global-customer-id>
1138                 </service-information>
1139                 <vnf-information>
1140                         <vnf-id>${vnfId}</vnf-id>
1141                         <vnf-type>${vnfType}</vnf-type>
1142                         ${vnfEcompModelInformation}                     
1143                 </vnf-information>
1144                 <vf-module-information>
1145                         <vf-module-id>${vfModuleId}</vf-module-id>
1146                         <vf-module-type>${vfModuleModelName}</vf-module-type>
1147                         ${vfModuleEcompModelInformation}                        
1148                 </vf-module-information>
1149                 <vf-module-request-input>                       
1150                         <vf-module-name>${vfModuleName}</vf-module-name>
1151                         <tenant>${tenantId}</tenant>
1152                         <aic-cloud-region>${cloudSiteId}</aic-cloud-region>                             
1153                 ${sdncVNFParamsXml}
1154                 </vf-module-request-input>              
1155         </sdncadapterworkflow:SDNCRequestData>
1156         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
1157                         */
1158                         
1159                 }
1160
1161         utils.logAudit("sdncRequest:  " + sdncRequest)
1162         return sdncRequest
1163
1164         }
1165
1166         public void preProcessSDNCActivateRequest(Execution execution) {
1167                 def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
1168                         'execution=' + execution.getId() +
1169                         ')'
1170                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1171                 logDebug('Entered ' + method, isDebugLogEnabled)
1172                 execution.setVariable("prefix", Prefix)
1173                 logDebug(" ======== STARTED preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
1174                 try{
1175                         String vnfId = execution.getVariable("DCVFM_vnfId")
1176                         String vfModuleId = execution.getVariable("DCVFM_vfModuleId")
1177                         String serviceInstanceId = execution.getVariable("DCVFM_serviceInstanceId")
1178
1179                         def svcInstId = ""
1180                         if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {
1181                                 svcInstId = vfModuleId
1182                         }
1183                         else {
1184                                 svcInstId = serviceInstanceId
1185                         }
1186                         String activateSDNCRequest = buildSDNCRequest(execution, svcInstId, "activate")
1187
1188                         execution.setVariable("DCVFM_activateSDNCRequest", activateSDNCRequest)
1189                         logDebug("Outgoing CommitSDNCRequest is: \n" + activateSDNCRequest, isDebugLogEnabled)
1190                         utils.logAudit("Outgoing CommitSDNCRequest is: \n"  + activateSDNCRequest)
1191
1192                 }catch(Exception e){
1193                         log.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e, isDebugLogEnabled)
1194                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
1195                 }
1196                 logDebug("======== COMPLETED  preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
1197         }
1198
1199         public void postProcessVNFAdapterRequest(Execution execution) {
1200                 def method = getClass().getSimpleName() + '.postProcessVNFAdapterRequest(' +
1201                         'execution=' + execution.getId() +
1202                         ')'
1203                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1204                 logDebug('Entered ' + method, isDebugLogEnabled)
1205                 execution.setVariable("prefix",Prefix)
1206                 try{
1207                 logDebug(" *** STARTED postProcessVNFAdapterRequest Process*** ", isDebugLogEnabled)
1208
1209                 String vnfResponse = execution.getVariable("DCVFM_createVnfAResponse")
1210                 logDebug("VNF Adapter Response is: " + vnfResponse, isDebugLogEnabled)
1211                 utils.logAudit("createVnfAResponse is: \n"  + vnfResponse)
1212
1213                 RollbackData rollbackData = execution.getVariable("RollbackData")
1214                 if(vnfResponse != null){
1215
1216                         if(vnfResponse.contains("createVfModuleResponse")){
1217                                 logDebug("Received a Good Response from VNF Adapter for CREATE_VF_MODULE Call.", isDebugLogEnabled)
1218                                 execution.setVariable("DCVFM_vnfVfModuleCreateCompleted", true)
1219                                 String heatStackId = utils.getNodeText1(vnfResponse, "vfModuleStackId")
1220                                 execution.setVariable("DCVFM_heatStackId", heatStackId)
1221                                 logDebug("Received heat stack id from VNF Adapter: " + heatStackId, isDebugLogEnabled)
1222                                 rollbackData.put("VFMODULE", "heatstackid", heatStackId)
1223                                 // Parse vnfOutputs for network_fqdn
1224                                 if (vnfResponse.contains("vfModuleOutputs")) {
1225                                         def vfModuleOutputsXml = utils.getNodeXml(vnfResponse, "vfModuleOutputs")
1226                                         InputSource source = new InputSource(new StringReader(vfModuleOutputsXml));
1227                         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
1228                         docFactory.setNamespaceAware(true)
1229                         DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
1230                         Document outputsXml = docBuilder.parse(source)
1231
1232                                         NodeList entries = outputsXml.getElementsByTagNameNS("*", "entry")
1233                                         List contrailNetworkPolicyFqdnList = []
1234                                         for (int i = 0; i< entries.getLength(); i++) {
1235                                                 Node node = entries.item(i)
1236                                                 if (node.getNodeType() == Node.ELEMENT_NODE) {
1237                                                         Element element = (Element) node
1238                                                         String key = element.getElementsByTagNameNS("*", "key").item(0).getTextContent()
1239                                                         if (key.equals("contrail-service-instance-fqdn")) {
1240                                                                 String contrailServiceInstanceFqdn = element.getElementsByTagNameNS("*", "value").item(0).getTextContent()
1241                                                                 logDebug("Obtained contrailServiceInstanceFqdn: " + contrailServiceInstanceFqdn, isDebugLogEnabled)
1242                                                                 execution.setVariable("DCVFM_contrailServiceInstanceFqdn", contrailServiceInstanceFqdn)
1243                                                         }
1244                                                         else if (key.endsWith("contrail_network_policy_fqdn")) {
1245                                                                 String contrailNetworkPolicyFqdn = element.getElementsByTagNameNS("*", "value").item(0).getTextContent()
1246                                                                 logDebug("Obtained contrailNetworkPolicyFqdn: " + contrailNetworkPolicyFqdn, isDebugLogEnabled)
1247                                                                 contrailNetworkPolicyFqdnList.add(contrailNetworkPolicyFqdn)
1248                                                         }
1249                                                         else if (key.equals("oam_management_v4_address")) {
1250                                                                 String oamManagementV4Address = element.getElementsByTagNameNS("*", "value").item(0).getTextContent()
1251                                                                 logDebug("Obtained oamManagementV4Address: " + oamManagementV4Address, isDebugLogEnabled)
1252                                                                 execution.setVariable("DCVFM_oamManagementV4Address", oamManagementV4Address)
1253                                                         }
1254                                                         else if (key.equals("oam_management_v6_address")) {
1255                                                                 String oamManagementV6Address = element.getElementsByTagNameNS("*", "value").item(0).getTextContent()
1256                                                                 logDebug("Obtained oamManagementV6Address: " + oamManagementV6Address, isDebugLogEnabled)
1257                                                                 execution.setVariable("DCVFM_oamManagementV6Address", oamManagementV6Address)
1258                                                         }
1259
1260                                                 }
1261                                         }
1262                                         if (!contrailNetworkPolicyFqdnList.isEmpty()) {
1263                                                 execution.setVariable("DCVFM_contrailNetworkPolicyFqdnList", contrailNetworkPolicyFqdnList)
1264                                         }
1265                                 }
1266                         }else{
1267                                 logDebug("Received a BAD Response from VNF Adapter for CREATE_VF_MODULE Call.", isDebugLogEnabled)
1268                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "VNF Adapter Error")
1269                         }
1270                 }else{
1271                         logDebug("Response from VNF Adapter is Null for CREATE_VF_MODULE Call.", isDebugLogEnabled)
1272                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Empty response from VNF Adapter")
1273                 }
1274
1275                 rollbackData.put("VFMODULE", "rollbackVnfAdapterCreate", "true")
1276                 execution.setVariable("RollbackData", rollbackData)
1277
1278                 }catch(BpmnError b){
1279                         throw b
1280                 }catch(Exception e){
1281                         logDebug("Internal Error Occured in PostProcess Method", isDebugLogEnabled)
1282                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Internal Error Occured in PostProcess Method")
1283                 }
1284                 logDebug(" *** COMPLETED postProcessVnfAdapterResponse Process*** ", isDebugLogEnabled)
1285         }
1286
1287
1288         public void preProcessUpdateAAIVfModuleRequestOrch(Execution execution) {
1289                 def method = getClass().getSimpleName() + '.preProcessUpdateAAIVfModuleRequestOrch(' +
1290                         'execution=' + execution.getId() +
1291                         ')'
1292                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1293                 logDebug('Entered ' + method, isDebugLogEnabled)
1294                 execution.setVariable("prefix", Prefix)
1295                 logDebug(" ======== STARTED preProcessUpdateAAIVfModuleRequestOrch ======== ", isDebugLogEnabled)
1296
1297                 try{
1298
1299                         //Build UpdateAAIVfModule Request
1300                         boolean setContrailServiceInstanceFqdn = false
1301                         def contrailServiceInstanceFqdn = execution.getVariable("DCVFM_contrailServiceInstanceFqdn")
1302                         if (!contrailServiceInstanceFqdn.equals("")) {
1303                                 setContrailServiceInstanceFqdn = true
1304                         }
1305
1306                         String updateAAIVfModuleRequest = buildUpdateAAIVfModuleRequest(execution, false, true, true, setContrailServiceInstanceFqdn)
1307
1308                         updateAAIVfModuleRequest = utils.formatXml(updateAAIVfModuleRequest)
1309                         execution.setVariable("DCVFM_updateAAIVfModuleRequest", updateAAIVfModuleRequest)
1310                         logDebug("Outgoing UpdateAAIVfModuleRequest is: \n" + updateAAIVfModuleRequest, isDebugLogEnabled)
1311                         utils.logAudit("Outgoing UpdateAAIVfModuleRequest is: \n"  + updateAAIVfModuleRequest)
1312
1313                 }catch(Exception e){
1314                         utils.log("ERROR", "Exception Occured Processing preProcessUpdateAAIVfModuleRequestOrch. Exception is:\n" + e, isDebugLogEnabled)
1315                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during preProcessUpdateAAIVfModuleRequestOrch Method:\n" + e.getMessage())
1316                 }
1317                 logDebug("======== COMPLETED preProcessUpdateAAIVfModuleRequestOrch ======== ", isDebugLogEnabled)
1318
1319         }
1320
1321         public void preProcessUpdateAAIVfModuleRequestGroup(Execution execution) {
1322                 def method = getClass().getSimpleName() + '.preProcessUpdateAAIVfModuleRequestGroup(' +
1323                         'execution=' + execution.getId() +
1324                         ')'
1325                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1326                 logDebug('Entered ' + method, isDebugLogEnabled)
1327                 execution.setVariable("prefix", Prefix)
1328                 logDebug(" ======== STARTED preProcessUpdateAAIVfModuleRequestGroup ======== ", isDebugLogEnabled)
1329
1330                 try{
1331
1332                         //Build UpdateAAIVfModule Request
1333
1334                         String updateAAIVfModuleRequest = buildUpdateAAIVfModuleRequest(execution, true, false, false, false)
1335
1336                         updateAAIVfModuleRequest = utils.formatXml(updateAAIVfModuleRequest)
1337                         execution.setVariable("DCVFM_updateAAIVfModuleRequest", updateAAIVfModuleRequest)
1338                         logDebug("Outgoing UpdateAAIVfModuleRequest is: \n" + updateAAIVfModuleRequest, isDebugLogEnabled)
1339                         utils.logAudit("Outgoing UpdateAAIVfModuleRequest is: \n"  + updateAAIVfModuleRequest)
1340
1341                 }catch(Exception e){
1342                         utils.log("ERROR", "Exception Occured Processing preProcessUpdateAAIVfModuleRequestGroup. Exception is:\n" + e, isDebugLogEnabled)
1343                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during preProcessUpdateAAIVfModuleRequestGroup Method:\n" + e.getMessage())
1344                 }
1345                 logDebug("======== COMPLETED  preProcessUpdateAAIVfModuleRequestGroup ======== ", isDebugLogEnabled)
1346
1347         }
1348
1349         public void validateSDNCResponse(Execution execution, String response, String method){
1350                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
1351                 execution.setVariable("prefix",Prefix)
1352                 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
1353
1354                 WorkflowException workflowException = execution.getVariable("WorkflowException")
1355                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
1356
1357                 utils.logAudit("workflowException: " + workflowException)
1358
1359                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
1360                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
1361
1362                 utils.logAudit("SDNCResponse: " + response)
1363
1364                 String sdncResponse = response
1365                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
1366                         logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse, isDebugLogEnabled)
1367                         RollbackData rollbackData = execution.getVariable("RollbackData")
1368
1369                         if(method.equals("assign")){
1370                                 rollbackData.put("VFMODULE", "rollbackSDNCRequestAssign", "true")
1371                                 execution.setVariable("CRTGVNF_sdncAssignCompleted", true)
1372                         }
1373                         else if (method.equals("activate")) {
1374                                 rollbackData.put("VFMODULE", "rollbackSDNCRequestActivate", "true")
1375                         }
1376                         execution.setVariable("RollbackData", rollbackData)
1377                 }else{
1378                         logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
1379                         throw new BpmnError("MSOWorkflowException")
1380                 }
1381                 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
1382         }
1383
1384         public void preProcessUpdateAfterCreateRequest(Execution execution){
1385                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
1386                 execution.setVariable("prefix", Prefix)
1387                 utils.log("DEBUG", " ======== STARTED preProcessRequest Process ======== ", isDebugLogEnabled)
1388                 try{
1389                         String response = execution.getVariable("DCVFM_assignSDNCAdapterResponse")
1390                         utils.logAudit("DCVFM_assignSDNCAdapterResponse: " + response)
1391
1392                         String data = utils.getNodeXml(response, "response-data")
1393                         data = data.replaceAll("&lt;", "<")
1394                         data = data.replaceAll("&gt;", ">")
1395                         String vnfId = utils.getNodeText1(data, "vnf-id")
1396
1397                         String uuid = execution.getVariable('testReqId') // for junits
1398                         if(uuid==null){
1399                                 uuid = execution.getVariable("mso-request-id") + "-" +          System.currentTimeMillis()
1400                         }
1401
1402                         String serviceOperation = "/VNF-API:vnfs/vnf-list/" + vnfId
1403                         def callbackUrl = execution.getVariable("DCVFM_sdncCallbackUrl")
1404                         utils.logAudit("callbackUrl: " + callbackUrl)
1405
1406                         String SDNCGetRequest =
1407                                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
1408                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
1409                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
1410                                         <sdncadapter:RequestHeader>
1411                                         <sdncadapter:RequestId>${uuid}</sdncadapter:RequestId>
1412                                         <sdncadapter:SvcAction>query</sdncadapter:SvcAction>
1413                                         <sdncadapter:SvcOperation>${serviceOperation}</sdncadapter:SvcOperation>
1414                                         <sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
1415                                         <sdncadapter:MsoAction>mobility</sdncadapter:MsoAction>
1416                                 </sdncadapter:RequestHeader>
1417                                         <sdncadapterworkflow:SDNCRequestData></sdncadapterworkflow:SDNCRequestData>
1418                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
1419
1420                         execution.setVariable("DCVFM_getSDNCRequest", SDNCGetRequest)
1421                         utils.log("DEBUG", "Outgoing GetSDNCRequest is: \n" + SDNCGetRequest, isDebugLogEnabled)
1422                         utils.logAudit("Outgoing GetSDNCRequest: " + SDNCGetRequest)
1423
1424                 }catch(Exception e){
1425                         utils.log("ERROR", "Exception Occured Processing preProcessSDNCGetRequest. Exception is:\n" + e, isDebugLogEnabled)
1426                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during prepareProvision Method:\n" + e.getMessage())
1427                 }
1428                 utils.log("DEBUG", "======== COMPLETED preProcessSDNCGetRequest Process ======== ", isDebugLogEnabled)
1429         }
1430
1431         public String buildUpdateAAIVfModuleRequest(Execution execution, boolean updateVolumeGroupId,
1432                 boolean updateOrchestrationStatus, boolean updateHeatStackId, boolean updateContrailFqdn){
1433
1434                 def vnfId = execution.getVariable("DCVFM_vnfId")
1435                 def vfModuleId = execution.getVariable("DCVFM_vfModuleId")
1436                 def volumeGroupIdString = ""
1437                 if (updateVolumeGroupId) {
1438                         volumeGroupIdString = "<volume-group-id>" + execution.getVariable("DCVFM_volumeGroupId") +
1439                                         "</volume-group-id>"
1440                 }
1441                 def orchestrationStatusString = ""
1442                 if (updateOrchestrationStatus) {
1443                         orchestrationStatusString = "<orchestration-status>Created</orchestration-status>"
1444                 }
1445                 def heatStackIdString = ""
1446                 if (updateHeatStackId) {
1447                         heatStackIdString = "<heat-stack-id>" + execution.getVariable("DCVFM_heatStackId") + "</heat-stack-id>"
1448                 }
1449                 def contrailFqdnString = ""
1450                 if (updateContrailFqdn) {
1451                         contrailFqdnString = "<contrail-service-instance-fqdn>" + execution.getVariable("DCVFM_contrailServiceInstanceFqdn") +
1452                                 "</contrail-service-instance-fqdn>"
1453                 }
1454
1455                 String updateAAIVfModuleRequest =
1456                         """<UpdateAAIVfModuleRequest>
1457                                 <vnf-id>${vnfId}</vnf-id>
1458                                 <vf-module-id>${vfModuleId}</vf-module-id>
1459                                 ${heatStackIdString}
1460                                 ${orchestrationStatusString}
1461                                 ${volumeGroupIdString}
1462                                 ${contrailFqdnString}
1463                         </UpdateAAIVfModuleRequest>"""
1464
1465         utils.logAudit("updateAAIVfModule Request: " + updateAAIVfModuleRequest)
1466         return updateAAIVfModuleRequest
1467
1468         }
1469
1470         public String buildSDNCParamsXml(Execution execution){
1471
1472                 String params = ""
1473                 StringBuilder sb = new StringBuilder()
1474                 Map<String, String> paramsMap = execution.getVariable("DCVFM_vnfParamsMap")
1475
1476                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
1477                         String paramsXml
1478                         String key = entry.getKey();
1479                         if(key.endsWith("_network")){
1480                                 String requestKey = key.substring(0, key.indexOf("_network"))
1481                                 String requestValue = entry.getValue()
1482                                 paramsXml =
1483 """<vnf-networks>
1484         <network-role>{ functx:substring-before-match(data($param/@name), '_network') }</network-role>
1485         <network-name>{ $param/text() }</network-name>
1486 </vnf-networks>"""
1487                         }else{
1488                         paramsXml = ""
1489                         }
1490                         params = sb.append(paramsXml)
1491                 }
1492                 return params
1493         }
1494
1495    public void queryCloudRegion (Execution execution) {
1496                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
1497                 execution.setVariable("prefix", Prefix)
1498                 utils.log("DEBUG", " ======== STARTED queryCloudRegion ======== ", isDebugLogEnabled)
1499
1500                 try {
1501                         String cloudRegion = execution.getVariable("DCVFM_cloudSiteId")
1502
1503                         // Prepare AA&I url
1504                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
1505                         AaiUtil aaiUtil = new AaiUtil(this)
1506                         String aai_uri = aaiUtil.getCloudInfrastructureCloudRegionUri(execution)
1507                         String queryCloudRegionRequest = "${aai_endpoint}${aai_uri}/" + cloudRegion
1508                         utils.logAudit("CloudRegion Request: " + queryCloudRegionRequest)
1509
1510                         execution.setVariable("DCVFM_queryCloudRegionRequest", queryCloudRegionRequest)
1511                         utils.log("DEBUG", "DCVFM_queryCloudRegionRequest - " + "\n" + queryCloudRegionRequest, isDebugLogEnabled)
1512
1513                         cloudRegion = aaiUtil.getAAICloudReqion(execution, queryCloudRegionRequest, "PO", cloudRegion)
1514
1515                         if ((cloudRegion != "ERROR")) {
1516                                 if(execution.getVariable("DCVFM_queryCloudRegionReturnCode") == "404"){
1517                                         execution.setVariable("DCVFM_cloudRegionForVolume", "AAIAIC25")
1518                                 }else{
1519                                 execution.setVariable("DCVFM_cloudRegionForVolume", cloudRegion)
1520                                 }
1521                                 execution.setVariable("DCVFM_isCloudRegionGood", true)
1522                         } else {
1523                                 String errorMessage = "AAI Query Cloud Region Unsuccessful. AAI Response Code: " + execution.getVariable("DCVFM_queryCloudRegionReturnCode")
1524                                 utils.log("DEBUG", errorMessage, isDebugLogEnabled)
1525                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, errorMessage)
1526                                 execution.setVariable("DCVFM_isCloudRegionGood", false)
1527                         }
1528                         utils.log("DEBUG", " is Cloud Region Good: " + execution.getVariable("DCVFM_isCloudRegionGood"), isDebugLogEnabled)
1529
1530                 } catch(BpmnError b){
1531                         utils.log("ERROR", "Rethrowing MSOWorkflowException", isDebugLogEnabled)
1532                         throw b
1533                 }catch (Exception ex) {
1534                         // try error
1535                         String errorMessage = "Bpmn error encountered in CreateVfModule flow. Unexpected Response from AAI - " + ex.getMessage()
1536                         utils.log("ERROR", " AAI Query Cloud Region Failed.  Exception - " + "\n" + errorMessage, isDebugLogEnabled)
1537                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Exception occured during queryCloudRegion method")
1538                 }
1539         }
1540
1541    /**
1542     *
1543     *This method occurs when an MSOWorkflowException is caught.  It logs the
1544     *variables and ensures that the "WorkflowException" Variable is set.
1545     *
1546     */
1547    public void processBPMNException(Execution execution){
1548            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
1549            execution.setVariable("prefix",Prefix)
1550            try{
1551                    utils.log("DEBUG", "Caught a BPMN Exception", isDebugEnabled)
1552                    utils.log("DEBUG", "Started processBPMNException Method", isDebugEnabled)
1553                    utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)
1554                    if(execution.getVariable("WorkflowException") == null){
1555                            exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Exception occured during DoCreateVfModule Sub Process")
1556                    }
1557
1558            }catch(Exception e){
1559                    utils.log("DEBUG", "Caught Exception during processBPMNException Method: " + e, isDebugEnabled)
1560            }
1561            utils.log("DEBUG", "Completed processBPMNException Method", isDebugEnabled)
1562    }
1563
1564    public void prepareCreateAAIVfModuleVolumeGroupRequest(Execution execution) {
1565                 def method = getClass().getSimpleName() + '.prepareCreateAAIVfModuleVolumeGroupRequest(' +
1566                         'execution=' + execution.getId() +
1567                         ')'
1568                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1569                 logDebug('Entered ' + method, isDebugLogEnabled)
1570                 execution.setVariable("prefix", Prefix)
1571                 logDebug(" ======== STARTED prepareCreateAAIVfModuleVolumeGroupRequest ======== ", isDebugLogEnabled)
1572
1573                 try{
1574
1575                         //Build CreateAAIVfModuleVolumeGroup Request
1576
1577                         def vnfId = execution.getVariable("DCVFM_vnfId")
1578                         def vfModuleId = execution.getVariable("DCVFM_vfModuleId")
1579                         def volumeGroupId = execution.getVariable("DCVFM_volumeGroupId")
1580                         //def aicCloudRegion = execution.getVariable("DCVFM_cloudSiteId")
1581                         def aicCloudRegion = execution.getVariable("DCVFM_cloudRegionForVolume")
1582                         String createAAIVfModuleVolumeGroupRequest =
1583                         """<CreateAAIVfModuleVolumeGroupRequest>
1584                                 <vnf-id>${vnfId}</vnf-id>
1585                                 <vf-module-id>${vfModuleId}</vf-module-id>
1586                                 <volume-group-id>${volumeGroupId}</volume-group-id>
1587                                 <aic-cloud-region>${aicCloudRegion}</aic-cloud-region>
1588                         </CreateAAIVfModuleVolumeGroupRequest>"""
1589
1590                         createAAIVfModuleVolumeGroupRequest = utils.formatXml(createAAIVfModuleVolumeGroupRequest)
1591                         execution.setVariable("DCVFM_createAAIVfModuleVolumeGroupRequest", createAAIVfModuleVolumeGroupRequest)
1592                         logDebug("Outgoing CreateAAIVfModuleVolumeGroupRequest is: \n" + createAAIVfModuleVolumeGroupRequest, isDebugLogEnabled)
1593                         utils.logAudit("Outgoing CreateAAIVfModuleVolumeGroupRequest is: \n"  + createAAIVfModuleVolumeGroupRequest)
1594
1595                 }catch(Exception e){
1596                         utils.log("ERROR", "Exception Occured Processing prepareCreateAAIVfModuleVolumeGroupRequest. Exception is:\n" + e, isDebugLogEnabled)
1597                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during prepareCreateAAIVfModuleVolumeGroupRequest Method:\n" + e.getMessage())
1598                 }
1599                 logDebug("======== COMPLETED  prepareCreateAAIVfModuleVolumeGroupRequest ======== ", isDebugLogEnabled)
1600
1601         }
1602
1603    public void createNetworkPoliciesInAAI(Execution execution) {
1604            def method = getClass().getSimpleName() + '.createNetworkPoliciesInAAI(' +
1605            'execution=' + execution.getId() +
1606            ')'
1607            def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1608            logDebug('Entered ' + method, isDebugLogEnabled)
1609            execution.setVariable("prefix", Prefix)
1610            logDebug(" ======== STARTED createNetworkPoliciesInAAI ======== ", isDebugLogEnabled)
1611
1612            try {
1613                    // get variables
1614                    List fqdnList = execution.getVariable("DCVFM_contrailNetworkPolicyFqdnList")
1615                    int fqdnCount = fqdnList.size()
1616                    def rollbackData = execution.getVariable("RollbackData")
1617
1618                    execution.setVariable("DCVFM_networkPolicyFqdnCount", fqdnCount)
1619                    logDebug("DCVFM_networkPolicyFqdnCount - " + fqdnCount, isDebugLogEnabled)
1620
1621                    String aai_endpoint = execution.getVariable("URN_aai_endpoint")
1622                    AaiUtil aaiUriUtil = new AaiUtil(this)
1623                    String aai_uri = aaiUriUtil.getNetworkPolicyUri(execution)
1624
1625                    if (fqdnCount > 0) {
1626
1627                            // AII loop call over contrail network policy fqdn list
1628                            for (i in 0..fqdnCount-1) {
1629
1630                                    int counting = i+1
1631                                    String fqdn = fqdnList[i]
1632
1633                                    // Query AAI for this network policy FQDN
1634
1635                                    String queryNetworkPolicyByFqdnAAIRequest = "${aai_endpoint}${aai_uri}?network-policy-fqdn=" + UriUtils.encode(fqdn, "UTF-8")
1636                                    utils.logAudit("AAI request endpoint: " + queryNetworkPolicyByFqdnAAIRequest)
1637
1638                                    def aaiRequestId = UUID.randomUUID().toString()
1639                                    RESTConfig config = new RESTConfig(queryNetworkPolicyByFqdnAAIRequest);
1640                                    RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId)
1641                                                                                                                          .addHeader("X-FromAppId", "MSO")
1642                                                                                                                          .addHeader("Content-Type", "application/xml")
1643                                                                                                                          .addHeader("Accept","application/xml");
1644                                    APIResponse response = client.get()
1645                                    int returnCode = response.getStatusCode()
1646                                    execution.setVariable("DCVFM_aaiQqueryNetworkPolicyByFqdnReturnCode", returnCode)
1647                                    logDebug(" ***** AAI query network policy Response Code, NetworkPolicy #" + counting + " : " + returnCode, isDebugLogEnabled)
1648
1649                                    String aaiResponseAsString = response.getResponseBodyAsString()
1650
1651                                    if (isOneOf(returnCode, 200, 201)) {
1652                                            logDebug("The return code is: "  + returnCode, isDebugLogEnabled)
1653                                            // This network policy FQDN already exists in AAI
1654                                            utils.logAudit(aaiResponseAsString)
1655                                            execution.setVariable("DCVFM_queryNetworkPolicyByFqdnAAIResponse", aaiResponseAsString)
1656                                            logDebug(" QueryAAINetworkPolicyByFQDN Success REST Response, , NetworkPolicy #" + counting + " : " + "\n" + aaiResponseAsString, isDebugLogEnabled)
1657
1658                                    } else {
1659                                            if (returnCode == 404) {
1660                                                    // This network policy FQDN is not in AAI yet. Add it now
1661                                                    logDebug("The return code is: "  + returnCode, isDebugLogEnabled)
1662                                                    logDebug("This network policy FQDN is not in AAI yet: " + fqdn, isDebugLogEnabled)
1663                                                    utils.logAudit("Network policy FQDN is not in AAI yet")
1664                                                    // Add the network policy with this FQDN to AAI
1665                                                    def networkPolicyId = UUID.randomUUID().toString()
1666                                                    logDebug("Adding network-policy with network-policy-id " + networkPolicyId, isDebugLogEnabled)
1667
1668                                                    String aaiNamespace = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
1669                                                    logDebug('AAI namespace is: ' + aaiNamespace, isDebugLogEnabled)
1670                                                    String payload = """<network-policy xmlns="${aaiNamespace}">
1671                                                                 <network-policy-id>${networkPolicyId}</network-policy-id>
1672                                                                 <network-policy-fqdn>${fqdn}</network-policy-fqdn>
1673                                                                 <heat-stack-id>${execution.getVariable("DCVFM_heatStackId")}</heat-stack-id>
1674                                                                 </network-policy>""" as String
1675
1676                                                    execution.setVariable("DCVFM_addNetworkPolicyAAIRequestBody", payload)
1677
1678                                                    String addNetworkPolicyAAIRequest = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(networkPolicyId, "UTF-8")
1679                                                    utils.logAudit("AAI request endpoint: " + addNetworkPolicyAAIRequest)
1680                                                    logDebug("AAI request endpoint: " + addNetworkPolicyAAIRequest, isDebugLogEnabled)
1681
1682                                                    def aaiRequestIdPut = UUID.randomUUID().toString()
1683                                                    RESTConfig configPut = new RESTConfig(addNetworkPolicyAAIRequest);
1684                                                    RESTClient clientPut = new RESTClient(configPut).addHeader("X-TransactionId", aaiRequestIdPut)
1685                                                                                                                                          .addHeader("X-FromAppId", "MSO")
1686                                                                                                                                          .addHeader("Content-Type", "application/xml")
1687                                                                                                                                          .addHeader("Accept","application/xml");
1688                                                    logDebug("invoking PUT call to AAI with payload:"+System.lineSeparator()+payload, isDebugLogEnabled)
1689                                                    utils.logAudit("Sending PUT call to AAI with Endpoint /n" + addNetworkPolicyAAIRequest + " with payload /n" + payload)
1690                                                    APIResponse responsePut = clientPut.httpPut(payload)
1691                                                    int returnCodePut = responsePut.getStatusCode()
1692                                                    execution.setVariable("DCVFM_aaiAddNetworkPolicyReturnCode", returnCodePut)
1693                                                    logDebug(" ***** AAI add network policy Response Code, NetworkPolicy #" + counting + " : " + returnCodePut, isDebugLogEnabled)
1694
1695                                                    String aaiResponseAsStringPut = responsePut.getResponseBodyAsString()
1696                                                    if (isOneOf(returnCodePut, 200, 201)) {
1697                                                            logDebug("The return code from adding network policy is: "  + returnCodePut, isDebugLogEnabled)
1698                                                            // This network policy was created in AAI successfully
1699                                                            utils.logAudit(aaiResponseAsStringPut)
1700                                                            execution.setVariable("DCVFM_addNetworkPolicyAAIResponse", aaiResponseAsStringPut)
1701                                                            logDebug(" AddAAINetworkPolicy Success REST Response, , NetworkPolicy #" + counting + " : " + "\n" + aaiResponseAsStringPut, isDebugLogEnabled)
1702                                                            rollbackData.put("VFMODULE", "rollbackCreateNetworkPoliciesAAI", "true")
1703                                                            rollbackData.put("VFMODULE", "contrailNetworkPolicyFqdn" + i, fqdn)
1704                                                            execution.setVariable("RollbackData", rollbackData)
1705
1706                                                    } else {
1707                                                                 // aai all errors
1708                                                                 String putErrorMessage = "Unable to add network-policy to AAI createNetworkPoliciesInAAI - " + returnCodePut
1709                                                                 logDebug(putErrorMessage, isDebugLogEnabled)
1710                                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, putErrorMessage)
1711                                                    }
1712
1713                                            } else {
1714                                                   if (aaiResponseAsString.contains("RESTFault")) {
1715                                                           WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
1716                                                           execution.setVariable("WorkflowException", exceptionObject)
1717                                                           throw new BpmnError("MSOWorkflowException")
1718
1719                                                           } else {
1720                                                                    // aai all errors
1721                                                                    String dataErrorMessage = "Unexpected Response from createNetworkPoliciesInAAI - " + returnCode
1722                                                                    logDebug(dataErrorMessage, isDebugLogEnabled)
1723                                                                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
1724
1725                                                          }
1726                                            }
1727                                    }
1728
1729                            } // end loop
1730
1731
1732                    } else {
1733                            logDebug("No contrail network policies to query/create", isDebugLogEnabled)
1734
1735                    }
1736
1737            } catch (BpmnError e) {
1738                    throw e;
1739
1740            } catch (Exception ex) {
1741                    String exceptionMessage = "Bpmn error encountered in DoCreateVfModule flow. createNetworkPoliciesInAAI() - " + ex.getMessage()
1742                    logDebug(exceptionMessage, isDebugLogEnabled)
1743                    exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
1744            }
1745
1746    }
1747
1748    /**
1749         * Prepare a Request for invoking the UpdateAAIGenericVnf subflow.
1750         *
1751         * @param execution The flow's execution instance.
1752         */
1753    public void prepUpdateAAIGenericVnf(Execution execution) {
1754            def method = getClass().getSimpleName() + '.prepUpdateAAIGenericVnf(' +
1755                    'execution=' + execution.getId() +
1756                    ')'
1757            def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1758            logDebug('Entered ' + method, isDebugLogEnabled)
1759
1760            try {
1761                    def rollbackData = execution.getVariable("RollbackData")
1762                    def vnfId = execution.getVariable('DCVFM_vnfId')
1763                    def oamManagementV4Address = execution.getVariable("DCVFM_oamManagementV4Address")
1764                    def oamManagementV6Address = execution.getVariable("DCVFM_oamManagementV6Address")
1765                    def ipv4OamAddressElement = ''
1766                    def managementV6AddressElement = ''
1767
1768                    if (oamManagementV4Address != null && !oamManagementV4Address.isEmpty()) {
1769                            ipv4OamAddressElement = '<ipv4-oam-address>' + oamManagementV4Address + '</ipv4-oam-address>'
1770                    }
1771
1772                    if (oamManagementV6Address != null && !oamManagementV6Address.isEmpty()) {
1773                            managementV6AddressElement = '<management-v6-address>' + oamManagementV6Address + '</management-v6-address>'
1774                    }
1775
1776                    rollbackData.put("VFMODULE", "oamManagementV4Address", oamManagementV4Address)
1777
1778
1779                    String updateAAIGenericVnfRequest = """
1780                                         <UpdateAAIGenericVnfRequest>
1781                                                 <vnf-id>${vnfId}</vnf-id>
1782                                                 ${ipv4OamAddressElement}
1783                                                 ${managementV6AddressElement}
1784                                         </UpdateAAIGenericVnfRequest>
1785                                 """
1786                            updateAAIGenericVnfRequest = utils.formatXml(updateAAIGenericVnfRequest)
1787                            execution.setVariable('DCVM_updateAAIGenericVnfRequest', updateAAIGenericVnfRequest)
1788                            utils.logAudit("updateAAIGenericVnfRequest : " + updateAAIGenericVnfRequest)
1789                            logDebug('Request for UpdateAAIGenericVnf:\n' + updateAAIGenericVnfRequest, isDebugLogEnabled)
1790
1791
1792                    logDebug('Exited ' + method, isDebugLogEnabled)
1793            } catch (BpmnError e) {
1794                    throw e;
1795            } catch (Exception e) {
1796                    logError('Caught exception in ' + method, e)
1797                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateAAIGenericVnf(): ' + e.getMessage())
1798            }
1799    }
1800
1801    /**
1802         * Post process a result from invoking the UpdateAAIGenericVnf subflow.
1803         *
1804         * @param execution The flow's execution instance.
1805         */
1806    public void postProcessUpdateAAIGenericVnf(Execution execution) {
1807            def method = getClass().getSimpleName() + '.postProcessUpdateAAIGenericVnf(' +
1808                    'execution=' + execution.getId() +
1809                    ')'
1810            def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
1811            logDebug('Entered ' + method, isDebugLogEnabled)
1812
1813            try {
1814                    def rollbackData = execution.getVariable("RollbackData")
1815
1816                    rollbackData.put("VFMODULE", "rollbackUpdateVnfAAI", "true")
1817
1818                    def vnfId = execution.getVariable('DCVFM_vnfId')
1819                    def oamManagementV4Address = execution.getVariable("DCVFM_oamManagementV4Address")
1820                    def oamManagementV6Address = execution.getVariable("DCVFM_oamManagementV6Address")
1821                    def ipv4OamAddressElement = ''
1822                    def managementV6AddressElement = ''
1823
1824                    if (oamManagementV4Address != null && !oamManagementV4Address.isEmpty()) {
1825                            rollbackData.put("VFMODULE", "oamManagementV4Address", oamManagementV4Address)
1826                    }
1827
1828                    if (oamManagementV6Address != null && !oamManagementV6Address.isEmpty()) {
1829                            rollbackData.put("VFMODULE", "oamManagementV6Address", oamManagementV6Address)
1830                    }
1831
1832                    execution.setVariable("RollbackData", rollbackData)
1833
1834                    logDebug('Exited ' + method, isDebugLogEnabled)
1835            } catch (BpmnError e) {
1836                    throw e;
1837            } catch (Exception e) {
1838                    logError('Caught exception in ' + method, e)
1839                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in postProcessUpdateAAIGenericVnf(): ' + e.getMessage())
1840            }
1841    }
1842
1843
1844 }