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