Dynamic Cloud Owner Support
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / CreateAAIVfModule.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.bpmn.common.scripts
22 import org.camunda.bpm.engine.delegate.DelegateExecution
23 import org.onap.so.bpmn.core.RollbackData
24 import org.onap.so.bpmn.core.UrnPropertiesReader
25 import org.onap.so.client.aai.AAIObjectType
26 import org.onap.so.client.aai.entities.uri.AAIResourceUri
27 import org.onap.so.client.aai.entities.uri.AAIUriFactory
28 import org.onap.so.client.graphinventory.entities.uri.Depth
29 import org.onap.so.logger.MessageEnum
30 import org.onap.so.logger.MsoLogger
31 import org.onap.so.rest.APIResponse
32 import org.springframework.web.util.UriUtils
33
34 public class CreateAAIVfModule extends AbstractServiceTaskProcessor{
35         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateAAIVfModule.class);
36
37         def Prefix="CAAIVfMod_"
38         ExceptionUtil exceptionUtil = new ExceptionUtil()
39         public void initProcessVariables(DelegateExecution execution) {
40                 execution.setVariable("prefix",Prefix)
41                 execution.setVariable("CAAIVfMod_vnfId",null)
42                 execution.setVariable("CAAIVfMod_vnfName",null)
43                 execution.setVariable("CAAIVfMod_vnfType",null)
44                 execution.setVariable("CAAIVfMod_serviceId",null)
45                 execution.setVariable("CAAIVfMod_personaId",null)
46                 execution.setVariable("CAAIVfMod_personaVer",null)
47                 execution.setVariable("CAAIVfMod_modelCustomizationId",null)
48                 execution.setVariable("CAAIVfMod_vnfPersonaId",null)
49                 execution.setVariable("CAAIVfMod_vnfPersonaVer",null)
50                 execution.setVariable("CAAIVfMod_isBaseVfModule", false)
51                 execution.setVariable("CAAIVfMod_moduleName",null)
52                 execution.setVariable("CAAIVfMod_moduleModelName",null)
53                 execution.setVariable("CAAIVfMod_newGenericVnf",false)
54                 execution.setVariable("CAAIVfMod_genericVnfGetEndpoint",null)
55                 execution.setVariable("CAAIVfMod_genericVnfPutEndpoint",null)
56                 execution.setVariable("CAAIVfMod_aaiNamespace",null)
57                 execution.setVariable("CAAIVfMod_moduleExists",false)
58                 execution.setVariable("CAAIVfMod_baseModuleConflict", false)
59                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", null)
60                 
61                 
62                 // CreateAAIVfModule workflow response variable placeholders
63                 execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode",null)
64                 execution.setVariable("CAAIVfMod_queryGenericVnfResponse","")
65                 execution.setVariable("CAAIVfMod_createGenericVnfResponseCode",null)
66                 execution.setVariable("CAAIVfMod_createGenericVnfResponse","")
67                 execution.setVariable("CAAIVfMod_createVfModuleResponseCode",null)
68                 execution.setVariable("CAAIVfMod_createVfModuleResponse","")
69                 execution.setVariable("CAAIVfMod_parseModuleResponse","")
70                 execution.setVariable("CAAIVfMod_deleteGenericVnfResponseCode",null)
71                 execution.setVariable("CAAIVfMod_deleteGenericVnfResponse","")
72                 execution.setVariable("CAAIVfMod_deleteVfModuleResponseCode",null)
73                 execution.setVariable("CAAIVfMod_deleteVfModuleResponse","")
74                 execution.setVariable("CreateAAIVfModuleResponse","")
75                 execution.setVariable("RollbackData", null)
76
77         }       
78         
79         // parse the incoming CREATE_VF_MODULE request and store the Generic VNF
80         // and VF Module data in the flow DelegateExecution
81         public void preProcessRequest(DelegateExecution execution) {
82                 initProcessVariables(execution)
83
84                 def vnfId = execution.getVariable("vnfId")              
85                 if (vnfId == null || vnfId.isEmpty()) {
86                         execution.setVariable("CAAIVfMod_newGenericVnf", true)
87                         execution.setVariable("CAAIVfMod_vnfId","")
88                 }
89                 else {
90                         execution.setVariable("CAAIVfMod_vnfId",vnfId)
91                 }
92
93                 def vnfName = execution.getVariable("vnfName")          
94                 execution.setVariable("CAAIVfMod_vnfName", vnfName)
95
96                 String vnfType = execution.getVariable("vnfType")
97                 if (vnfType != null && !vnfType.isEmpty()) {
98                         execution.setVariable("CAAIVfMod_vnfType", vnfType)
99                 } else {
100                         execution.setVariable("CAAIVfMod_vnfType","")
101                 }
102
103                 execution.setVariable("CAAIVfMod_serviceId", execution.getVariable("serviceId"))
104                 
105                 String personaModelId = execution.getVariable("personaModelId")
106
107                 if (personaModelId != null && !personaModelId.isEmpty()) {
108                         execution.setVariable("CAAIVfMod_personaId",personaModelId)
109                 } else {
110                         execution.setVariable("CAAIVfMod_personaId","")
111                 }
112                 
113                 String personaModelVersion = execution.getVariable("personaModelVersion")
114
115                 if (personaModelVersion != null && !personaModelVersion.isEmpty()) {
116                         execution.setVariable("CAAIVfMod_personaVer", personaModelVersion)
117                 } else {
118                         execution.setVariable("CAAIVfMod_personaVer","")
119                 }
120                 
121                 
122                 String modelCustomizationId = execution.getVariable("modelCustomizationId")
123
124                 if (modelCustomizationId != null && !modelCustomizationId.isEmpty()) {
125                         execution.setVariable("CAAIVfMod_modelCustomizationId",modelCustomizationId)
126                 } else {
127                         execution.setVariable("CAAIVfMod_modelCustomizationId","")
128                 }
129                 
130                 String vnfPersonaModelId = execution.getVariable("vnfPersonaModelId")
131                 
132                 if (vnfPersonaModelId != null && !vnfPersonaModelId.isEmpty()) {
133                         execution.setVariable("CAAIVfMod_vnfPersonaId",vnfPersonaModelId)
134                 } else {
135                         execution.setVariable("CAAIVfMod_vnfPersonaId","")
136                 }
137                 
138                 String vnfPersonaModelVersion = execution.getVariable("vnfPersonaModelVersion")
139
140                 if (vnfPersonaModelVersion != null && !vnfPersonaModelVersion.isEmpty()) {
141                         execution.setVariable("CAAIVfMod_vnfPersonaVer",vnfPersonaModelVersion)
142                 } else {
143                         execution.setVariable("CAAIVfMod_vnfPersonaVer","")
144                 }
145                 
146                 //isBaseVfModule
147                 Boolean isBaseVfModule = false
148                 String isBaseVfModuleString = execution.getVariable("isBaseVfModule")
149                 if (isBaseVfModuleString != null && isBaseVfModuleString.equals("true")) {
150                                 isBaseVfModule = true                   
151                 }
152                 execution.setVariable("CAAIVfMod_isBaseVfModule", isBaseVfModule)
153                 
154                 String isVidRequest = execution.getVariable("isVidRequest")
155                 if (isVidRequest != null && "true".equals(isVidRequest)) {
156                         msoLogger.debug("VID Request received")         
157                 }
158
159                 execution.setVariable("CAAIVfMod_moduleName",execution.getVariable("vfModuleName"))
160                 execution.setVariable("CAAIVfMod_moduleModelName",execution.getVariable("vfModuleModelName"))
161
162                 AaiUtil aaiUriUtil = new AaiUtil(this)
163                 String aaiNamespace = aaiUriUtil.getNamespace()
164                 msoLogger.debug('AAI namespace is: ' + aaiNamespace)
165         
166                 execution.setVariable("CAAIVfMod_aaiNamespace","${aaiNamespace}")               
167
168         }
169         
170         // send a GET request to AA&I to retrieve the Generic VNF/VF Module information based on a Vnf Name
171         // expect a 200 response with the information in the response body or a 404 if the Generic VNF does not exist
172         public void queryAAIForGenericVnf(DelegateExecution execution) {
173                 
174                 AaiUtil aaiUtil = new AaiUtil(this)
175                 AAIResourceUri uri
176                 
177                 def vnfId = execution.getVariable("CAAIVfMod_vnfId")
178                 def vnfName = execution.getVariable("CAAIVfMod_vnfName")
179                 
180                 if (vnfId == null || vnfId.isEmpty()) {
181                         uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "")
182                         uri.queryParam("vnf-name", vnfName)
183                 } else {
184                         uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
185                 }
186                 
187                 uri.depth(Depth.ONE)
188                 String endPoint = aaiUtil.createAaiUri(uri)
189
190                 try {
191                         msoLogger.debug("queryAAIForGenericVnf() endpoint-" + endPoint)
192                         msoLogger.debug("invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint)
193                         msoLogger.debug("CreateAAIVfModule sending GET call to AAI Endpoint: " + endPoint)
194
195                         APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)
196                         def responseData = response.getResponseBodyAsString()
197                         def statusCode = response.getStatusCode()
198                         execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode", statusCode)
199                         execution.setVariable("CAAIVfMod_queryGenericVnfResponse", responseData)
200
201                         msoLogger.debug("CreateAAIVfModule Response Code: " + statusCode)
202                         msoLogger.debug("CreateAAIVfModule Response data: " + responseData)
203                         msoLogger.debug("Response code:" + statusCode)
204                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
205                 } catch (Exception ex) {
206                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
207                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in queryAAIForGenericVnf.")
208
209                 }
210         }
211         
212         // process the result from queryAAIForGenericVnf()
213         // note: this method is primarily for logging as the actual decision logic is embedded in the bpmn flow 
214         public void processAAIGenericVnfQuery(DelegateExecution execution) {
215                 def result = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
216                 
217                 if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
218                         execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
219                         msoLogger.debug("New Generic VNF requested and it does not already exist")
220                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
221                                 !execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
222                         msoLogger.debug("Adding module to existing Generic VNF")        
223                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
224                                 execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
225                         msoLogger.debug("Invalid request for new Generic VNF which already exists")
226                         execution.setVariable("CAAIVfMod_queryGenericVnfResponse",
227                                 "Invalid request for new Generic VNF which already exists, Vnf Name=" +
228                                  execution.getVariable("CAAIVfMod_vnfName"))    
229                 } else { // execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
230                            // !execution.getVariable("CAAIVfMod_vnfId").isEmpty())
231                         msoLogger.debug("Invalid request for Add-on Module requested for non-existant Generic VNF")     
232                         execution.setVariable("CAAIVfMod_createVfModuleResponse",
233                                 "Invalid request for Add-on Module requested for non-existant Generic VNF, VNF Id=" +
234                                 execution.getVariable("CAAIVfMod_vnfId"))
235                 }
236         }
237
238         // construct and send a PUT request to A&AI to create a new Generic VNF
239         // note: to get here, the vnf-id in the original CREATE_VF_MODULE request was absent or ""
240         public void createGenericVnf(DelegateExecution execution) {
241                 // TBD - is this how we want to generate the Id for the new Generic VNF?
242                 def newVnfId = UUID.randomUUID().toString()
243                 execution.setVariable("CAAIVfMod_vnfId",newVnfId)
244
245                 String payload = """<generic-vnf xmlns="${execution.getVariable("CAAIVfMod_aaiNamespace")}">
246                                                                 <vnf-id>${MsoUtils.xmlEscape(newVnfId)}</vnf-id>
247                                                                 <vnf-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfName"))}</vnf-name>
248                                                                 <vnf-type>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfType"))}</vnf-type>
249                                                                 <service-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_serviceId"))}</service-id>
250                                                                 <orchestration-status>Active</orchestration-status>
251                                                                 <model-invariant-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfPersonaId"))}</model-invariant-id>
252                                                                 <model-version-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfPersonaVer"))}</model-version-id>
253                                                         </generic-vnf>""" as String
254                 execution.setVariable("CAAIVfMod_createGenericVnfPayload", payload)
255
256                 try {
257                         AaiUtil aaiUtil = new AaiUtil(this)
258                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, newVnfId)
259                         String endPoint = aaiUtil.createAaiUri(uri)
260                         
261                         msoLogger.debug("createGenericVnf() endpoint-" + endPoint)
262                         msoLogger.debug("invoking PUT call to AAI with payload:"+System.lineSeparator()+payload)
263                         msoLogger.debug("Sending PUT call to AAI with Endpoint /n" + endPoint + " with payload /n" + payload)
264
265                         APIResponse response = aaiUtil.executeAAIPutCall(execution, endPoint, payload);
266                         def     responseData = response.getResponseBodyAsString()
267                         def responseStatusCode = response.getStatusCode()
268                         execution.setVariable("CAAIVfMod_createGenericVnfResponseCode", responseStatusCode)
269                         execution.setVariable("CAAIVfMod_createGenericVnfResponse", responseData)
270                         
271                         msoLogger.debug("Response Code: " + responseStatusCode)
272                         msoLogger.debug("Response Data: " + responseData)
273                         msoLogger.debug("Response code:" + responseStatusCode)
274                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
275                 } catch (Exception ex) {
276                         ex.printStackTrace()
277                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
278                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createGenericVnf.")
279                 }
280         }
281
282         // construct and send a PUT request to A&AI to create a Base or Add-on VF Module
283         public void createVfModule(DelegateExecution execution, Boolean isBaseModule) {
284                 // TBD - is this how we want to generate the Id for the new (Base) VF Module?
285                 
286                 // Generate the new VF Module ID here if it has not been provided by the parent process
287                 def newModuleId = execution.getVariable('newVfModuleId')
288                 if (newModuleId == null || newModuleId.isEmpty()) {
289                         newModuleId = UUID.randomUUID().toString()
290                 }
291                 
292                 String vnfId = execution.getVariable("CAAIVfMod_vnfId")
293                 
294                 int moduleIndex = 0
295                 if (!isBaseModule) {
296                         def aaiVnfResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
297                         AaiUtil aaiUtil = new AaiUtil(this)
298                         def personaModelId = execution.getVariable("CAAIVfMod_personaId")
299                         
300                         // Check if the response includes model-invariant-id or persona-model-id
301                         // note: getRequiredNodeText() throws an exception if the field is missing
302                         // need to retun a null for the subsequent "either/or" logic to work properly
303 //                      def modelInvariantId = getRequiredNodeText(execution, aaiVnfResponse,'model-invariant-id')
304                         def modelInvariantId = getNodeText(aaiVnfResponse,'model-invariant-id', null)
305                         def fieldToCheck = 'model-invariant-id'
306                         if (!modelInvariantId) {
307                                 fieldToCheck = 'persona-model-id'
308                         }
309                         
310                         moduleIndex = aaiUtil.getLowestUnusedVfModuleIndexFromAAIVnfResponse(execution, aaiVnfResponse, 
311                                 fieldToCheck, personaModelId)
312                 }
313                 def moduleIndexString = String.valueOf(moduleIndex)
314
315                 // if we get to this point, we may be about to create the Vf Module,
316                 // add rollback information about the Generic VNF for this base/add-on module
317                 def rollbackData = execution.getVariable("RollbackData")
318                 if (rollbackData == null) {
319                         rollbackData = new RollbackData();
320                 }
321                 rollbackData.put("VFMODULE", "vnfId", execution.getVariable("CAAIVfMod_vnfId"))
322                 rollbackData.put("VFMODULE", "vnfName", execution.getVariable("CAAIVfMod_vnfName"))
323                 rollbackData.put("VFMODULE", "isBaseModule", isBaseModule.toString())
324                 execution.setVariable("RollbackData", rollbackData)
325                 msoLogger.debug("RollbackData:" + rollbackData)
326                 String payload = """<vf-module xmlns="${execution.getVariable("CAAIVfMod_aaiNamespace")}">
327                                                                 <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
328                                                                 <vf-module-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_moduleName"))}</vf-module-name>
329                                                                 <model-invariant-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_personaId"))}</model-invariant-id>
330                                                                 <model-version-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_personaVer"))}</model-version-id>
331                                                                 <model-customization-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_modelCustomizationId"))}</model-customization-id>
332                                                                 <is-base-vf-module>${MsoUtils.xmlEscape(isBaseModule)}</is-base-vf-module>
333                                                                 <orchestration-status>PendingCreate</orchestration-status>
334                                                                 <module-index>${MsoUtils.xmlEscape(moduleIndex)}</module-index>
335                                                                 </vf-module>""" as String
336                 execution.setVariable("CAAIVfMod_createVfModulePayload", payload)
337
338                 try {
339                         
340                         AaiUtil aaiUtil = new AaiUtil(this)
341                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, newModuleId)
342                         String endPoint = aaiUtil.createAaiUri(uri)
343                         
344                         msoLogger.debug("createVfModule() endpoint-" + endPoint)
345                         msoLogger.debug("invoking PUT call to AAI with payload:"+System.lineSeparator()+payload)
346                         msoLogger.debug("CreateAAIVfModule sending PUT call to AAI with endpoint /n" + endPoint + " with payload /n " + payload)
347
348                         APIResponse response = aaiUtil.executeAAIPutCall(execution, endPoint, payload)  
349                         def responseData = response.getResponseBodyAsString()
350                         def statusCode = response.getStatusCode()
351                         execution.setVariable("CAAIVfMod_createVfModuleResponseCode", statusCode)
352                         execution.setVariable("CAAIVfMod_createVfModuleResponse", responseData)
353                         
354                         msoLogger.debug("Response code:" + statusCode)
355                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
356                         msoLogger.debug("Response Code: " + statusCode)
357                         msoLogger.debug("Response data: " + responseData)
358                         // the base or add-on VF Module was successfully created,
359                         // add the module name to the rollback data and the response
360                         if (isOneOf(statusCode, 200, 201)) {
361                                 rollbackData.put("VFMODULE", "vfModuleId", newModuleId)
362                                 rollbackData.put("VFMODULE", "vfModuleName", execution.getVariable("CAAIVfMod_moduleName"))
363                                 execution.setVariable("RollbackData", rollbackData)
364                                 msoLogger.debug("RollbackData:" + rollbackData)
365                                 
366                                 String responseOut = ""
367                                 
368                                 String isVidRequest = execution.getVariable("isVidRequest")
369                                 
370                                 if (isBaseModule && (isVidRequest == null || "false".equals(isVidRequest))) {                           
371                                 
372                                         responseOut = """<CreateAAIVfModuleResponse>
373                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
374                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
375                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
376                                                                                 </CreateAAIVfModuleResponse>""" as String
377                                 }
378                                 else {
379                                         responseOut = """<CreateAAIVfModuleResponse>
380                                                                                         <vnf-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfNameFromAAI"))}</vnf-name>
381                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
382                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
383                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
384                                                                                 </CreateAAIVfModuleResponse>""" as String
385                                 }
386                                 
387                                 execution.setVariable("CreateAAIVfModuleResponse", responseOut)
388                                 msoLogger.debug("CreateAAIVfModuleResponse:" + System.lineSeparator()+responseOut)
389                                 msoLogger.debug("CreateAAIVfModule Response /n " + responseOut)
390                         }
391                 } catch (Exception ex) {
392                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
393                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createVfModule.")
394                 }
395         }
396         
397         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
398         // requested for an Add-on VF Module does not already exist for the specified Generic VNF
399         // also retrieves VNF name from AAI response for existing VNF
400         public void parseForAddOnModule(DelegateExecution execution) {
401                 def xml = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
402                 def vnfNameFromAAI = utils.getNodeText(xml, "vnf-name")
403                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
404                 msoLogger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)       
405                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
406                 msoLogger.debug("VF Module to be added: " + newModuleName)
407                 def qryModuleNameList = utils.getMultNodes(xml, "vf-module-name")
408                 execution.setVariable("CAAIVfMod_moduleExists", false)
409                 if (qryModuleNameList != null) {
410                         msoLogger.debug("Existing VF Module List: " + qryModuleNameList)
411                         for (String qryModuleName : qryModuleNameList) {
412                                 if (newModuleName.equals(qryModuleName)) {
413                                         // a module with the requested name already exists - failure
414                                         msoLogger.debug("VF Module " + qryModuleName + " already exists for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
415                                         execution.setVariable("CAAIVfMod_moduleExists", true)
416                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
417                                                 "VF Module " + qryModuleName + " already exists for Generic VNF " +
418                                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
419                                         break
420                                 }
421                         }
422                 }
423                 if (execution.getVariable("CAAIVfMod_moduleExists") == false) {
424                         msoLogger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
425                         execution.setVariable("CAAIVfMod_parseModuleResponse",
426                                 "VF Module " + newModuleName + " does not exist for Generic VNF " +
427                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
428                 }               
429         }
430         
431         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
432         // requested for an Add-on VF Module does not already exist for the specified Generic VNF; 
433         // also retrieves VNF name from AAI response for existing VNF
434         public void parseForBaseModule(DelegateExecution execution) {
435                 def xml = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
436                 def vnfNameFromAAI = utils.getNodeText(xml, "vnf-name")
437                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
438                 msoLogger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)       
439                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
440                 msoLogger.debug("VF Module to be added: " + newModuleName)
441                 def qryModuleNameList = utils.getMultNodes(xml, "vf-module-name")
442                 execution.setVariable("CAAIVfMod_moduleExists", false)
443                 if (qryModuleNameList != null) {
444                         msoLogger.debug("Existing VF Module List: " + qryModuleNameList)
445                         for (String qryModuleName : qryModuleNameList) {
446                                 if (newModuleName.equals(qryModuleName)) {
447                                         // a module with the requested name already exists - failure
448                                         msoLogger.debug("VF Module " + qryModuleName + " already exists for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
449                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
450                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
451                                                 "VF Module " + qryModuleName + " already exists for Generic VNF " +
452                                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
453                                         break
454                                 }
455                         }
456                 }
457                 def isBaseVfModuleList = utils.getMultNodes(xml, "is-base-vf-module")
458                 if (isBaseVfModuleList != null && !execution.getVariable("CAAIVfMod_baseModuleConflict")) {
459                         
460                         for (String baseValue : isBaseVfModuleList) {
461                                 if (baseValue.equals("true")) {
462                                         // a base module already exists in this VNF - failure
463                                         msoLogger.debug("Base VF Module already exists for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
464                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
465                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
466                                                 "Base VF Module already exists for Generic VNF " +
467                                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
468                                         break
469                                 }
470                         }
471                 
472                 }
473                 if (execution.getVariable("CAAIVfMod_moduleExists") == false && execution.getVariable("CAAIVfMod_baseModuleConflict") == false) {
474                         msoLogger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
475                         execution.setVariable("CAAIVfMod_parseModuleResponse",
476                                 "VF Module " + newModuleName + " does not exist for Generic VNF " +
477                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
478                 }               
479         }
480         
481         // generates a WorkflowException when the A&AI query returns a response code other than 200 or 404
482         public void handleAAIQueryFailure(DelegateExecution execution) {
483                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred attempting to query AAI, Response Code " + execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") + ", Error Response " + execution.getVariable("CAAIVfMod_queryGenericVnfResponse"), "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
484                 int code = execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode")
485                 exceptionUtil.buildAndThrowWorkflowException(execution, code, "Error occurred attempting to query AAI")
486
487         }
488         
489         // generates a WorkflowException if
490         //              - the A&AI Generic VNF PUT returns a response code other than 200 or 201
491         //              - the requested Generic VNF already exists but vnf-id == null
492         //              - the requested Generic VNF does not exist but vnf-id != null
493         //              - the A&AI VF Module PUT returns a response code other than 200 or 201
494         //              - the requested VF Module already exists for the Generic VNF
495         public void handleCreateVfModuleFailure(DelegateExecution execution) {
496                 def errorCode
497                 def errorResponse
498                 if (execution.getVariable("CAAIVfMod_createGenericVnfResponseCode") != null &&
499                                 !isOneOf(execution.getVariable("CAAIVfMod_createGenericVnfResponseCode"), 200, 201)) {
500                         msoLogger.debug("Failure creating Generic VNF: " + execution.getVariable("CAAIVfMod_createGenericVnfResponse"))
501                         errorResponse = execution.getVariable("CAAIVfMod_createGenericVnfResponse")
502                         errorCode = 5000
503                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponse") != null &&
504                                 execution.getVariable("CAAIVfMod_newGenericVnf") == true) {
505                         // attempted to create a Generic VNF that already exists but vnf-id == null
506                         msoLogger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
507                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
508                         errorCode = 1002
509                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
510                                 execution.getVariable("CAAIVfMod_newGenericVnf") == false) {
511                         // attempted to create a Generic VNF where vnf-name does not exist but vnf-id != null
512                         msoLogger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
513                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
514                         errorCode = 1002
515                 } else if (execution.getVariable("CAAIVfMod_createVfModuleResponseCode") != null) {
516                         msoLogger.debug("Failed to add VF Module: " + execution.getVariable("CAAIVfMod_createVfModuleResponse"))
517                         errorResponse = execution.getVariable("CAAIVfMod_createVfModuleResponse")
518                         errorCode = 5000
519                 } else if (execution.getVariable("CAAIVfMod_moduleExists") == true) {
520                         msoLogger.debug("Attempting to add VF Module that already exists: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
521                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
522                         errorCode = 1002
523                 } else if (execution.getVariable("CAAIVfMod_baseModuleConflict") == true) {
524                         msoLogger.debug("Attempting to add Base VF Module to VNF that already has a Base VF Module: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
525                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
526                         errorCode = 1002
527                 } else {
528                         // if the responses get populated corerctly, we should never get here
529                         errorResponse = "Unknown error occurred during CreateAAIVfModule flow"
530                         errorCode = 2000
531                 }
532
533                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred during CreateAAIVfModule flow", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, errorResponse);
534                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
535                 msoLogger.debug("Workflow exception occurred in CreateAAIVfModule: " + errorResponse)
536         }
537
538         /**
539          * Performs a rollback.
540          * TBD: This method requires additional testing once integrated with the
541          *      main CreateVfModule flow.
542          * @param execution the execution
543          */
544         public void rollback(DelegateExecution execution) {
545                 def method = getClass().getSimpleName() + ".rollback(" +
546                         "execution=" + execution.getId() +
547                         ")"
548                 msoLogger.debug("Entered " + method)
549
550                 try {
551                         RollbackData rollbackData = (RollbackData) execution.getVariable("RollbackData")
552                         msoLogger.debug("RollbackData:" + rollbackData)
553
554                         AaiUtil aaiUriUtil = new AaiUtil(this)
555                         
556                         if (rollbackData != null) {
557                                 if (rollbackData.hasType("VFMODULE")) {
558                                         // use the DeleteAAIVfModule groovy methods for the rollback
559                                         def vnfId = rollbackData.get("VFMODULE", "vnfId")
560                                         def vfModuleId = rollbackData.get("VFMODULE", "vfModuleId")
561                                         def isBaseModule = rollbackData.get("VFMODULE", "isBaseModule")
562                                         execution.setVariable("DAAIVfMod_vnfId", vnfId)         
563                                         execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
564
565                                         DeleteAAIVfModule dvm = new DeleteAAIVfModule()
566                                         // query A&AI to get the needed information for the delete(s)
567                                         dvm.queryAAIForGenericVnf(execution)
568                                         dvm.parseForVfModule(execution)
569                                         
570                                         // roll back the base or add-on module
571                                         dvm.deleteVfModule(execution)
572                                         def responseCode = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode")
573                                         def response = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode")
574
575                                         if (isOneOf(responseCode, 200, 204)) {
576                                                 msoLogger.debug("Received " + responseCode + " to VF Module rollback request")
577                                         } else {
578                                                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Received " + responseCode + " to VF Module rollback request", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, rollbackData + System.lineSeparator() + "Response: " + response);
579                                         }
580                                         
581                                         // a new Generic VNF was created that needs to be rolled back
582                                         if (isBaseModule.equals("true")) {
583                                                 dvm.queryAAIForGenericVnf(execution)
584                                                 dvm.parseForResourceVersion(execution)
585                                                 dvm.deleteGenericVnf(execution)
586                                                 responseCode = execution.getVariable("DAAIVfMod_deleteGenericVnfResponseCode")
587                                                 response = execution.getVariable("DAAIVfMod_deleteGenericVnfResponse")
588         
589                                                 if (isOneOf(responseCode, 200, 204)) {
590                                                         msoLogger.debug("Received " + responseCode + " to Generic VNF rollback request")
591                                                         execution.setVariable("RollbackResult", "SUCCESS")
592                                                 } else {
593                                                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Received " + responseCode + " to Generic VNF rollback request", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, rollbackData + System.lineSeparator() + "Response: " + response);
594                                                 }
595                                         } else {
596                                                 execution.setVariable("RollbackResult", "SUCCESS")
597                                         }
598                                 }
599                         }
600
601                         msoLogger.debug("Exited " + method)
602                 } catch (Exception e) {
603                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Caught exception in " + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
604                 }
605         }
606 }