Cleaned up content of MsoLogger
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.common.scripts
24
25 import org.apache.commons.lang.StringUtils
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.GenericVnf
28 import org.onap.so.bpmn.core.RollbackData
29 import org.onap.so.client.aai.AAIObjectPlurals
30 import org.onap.so.client.aai.AAIObjectType
31 import org.onap.so.client.aai.entities.uri.AAIResourceUri
32 import org.onap.so.client.aai.entities.uri.AAIUriFactory
33 import org.onap.so.client.graphinventory.entities.uri.Depth
34 import org.onap.so.db.catalog.beans.OrchestrationStatus
35 import org.onap.so.logger.MessageEnum
36 import org.onap.so.logger.MsoLogger
37 import org.slf4j.Logger
38 import org.slf4j.LoggerFactory
39
40 public class CreateAAIVfModule extends AbstractServiceTaskProcessor{
41     private static final Logger logger = LoggerFactory.getLogger( CreateAAIVfModule.class);
42
43         def prefix="CAAIVfMod_"
44         ExceptionUtil exceptionUtil = new ExceptionUtil()
45         public void initProcessVariables(DelegateExecution execution) {
46                 execution.setVariable("prefix",prefix)
47                 execution.setVariable("CAAIVfMod_vnfId",null)
48                 execution.setVariable("CAAIVfMod_vnfName",null)
49                 execution.setVariable("CAAIVfMod_vnfType",null)
50                 execution.setVariable("CAAIVfMod_serviceId",null)
51                 execution.setVariable("CAAIVfMod_personaId",null)
52                 execution.setVariable("CAAIVfMod_personaVer",null)
53                 execution.setVariable("CAAIVfMod_modelCustomizationId",null)
54                 execution.setVariable("CAAIVfMod_vnfPersonaId",null)
55                 execution.setVariable("CAAIVfMod_vnfPersonaVer",null)
56                 execution.setVariable("CAAIVfMod_isBaseVfModule", false)
57                 execution.setVariable("CAAIVfMod_moduleName",null)
58                 execution.setVariable("CAAIVfMod_moduleModelName",null)
59                 execution.setVariable("CAAIVfMod_newGenericVnf",false)
60                 execution.setVariable("CAAIVfMod_genericVnfGetEndpoint",null)
61                 execution.setVariable("CAAIVfMod_genericVnfPutEndpoint",null)
62                 execution.setVariable("CAAIVfMod_aaiNamespace",null)
63                 execution.setVariable("CAAIVfMod_moduleExists",false)
64                 execution.setVariable("CAAIVfMod_baseModuleConflict", false)
65                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", null)
66                 
67                 
68                 // CreateAAIVfModule workflow response variable placeholders
69                 execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode",null)
70                 execution.setVariable("CAAIVfMod_queryGenericVnfResponse","")
71                 execution.setVariable("CAAIVfMod_createGenericVnfResponseCode",null)
72                 execution.setVariable("CAAIVfMod_createGenericVnfResponse","")
73                 execution.setVariable("CAAIVfMod_createVfModuleResponseCode",null)
74                 execution.setVariable("CAAIVfMod_createVfModuleResponse","")
75                 execution.setVariable("CAAIVfMod_parseModuleResponse","")
76                 execution.setVariable("CAAIVfMod_deleteGenericVnfResponseCode",null)
77                 execution.setVariable("CAAIVfMod_deleteGenericVnfResponse","")
78                 execution.setVariable("CAAIVfMod_deleteVfModuleResponseCode",null)
79                 execution.setVariable("CAAIVfMod_deleteVfModuleResponse","")
80                 execution.setVariable("CreateAAIVfModuleResponse","")
81                 execution.setVariable("RollbackData", null)
82
83         }       
84         
85         // parse the incoming CREATE_VF_MODULE request and store the Generic VNF
86         // and VF Module data in the flow DelegateExecution
87         public void preProcessRequest(DelegateExecution execution) {
88                 initProcessVariables(execution)
89
90                 def vnfId = execution.getVariable("vnfId")              
91                 if (vnfId == null || vnfId.isEmpty()) {
92                         execution.setVariable("CAAIVfMod_newGenericVnf", true)
93                         execution.setVariable("CAAIVfMod_vnfId","")
94                 }
95                 else {
96                         execution.setVariable("CAAIVfMod_vnfId",vnfId)
97                 }
98
99                 def vnfName = execution.getVariable("vnfName")          
100                 execution.setVariable("CAAIVfMod_vnfName", vnfName)
101
102                 String vnfType = execution.getVariable("vnfType")
103         execution.setVariable("CAAIVfMod_vnfType", StringUtils.defaultString(vnfType))
104
105                 execution.setVariable("CAAIVfMod_serviceId", execution.getVariable("serviceId"))
106                 
107                 String personaModelId = execution.getVariable("personaModelId")
108         execution.setVariable("CAAIVfMod_personaId",StringUtils.defaultString(personaModelId))
109
110                 String personaModelVersion = execution.getVariable("personaModelVersion")
111         execution.setVariable("CAAIVfMod_personaVer", StringUtils.defaultString(personaModelVersion))
112
113                 String modelCustomizationId = execution.getVariable("modelCustomizationId")
114         execution.setVariable("CAAIVfMod_modelCustomizationId",StringUtils.defaultString(modelCustomizationId))
115
116                 String vnfPersonaModelId = execution.getVariable("vnfPersonaModelId")
117         execution.setVariable("CAAIVfMod_vnfPersonaId", StringUtils.defaultString(vnfPersonaModelId))
118
119                 String vnfPersonaModelVersion = execution.getVariable("vnfPersonaModelVersion")
120                 execution.setVariable("CAAIVfMod_vnfPersonaVer", StringUtils.defaultString(vnfPersonaModelVersion))
121
122                 //isBaseVfModule
123                 Boolean isBaseVfModule = false
124                 String isBaseVfModuleString = execution.getVariable("isBaseVfModule")
125                 if (isBaseVfModuleString != null && isBaseVfModuleString.equals("true")) {
126                                 isBaseVfModule = true
127                 }
128                 execution.setVariable("CAAIVfMod_isBaseVfModule", isBaseVfModule)
129
130                 String isVidRequest = execution.getVariable("isVidRequest")
131                 if (isVidRequest != null && "true".equals(isVidRequest)) {
132                         logger.debug("VID Request received")
133                 }
134
135                 execution.setVariable("CAAIVfMod_moduleName",execution.getVariable("vfModuleName"))
136                 execution.setVariable("CAAIVfMod_moduleModelName",execution.getVariable("vfModuleModelName"))
137
138                 AaiUtil aaiUriUtil = new AaiUtil(this)
139                 String aaiNamespace = aaiUriUtil.getNamespace()
140                 logger.debug('AAI namespace is: ' + aaiNamespace)
141
142                 execution.setVariable("CAAIVfMod_aaiNamespace",aaiNamespace)
143
144         }
145
146         // send a GET request to AA&I to retrieve the Generic VNF/VF Module information based on a Vnf Name
147         // expect a 200 response with the information in the response body or a 404 if the Generic VNF does not exist
148         public void queryAAIForGenericVnf(DelegateExecution execution) {
149
150                 AAIResourceUri uri
151                 def vnfId = execution.getVariable("CAAIVfMod_vnfId")
152                 def vnfName = execution.getVariable("CAAIVfMod_vnfName")
153                 if (vnfId == null || vnfId.isEmpty()) {
154                         uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF)
155                         uri.queryParam("vnf-name", vnfName)
156                 } else {
157                         uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
158                 }
159                 uri.depth(Depth.ONE)
160                 try {
161                         Optional<GenericVnf> genericVnfOp = getAAIClient().get(GenericVnf.class,  uri)
162             if(genericVnfOp.isPresent()){
163                 execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode", 200)
164                 execution.setVariable("CAAIVfMod_queryGenericVnfResponse", genericVnfOp.get())
165             }else{
166                 execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode", 404)
167                 execution.setVariable("CAAIVfMod_queryGenericVnfResponse", "Generic Vnf not Found!")
168             }
169                 } catch (Exception ex) {
170                         logger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
171                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in queryAAIForGenericVnf.")
172
173                 }
174         }
175
176         // process the result from queryAAIForGenericVnf()
177         // note: this method is primarily for logging as the actual decision logic is embedded in the bpmn flow
178         public void processAAIGenericVnfQuery(DelegateExecution execution) {
179                 if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
180                         execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
181                         logger.debug("New Generic VNF requested and it does not already exist")
182                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
183                                 !execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
184                         logger.debug("Adding module to existing Generic VNF")
185                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
186                                 execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
187                         logger.debug("Invalid request for new Generic VNF which already exists")
188                         execution.setVariable("CAAIVfMod_queryGenericVnfResponse",
189                                 "Invalid request for new Generic VNF which already exists, Vnf Name=" +
190                                  execution.getVariable("CAAIVfMod_vnfName"))
191                 } else { // execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
192                            // !execution.getVariable("CAAIVfMod_vnfId").isEmpty())
193                         logger.debug("Invalid request for Add-on Module requested for non-existant Generic VNF")
194                         execution.setVariable("CAAIVfMod_createVfModuleResponse",
195                                 "Invalid request for Add-on Module requested for non-existant Generic VNF, VNF Id=" +
196                                 execution.getVariable("CAAIVfMod_vnfId"))
197                 }
198         }
199
200         // construct and send a PUT request to A&AI to create a new Generic VNF
201         // note: to get here, the vnf-id in the original CREATE_VF_MODULE request was absent or ""
202         public void createGenericVnf(DelegateExecution execution) {
203                 // TBD - is this how we want to generate the Id for the new Generic VNF?
204                 def newVnfId = UUID.randomUUID().toString()
205                 execution.setVariable("CAAIVfMod_vnfId",newVnfId)
206
207         GenericVnf genericVnf = new GenericVnf()
208         genericVnf.setVnfId(newVnfId)
209         genericVnf.setVnfName(execution.getVariable("CAAIVfMod_vnfName"))
210         genericVnf.setVnfType(execution.getVariable("CAAIVfMod_vnfType"))
211         genericVnf.setServiceId(execution.getVariable("CAAIVfMod_serviceId"))
212         genericVnf.setOrchestrationStatus(OrchestrationStatus.ACTIVE.toString())
213         genericVnf.setModelInvariantId(execution.getVariable("CAAIVfMod_vnfPersonaId"))
214         genericVnf.setModelVersionId(execution.getVariable("CAAIVfMod_vnfPersonaVer"))
215
216                 try {
217                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, newVnfId)
218             getAAIClient().create(uri,genericVnf)
219                         execution.setVariable("CAAIVfMod_createGenericVnfResponseCode", 201)
220                         execution.setVariable("CAAIVfMod_createGenericVnfResponse", "Vnf Created")
221                 } catch (Exception ex) {
222                         ex.printStackTrace()
223                         logger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
224                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createGenericVnf.")
225                 }
226         }
227
228         // construct and send a PUT request to A&AI to create a Base or Add-on VF Module
229         public void createVfModule(DelegateExecution execution, Boolean isBaseModule) {
230                 // TBD - is this how we want to generate the Id for the new (Base) VF Module?
231
232                 // Generate the new VF Module ID here if it has not been provided by the parent process
233                 def newModuleId = execution.getVariable('newVfModuleId')
234                 if (newModuleId == null || newModuleId.isEmpty()) {
235                         newModuleId = UUID.randomUUID().toString()
236                 }
237                 
238                 String vnfId = execution.getVariable("CAAIVfMod_vnfId")
239                 
240                 int moduleIndex = 0
241                 if (!isBaseModule) {
242             GenericVnf aaiVnfResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
243             moduleIndex = getLowestUnusedVfModuleIndexFromAAIVnfResponse(aaiVnfResponse,execution)
244                 }
245
246                 // if we get to this point, we may be about to create the Vf Module,
247                 // add rollback information about the Generic VNF for this base/add-on module
248                 def rollbackData = execution.getVariable("RollbackData")
249                 if (rollbackData == null) {
250                         rollbackData = new RollbackData();
251                 }
252                 rollbackData.put("VFMODULE", "vnfId", execution.getVariable("CAAIVfMod_vnfId"))
253                 rollbackData.put("VFMODULE", "vnfName", execution.getVariable("CAAIVfMod_vnfName"))
254                 rollbackData.put("VFMODULE", "isBaseModule", isBaseModule.toString())
255                 execution.setVariable("RollbackData", rollbackData)
256                 logger.debug("RollbackData:" + rollbackData)
257
258         org.onap.aai.domain.yang.VfModule vfModule = new org.onap.aai.domain.yang.VfModule()
259         vfModule.setVfModuleId(newModuleId)
260         vfModule.setVfModuleName(execution.getVariable("CAAIVfMod_moduleName"))
261         vfModule.setModelInvariantId(execution.getVariable("CAAIVfMod_personaId"))
262         vfModule.setModelVersionId(execution.getVariable("CAAIVfMod_personaVer"))
263         vfModule.setModelCustomizationId(execution.getVariable("CAAIVfMod_modelCustomizationId"))
264         vfModule.setIsBaseVfModule(isBaseModule)
265         vfModule.setOrchestrationStatus(OrchestrationStatus.PENDING_CREATE.toString())
266         vfModule.setModuleIndex(moduleIndex)
267
268                 try {
269
270                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, newModuleId)
271             getAAIClient().create(uri,vfModule)
272             def statusCode = 201
273                         execution.setVariable("CAAIVfMod_createVfModuleResponseCode", statusCode)
274                         execution.setVariable("CAAIVfMod_createVfModuleResponse", "Vf Module Created")
275
276
277                         // the base or add-on VF Module was successfully created,
278                         // add the module name to the rollback data and the response
279                         if (isOneOf(statusCode, 200, 201)) {
280                                 rollbackData.put("VFMODULE", "vfModuleId", newModuleId)
281                                 rollbackData.put("VFMODULE", "vfModuleName", execution.getVariable("CAAIVfMod_moduleName"))
282                                 execution.setVariable("RollbackData", rollbackData)
283                                 logger.debug("RollbackData:" + rollbackData)
284
285                                 String responseOut = ""
286
287                                 String isVidRequest = execution.getVariable("isVidRequest")
288                                 def moduleIndexString = String.valueOf(moduleIndex)
289                                 if (isBaseModule && (isVidRequest == null || "false".equals(isVidRequest))) {
290
291                                         responseOut = """<CreateAAIVfModuleResponse>
292                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
293                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
294                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
295                                                                                 </CreateAAIVfModuleResponse>""" as String
296                                 }
297                                 else {
298                                         responseOut = """<CreateAAIVfModuleResponse>
299                                                                                         <vnf-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfNameFromAAI"))}</vnf-name>
300                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
301                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
302                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
303                                                                                 </CreateAAIVfModuleResponse>""" as String
304                                 }
305                                 
306                                 execution.setVariable("CreateAAIVfModuleResponse", responseOut)
307                                 logger.debug("CreateAAIVfModuleResponse:" + System.lineSeparator()+responseOut)
308                                 logger.debug("CreateAAIVfModule Response /n " + responseOut)
309                         }
310                 } catch (Exception ex) {
311             execution.setVariable("CAAIVfMod_createVfModuleResponseCode", 500)
312             execution.setVariable("CAAIVfMod_createVfModuleResponse", ex.getMessage())
313                         logger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
314                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createVfModule.")
315                 }
316         }
317
318     private int getLowestUnusedVfModuleIndexFromAAIVnfResponse(GenericVnf genericVnf,DelegateExecution execution){
319         String personaModelId = execution.getVariable("CAAIVfMod_personaId")
320         if(genericVnf!=null && genericVnf.getVfModules()!= null &&
321                 !genericVnf.getVfModules().getVfModule().isEmpty()){
322             Set<Integer> moduleIndices = new TreeSet<>()
323             for(org.onap.aai.domain.yang.VfModule vfModule in genericVnf.getVfModules().getVfModule()){
324                 if(genericVnf.getModelInvariantId()==null){
325                     if(vfModule.getPersonaModelVersion().equals(personaModelId) && vfModule.getModuleIndex()!=null)
326                         moduleIndices.add(vfModule.getModuleIndex())
327                 }else{
328                     if(vfModule.getModelInvariantId().equals(personaModelId) && vfModule.getModuleIndex()!=null)
329                         moduleIndices.add(vfModule.getModuleIndex())
330                 }
331             }
332             for(i in 0..moduleIndices.size()-1){
333                 if(moduleIndices.getAt(i) != i){
334                     return i;
335                 }
336             }
337             return moduleIndices.size()
338         }else{
339             return 0
340         }
341
342     }
343
344         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
345         // requested for an Add-on VF Module does not already exist for the specified Generic VNF
346         // also retrieves VNF name from AAI response for existing VNF
347         public void parseForAddOnModule(DelegateExecution execution) {
348                 GenericVnf genericVnf = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
349                 def vnfNameFromAAI = genericVnf.getVnfName()
350                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
351                 logger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)
352                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
353                 logger.debug("VF Module to be added: " + newModuleName)
354                 execution.setVariable("CAAIVfMod_moduleExists", false)
355                 if (genericVnf !=null && genericVnf.getVfModules()!=null && !genericVnf.getVfModules().getVfModule().isEmpty()) {
356             def qryModuleList =  genericVnf.getVfModules().getVfModule()
357                         logger.debug("Existing VF Module List: " + qryModuleList)
358                         for (org.onap.aai.domain.yang.VfModule qryModule : qryModuleList) {
359                 def qryModuleName = qryModule.getVfModuleName()
360                                 if (newModuleName.equals(qryModuleName)) {
361                                         // a module with the requested name already exists - failure
362                                         logger.debug("VF Module " + qryModuleName + " already exists for Generic VNF " + vnfNameFromAAI)
363                                         execution.setVariable("CAAIVfMod_moduleExists", true)
364                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
365                                                 "VF Module " + qryModuleName + " already exists for Generic VNF " + vnfNameFromAAI)
366                                         break
367                                 }
368                         }
369                 }
370                 if (execution.getVariable("CAAIVfMod_moduleExists") == false) {
371                         logger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + vnfNameFromAAI)
372                         execution.setVariable("CAAIVfMod_parseModuleResponse",
373                                 "VF Module " + newModuleName + " does not exist for Generic VNF " + vnfNameFromAAI)
374                 }
375         }
376
377         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
378         // requested for an Add-on VF Module does not already exist for the specified Generic VNF;
379         // also retrieves VNF name from AAI response for existing VNF
380         public void parseForBaseModule(DelegateExecution execution) {
381         GenericVnf genericVnf = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
382                 def vnfNameFromAAI = genericVnf.getVnfName()
383                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
384                 logger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)
385                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
386                 logger.debug("VF Module to be added: " + newModuleName)
387                 def qryModuleList = genericVnf !=null ? genericVnf.getVfModules():null;
388                 execution.setVariable("CAAIVfMod_moduleExists", false)
389                 if (qryModuleList != null && !qryModuleList.getVfModule().isEmpty()) {
390             def qryModules = qryModuleList.getVfModule()
391                         logger.debug("Existing VF Module List: " + qryModules)
392                         for (org.onap.aai.domain.yang.VfModule qryModule : qryModules) {
393                                 if (newModuleName.equals(qryModule.getVfModuleName())) {
394                                         // a module with the requested name already exists - failure
395                                         logger.debug("VF Module " + qryModule.getVfModuleName() + " already exists for Generic VNF " + vnfNameFromAAI)
396                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
397                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
398                                                 "VF Module " + qryModule.getVfModuleName() + " already exists for Generic VNF " + vnfNameFromAAI)
399                                         break
400                                 }
401                         }
402                 }
403
404                 if (qryModuleList != null && !qryModuleList.getVfModule().isEmpty() && !execution.getVariable("CAAIVfMod_baseModuleConflict")) {
405                         def qryModules = qryModuleList.getVfModule()
406                         for (org.onap.aai.domain.yang.VfModule qryModule : qryModules) {
407                                 if (qryModule.isBaseVfModule) {
408                                         // a base module already exists in this VNF - failure
409                                         logger.debug("Base VF Module already exists for Generic VNF " + vnfNameFromAAI)
410                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
411                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
412                                                 "Base VF Module already exists for Generic VNF " + vnfNameFromAAI)
413                                         break
414                                 }
415                         }
416
417                 }
418                 if (execution.getVariable("CAAIVfMod_baseModuleConflict") == false) {
419                         logger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
420                         execution.setVariable("CAAIVfMod_parseModuleResponse",
421                                 "VF Module " + newModuleName + " does not exist for Generic VNF " + vnfNameFromAAI)
422                 }
423         }
424
425         // generates a WorkflowException when the A&AI query returns a response code other than 200 or 404
426         public void handleAAIQueryFailure(DelegateExecution execution) {
427                 logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
428                                 "Error occurred attempting to query AAI, Response Code " + execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") + ", Error Response " + execution.getVariable("CAAIVfMod_queryGenericVnfResponse"),
429                                 "BPMN", MsoLogger.ErrorCode.UnknownError.getValue());
430                 int code = execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode")
431                 exceptionUtil.buildAndThrowWorkflowException(execution, code, "Error occurred attempting to query AAI")
432
433         }
434
435         // generates a WorkflowException if
436         //              - the A&AI Generic VNF PUT returns a response code other than 200 or 201
437         //              - the requested Generic VNF already exists but vnf-id == null
438         //              - the requested Generic VNF does not exist but vnf-id != null
439         //              - the A&AI VF Module PUT returns a response code other than 200 or 201
440         //              - the requested VF Module already exists for the Generic VNF
441         public void handleCreateVfModuleFailure(DelegateExecution execution) {
442                 def errorCode
443                 def errorResponse
444                 if (execution.getVariable("CAAIVfMod_createGenericVnfResponseCode") != null &&
445                                 !isOneOf(execution.getVariable("CAAIVfMod_createGenericVnfResponseCode"), 200, 201)) {
446                         logger.debug("Failure creating Generic VNF: " + execution.getVariable("CAAIVfMod_createGenericVnfResponse"))
447                         errorResponse = execution.getVariable("CAAIVfMod_createGenericVnfResponse")
448                         errorCode = 5000
449                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponse") != null &&
450                                 execution.getVariable("CAAIVfMod_newGenericVnf") == true) {
451                         // attempted to create a Generic VNF that already exists but vnf-id == null
452                         logger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
453                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
454                         errorCode = 1002
455                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
456                                 execution.getVariable("CAAIVfMod_newGenericVnf") == false) {
457                         // attempted to create a Generic VNF where vnf-name does not exist but vnf-id != null
458                         logger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
459                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
460                         errorCode = 1002
461                 } else if (execution.getVariable("CAAIVfMod_createVfModuleResponseCode") != null) {
462                         logger.debug("Failed to add VF Module: " + execution.getVariable("CAAIVfMod_createVfModuleResponse"))
463                         errorResponse = execution.getVariable("CAAIVfMod_createVfModuleResponse")
464                         errorCode = 5000
465                 } else if (execution.getVariable("CAAIVfMod_moduleExists") == true) {
466                         logger.debug("Attempting to add VF Module that already exists: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
467                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
468                         errorCode = 1002
469                 } else if (execution.getVariable("CAAIVfMod_baseModuleConflict") == true) {
470                         logger.debug("Attempting to add Base VF Module to VNF that already has a Base VF Module: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
471                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
472                         errorCode = 1002
473                 } else {
474                         // if the responses get populated corerctly, we should never get here
475                         errorResponse = "Unknown error occurred during CreateAAIVfModule flow"
476                         errorCode = 2000
477                 }
478
479                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
480                                 "Error occurred during CreateAAIVfModule flow", "BPMN",
481                                 MsoLogger.ErrorCode.UnknownError.getValue(), errorResponse);
482                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
483                 logger.debug("Workflow exception occurred in CreateAAIVfModule: " + errorResponse)
484         }
485 }