Springboot 2.0 upgrade
[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.aai.domain.yang.GenericVnf
24 import org.onap.so.bpmn.core.RollbackData
25 import org.onap.so.client.aai.AAIObjectPlurals
26 import org.onap.so.client.aai.AAIObjectType
27 import org.onap.so.client.aai.entities.uri.AAIResourceUri
28 import org.onap.so.client.aai.entities.uri.AAIUriFactory
29 import org.onap.so.client.graphinventory.entities.uri.Depth
30 import org.onap.so.db.catalog.beans.OrchestrationStatus
31 import org.onap.so.logger.MessageEnum
32 import org.onap.so.logger.MsoLogger
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                 AAIResourceUri uri
175                 def vnfId = execution.getVariable("CAAIVfMod_vnfId")
176                 def vnfName = execution.getVariable("CAAIVfMod_vnfName")
177                 if (vnfId == null || vnfId.isEmpty()) {
178                         uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF)
179                         uri.queryParam("vnf-name", vnfName)
180                 } else {
181                         uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
182                 }
183                 uri.depth(Depth.ONE)
184                 try {
185                         Optional<GenericVnf> genericVnfOp = getAAIClient().get(GenericVnf.class,  uri)
186             if(genericVnfOp.isPresent()){
187                 execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode", 200)
188                 execution.setVariable("CAAIVfMod_queryGenericVnfResponse", genericVnfOp.get())
189             }else{
190                 execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode", 404)
191                 execution.setVariable("CAAIVfMod_queryGenericVnfResponse", "Generic Vnf not Found!")
192             }
193                 } catch (Exception ex) {
194                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
195                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in queryAAIForGenericVnf.")
196
197                 }
198         }
199         
200         // process the result from queryAAIForGenericVnf()
201         // note: this method is primarily for logging as the actual decision logic is embedded in the bpmn flow 
202         public void processAAIGenericVnfQuery(DelegateExecution execution) {
203                 if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
204                         execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
205                         msoLogger.debug("New Generic VNF requested and it does not already exist")
206                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
207                                 !execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
208                         msoLogger.debug("Adding module to existing Generic VNF")        
209                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
210                                 execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
211                         msoLogger.debug("Invalid request for new Generic VNF which already exists")
212                         execution.setVariable("CAAIVfMod_queryGenericVnfResponse",
213                                 "Invalid request for new Generic VNF which already exists, Vnf Name=" +
214                                  execution.getVariable("CAAIVfMod_vnfName"))    
215                 } else { // execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
216                            // !execution.getVariable("CAAIVfMod_vnfId").isEmpty())
217                         msoLogger.debug("Invalid request for Add-on Module requested for non-existant Generic VNF")     
218                         execution.setVariable("CAAIVfMod_createVfModuleResponse",
219                                 "Invalid request for Add-on Module requested for non-existant Generic VNF, VNF Id=" +
220                                 execution.getVariable("CAAIVfMod_vnfId"))
221                 }
222         }
223
224         // construct and send a PUT request to A&AI to create a new Generic VNF
225         // note: to get here, the vnf-id in the original CREATE_VF_MODULE request was absent or ""
226         public void createGenericVnf(DelegateExecution execution) {
227                 // TBD - is this how we want to generate the Id for the new Generic VNF?
228                 def newVnfId = UUID.randomUUID().toString()
229                 execution.setVariable("CAAIVfMod_vnfId",newVnfId)
230
231         GenericVnf genericVnf = new GenericVnf()
232         genericVnf.setVnfId(newVnfId)
233         genericVnf.setVnfName(execution.getVariable("CAAIVfMod_vnfName"))
234         genericVnf.setVnfType(execution.getVariable("CAAIVfMod_vnfType"))
235         genericVnf.setServiceId(execution.getVariable("CAAIVfMod_serviceId"))
236         genericVnf.setOrchestrationStatus(OrchestrationStatus.ACTIVE.toString())
237         genericVnf.setModelInvariantId(execution.getVariable("CAAIVfMod_vnfPersonaId"))
238         genericVnf.setModelVersionId(execution.getVariable("CAAIVfMod_vnfPersonaVer"))
239
240                 try {
241                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, newVnfId)
242             getAAIClient().create(uri,genericVnf)
243                         execution.setVariable("CAAIVfMod_createGenericVnfResponseCode", 201)
244                         execution.setVariable("CAAIVfMod_createGenericVnfResponse", "Vnf Created")
245                 } catch (Exception ex) {
246                         ex.printStackTrace()
247                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
248                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createGenericVnf.")
249                 }
250         }
251
252         // construct and send a PUT request to A&AI to create a Base or Add-on VF Module
253         public void createVfModule(DelegateExecution execution, Boolean isBaseModule) {
254                 // TBD - is this how we want to generate the Id for the new (Base) VF Module?
255                 
256                 // Generate the new VF Module ID here if it has not been provided by the parent process
257                 def newModuleId = execution.getVariable('newVfModuleId')
258                 if (newModuleId == null || newModuleId.isEmpty()) {
259                         newModuleId = UUID.randomUUID().toString()
260                 }
261                 
262                 String vnfId = execution.getVariable("CAAIVfMod_vnfId")
263                 
264                 int moduleIndex = 0
265                 if (!isBaseModule) {
266             GenericVnf aaiVnfResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
267             moduleIndex = getLowestUnusedVfModuleIndexFromAAIVnfResponse(aaiVnfResponse,execution)
268                 }
269
270                 // if we get to this point, we may be about to create the Vf Module,
271                 // add rollback information about the Generic VNF for this base/add-on module
272                 def rollbackData = execution.getVariable("RollbackData")
273                 if (rollbackData == null) {
274                         rollbackData = new RollbackData();
275                 }
276                 rollbackData.put("VFMODULE", "vnfId", execution.getVariable("CAAIVfMod_vnfId"))
277                 rollbackData.put("VFMODULE", "vnfName", execution.getVariable("CAAIVfMod_vnfName"))
278                 rollbackData.put("VFMODULE", "isBaseModule", isBaseModule.toString())
279                 execution.setVariable("RollbackData", rollbackData)
280                 msoLogger.debug("RollbackData:" + rollbackData)
281
282         org.onap.aai.domain.yang.VfModule vfModule = new org.onap.aai.domain.yang.VfModule()
283         vfModule.setVfModuleId(newModuleId)
284         vfModule.setVfModuleName(execution.getVariable("CAAIVfMod_moduleName"))
285         vfModule.setModelInvariantId(execution.getVariable("CAAIVfMod_personaId"))
286         vfModule.setModelVersionId(execution.getVariable("CAAIVfMod_personaVer"))
287         vfModule.setModelCustomizationId(execution.getVariable("CAAIVfMod_modelCustomizationId"))
288         vfModule.setIsBaseVfModule(isBaseModule)
289         vfModule.setOrchestrationStatus(OrchestrationStatus.PENDING_CREATE.toString())
290         vfModule.setModuleIndex(moduleIndex)
291
292                 try {
293
294                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, newModuleId)
295             getAAIClient().create(uri,vfModule)
296             def statusCode = 201
297                         execution.setVariable("CAAIVfMod_createVfModuleResponseCode", statusCode)
298                         execution.setVariable("CAAIVfMod_createVfModuleResponse", "Vf Module Created")
299
300
301                         // the base or add-on VF Module was successfully created,
302                         // add the module name to the rollback data and the response
303                         if (isOneOf(statusCode, 200, 201)) {
304                                 rollbackData.put("VFMODULE", "vfModuleId", newModuleId)
305                                 rollbackData.put("VFMODULE", "vfModuleName", execution.getVariable("CAAIVfMod_moduleName"))
306                                 execution.setVariable("RollbackData", rollbackData)
307                                 msoLogger.debug("RollbackData:" + rollbackData)
308                                 
309                                 String responseOut = ""
310                                 
311                                 String isVidRequest = execution.getVariable("isVidRequest")
312                                 def moduleIndexString = String.valueOf(moduleIndex)
313                                 if (isBaseModule && (isVidRequest == null || "false".equals(isVidRequest))) {                           
314                                 
315                                         responseOut = """<CreateAAIVfModuleResponse>
316                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
317                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
318                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
319                                                                                 </CreateAAIVfModuleResponse>""" as String
320                                 }
321                                 else {
322                                         responseOut = """<CreateAAIVfModuleResponse>
323                                                                                         <vnf-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfNameFromAAI"))}</vnf-name>
324                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
325                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
326                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
327                                                                                 </CreateAAIVfModuleResponse>""" as String
328                                 }
329                                 
330                                 execution.setVariable("CreateAAIVfModuleResponse", responseOut)
331                                 msoLogger.debug("CreateAAIVfModuleResponse:" + System.lineSeparator()+responseOut)
332                                 msoLogger.debug("CreateAAIVfModule Response /n " + responseOut)
333                         }
334                 } catch (Exception ex) {
335             execution.setVariable("CAAIVfMod_createVfModuleResponseCode", 500)
336             execution.setVariable("CAAIVfMod_createVfModuleResponse", ex.getMessage())
337                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
338                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createVfModule.")
339                 }
340         }
341
342     private int getLowestUnusedVfModuleIndexFromAAIVnfResponse(GenericVnf genericVnf,DelegateExecution execution){
343         String personaModelId = execution.getVariable("CAAIVfMod_personaId")
344         if(genericVnf!=null && genericVnf.getVfModules()!= null &&
345                 !genericVnf.getVfModules().getVfModule().isEmpty()){
346             Set<Integer> moduleIndices = new TreeSet<>()
347             for(org.onap.aai.domain.yang.VfModule vfModule in genericVnf.getVfModules().getVfModule()){
348                 if(genericVnf.getModelInvariantId()==null){
349                     if(vfModule.getPersonaModelVersion().equals(personaModelId) && vfModule.getModuleIndex()!=null)
350                         moduleIndices.add(vfModule.getModuleIndex())
351                 }else{
352                     if(vfModule.getModelInvariantId().equals(personaModelId) && vfModule.getModuleIndex()!=null)
353                         moduleIndices.add(vfModule.getModuleIndex())
354                 }
355             }
356             for(i in 0..moduleIndices.size()-1){
357                 if(moduleIndices.getAt(i) != i){
358                     return i;
359                 }
360             }
361             return moduleIndices.size()
362         }else{
363             return 0
364         }
365
366     }
367
368         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
369         // requested for an Add-on VF Module does not already exist for the specified Generic VNF
370         // also retrieves VNF name from AAI response for existing VNF
371         public void parseForAddOnModule(DelegateExecution execution) {
372                 GenericVnf genericVnf = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
373                 def vnfNameFromAAI = genericVnf.getVnfName()
374                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
375                 msoLogger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)       
376                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
377                 msoLogger.debug("VF Module to be added: " + newModuleName)
378                 execution.setVariable("CAAIVfMod_moduleExists", false)
379                 if (genericVnf !=null && genericVnf.getVfModules()!=null && !genericVnf.getVfModules().getVfModule().isEmpty()) {
380             def qryModuleList =  genericVnf.getVfModules().getVfModule()
381                         msoLogger.debug("Existing VF Module List: " + qryModuleList)
382                         for (org.onap.aai.domain.yang.VfModule qryModule : qryModuleList) {
383                 def qryModuleName = qryModule.getVfModuleName()
384                                 if (newModuleName.equals(qryModuleName)) {
385                                         // a module with the requested name already exists - failure
386                                         msoLogger.debug("VF Module " + qryModuleName + " already exists for Generic VNF " + vnfNameFromAAI)
387                                         execution.setVariable("CAAIVfMod_moduleExists", true)
388                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
389                                                 "VF Module " + qryModuleName + " already exists for Generic VNF " + vnfNameFromAAI)
390                                         break
391                                 }
392                         }
393                 }
394                 if (execution.getVariable("CAAIVfMod_moduleExists") == false) {
395                         msoLogger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + vnfNameFromAAI)
396                         execution.setVariable("CAAIVfMod_parseModuleResponse",
397                                 "VF Module " + newModuleName + " does not exist for Generic VNF " + vnfNameFromAAI)
398                 }               
399         }
400         
401         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
402         // requested for an Add-on VF Module does not already exist for the specified Generic VNF; 
403         // also retrieves VNF name from AAI response for existing VNF
404         public void parseForBaseModule(DelegateExecution execution) {
405         GenericVnf genericVnf = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
406                 def vnfNameFromAAI = genericVnf.getVnfName()
407                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
408                 msoLogger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)       
409                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
410                 msoLogger.debug("VF Module to be added: " + newModuleName)
411                 def qryModuleList = genericVnf !=null ? genericVnf.getVfModules():null;
412                 execution.setVariable("CAAIVfMod_moduleExists", false)
413                 if (qryModuleList != null && !qryModuleList.getVfModule().isEmpty()) {
414             def qryModules = qryModuleList.getVfModule()
415                         msoLogger.debug("Existing VF Module List: " + qryModules)
416                         for (org.onap.aai.domain.yang.VfModule qryModule : qryModules) {
417                                 if (newModuleName.equals(qryModule.getVfModuleName())) {
418                                         // a module with the requested name already exists - failure
419                                         msoLogger.debug("VF Module " + qryModule.getVfModuleName() + " already exists for Generic VNF " + vnfNameFromAAI)
420                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
421                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
422                                                 "VF Module " + qryModule.getVfModuleName() + " already exists for Generic VNF " + vnfNameFromAAI)
423                                         break
424                                 }
425                         }
426                 }
427                 if (qryModuleList != null && !qryModuleList.getVfModule().isEmpty() && !execution.getVariable("CAAIVfMod_baseModuleConflict")) {
428             def qryModules = qryModuleList.getVfModule()
429                         for (org.onap.aai.domain.yang.VfModule qryModule : qryModules) {
430                                 if (qryModule.isBaseVfModule) {
431                                         // a base module already exists in this VNF - failure
432                                         msoLogger.debug("Base VF Module already exists for Generic VNF " + vnfNameFromAAI)
433                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
434                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
435                                                 "Base VF Module already exists for Generic VNF " + vnfNameFromAAI)
436                                         break
437                                 }
438                         }
439                 
440                 }
441                 if (execution.getVariable("CAAIVfMod_baseModuleConflict") == false) {
442                         msoLogger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
443                         execution.setVariable("CAAIVfMod_parseModuleResponse",
444                                 "VF Module " + newModuleName + " does not exist for Generic VNF " + vnfNameFromAAI)
445                 }               
446         }
447         
448         // generates a WorkflowException when the A&AI query returns a response code other than 200 or 404
449         public void handleAAIQueryFailure(DelegateExecution execution) {
450                 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, "");
451                 int code = execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode")
452                 exceptionUtil.buildAndThrowWorkflowException(execution, code, "Error occurred attempting to query AAI")
453
454         }
455         
456         // generates a WorkflowException if
457         //              - the A&AI Generic VNF PUT returns a response code other than 200 or 201
458         //              - the requested Generic VNF already exists but vnf-id == null
459         //              - the requested Generic VNF does not exist but vnf-id != null
460         //              - the A&AI VF Module PUT returns a response code other than 200 or 201
461         //              - the requested VF Module already exists for the Generic VNF
462         public void handleCreateVfModuleFailure(DelegateExecution execution) {
463                 def errorCode
464                 def errorResponse
465                 if (execution.getVariable("CAAIVfMod_createGenericVnfResponseCode") != null &&
466                                 !isOneOf(execution.getVariable("CAAIVfMod_createGenericVnfResponseCode"), 200, 201)) {
467                         msoLogger.debug("Failure creating Generic VNF: " + execution.getVariable("CAAIVfMod_createGenericVnfResponse"))
468                         errorResponse = execution.getVariable("CAAIVfMod_createGenericVnfResponse")
469                         errorCode = 5000
470                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponse") != null &&
471                                 execution.getVariable("CAAIVfMod_newGenericVnf") == true) {
472                         // attempted to create a Generic VNF that already exists but vnf-id == null
473                         msoLogger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
474                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
475                         errorCode = 1002
476                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
477                                 execution.getVariable("CAAIVfMod_newGenericVnf") == false) {
478                         // attempted to create a Generic VNF where vnf-name does not exist but vnf-id != null
479                         msoLogger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
480                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
481                         errorCode = 1002
482                 } else if (execution.getVariable("CAAIVfMod_createVfModuleResponseCode") != null) {
483                         msoLogger.debug("Failed to add VF Module: " + execution.getVariable("CAAIVfMod_createVfModuleResponse"))
484                         errorResponse = execution.getVariable("CAAIVfMod_createVfModuleResponse")
485                         errorCode = 5000
486                 } else if (execution.getVariable("CAAIVfMod_moduleExists") == true) {
487                         msoLogger.debug("Attempting to add VF Module that already exists: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
488                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
489                         errorCode = 1002
490                 } else if (execution.getVariable("CAAIVfMod_baseModuleConflict") == true) {
491                         msoLogger.debug("Attempting to add Base VF Module to VNF that already has a Base VF Module: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
492                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
493                         errorCode = 1002
494                 } else {
495                         // if the responses get populated corerctly, we should never get here
496                         errorResponse = "Unknown error occurred during CreateAAIVfModule flow"
497                         errorCode = 2000
498                 }
499
500                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred during CreateAAIVfModule flow", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, errorResponse);
501                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
502                 msoLogger.debug("Workflow exception occurred in CreateAAIVfModule: " + errorResponse)
503         }
504
505         /**
506          * Performs a rollback.
507          * TBD: This method requires additional testing once integrated with the
508          *      main CreateVfModule flow.
509          * @param execution the execution
510          */
511         public void rollback(DelegateExecution execution) {
512                 def method = getClass().getSimpleName() + ".rollback(" +
513                         "execution=" + execution.getId() +
514                         ")"
515                 msoLogger.debug("Entered " + method)
516
517                 try {
518                         RollbackData rollbackData = (RollbackData) execution.getVariable("RollbackData")
519                         msoLogger.debug("RollbackData:" + rollbackData)
520
521                         AaiUtil aaiUriUtil = new AaiUtil(this)
522                         
523                         if (rollbackData != null) {
524                                 if (rollbackData.hasType("VFMODULE")) {
525                                         // use the DeleteAAIVfModule groovy methods for the rollback
526                                         def vnfId = rollbackData.get("VFMODULE", "vnfId")
527                                         def vfModuleId = rollbackData.get("VFMODULE", "vfModuleId")
528                                         def isBaseModule = rollbackData.get("VFMODULE", "isBaseModule")
529                                         execution.setVariable("DAAIVfMod_vnfId", vnfId)         
530                                         execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
531
532                                         DeleteAAIVfModule dvm = new DeleteAAIVfModule()
533                                         // query A&AI to get the needed information for the delete(s)
534                                         dvm.queryAAIForGenericVnf(execution)
535                                         dvm.parseForVfModule(execution)
536                                         
537                                         // roll back the base or add-on module
538                                         dvm.deleteVfModule(execution)
539                                         def responseCode = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode")
540                                         def response = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode")
541
542                                         if (isOneOf(responseCode, 200, 204)) {
543                                                 msoLogger.debug("Received " + responseCode + " to VF Module rollback request")
544                                         } else {
545                                                 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);
546                                         }
547                                         
548                                         // a new Generic VNF was created that needs to be rolled back
549                                         if (isBaseModule.equals("true")) {
550                                                 dvm.queryAAIForGenericVnf(execution)
551                                                 dvm.parseForResourceVersion(execution)
552                                                 dvm.deleteGenericVnf(execution)
553                                                 responseCode = execution.getVariable("DAAIVfMod_deleteGenericVnfResponseCode")
554                                                 response = execution.getVariable("DAAIVfMod_deleteGenericVnfResponse")
555         
556                                                 if (isOneOf(responseCode, 200, 204)) {
557                                                         msoLogger.debug("Received " + responseCode + " to Generic VNF rollback request")
558                                                         execution.setVariable("RollbackResult", "SUCCESS")
559                                                 } else {
560                                                         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);
561                                                 }
562                                         } else {
563                                                 execution.setVariable("RollbackResult", "SUCCESS")
564                                         }
565                                 }
566                         }
567
568                         msoLogger.debug("Exited " + method)
569                 } catch (Exception e) {
570                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Caught exception in " + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
571                 }
572         }
573 }