Initial OpenECOMP MSO commit
[so.git] / bpmn / MSOGammaBPMN / src / main / groovy / com / att / bpm / scripts / DoUpdateVfModule.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 com.att.bpm.scripts
22
23 import groovy.util.Node
24 import groovy.xml.QName
25
26 import java.io.Serializable;
27
28 import org.springframework.web.util.UriUtils
29
30 import org.camunda.bpm.engine.delegate.BpmnError
31 import org.camunda.bpm.engine.runtime.Execution
32
33 import org.openecomp.mso.rest.APIResponse
34 import org.openecomp.mso.rest.RESTClient
35 import org.openecomp.mso.rest.RESTConfig
36 import org.openecomp.mso.bpmn.core.RollbackData
37 import org.openecomp.mso.bpmn.core.WorkflowException
38
39 public class DoUpdateVfModule extends VfModuleBase {
40
41         ExceptionUtil exceptionUtil = new ExceptionUtil()
42         
43         /**
44          * Initialize the flow's variables.
45          * 
46          * @param execution The flow's execution instance.
47          */
48         public void initProcessVariables(Execution execution) {
49                 execution.setVariable('prefix', 'DOUPVfMod_')
50                 execution.setVariable('DOUPVfMod_requestInfo', null)
51                 execution.setVariable('DOUPVfMod_serviceInstanceId', null)
52                 execution.setVariable('DOUPVfMod_requestId', null)
53                 execution.setVariable('DOUPVfMod_vnfInputs', null)
54                 execution.setVariable('DOUPVfMod_vnfId', null)
55                 execution.setVariable('DOUPVfMod_vnfName', null)
56                 execution.setVariable('DOUPVfMod_vnfNameFromAAI', null)
57                 execution.setVariable('DOUPVfMod_vfModuleName', null)
58                 execution.setVariable('DOUPVfMod_vfModuleId', null)
59                 execution.setVariable('DOUPVfMod_vnfType', null)
60                 execution.setVariable('DOUPVfMod_asdcServiceModelVersion', null)
61                 execution.setVariable('DOUPVfMod_vfModuleModelName', null)
62                 execution.setVariable("DOUPVfMod_isBaseVfModule", "false")
63                 execution.setVariable('DOUPVfMod_serviceId', null)
64                 execution.setVariable('DOUPVfMod_aicCloudRegion', null)
65                 execution.setVariable('DOUPVfMod_tenantId', null)
66                 execution.setVariable('DOUPVfMod_volumeGroupId', null)
67                 execution.setVariable('DOUPVfMod_vfModule', null)
68                 execution.setVariable('DOUPVfMod_vnfParams', null)
69                 execution.setVariable("DOUPVfMod_baseVfModuleId", "")
70                 execution.setVariable("DOUPVfMod_baseVfModuleHeatStackId", "")
71                 execution.setVariable('DOUPVfMod_prepareUpdateAAIVfModuleRequest', null)
72                 execution.setVariable('DOUPVfMod_sdncTopologyRequest', null)
73                 execution.setVariable('DOUPVfMod_sdncTopologyResponse', null)
74                 execution.setVariable('DOUPVfMod_vnfAdapterRestRequest', null)
75                 execution.setVariable('DOUPVfMod_updateAAIGenericVnfRequest', null)
76                 execution.setVariable('DOUPVfMod_updateAAIVfModuleRequest', null)
77                 execution.setVariable('DOUPVfMod_skipUpdateGenericVnf', false)
78                 execution.setVariable('DoUpdateVfModuleSuccessIndicator', false)
79         }       
80         
81         /**
82          * Check for missing elements in the received request.
83          * 
84          * @param execution The flow's execution instance.
85          */
86         public void preProcessRequest(Execution execution) {
87                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
88                         'execution=' + execution.getId() +
89                         ')'
90                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
91                 logDebug('Entered ' + method, isDebugLogEnabled)
92
93                 try {
94                         initProcessVariables(execution)
95                         def xml = getVariable(execution, 'DoUpdateVfModuleRequest')
96                         utils.logAudit("DoUpdateVfModule request: " + xml)
97                         logDebug('Received request xml:\n' + xml, isDebugLogEnabled)
98                         
99                         def requestInfo = getRequiredNodeXml(execution, xml, 'request-info')
100                         execution.setVariable('DOUPVfMod_requestInfo', requestInfo)
101                         execution.setVariable('DOUPVfMod_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
102                         def serviceInstanceId = execution.getVariable('att-mso-service-instance-id')
103                         if (serviceInstanceId == null) {
104                                 serviceInstanceId = ''
105                         }
106                         execution.setVariable('DOUPVfMod_serviceInstanceId', serviceInstanceId)
107                         
108                         def vnfInputs = getRequiredNodeXml(execution, xml, 'vnf-inputs')
109                         execution.setVariable('DOUPVfMod_vnfInputs', vnfInputs)
110                         execution.setVariable('DOUPVfMod_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
111                         execution.setVariable('DOUPVfMod_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
112                         execution.setVariable('DOUPVfMod_vfModuleName', getNodeTextForce(vnfInputs, 'vf-module-name'))
113                         execution.setVariable('DOUPVfMod_vnfType', getNodeTextForce(vnfInputs, 'vnf-type'))
114                         execution.setVariable('DOUPVfMod_vnfName', getNodeTextForce(vnfInputs, 'vnf-name'))
115                         execution.setVariable('DOUPVfMod_asdcServiceModelVersion', getNodeTextForce(vnfInputs, 'asdc-service-model-version'))
116                         execution.setVariable('DOUPVfMod_vfModuleModelName', getRequiredNodeText(execution, vnfInputs, 'vf-module-model-name'))
117                         execution.setVariable('DOUPVfMod_serviceId', getRequiredNodeText(execution, vnfInputs, 'service-id'))
118                         execution.setVariable('DOUPVfMod_aicCloudRegion', getRequiredNodeText(execution, vnfInputs, 'aic-cloud-region'))
119                         execution.setVariable('DOUPVfMod_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
120                         //isBaseVfModule
121                         def isBaseVfModule = "false"
122                         if (utils.nodeExists(xml, "is-base-vf-module")) {
123                                 isBaseVfModule = utils.getNodeText(xml, "is-base-vf-module")
124                                 execution.setVariable("DOUPVfMod_isBaseVfModule", isBaseVfModule)
125                         }                       
126                         logDebug("isBaseVfModule: " + isBaseVfModule, isDebugLogEnabled)
127                         
128                         NetworkUtils networkUtils = new NetworkUtils()
129                         def backoutOnFailure = networkUtils.isRollbackEnabled(execution, xml)
130                         execution.setVariable("DOUPVfMod_backoutOnFailure", backoutOnFailure)
131                         
132                         def String vgi = getNodeTextForce(vnfInputs, 'volume-group-id')
133                         execution.setVariable('DOUPVfMod_volumeGroupId', vgi)
134                         
135                         execution.setVariable('DOUPVfMod_vnfParams', utils.getNodeXml(xml, 'vnf-params', false))
136                         
137                         def sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
138                         if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
139                                 def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'
140                                 logError(msg)
141                                 createWorkflowException(execution, 2000, msg)
142                         }
143                         def vnfCallbackUrl = (String) execution.getVariable('URN_mso_workflow_vnfadapter_rest_callback')
144                         if (vnfCallbackUrl == null || vnfCallbackUrl.trim().isEmpty()) {
145                                 def msg = 'Required variable \'URN_mso_workflow_vnfadapter_rest_callback\' is missing'
146                                 logError(msg)
147                                 createWorkflowException(execution, 2000, msg)
148                         }
149
150                         logDebug('Exited ' + method, isDebugLogEnabled)
151                 } catch (BpmnError e) {
152                         throw e;
153                 } catch (Exception e) {
154                         logError('Caught exception in ' + method, e)
155                         createWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
156                 }
157         }
158         
159         /**
160          * Prepare a Request for invoking the PrepareUpdateAAIVfModule subflow.  This will
161          * set the orchestration-status to 'pending-update'.
162          * 
163          * @param execution The flow's execution instance.
164          */
165         public void prepPrepareUpdateAAIVfModule(Execution execution) {
166                 def method = getClass().getSimpleName() + '.preparePrepareUpdateAAIVfModule(' +
167                         'execution=' + execution.getId() +
168                         ')'
169                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
170                 logDebug('Entered ' + method, isDebugLogEnabled)
171
172                 try {
173                         def vnfId = execution.getVariable('DOUPVfMod_vnfId')
174                         def vfModuleId = execution.getVariable('DOUPVfMod_vfModuleId')
175                         def orchestrationStatus = 'pending-update'
176                         
177                         String prepareUpdateAAIVfModuleRequest = """
178                                 <PrepareUpdateAAIVfModuleRequest>
179                                         <vnf-id>${vnfId}</vnf-id>
180                                         <vf-module-id>${vfModuleId}</vf-module-id>
181                                         <orchestration-status>${orchestrationStatus}</orchestration-status>
182                                 </PrepareUpdateAAIVfModuleRequest>
183                         """
184                         prepareUpdateAAIVfModuleRequest = utils.formatXml(prepareUpdateAAIVfModuleRequest)
185                         execution.setVariable('DOUPVfMod_prepareUpdateAAIVfModuleRequest', prepareUpdateAAIVfModuleRequest)
186                         utils.logAudit("DoUpdateAAIVfModule request: " + prepareUpdateAAIVfModuleRequest)
187                         logDebug('Request for PrepareUpdateAAIVfModule:\n' + prepareUpdateAAIVfModuleRequest, isDebugLogEnabled)
188                         
189                         logDebug('Exited ' + method, isDebugLogEnabled)
190                 } catch (BpmnError e) {
191                         throw e;
192                 } catch (Exception e) {
193                         logError('Caught exception in ' + method, e)
194                         createWorkflowException(execution, 1002, 'Error in preparePrepareUpdateAAIVfModule(): ' + e.getMessage())
195                 }
196         }
197         
198         /**
199          * Prepare a Request for invoking the ConfirmVolumeGroupTenant subflow.  Currently,
200          * there is really nothing to do, so we just log that we're passing through.
201          *
202          * @param execution The flow's execution instance.
203          */
204         public void prepConfirmVolumeGroupTenant(Execution execution) {
205                 def method = getClass().getSimpleName() + '.prepConfirmVolumeGroupTenant(' +
206                         'execution=' + execution.getId() +
207                         ')'
208                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
209                 logDebug('Entered ' + method, isDebugLogEnabled)
210
211                 try {
212                         // Nothing to do - just log that we're passing through here
213                         
214                         logDebug('Exited ' + method, isDebugLogEnabled)
215                 } catch (BpmnError e) {
216                         throw e;
217                 } catch (Exception e) {
218                         logError('Caught exception in ' + method, e)
219                         createWorkflowException(execution, 1002, 'Error in prepConfirmVolumeGroupTenant(): ' + e.getMessage())
220                 }
221         }
222
223         /**
224          * Prepare a Request for invoking the SDNC Adapter subflow to perform
225          * a VNF topology 'changeassign' operation.
226          *
227          * @param execution The flow's execution instance.
228          */
229         public void prepSDNCTopologyChg(Execution execution) {
230                 def method = getClass().getSimpleName() + '.prepSDNCTopologyChg(' +
231                         'execution=' + execution.getId() +
232                         ')'
233                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
234                 logDebug('Entered ' + method, isDebugLogEnabled)
235
236                 try {
237                         def requestId = execution.getVariable('DOUPVfMod_requestId')
238                         def serviceInstanceId = execution.getVariable('DOUPVfMod_serviceInstanceId')
239                         def callbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
240                         def serviceId = execution.getVariable('DOUPVfMod_serviceId')
241                         def vnfId = execution.getVariable('DOUPVfMod_vnfId')
242                         def vnfType = execution.getVariable('DOUPVfMod_vnfType')                        
243                         def vfModuleId = execution.getVariable('DOUPVfMod_vfModuleId')
244                         def vfModuleModelName = execution.getVariable('DOUPVfMod_vfModuleModelName')
245                         def VfModule vfModule = (VfModule) execution.getVariable('DOUPVfMod_vfModule')
246                         def vfModuleName = vfModule.getElementText('vf-module-name')
247                         def tenantId = execution.getVariable('DOUPVfMod_tenantId')
248                         def aicCloudRegion = execution.getVariable('DOUPVfMod_aicCloudRegion')
249                         
250                         // Retrieve vnf name from AAI response
251                         def vnfName = execution.getVariable('DOUPVfMod_vnfNameFromAAI')
252                         execution.setVariable('DOUPVfMod_vnfName', vnfName)
253                         
254                         def vnfParamsXml = execution.getVariable('DOUPVfMod_vnfParams')
255                         def vnfNetworks = transformNetworkParamsToVnfNetworks(vnfParamsXml)
256                         
257                         String sdncTopologyRequest = """
258                                 <sdncadapterworkflow:SDNCAdapterWorkflowRequest
259                                                 xmlns:sdncadapterworkflow="http://ecomp.att.com/mso/workflow/schema/v1"
260                                                 xmlns:sdncadapter="http://domain2.att.com/workflow/sdnc/adapter/schema/v1">
261                                         <sdncadapter:RequestHeader>
262                                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
263                                                 <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>   
264                                                 <sdncadapter:SvcAction>changeassign</sdncadapter:SvcAction>
265                                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
266                                                 <sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
267                                         </sdncadapter:RequestHeader>
268                                         <sdncadapterworkflow:SDNCRequestData>
269                                               <request-information>
270                                                  <request-id>${requestId}</request-id>
271                                                  <request-action>ChangeVNFActivateRequest</request-action>
272                                                  <source>PORTAL</source>
273                                                  <notification-url/>
274                                                  <order-number/>
275                                                  <order-version/>
276                                               </request-information>
277                                               <service-information>
278                                                  <service-type>${serviceId}</service-type>
279                                                  <service-instance-id>${vnfId}</service-instance-id>
280                                                  <subscriber-name>dontcare</subscriber-name>
281                                               </service-information>
282                                               <vnf-request-information>
283                                                  <vnf-id>${vfModuleId}</vnf-id>
284                                                  <vnf-type>${vfModuleModelName}</vnf-type>                                               
285                                                  <vnf-name>${vfModuleName}</vnf-name>
286                                                  <generic-vnf-id>${vnfId}</generic-vnf-id>
287                                                  <generic-vnf-name>${vnfName}</generic-vnf-name>
288                                                          <generic-vnf-type>${vnfType}</generic-vnf-type>
289                                                  <tenant>${tenantId}</tenant>
290                                                  <aic-cloud-region>${aicCloudRegion}</aic-cloud-region> 
291                                                  ${vnfNetworks}                                                 
292                                               </vnf-request-information>
293                                         </sdncadapterworkflow:SDNCRequestData>
294                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>
295                         """
296                         sdncTopologyRequest = utils.formatXml(sdncTopologyRequest)
297                         execution.setVariable('DOUPVfMod_sdncTopologyRequest', sdncTopologyRequest)
298                         utils.logAudit("sdncTopologyRequest : " + sdncTopologyRequest)
299                         logDebug('Request for SDNCAdapter topology/changeassign:\n' + sdncTopologyRequest, isDebugLogEnabled)
300
301                         logDebug('Exited ' + method, isDebugLogEnabled)
302                 } catch (BpmnError e) {
303                         throw e;
304                 } catch (Exception e) {
305                         logError('Caught exception in ' + method, e)
306                         createWorkflowException(execution, 1002, 'Error in prepSDNCTopologyChg(): ' + e.getMessage())
307                 }
308         }
309         
310         /**
311          * Prepare a Request for invoking the SDNC Adapter subflow to perform
312          * a VNF topology 'query' operation.
313          *
314          * @param execution The flow's execution instance.
315          */
316         public void prepSDNCTopologyQuery(Execution execution) {
317                 def method = getClass().getSimpleName() + '.prepSDNCTopologyQuery(' +
318                         'execution=' + execution.getId() +
319                         ')'
320                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
321                 logDebug('Entered ' + method, isDebugLogEnabled)
322
323                 try {
324                         def requestId = execution.getVariable('DOUPVfMod_requestId')
325                         def serviceInstanceId = execution.getVariable('DOUPVfMod_serviceInstanceId')
326                         def callbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
327                         def vfModuleId = execution.getVariable('DOUPVfMod_vfModuleId')
328                         
329                         def svcInstId = ""
330                         if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {
331                                 svcInstId = vfModuleId
332                         }
333                         else {
334                                 svcInstId = serviceInstanceId
335                         }
336                         
337                         //!!!! TEMPORARY WORKAROUND FOR SDNC REPLICATION ISSUE
338                         sleep(5000)             
339
340                         String sdncTopologyRequest = """
341                                 <sdncadapterworkflow:SDNCAdapterWorkflowRequest
342                                                 xmlns:sdncadapterworkflow="http://ecomp.att.com/mso/workflow/schema/v1"
343                                                 xmlns:sdncadapter="http://domain2.att.com/workflow/sdnc/adapter/schema/v1">
344                                         <sdncadapter:RequestHeader>
345                                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
346                                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>   
347                                                 <sdncadapter:SvcAction>query</sdncadapter:SvcAction>
348                                                 <sdncadapter:SvcOperation>/VNF-API:vnfs/vnf-list/${vfModuleId}</sdncadapter:SvcOperation>
349                                                 <sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
350                                                 <sdncadapter:MsoAction>mobility</sdncadapter:MsoAction>
351                                         </sdncadapter:RequestHeader>
352                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>
353                         """
354                         sdncTopologyRequest = utils.formatXml(sdncTopologyRequest)
355                         execution.setVariable('DOUPVfMod_sdncTopologyRequest', sdncTopologyRequest)
356                         utils.logAudit("sdncTopologyRequest : " + sdncTopologyRequest)
357                         logDebug('Request for SDNCAdapter query:\n' + sdncTopologyRequest, isDebugLogEnabled)
358                         
359                         logDebug('Exited ' + method, isDebugLogEnabled)
360                 } catch (BpmnError e) {
361                         throw e;
362                 } catch (Exception e) {
363                         logError('Caught exception in ' + method, e)
364                         createWorkflowException(execution, 1002, 'Error in prepSDNCTopologyQuery(): ' + e.getMessage())
365                 }
366         }
367
368         /**
369          * Prepare a Request for invoking the VnfAdapterRest subflow.
370          *
371          * @param execution The flow's execution instance.
372          */
373         public void prepVnfAdapterRest(Execution execution) {
374                 def method = getClass().getSimpleName() + '.prepVnfAdapterRest(' +
375                         'execution=' + execution.getId() +
376                         ')'
377                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
378                 logDebug('Entered ' + method, isDebugLogEnabled)
379
380                 try {
381                         def requestId = execution.getVariable('DOUPVfMod_requestId')
382                         def serviceInstanceId = execution.getVariable('DOUPVfMod_serviceInstanceId')
383                         def vnfId = execution.getVariable('DOUPVfMod_vnfId')
384                         def vfModuleId = execution.getVariable('DOUPVfMod_vfModuleId')
385                         def vfModuleName = execution.getVariable('DOUPVfMod_vfModuleName')
386                         def vnfInputs = execution.getVariable('DOUPVfMod_vnfInputs')
387                         def tenantId = execution.getVariable('DOUPVfMod_tenantId')
388                         def volumeGroupId = execution.getVariable('DOUPVfMod_volumeGroupId')
389                         def VfModule vfModule = (VfModule) execution.getVariable('DOUPVfMod_vfModule')
390                         def heatStackId = vfModule.getElementText('heat-stack-id')
391                         def cloudId = execution.getVariable('DOUPVfMod_aicCloudRegion')
392                         def vnfType = execution.getVariable('DOUPVfMod_vnfType')
393                         def vnfName = execution.getVariable('DOUPVfMod_vnfName')
394                         def vfModuleModelName = execution.getVariable('DOUPVfMod_vfModuleModelName')                    
395                         def baseVfModuleId = execution.getVariable("DOUPVfMod_baseVfModuleId")          
396                         def baseVfModuleStackId = execution.getVariable("DOUPVfMod_baseVfModuleHeatStackId")
397                         def asdcServiceModelVersion = execution.getVariable('DOUPVfMod_asdcServiceModelVersion')
398                         def backoutOnFailure = execution.getVariable("DOUPVfMod_backoutOnFailure")
399                         
400                         def vnfParamsXml = execution.getVariable('DOUPVfMod_vnfParams')
401                         def vfModuleParamsEntries = transformParamsToEntries(vnfParamsXml)
402                         
403                         def messageId = execution.getVariable('att-mso-request-id') + '-' + System.currentTimeMillis()
404                         def notificationUrl = execution.getVariable("URN_mso_workflow_vnfadapter_rest_callback")
405                         def useQualifiedHostName = execution.getVariable("URN_mso_use_qualified_host")
406                         if ('true'.equals(useQualifiedHostName)) {
407                                         notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
408                         }
409                         
410                         String sdncGetResponse = execution.getVariable('DOUPVfMod_sdncTopologyResponse')
411                         
412                         String vfModuleParams = buildVfModuleParams(vfModuleParamsEntries, sdncGetResponse, vnfId, vnfName,
413                                         vfModuleId, vfModuleName)               
414                         
415                         
416                         String vnfAdapterRestRequest = """
417                                 <updateVfModuleRequest>
418                                         <cloudSiteId>${cloudId}</cloudSiteId>
419                                         <tenantId>${tenantId}</tenantId>
420                                         <vnfId>${vnfId}</vnfId>
421                                         <vfModuleId>${vfModuleId}</vfModuleId>
422                                         <vfModuleStackId>${heatStackId}</vfModuleStackId>
423                                         <vnfType>${vnfType}</vnfType>
424                                         <vnfVersion>${asdcServiceModelVersion}</vnfVersion>
425                                         <vfModuleType>${vfModuleModelName}</vfModuleType>
426                                         <volumeGroupId>${volumeGroupId}</volumeGroupId>
427                                         <baseVfModuleId>${baseVfModuleId}</baseVfModuleId>
428                                 <baseVfModuleStackId>${baseVfModuleStackId}</baseVfModuleStackId>
429                                         <skipAAI>true</skipAAI>
430                                         <backout>${backoutOnFailure}</backout>
431                                     <failIfExists>false</failIfExists>
432                                         <vfModuleParams>                                                
433                                                 ${vfModuleParams}
434                                     </vfModuleParams>
435                                     <msoRequest>
436                                         <requestId>${requestId}</requestId>
437                                         <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>
438                                     </msoRequest>
439                                     <messageId>${messageId}</messageId>
440                                     <notificationUrl>${notificationUrl}</notificationUrl>
441                                 </updateVfModuleRequest>
442                         """
443                         vnfAdapterRestRequest = utils.formatXml(vnfAdapterRestRequest)
444                         execution.setVariable('DOUPVfMod_vnfAdapterRestRequest', vnfAdapterRestRequest)
445                         utils.logAudit("vnfAdapterRestRequest : " + vnfAdapterRestRequest)
446                         logDebug('Request for VNFAdapter Rest:\n' + vnfAdapterRestRequest, isDebugLogEnabled)
447
448                         logDebug('Exited ' + method, isDebugLogEnabled)
449                 } catch (BpmnError e) {
450                         throw e;
451                 } catch (Exception e) {
452                         logError('Caught exception in ' + method, e)
453                         createWorkflowException(execution, 1002, 'Error in prepVnfAdapterRest(): ' + e.getMessage())
454                 }
455         }
456         
457         /**
458          * Prepare a Request for invoking the UpdateAAIGenericVnf subflow.
459          *
460          * @param execution The flow's execution instance.
461          */
462         public void prepUpdateAAIGenericVnf(Execution execution) {
463                 def method = getClass().getSimpleName() + '.prepUpdateAAIGenericVnf(' +
464                         'execution=' + execution.getId() +
465                         ')'
466                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
467                 logDebug('Entered ' + method, isDebugLogEnabled)
468
469                 try {
470                         def vnfId = execution.getVariable('DOUPVfMod_vnfId')
471                         def vnfInputs = execution.getVariable('DOUPVfMod_vnfInputs')
472                         
473                         def personaModelId = utils.getNodeText1(vnfInputs, 'vnf-persona-model-id')
474                         def personaModelVersion = utils.getNodeText1(vnfInputs, 'vnf-persona-model-version')
475                         if ((personaModelId == null) || (personaModelVersion == null)) {
476                                 logDebug('Skipping update for Generic VNF ' + vnfId +
477                                         ' because either \'vnf-persona-model-id\' or \'vnf-persona-model-version\' is absent', isDebugLogEnabled)
478                                 execution.setVariable('DOUPVfMod_skipUpdateGenericVnf', true)
479                         } else {
480                                 def personaModelIdElement = '<persona-model-id>' + personaModelId + '</persona-model-id>'
481                                 def personaModelVersionElement = '<persona-model-version>' + personaModelVersion + '</persona-model-version>'
482                                 
483                                 String updateAAIGenericVnfRequest = """
484                                         <UpdateAAIGenericVnfRequest>
485                                                 <vnf-id>${vnfId}</vnf-id>
486                                                 ${personaModelIdElement}
487                                                 ${personaModelVersionElement}
488                                         </UpdateAAIGenericVnfRequest>
489                                 """
490                                 updateAAIGenericVnfRequest = utils.formatXml(updateAAIGenericVnfRequest)
491                                 execution.setVariable('DOUPVfMod_updateAAIGenericVnfRequest', updateAAIGenericVnfRequest)
492                                 utils.logAudit("updateAAIGenericVnfRequest : " + updateAAIGenericVnfRequest)
493                                 logDebug('Request for UpdateAAIGenericVnf:\n' + updateAAIGenericVnfRequest, isDebugLogEnabled)
494                         }
495
496                         logDebug('Exited ' + method, isDebugLogEnabled)
497                 } catch (BpmnError e) {
498                         throw e;
499                 } catch (Exception e) {
500                         logError('Caught exception in ' + method, e)
501                         createWorkflowException(execution, 1002, 'Error in prepUpdateAAIGenericVnf(): ' + e.getMessage())
502                 }
503         }
504
505         /**
506          * Prepare a Request for invoking the UpdateAAIVfModule subflow.
507          *
508          * @param execution The flow's execution instance.
509          */
510         public void prepUpdateAAIVfModule(Execution execution) {
511                 def method = getClass().getSimpleName() + '.prepUpdateAAIVfModule(' +
512                         'execution=' + execution.getId() +
513                         ')'
514                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
515                 logDebug('Entered ' + method, isDebugLogEnabled)
516
517                 try {
518                         def vnfId = execution.getVariable('DOUPVfMod_vnfId')
519                         def vfModuleId = execution.getVariable('DOUPVfMod_vfModuleId')
520                         def orchestrationStatus = 'updated'
521                         def vnfInputs = execution.getVariable('DOUPVfMod_vnfInputs')
522                         
523                         def volumeGroupIdElement = ''
524                         def volumeGroupId = execution.getVariable('DOUPVfMod_volumeGroupId')
525                         if (volumeGroupId != null) {
526                                 volumeGroupIdElement = '<volume-group-id>' + volumeGroupId + '</volume-group-id>'
527                         }
528                         def personaModelIdElement = ''
529                         def personaModelId = utils.getNodeText1(vnfInputs, 'persona-model-id')
530                         if (personaModelId != null) {
531                                 personaModelIdElement = '<persona-model-id>' + personaModelId + '</persona-model-id>'
532                         }
533                         def personaModelVersionElement = ''
534                         def personaModelVersion = utils.getNodeText1(vnfInputs, 'persona-model-version')
535                         if (personaModelVersion != null) {
536                                 personaModelVersionElement = '<persona-model-version>' + personaModelVersion + '</persona-model-version>'
537                         }
538                         def contrailServiceInstanceFqdnElement = ''
539                         def contrailServiceInstanceFqdn = utils.getNodeText1(vnfInputs, 'contrail-service-instance-fqdn')
540                         if (contrailServiceInstanceFqdn != null) {
541                                 contrailServiceInstanceFqdnElement = '<contrail-service-instance-fqdn>' + contrailServiceInstanceFqdn + '</contrail-service-instance-fqdn>'
542                         }
543                         
544                         String updateAAIVfModuleRequest = """
545                                 <UpdateAAIVfModuleRequest>
546                                         <vnf-id>${vnfId}</vnf-id>
547                                         <vf-module-id>${vfModuleId}</vf-module-id>
548                                         <orchestration-status>${orchestrationStatus}</orchestration-status>
549                                         ${volumeGroupIdElement}
550                                         ${personaModelIdElement}
551                                         ${personaModelVersionElement}
552                                         ${contrailServiceInstanceFqdnElement}
553                                 </UpdateAAIVfModuleRequest>
554                         """
555                         updateAAIVfModuleRequest = utils.formatXml(updateAAIVfModuleRequest)
556                         execution.setVariable('DOUPVfMod_updateAAIVfModuleRequest', updateAAIVfModuleRequest)
557                         utils.logAudit("updateAAIVfModuleRequest : " + updateAAIVfModuleRequest)
558                         logDebug('Request for UpdateAAIVfModule:\n' + updateAAIVfModuleRequest, isDebugLogEnabled)
559
560                         logDebug('Exited ' + method, isDebugLogEnabled)
561                 } catch (BpmnError e) {
562                         throw e;
563                 } catch (Exception e) {
564                         logError('Caught exception in ' + method, e)
565                         createWorkflowException(execution, 1002, 'Error in prepUpdateAAIVfModule(): ' + e.getMessage())
566                 }
567         }
568
569         /**
570          * Prepare a Request for invoking the SDNC Adapter subflow to perform
571          * a VNF topology 'activate' operation.
572          *
573          * @param execution The flow's execution instance.
574          */
575         public void prepSDNCTopologyAct(Execution execution) {
576                 def method = getClass().getSimpleName() + '.prepSDNCTopologyAct(' +
577                         'execution=' + execution.getId() +
578                         ')'
579                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
580                 logDebug('Entered ' + method, isDebugLogEnabled)
581
582                 try {
583                         def requestId = execution.getVariable('DOUPVfMod_requestId')
584                         def serviceInstanceId = execution.getVariable('DOUPVfMod_serviceInstanceId')
585                         def callbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
586                         def serviceId = execution.getVariable('DOUPVfMod_serviceId')
587                         def vnfId = execution.getVariable('DOUPVfMod_vnfId')
588                         def vfModuleId = execution.getVariable('DOUPVfMod_vfModuleId')
589                         def vfModuleModelName = execution.getVariable('DOUPVfMod_vfModuleModelName')
590                         def VfModule vfModule = (VfModule) execution.getVariable('DOUPVfMod_vfModule')
591                         def vfModuleName = vfModule.getElementText('vf-module-name')
592                         def tenantId = execution.getVariable('DOUPVfMod_tenantId')
593                         def aicCloudRegion = execution.getVariable('DOUPVfMod_aicCloudRegion')
594                         
595                         def vnfParamsXml = execution.getVariable('DOUPVfMod_vnfParams')
596                         def vnfNetworks = transformNetworkParamsToVnfNetworks(vnfParamsXml)
597
598                         String sdncTopologyRequest = """
599                                 <sdncadapterworkflow:SDNCAdapterWorkflowRequest
600                                                 xmlns:sdncadapterworkflow="http://ecomp.att.com/mso/workflow/schema/v1"
601                                                 xmlns:sdncadapter="http://domain2.att.com/workflow/sdnc/adapter/schema/v1">
602                                         <sdncadapter:RequestHeader>
603                                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
604                                                 <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>   
605                                                 <sdncadapter:SvcAction>activate</sdncadapter:SvcAction>
606                                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
607                                                 <sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
608                                         </sdncadapter:RequestHeader>
609                                         <sdncadapterworkflow:SDNCRequestData>
610                                               <request-information>
611                                                  <request-id>${requestId}</request-id>
612                                                  <request-action>ChangeVNFActivateRequest</request-action>
613                                                  <source>PORTAL</source>
614                                                  <notification-url/>
615                                                  <order-number/>
616                                                  <order-version/>
617                                               </request-information>
618                                               <service-information>
619                                                  <service-type>${serviceId}</service-type>
620                                                  <service-instance-id>${vnfId}</service-instance-id>
621                                                  <subscriber-name>dontcare</subscriber-name>
622                                               </service-information>
623                                               <vnf-request-information>
624                                                  <vnf-id>${vfModuleId}</vnf-id>
625                                                  <vnf-type>${vfModuleModelName}</vnf-type>
626                                                  <vnf-name>${vfModuleName}</vnf-name>
627                                                  <tenant>${tenantId}</tenant>
628                                                  <aic-clli>${aicCloudRegion}</aic-clli> <!-- Optional -->
629                                                  ${vnfNetworks}                                                 <!-- Optional -->
630                                               </vnf-request-information>
631                                         </sdncadapterworkflow:SDNCRequestData>
632                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>
633                         """
634                         sdncTopologyRequest = utils.formatXml(sdncTopologyRequest)
635                         execution.setVariable('DOUPVfMod_sdncTopologyRequest', sdncTopologyRequest)
636                         utils.logAudit("sdncTopologyRequest : " + sdncTopologyRequest)
637                         logDebug('Request for SDNCAdapter topology/activate:\n' + sdncTopologyRequest, isDebugLogEnabled)
638                         
639
640                         logDebug('Exited ' + method, isDebugLogEnabled)
641                 } catch (BpmnError e) {
642                         throw e;
643                 } catch (Exception e) {
644                         logError('Caught exception in ' + method, e)
645                         createWorkflowException(execution, 1002, 'Error in prepSDNCTopologyAct(): ' + e.getMessage())
646                 }
647         }
648
649         /**
650          * Log a WorkflowException that has been created. 
651          *
652          * @param execution The flow's execution instance.
653          */
654         public void handleWorkflowException(Execution execution) {
655                 def method = getClass().getSimpleName() + '.handleWorkflowException(' +
656                         'execution=' + execution.getId() +
657                         ')'
658                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
659                 logDebug('Entered ' + method, isDebugLogEnabled)
660
661                 try {
662                         def WorkflowException workflowException = (WorkflowException) execution.getVariable('WorkflowException')
663                         logError(method + ' caught WorkflowException: ' + workflowException.getErrorMessage())
664                         
665                         logDebug('Exited ' + method, isDebugLogEnabled)
666                 } catch (BpmnError e) {
667                         throw e;
668                 } catch (Exception e) {
669                         logError('Caught exception in ' + method, e)
670                         createWorkflowException(execution, 1002, 'Error in handleWorkflowException(): ' + e.getMessage())
671                 }
672         }
673         
674         public void validateSDNCResponse(Execution execution, String response, String method){
675                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
676                 def prefix = execution.getVariable("prefix")
677                 
678                 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
679
680                 WorkflowException workflowException = execution.getVariable("WorkflowException")
681                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
682
683                 utils.logAudit("workflowException: " + workflowException)
684
685                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
686                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
687
688                 utils.logAudit("SDNCResponse: " + response)
689
690                 String sdncResponse = response
691                 if(execution.getVariable(prefix + 'sdncResponseSuccess') == true){
692                         logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse, isDebugLogEnabled)                 
693                 }else{
694                         logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
695                         throw new BpmnError("MSOWorkflowException")
696                 }
697                 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
698         }
699         
700         /**
701          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VNF info.
702          * A 200 response is expected with the VNF info in the response body. Will find out the base module info.
703          *
704          * @param execution The flow's execution instance.
705          */
706         public void queryAAIVfModule(Execution execution) {
707                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
708                 def method = getClass().getSimpleName() + '.getVfModule(' +
709                         'execution=' + execution.getId() +
710                         ')'
711                 logDebug('Entered ' + method, isDebugLogEnabled)
712
713                 try {
714                         def vnfId = execution.getVariable('DOUPVfMod_vnfId')
715                         def vfModuleId = execution.getVariable('DOUPVfMod_vfModuleId')
716
717                         AaiUtil aaiUriUtil = new AaiUtil(this)
718                         String  aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
719                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
720
721                         String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")
722                         utils.logAudit("AAI endPoint: " + endPoint)
723
724                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
725
726                         try {
727                                 RESTConfig config = new RESTConfig(endPoint);
728                                 def responseData = ''
729                                 def aaiRequestId = UUID.randomUUID().toString()
730                                 RESTClient client = new RESTClient(config).
731                                         addHeader('X-TransactionId', aaiRequestId).
732                                         addHeader('X-FromAppId', 'MSO').
733                                         addHeader('Content-Type', 'application/xml').
734                                         addHeader('Accept','application/xml');
735                                 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
736                                         client.addAuthorizationHeader(basicAuthCred)
737                                 }
738                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
739                                 APIResponse response = client.httpGet()
740                                 utils.logAudit("createVfModule - invoking httpGet() to AAI")
741
742                                 responseData = response.getResponseBodyAsString()
743                                 if (responseData != null) {
744                                         logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)
745
746                                 }
747
748                                 utils.logAudit("createVfModule - queryAAIVfModule Response: " + responseData)
749                                 utils.logAudit("createVfModule - queryAAIVfModule ResponseCode: " + response.getStatusCode())
750
751                                 execution.setVariable('DOUPVfMod_queryAAIVfModuleResponseCode', response.getStatusCode())
752                                 execution.setVariable('DOUPVfMod_queryAAIVfModuleResponse', responseData)
753                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
754                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
755                                 if (response.getStatusCode() == 200) {
756                                         // Parse the VNF record from A&AI to find base module info
757                                         logDebug('Parsing the VNF data to find base module info', isDebugLogEnabled)
758                                         if (responseData != null) {
759                                                 def vfModulesText = utils.getNodeXml(responseData, "vf-modules")
760                                                 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
761                                                 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
762                                                 int vfModulesSize = 0
763                                                 for (i in 0..vfModules.size()-1) {
764                                                         def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
765                                                         def isBaseVfModule = utils.getNodeText(vfModuleXml, "is-base-vf-module")
766
767                                                         if (isBaseVfModule == "true") {
768                                                             String baseModuleId = utils.getNodeText1(vfModuleXml, "vf-module-id")
769                                                             execution.setVariable("DOUPVfMod_baseVfModuleId", baseModuleId)
770                                                             logDebug('Received baseVfModuleId: ' + baseModuleId, isDebugLogEnabled)
771                                                             String baseModuleHeatStackId = utils.getNodeText1(vfModuleXml, "heat-stack-id")
772                                                             execution.setVariable("DOUPVfMod_baseVfModuleHeatStackId", baseModuleHeatStackId)
773                                                             logDebug('Received baseVfModuleHeatStackId: ' + baseModuleHeatStackId, isDebugLogEnabled)
774                                                         }
775                                                 }
776                                         }
777                                 }
778                         } catch (Exception ex) {
779                                 ex.printStackTrace()
780                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
781                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
782                         }
783                         logDebug('Exited ' + method, isDebugLogEnabled)
784                 } catch (BpmnError e) {
785                         throw e;
786                 } catch (Exception e) {
787                         logError('Caught exception in ' + method, e)
788                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())
789                 }
790         }
791         
792         
793 }