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