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