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