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