84dd70682d97db6300d4e6ba99f1eb29e1de4053
[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.bpmn.core.UrnPropertiesReader
25 import org.onap.so.logger.MessageEnum
26 import org.onap.so.logger.MsoLogger
27 import org.onap.so.rest.APIResponse
28 import org.springframework.web.util.UriUtils
29
30 public class CreateAAIVfModule extends AbstractServiceTaskProcessor{
31         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateAAIVfModule.class);
32
33         def Prefix="CAAIVfMod_"
34         ExceptionUtil exceptionUtil = new ExceptionUtil()
35         public void initProcessVariables(DelegateExecution execution) {
36                 execution.setVariable("prefix",Prefix)
37                 execution.setVariable("CAAIVfMod_vnfId",null)
38                 execution.setVariable("CAAIVfMod_vnfName",null)
39                 execution.setVariable("CAAIVfMod_vnfType",null)
40                 execution.setVariable("CAAIVfMod_serviceId",null)
41                 execution.setVariable("CAAIVfMod_personaId",null)
42                 execution.setVariable("CAAIVfMod_personaVer",null)
43                 execution.setVariable("CAAIVfMod_modelCustomizationId",null)
44                 execution.setVariable("CAAIVfMod_vnfPersonaId",null)
45                 execution.setVariable("CAAIVfMod_vnfPersonaVer",null)
46                 execution.setVariable("CAAIVfMod_isBaseVfModule", false)
47                 execution.setVariable("CAAIVfMod_moduleName",null)
48                 execution.setVariable("CAAIVfMod_moduleModelName",null)
49                 execution.setVariable("CAAIVfMod_newGenericVnf",false)
50                 execution.setVariable("CAAIVfMod_genericVnfGetEndpoint",null)
51                 execution.setVariable("CAAIVfMod_genericVnfPutEndpoint",null)
52                 execution.setVariable("CAAIVfMod_aaiNamespace",null)
53                 execution.setVariable("CAAIVfMod_moduleExists",false)
54                 execution.setVariable("CAAIVfMod_baseModuleConflict", false)
55                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", null)
56                 
57                 
58                 // CreateAAIVfModule workflow response variable placeholders
59                 execution.setVariable("CAAIVfMod_queryGenericVnfResponseCode",null)
60                 execution.setVariable("CAAIVfMod_queryGenericVnfResponse","")
61                 execution.setVariable("CAAIVfMod_createGenericVnfResponseCode",null)
62                 execution.setVariable("CAAIVfMod_createGenericVnfResponse","")
63                 execution.setVariable("CAAIVfMod_createVfModuleResponseCode",null)
64                 execution.setVariable("CAAIVfMod_createVfModuleResponse","")
65                 execution.setVariable("CAAIVfMod_parseModuleResponse","")
66                 execution.setVariable("CAAIVfMod_deleteGenericVnfResponseCode",null)
67                 execution.setVariable("CAAIVfMod_deleteGenericVnfResponse","")
68                 execution.setVariable("CAAIVfMod_deleteVfModuleResponseCode",null)
69                 execution.setVariable("CAAIVfMod_deleteVfModuleResponse","")
70 //              execution.setVariable("CAAIVfMod_ResponseCode",null)
71 //              execution.setVariable("CAAIVfMod_ErrorResponse","")
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                 def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
162                 msoLogger.debug('AAI URI is: ' + aai_uri)
163                 String aaiNamespace = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
164                 msoLogger.debug('AAI namespace is: ' + aaiNamespace)
165                 
166                 execution.setVariable("CAAIVfMod_aaiNamespace","${aaiNamespace}")               
167         
168                 if (vnfId == null || vnfId.isEmpty()) {
169                         // TBD - assert that the vnfName is not empty
170                         execution.setVariable("CAAIVfMod_genericVnfGetEndpoint",
171                                 "${aai_uri}/?vnf-name=" +
172                                         UriUtils.encode(vnfName,"UTF-8") + "&depth=1")
173                 } else {
174                         execution.setVariable("CAAIVfMod_genericVnfGetEndpoint",
175                                 "${aai_uri}/" + UriUtils.encode(vnfId,"UTF-8") + "?depth=1")
176                 }
177
178                 msoLogger.debug("CreateAAIVfModule VNF PUT Endpoint:   ${aai_uri}/")
179                 execution.setVariable("CAAIVfMod_genericVnfPutEndpoint","${aai_uri}/")
180         }
181         
182         // send a GET request to AA&I to retrieve the Generic VNF/VF Module information based on a Vnf Name
183         // expect a 200 response with the information in the response body or a 404 if the Generic VNF does not exist
184         public void queryAAIForGenericVnf(DelegateExecution execution) {
185                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + execution.getVariable("CAAIVfMod_genericVnfGetEndpoint")
186
187                 try {
188                         msoLogger.debug("queryAAIForGenericVnf() endpoint-" + endPoint)
189                         msoLogger.debug("invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint)
190                         msoLogger.debug("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                         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                 def result = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
214                 
215                 if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
216                         execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
217                         msoLogger.debug("New Generic VNF requested and it does not already exist")
218                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
219                                 !execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
220                         msoLogger.debug("Adding module to existing Generic VNF")        
221                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 200 &&
222                                 execution.getVariable("CAAIVfMod_vnfId").isEmpty()) {
223                         msoLogger.debug("Invalid request for new Generic VNF which already exists")
224                         execution.setVariable("CAAIVfMod_queryGenericVnfResponse",
225                                 "Invalid request for new Generic VNF which already exists, Vnf Name=" +
226                                  execution.getVariable("CAAIVfMod_vnfName"))    
227                 } else { // execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
228                            // !execution.getVariable("CAAIVfMod_vnfId").isEmpty())
229                         msoLogger.debug("Invalid request for Add-on Module requested for non-existant Generic VNF")     
230                         execution.setVariable("CAAIVfMod_createVfModuleResponse",
231                                 "Invalid request for Add-on Module requested for non-existant Generic VNF, VNF Id=" +
232                                 execution.getVariable("CAAIVfMod_vnfId"))
233                 }
234         }
235
236         // construct and send a PUT request to A&AI to create a new Generic VNF
237         // note: to get here, the vnf-id in the original CREATE_VF_MODULE request was absent or ""
238         public void createGenericVnf(DelegateExecution execution) {
239                 // TBD - is this how we want to generate the Id for the new Generic VNF?
240                 def newVnfId = UUID.randomUUID().toString()
241                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) +
242                         execution.getVariable("CAAIVfMod_genericVnfPutEndpoint") + newVnfId
243                 // update the flow execution with the new Vnf Id
244                 execution.setVariable("CAAIVfMod_vnfId",newVnfId)
245                 
246         //      AaiUriUtil aaiUriUtil = new AaiUriUtil(this)
247         //      def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
248         //      msoLogger.debug('AAI URI is: ' + aai_uri)
249         //      String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
250         //      msoLogger.debug('AAI namespace is: ' + namespace)
251                 
252                                 
253                 String payload = """<generic-vnf xmlns="${execution.getVariable("CAAIVfMod_aaiNamespace")}">
254                                                                 <vnf-id>${MsoUtils.xmlEscape(newVnfId)}</vnf-id>
255                                                                 <vnf-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfName"))}</vnf-name>
256                                                                 <vnf-type>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfType"))}</vnf-type>
257                                                                 <service-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_serviceId"))}</service-id>
258                                                                 <orchestration-status>Active</orchestration-status>
259                                                                 <model-invariant-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfPersonaId"))}</model-invariant-id>
260                                                                 <model-version-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfPersonaVer"))}</model-version-id>
261                                                         </generic-vnf>""" as String
262                 execution.setVariable("CAAIVfMod_createGenericVnfPayload", payload)
263
264                 try {
265                         msoLogger.debug("createGenericVnf() endpoint-" + endPoint)
266                         msoLogger.debug("invoking PUT call to AAI with payload:"+System.lineSeparator()+payload)
267                         msoLogger.debug("Sending PUT call to AAI with Endpoint /n" + endPoint + " with payload /n" + payload)
268
269                         AaiUtil aaiUtil = new AaiUtil(this);
270                         APIResponse response = aaiUtil.executeAAIPutCall(execution, endPoint, payload);
271                         def     responseData = response.getResponseBodyAsString()
272                         def responseStatusCode = response.getStatusCode()
273                         execution.setVariable("CAAIVfMod_createGenericVnfResponseCode", responseStatusCode)
274                         execution.setVariable("CAAIVfMod_createGenericVnfResponse", responseData)
275                         
276                         msoLogger.debug("Response Code: " + responseStatusCode)
277                         msoLogger.debug("Response Data: " + responseData)
278                         msoLogger.debug("Response code:" + responseStatusCode)
279                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
280                 } catch (Exception ex) {
281                         ex.printStackTrace()
282                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
283                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createGenericVnf.")
284                 }
285         }
286
287         // construct and send a PUT request to A&AI to create a Base or Add-on VF Module
288         public void createVfModule(DelegateExecution execution, Boolean isBaseModule) {
289                 // TBD - is this how we want to generate the Id for the new (Base) VF Module?
290                 
291                 // Generate the new VF Module ID here if it has not been provided by the parent process
292                 def newModuleId = execution.getVariable('newVfModuleId')
293                 if (newModuleId == null || newModuleId.isEmpty()) {
294                         newModuleId = UUID.randomUUID().toString()
295                 }
296                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + execution.getVariable("CAAIVfMod_genericVnfPutEndpoint")
297                 // need to append the existing Vnf Id or the one generated in createGenericVnf() to the url
298                 endPoint = endPoint + UriUtils.encode(execution.getVariable("CAAIVfMod_vnfId"), "UTF-8") +
299                         "/vf-modules/vf-module/" + newModuleId;
300                 int moduleIndex = 0
301                 if (!isBaseModule) {
302                         def aaiVnfResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
303                         AaiUtil aaiUtil = new AaiUtil(this)
304                         def personaModelId = execution.getVariable("CAAIVfMod_personaId")
305                         
306                         // Check if the response includes model-invariant-id or persona-model-id
307                         // note: getRequiredNodeText() throws an exception if the field is missing
308                         // need to retun a null for the subsequent "either/or" logic to work properly
309 //                      def modelInvariantId = getRequiredNodeText(execution, aaiVnfResponse,'model-invariant-id')
310                         def modelInvariantId = getNodeText(aaiVnfResponse,'model-invariant-id', null)
311                         def fieldToCheck = 'model-invariant-id'
312                         if (!modelInvariantId) {
313                                 fieldToCheck = 'persona-model-id'
314                         }
315                         
316                         moduleIndex = aaiUtil.getLowestUnusedVfModuleIndexFromAAIVnfResponse(execution, aaiVnfResponse, 
317                                 fieldToCheck, personaModelId)
318                 }
319                 def moduleIndexString = String.valueOf(moduleIndex)
320
321                 // if we get to this point, we may be about to create the Vf Module,
322                 // add rollback information about the Generic VNF for this base/add-on module
323                 def rollbackData = execution.getVariable("RollbackData")
324                 if (rollbackData == null) {
325                         rollbackData = new RollbackData();
326                 }
327                 rollbackData.put("VFMODULE", "vnfId", execution.getVariable("CAAIVfMod_vnfId"))
328                 rollbackData.put("VFMODULE", "vnfName", execution.getVariable("CAAIVfMod_vnfName"))
329                 rollbackData.put("VFMODULE", "isBaseModule", isBaseModule.toString())
330                 execution.setVariable("RollbackData", rollbackData)
331                 msoLogger.debug("RollbackData:" + rollbackData)
332                 String payload = """<vf-module xmlns="${execution.getVariable("CAAIVfMod_aaiNamespace")}">
333                                                                 <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
334                                                                 <vf-module-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_moduleName"))}</vf-module-name>
335                                                                 <model-invariant-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_personaId"))}</model-invariant-id>
336                                                                 <model-version-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_personaVer"))}</model-version-id>
337                                                                 <model-customization-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_modelCustomizationId"))}</model-customization-id>
338                                                                 <is-base-vf-module>${MsoUtils.xmlEscape(isBaseModule)}</is-base-vf-module>
339                                                                 <orchestration-status>PendingCreate</orchestration-status>
340                                                                 <module-index>${MsoUtils.xmlEscape(moduleIndex)}</module-index>
341                                                                 </vf-module>""" as String
342                 execution.setVariable("CAAIVfMod_createVfModulePayload", payload)
343
344                 try {
345                         msoLogger.debug("createVfModule() endpoint-" + endPoint)
346                         msoLogger.debug("invoking PUT call to AAI with payload:"+System.lineSeparator()+payload)
347                         msoLogger.debug("CreateAAIVfModule sending PUT call to AAI with endpoint /n" + endPoint + " with payload /n " + payload)
348
349                         AaiUtil aaiUtil = new AaiUtil(this)
350                         APIResponse response = aaiUtil.executeAAIPutCall(execution, endPoint, payload)  
351                         def responseData = response.getResponseBodyAsString()
352                         def statusCode = response.getStatusCode()
353                         execution.setVariable("CAAIVfMod_createVfModuleResponseCode", statusCode)
354                         execution.setVariable("CAAIVfMod_createVfModuleResponse", responseData)
355                         
356                         msoLogger.debug("Response code:" + statusCode)
357                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
358                         msoLogger.debug("Response Code: " + statusCode)
359                         msoLogger.debug("Response data: " + responseData)
360                         // the base or add-on VF Module was successfully created,
361                         // add the module name to the rollback data and the response
362                         if (isOneOf(statusCode, 200, 201)) {
363                                 rollbackData.put("VFMODULE", "vfModuleId", newModuleId)
364                                 rollbackData.put("VFMODULE", "vfModuleName", execution.getVariable("CAAIVfMod_moduleName"))
365                                 execution.setVariable("RollbackData", rollbackData)
366                                 msoLogger.debug("RollbackData:" + rollbackData)
367                                 
368                                 String responseOut = ""
369                                 
370                                 String isVidRequest = execution.getVariable("isVidRequest")
371                                 
372                                 if (isBaseModule && (isVidRequest == null || "false".equals(isVidRequest))) {                           
373                                 
374                                         responseOut = """<CreateAAIVfModuleResponse>
375                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
376                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
377                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
378                                                                                 </CreateAAIVfModuleResponse>""" as String
379                                 }
380                                 else {
381                                         responseOut = """<CreateAAIVfModuleResponse>
382                                                                                         <vnf-name>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfNameFromAAI"))}</vnf-name>
383                                                                                         <vnf-id>${MsoUtils.xmlEscape(execution.getVariable("CAAIVfMod_vnfId"))}</vnf-id>
384                                                                                         <vf-module-id>${MsoUtils.xmlEscape(newModuleId)}</vf-module-id>
385                                                                                         <vf-module-index>${MsoUtils.xmlEscape(moduleIndexString)}</vf-module-index>
386                                                                                 </CreateAAIVfModuleResponse>""" as String
387                                 }
388                                 
389                                 execution.setVariable("CreateAAIVfModuleResponse", responseOut)
390                                 msoLogger.debug("CreateAAIVfModuleResponse:" + System.lineSeparator()+responseOut)
391                                 msoLogger.debug("CreateAAIVfModule Response /n " + responseOut)
392                         }
393                 } catch (Exception ex) {
394                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
395                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in createVfModule.")
396                 }
397         }
398         
399         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
400         // requested for an Add-on VF Module does not already exist for the specified Generic VNF
401         // also retrieves VNF name from AAI response for existing VNF
402         public void parseForAddOnModule(DelegateExecution execution) {
403                 def xml = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
404                 def vnfNameFromAAI = utils.getNodeText(xml, "vnf-name")
405                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
406                 msoLogger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)       
407                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
408                 msoLogger.debug("VF Module to be added: " + newModuleName)
409                 def qryModuleNameList = utils.getMultNodes(xml, "vf-module-name")
410                 execution.setVariable("CAAIVfMod_moduleExists", false)
411                 if (qryModuleNameList != null) {
412                         msoLogger.debug("Existing VF Module List: " + qryModuleNameList)
413                         for (String qryModuleName : qryModuleNameList) {
414                                 if (newModuleName.equals(qryModuleName)) {
415                                         // a module with the requested name already exists - failure
416                                         msoLogger.debug("VF Module " + qryModuleName + " already exists for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
417                                         execution.setVariable("CAAIVfMod_moduleExists", true)
418                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
419                                                 "VF Module " + qryModuleName + " already exists for Generic VNF " +
420                                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
421                                         break
422                                 }
423                         }
424                 }
425                 if (execution.getVariable("CAAIVfMod_moduleExists") == false) {
426                         msoLogger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
427                         execution.setVariable("CAAIVfMod_parseModuleResponse",
428                                 "VF Module " + newModuleName + " does not exist for Generic VNF " +
429                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
430                 }               
431         }
432         
433         // parses the output from the result from queryAAIForGenericVnf() to determine if the vf-module-name
434         // requested for an Add-on VF Module does not already exist for the specified Generic VNF; 
435         // also retrieves VNF name from AAI response for existing VNF
436         public void parseForBaseModule(DelegateExecution execution) {
437                 def xml = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
438                 def vnfNameFromAAI = utils.getNodeText(xml, "vnf-name")
439                 execution.setVariable("CAAIVfMod_vnfNameFromAAI", vnfNameFromAAI)
440                 msoLogger.debug("Obtained vnf-name from AAI for existing VNF: " + vnfNameFromAAI)       
441                 def newModuleName = execution.getVariable("CAAIVfMod_moduleName")
442                 msoLogger.debug("VF Module to be added: " + newModuleName)
443                 def qryModuleNameList = utils.getMultNodes(xml, "vf-module-name")
444                 execution.setVariable("CAAIVfMod_moduleExists", false)
445                 if (qryModuleNameList != null) {
446                         msoLogger.debug("Existing VF Module List: " + qryModuleNameList)
447                         for (String qryModuleName : qryModuleNameList) {
448                                 if (newModuleName.equals(qryModuleName)) {
449                                         // a module with the requested name already exists - failure
450                                         msoLogger.debug("VF Module " + qryModuleName + " already exists for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
451                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
452                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
453                                                 "VF Module " + qryModuleName + " already exists for Generic VNF " +
454                                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
455                                         break
456                                 }
457                         }
458                 }
459                 def isBaseVfModuleList = utils.getMultNodes(xml, "is-base-vf-module")
460                 if (isBaseVfModuleList != null && !execution.getVariable("CAAIVfMod_baseModuleConflict")) {
461                         
462                         for (String baseValue : isBaseVfModuleList) {
463                                 if (baseValue.equals("true")) {
464                                         // a base module already exists in this VNF - failure
465                                         msoLogger.debug("Base VF Module already exists for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
466                                         execution.setVariable("CAAIVfMod_baseModuleConflict", true)
467                                         execution.setVariable("CAAIVfMod_parseModuleResponse",
468                                                 "Base VF Module already exists for Generic VNF " +
469                                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
470                                         break
471                                 }
472                         }
473                 
474                 }
475                 if (execution.getVariable("CAAIVfMod_moduleExists") == false && execution.getVariable("CAAIVfMod_baseModuleConflict") == false) {
476                         msoLogger.debug("VF Module " + execution.getVariable("CAAIVfMod_moduleName") + " does not exist for Generic VNF " + execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
477                         execution.setVariable("CAAIVfMod_parseModuleResponse",
478                                 "VF Module " + newModuleName + " does not exist for Generic VNF " +
479                                 execution.getVariable("CAAIVfMod_vnfNameFromAAI"))
480                 }               
481         }
482         
483         // generates a WorkflowException when the A&AI query returns a response code other than 200 or 404
484         public void handleAAIQueryFailure(DelegateExecution execution) {
485                 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, "");
486                 int code = execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode")
487                 exceptionUtil.buildAndThrowWorkflowException(execution, code, "Error occurred attempting to query AAI")
488
489         }
490         
491         // generates a WorkflowException if
492         //              - the A&AI Generic VNF PUT returns a response code other than 200 or 201
493         //              - the requested Generic VNF already exists but vnf-id == null
494         //              - the requested Generic VNF does not exist but vnf-id != null
495         //              - the A&AI VF Module PUT returns a response code other than 200 or 201
496         //              - the requested VF Module already exists for the Generic VNF
497         public void handleCreateVfModuleFailure(DelegateExecution execution) {
498                 def errorCode
499                 def errorResponse
500                 if (execution.getVariable("CAAIVfMod_createGenericVnfResponseCode") != null &&
501                                 !isOneOf(execution.getVariable("CAAIVfMod_createGenericVnfResponseCode"), 200, 201)) {
502                         msoLogger.debug("Failure creating Generic VNF: " + execution.getVariable("CAAIVfMod_createGenericVnfResponse"))
503                         errorResponse = execution.getVariable("CAAIVfMod_createGenericVnfResponse")
504                         errorCode = 5000
505                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponse") != null &&
506                                 execution.getVariable("CAAIVfMod_newGenericVnf") == true) {
507                         // attempted to create a Generic VNF that already exists but vnf-id == null
508                         msoLogger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
509                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
510                         errorCode = 1002
511                 } else if (execution.getVariable("CAAIVfMod_queryGenericVnfResponseCode") == 404 &&
512                                 execution.getVariable("CAAIVfMod_newGenericVnf") == false) {
513                         // attempted to create a Generic VNF where vnf-name does not exist but vnf-id != null
514                         msoLogger.debug(execution.getVariable("CAAIVfMod_queryGenericVnfResponse"))
515                         errorResponse = execution.getVariable("CAAIVfMod_queryGenericVnfResponse")
516                         errorCode = 1002
517                 } else if (execution.getVariable("CAAIVfMod_createVfModuleResponseCode") != null) {
518                         msoLogger.debug("Failed to add VF Module: " + execution.getVariable("CAAIVfMod_createVfModuleResponse"))
519                         errorResponse = execution.getVariable("CAAIVfMod_createVfModuleResponse")
520                         errorCode = 5000
521                 } else if (execution.getVariable("CAAIVfMod_moduleExists") == true) {
522                         msoLogger.debug("Attempting to add VF Module that already exists: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
523                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
524                         errorCode = 1002
525                 } else if (execution.getVariable("CAAIVfMod_baseModuleConflict") == true) {
526                         msoLogger.debug("Attempting to add Base VF Module to VNF that already has a Base VF Module: " + execution.getVariable("CAAIVfMod_parseModuleResponse"))
527                         errorResponse = execution.getVariable("CAAIVfMod_parseModuleResponse")
528                         errorCode = 1002
529                 } else {
530                         // if the responses get populated corerctly, we should never get here
531                         errorResponse = "Unknown error occurred during CreateAAIVfModule flow"
532                         errorCode = 2000
533                 }
534
535                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred during CreateAAIVfModule flow", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, errorResponse);
536                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
537                 msoLogger.debug("Workflow exception occurred in CreateAAIVfModule: " + errorResponse)
538         }
539
540         /**
541          * Performs a rollback.
542          * TBD: This method requires additional testing once integrated with the
543          *      main CreateVfModule flow.
544          * @param execution the execution
545          */
546         public void rollback(DelegateExecution execution) {
547                 def method = getClass().getSimpleName() + ".rollback(" +
548                         "execution=" + execution.getId() +
549                         ")"
550                 msoLogger.debug("Entered " + method)
551
552                 try {
553                         RollbackData rollbackData = (RollbackData) execution.getVariable("RollbackData")
554                         msoLogger.debug("RollbackData:" + rollbackData)
555
556                         AaiUtil aaiUriUtil = new AaiUtil(this)
557                         def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
558                         msoLogger.debug('AAI URI is: ' + aai_uri)
559                         msoLogger.debug("CreateAAIVfModule rollback AAI URI: " + aai_uri)
560                         
561                         if (rollbackData != null) {
562                                 if (rollbackData.hasType("VFMODULE")) {
563                                         // use the DeleteAAIVfModule groovy methods for the rollback
564                                         def vnfId = rollbackData.get("VFMODULE", "vnfId")
565                                         def vfModuleId = rollbackData.get("VFMODULE", "vfModuleId")
566                                         def isBaseModule = rollbackData.get("VFMODULE", "isBaseModule")
567                                         execution.setVariable("DAAIVfMod_vnfId", vnfId)         
568                                         execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
569                                         execution.setVariable("DAAIVfMod_genericVnfEndpoint", "${aai_uri}/" + vnfId)
570                                         execution.setVariable("DAAIVfMod_vfModuleEndpoint", "${aai_uri}/" + vnfId +
571                                                  "/vf-modules/vf-module/" + vfModuleId)
572                                         DeleteAAIVfModule dvm = new DeleteAAIVfModule()
573                                         // query A&AI to get the needed information for the delete(s)
574                                         dvm.queryAAIForGenericVnf(execution)
575                                         dvm.parseForVfModule(execution)
576                                         
577                                         // roll back the base or add-on module
578                                         dvm.deleteVfModule(execution)
579                                         def responseCode = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode")
580                                         def response = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode")
581
582                                         if (isOneOf(responseCode, 200, 204)) {
583                                                 msoLogger.debug("Received " + responseCode + " to VF Module rollback request")
584 //                                              execution.setVariable("RollbackResult", "SUCCESS")
585                                         } else {
586                                                 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);
587                                         }
588                                         
589                                         // a new Generic VNF was created that needs to be rolled back
590                                         if (isBaseModule.equals("true")) {
591                                                 dvm.deleteGenericVnf(execution)
592                                                 responseCode = execution.getVariable("DAAIVfMod_deleteGenericVnfResponseCode")
593                                                 response = execution.getVariable("DAAIVfMod_deleteGenericVnfResponse")
594         
595                                                 if (isOneOf(responseCode, 200, 204)) {
596                                                         msoLogger.debug("Received " + responseCode + " to Generic VNF rollback request")
597                                                         execution.setVariable("RollbackResult", "SUCCESS")
598                                                 } else {
599                                                         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);
600                                                 }
601                                         } else {
602                                                 execution.setVariable("RollbackResult", "SUCCESS")
603                                         }
604                                 }
605                         }
606
607                         msoLogger.debug("Exited " + method)
608                 } catch (Exception e) {
609                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Caught exception in " + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
610                 }
611         }
612 }