Groovy scripts header correction
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / DeleteAAIVfModule.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.WorkflowException\r
24 import org.openecomp.mso.rest.APIResponse\r
25 import org.openecomp.mso.rest.RESTClient;\r
26 import org.openecomp.mso.rest.RESTConfig;\r
27 \r
28 \r
29 public class DeleteAAIVfModule extends AbstractServiceTaskProcessor{\r
30         \r
31         def Prefix="DAAIVfMod_"\r
32         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
33     private MsoUtils utils = new MsoUtils()\r
34         public void initProcessVariables(Execution execution) {\r
35                 execution.setVariable("prefix",Prefix)\r
36                 execution.setVariable("DAAIVfMod_vnfId",null)\r
37                 execution.setVariable("DAAIVfMod_vnfName",null)\r
38                 execution.setVariable("DAAIVfMod_genVnfRsrcVer",null)\r
39                 execution.setVariable("DAAIVfMod_vfModuleId",null)\r
40                 execution.setVariable("DAAIVfMod_vfModRsrcVer",null)\r
41                 execution.setVariable("DAAIVfMod_genericVnfEndpoint",null)\r
42                 execution.setVariable("DAAIVfMod_vfModuleEndpoint",null)\r
43                 execution.setVariable("DAAIVfMod_moduleExists",false)\r
44                 execution.setVariable("DAAIVfMod_isBaseModule", false)\r
45                 execution.setVariable("DAAIVfMod_isLastModule", false)\r
46 \r
47                 // DeleteAAIVfModule workflow response variable placeholders\r
48                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode",null)\r
49                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse","")\r
50                 execution.setVariable("DAAIVfMod_parseModuleResponse","")\r
51                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode",null)\r
52                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponse","")\r
53                 execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode",null)\r
54                 execution.setVariable("DAAIVfMod_deleteVfModuleResponse","")\r
55 \r
56         }\r
57         \r
58         // parse the incoming DELETE_VF_MODULE request and store the Generic Vnf\r
59         // and Vf Module Ids in the flow Execution\r
60         public void preProcessRequest(Execution execution) {\r
61                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
62                 def xml = execution.getVariable("DeleteAAIVfModuleRequest")\r
63                 utils.logAudit("DeleteAAIVfModule Request: " + xml)\r
64                 utils.log("DEBUG", "input request xml:" + xml, isDebugEnabled)\r
65                 initProcessVariables(execution)\r
66                 def vnfId = utils.getNodeText(xml,"vnf-id")\r
67                 def vfModuleId = utils.getNodeText(xml,"vf-module-id")\r
68                 execution.setVariable("DAAIVfMod_vnfId", vnfId)\r
69                 execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)\r
70                 \r
71                 AaiUtil aaiUriUtil = new AaiUtil(this)\r
72                 def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
73                 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)\r
74                 \r
75                 execution.setVariable("DAAIVfMod_genericVnfEndpoint", "${aai_uri}/" + vnfId)\r
76                 execution.setVariable("DAAIVfMod_vfModuleEndpoint", "${aai_uri}/" + vnfId +\r
77                          "/vf-modules/vf-module/" + vfModuleId)\r
78         }\r
79         \r
80         // send a GET request to AA&I to retrieve the Generic Vnf/Vf Module information based on a Vnf Id\r
81         // expect a 200 response with the information in the response body or a 404 if the Generic Vnf does not exist\r
82         public void queryAAIForGenericVnf(Execution execution) {\r
83                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
84                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")\r
85                 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("DAAIVfMod_genericVnfEndpoint") + "?depth=1"\r
86                 utils.logAudit("DeleteAAIVfModule endPoint: " + endPoint)\r
87                 def aaiRequestId = utils.getRequestID()\r
88 \r
89                 RESTConfig config = new RESTConfig(endPoint);\r
90                 utils.log("DEBUG","queryAAIForGenericVnf() endpoint-" + endPoint, isDebugEnabled)\r
91                 def responseData = ""\r
92                 try {\r
93                         RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").\r
94                                 addHeader("Accept","application/xml");\r
95                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
96                                 \r
97                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
98                                 client.addAuthorizationHeader(basicAuthCred)\r
99                         }\r
100                         utils.log("DEBUG", "invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint,isDebugEnabled)\r
101                         APIResponse response = client.httpGet()\r
102                         utils.logAudit("DeleteAAIVfModule - invoking httpGet to AAI")\r
103 \r
104                         responseData = response.getResponseBodyAsString()\r
105                         execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", response.getStatusCode())\r
106                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", responseData)\r
107                         utils.logAudit("AAI Response: " + responseData)\r
108                         utils.log("DEBUG", "Response code:" + response.getStatusCode(), isDebugEnabled)\r
109                         utils.log("DEBUG", "Response:" + System.lineSeparator()+responseData,isDebugEnabled)\r
110                 } catch (Exception ex) {\r
111                         utils.log("DEBUG", "Exception occurred while executing AAI GET:" + ex.getMessage(),isDebugEnabled)\r
112                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "AAI GET Failed:" + ex.getMessage())\r
113                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during queryAAIForGenericVnf")\r
114 \r
115                 }\r
116         }\r
117         \r
118         // construct and send a DELETE request to A&AI to delete a Generic Vnf\r
119         // note: to get here, all the modules associated with the Generic Vnf must already be deleted\r
120         public void deleteGenericVnf(Execution execution) {\r
121                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
122                 def aaiRequestId = utils.getRequestID()\r
123                 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("DAAIVfMod_genericVnfEndpoint") +\r
124                         "/?resource-version=" + execution.getVariable("DAAIVfMod_genVnfRsrcVer")\r
125                 utils.logAudit("AAI endPoint: " + endPoint)\r
126                 RESTConfig config = new RESTConfig(endPoint);\r
127                 utils.log("DEBUG","deleteGenericVnf() endpoint-" + endPoint, isDebugEnabled)\r
128                 def responseData = ""\r
129                 try {\r
130                         RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").\r
131                                 addHeader("Accept","application/xml");\r
132                         \r
133                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
134                                         \r
135                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
136                                 client.addAuthorizationHeader(basicAuthCred)\r
137                         }\r
138                         APIResponse response = client.httpDelete()\r
139                                 \r
140                         responseData = response.getResponseBodyAsString()\r
141                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode", response.getStatusCode())\r
142                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponse", responseData)\r
143                         utils.log("DEBUG", "Response code:" + response.getStatusCode(), isDebugEnabled)\r
144                         utils.log("DEBUG", "Response:" + System.lineSeparator()+responseData,isDebugEnabled)\r
145                 } catch (Exception ex) {\r
146                         ex.printStackTrace()\r
147                         utils.log("DEBUG", "Exception occurred while executing AAI DELETE:" + ex.getMessage(),isDebugEnabled)\r
148                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteGenericVnf")\r
149                 }\r
150         }\r
151 \r
152         // construct and send a DELETE request to A&AI to delete the Base or Add-on Vf Module\r
153         public void deleteVfModule(Execution execution) {\r
154                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
155                 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("DAAIVfMod_vfModuleEndpoint") +\r
156                         "/?resource-version=" + execution.getVariable("DAAIVfMod_vfModRsrcVer")\r
157                 def aaiRequestId = utils.getRequestID()\r
158 \r
159                 RESTConfig config = new RESTConfig(endPoint);\r
160                 utils.log("DEBUG","deleteVfModule() endpoint-" + endPoint, isDebugEnabled)\r
161                 def responseData = ""\r
162                 try {\r
163                         RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").\r
164                                 addHeader("Accept","application/xml");\r
165                         \r
166                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
167                                         \r
168                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
169                                 client.addAuthorizationHeader(basicAuthCred)\r
170                         }\r
171                         APIResponse response = client.httpDelete()\r
172                         \r
173                         utils.logAudit("DeleteAAIVfModule - invoking httpDelete to AAI")\r
174                         \r
175                         responseData = response.getResponseBodyAsString()\r
176                         execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode", response.getStatusCode())\r
177                         execution.setVariable("DAAIVfMod_deleteVfModuleResponse", responseData)\r
178                         utils.logAudit("DeleteAAIVfModule - AAI Response" + responseData)\r
179                         utils.log("DEBUG", "Response code:" + response.getStatusCode(), isDebugEnabled)\r
180                         utils.log("DEBUG", "Response:" + System.lineSeparator()+responseData,isDebugEnabled)\r
181 \r
182                 } catch (Exception ex) {\r
183                         ex.printStackTrace()\r
184                         utils.log("DEBUG", "Exception occurred while executing AAI PUT:" + ex.getMessage(),isDebugEnabled)\r
185                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteVfModule")\r
186                 }\r
187         }\r
188         \r
189         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module\r
190         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,\r
191         // there are no Add-on Modules present\r
192         public void parseForVfModule(Execution execution) {\r
193                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
194                 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")\r
195                 utils.logAudit("DeleteAAIVfModule - queryGenericVnfResponse" + xml)\r
196                 \r
197                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")\r
198                 utils.log("DEBUG", "Vf Module to be deleted: " + delModuleId, isDebugEnabled)\r
199                 List <String> qryModuleIdList = utils.getMultNodes(xml, "vf-module-id")\r
200                 List <String> qryBaseModuleList = utils.getMultNodes(xml, "is-base-vf-module")\r
201                 List <String> qryResourceVerList = utils.getMultNodes(xml, "resource-version")\r
202                 execution.setVariable("DAAIVfMod_moduleExists", false)\r
203                 execution.setVariable("DAAIVfMod_isBaseModule", false)\r
204                 execution.setVariable("DAAIVfMod_isLastModule", false)\r
205                 //\r
206                 def isBaseVfModule = "false"\r
207                 // loop through the Vf Module Ids looking for a match\r
208                 if (qryModuleIdList != null && !qryModuleIdList.empty) {\r
209                         utils.log("DEBUG", "Existing Vf Module Id List: " + qryModuleIdList, isDebugEnabled)\r
210                         utils.log("DEBUG", "Existing Vf Module Resource Version List: " + qryResourceVerList, isDebugEnabled)\r
211                         def moduleCntr = 0\r
212                         // the Generic Vnf resource-version in the 1st entry in the query response\r
213                         execution.setVariable("DAAIVfMod_genVnfRsrcVer", qryResourceVerList[moduleCntr])\r
214                         for (String qryModuleId : qryModuleIdList) {\r
215                                 if (delModuleId.equals(qryModuleId)) {\r
216                                         // a Vf Module with the requested Id exists\r
217                                         execution.setVariable("DAAIVfMod_moduleExists", true)\r
218                                         // find the corresponding value for the is-base-vf-module field\r
219                                         isBaseVfModule = qryBaseModuleList[moduleCntr]\r
220                                         // find the corresponding value for the resource-version field\r
221                                         // note: the Generic Vnf entry also has a resource-version field, so\r
222                                         //       add 1 to the index to get the corresponding Vf Module value\r
223                                         execution.setVariable("DAAIVfMod_vfModRsrcVer", qryResourceVerList[moduleCntr+1])\r
224                                         utils.log("DEBUG", "Match found for Vf Module Id " + qryModuleId + " for Generic Vnf Id " +\r
225                                                 execution.getVariable("DAAIVfMod_vnfId") + ", Base Module is " + isBaseVfModule +\r
226                                                 ", Resource Version is " + execution.getVariable("vfModRsrcVer"), isDebugEnabled)\r
227                                         break\r
228                                 }\r
229                                 moduleCntr++\r
230                         }\r
231                 }\r
232                 \r
233                 // determine if the module to be deleted is a Base Module and/or the Last Module\r
234                 if (execution.getVariable("DAAIVfMod_moduleExists") == true) {\r
235                         if (isBaseVfModule.equals("true") && qryModuleIdList.size() != 1) {\r
236                                 execution.setVariable("DAAIVfMod_parseModuleResponse",\r
237                                         "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +\r
238                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module, not Last Module")\r
239                                 execution.setVariable("DAAIVfMod_isBaseModule", true)\r
240                         } else {\r
241                                 if (isBaseVfModule.equals("true") && qryModuleIdList.size() == 1) {\r
242                                         execution.setVariable("DAAIVfMod_parseModuleResponse",\r
243                                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +\r
244                                                 execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module and Last Module")\r
245                                         execution.setVariable("DAAIVfMod_isBaseModule", true)\r
246                                         execution.setVariable("DAAIVfMod_isLastModule", true)\r
247                                 } else {\r
248                                         if (qryModuleIdList.size() == 1) {\r
249                                                 execution.setVariable("DAAIVfMod_parseModuleResponse",\r
250                                                         "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +\r
251                                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module, is Last Module")\r
252                                                 execution.setVariable("DAAIVfMod_isLastModule", true)\r
253                                         } else {\r
254                                         execution.setVariable("DAAIVfMod_parseModuleResponse",\r
255                                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +\r
256                                                 execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module and Not Last Module")\r
257                                         }\r
258                                 }\r
259                         }\r
260                         utils.log("DEBUG", execution.getVariable("DAAIVfMod_parseModuleResponse"), isDebugEnabled)\r
261                 } else { // (execution.getVariable("DAAIVfMod_moduleExists") == false)\r
262                         utils.log("DEBUG", "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +\r
263                                  execution.getVariable("DAAIVfMod_vnfId"), isDebugEnabled)\r
264                         execution.setVariable("DAAIVfMod_parseModuleResponse",\r
265                                 "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +\r
266                                 execution.getVariable("DAAIVfMod_vnfName"))\r
267                 }\r
268         }\r
269         \r
270         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module\r
271         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,\r
272         // there are no Add-on Modules present\r
273         public void parseForResourceVersion(Execution execution) {\r
274                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
275                 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")\r
276                 utils.logAudit("DeleteAAIVfModule - queryGenericVnfResponse" + xml)\r
277                 String resourceVer = utils.getNodeText1(xml, "resource-version")\r
278                 execution.setVariable("DAAIVfMod_genVnfRsrcVer", resourceVer)\r
279                 utils.log("DEBUG", "Latest Generic VNF Resource Version: " + resourceVer)\r
280         }\r
281         \r
282         \r
283         // generates a WorkflowException if the A&AI query returns a response code other than 200\r
284         public void handleAAIQueryFailure(Execution execution) {\r
285                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
286                 \r
287                 utils.log("ERROR", "Error occurred attempting to query AAI, Response Code " +\r
288                         execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") + ", Error Response " +\r
289                         execution.getVariable("DAAIVfMod_queryGenericVnfResponse"), isDebugEnabled)\r
290                 def errorCode = 5000\r
291                 // set the errorCode to distinguish between a A&AI failure\r
292                 // and the Generic Vnf Id not found\r
293                 if (execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") == 404) {\r
294                         errorCode = 1002\r
295                 }\r
296                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))\r
297         }\r
298         \r
299         // generates a WorkflowException if\r
300         //              - the A&AI Vf Module DELETE returns a response code other than 200\r
301         //              - the Vf Module is a Base Module that is not the last Vf Module\r
302         //              - the Vf Module does not exist for the Generic Vnf\r
303         public void handleDeleteVfModuleFailure(Execution execution) {\r
304                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
305                 \r
306                 def errorCode = 2000\r
307                 def errorResponse = ""\r
308                 if (execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != null &&\r
309                         execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != 200) {\r
310                         utils.log("DEBUG", "AAI failure deleting a Vf Module: " +\r
311                                 execution.getVariable("DAAIVfMod_deleteVfModuleResponse"), isDebugEnabled)\r
312                         errorResponse = execution.getVariable("DAAIVfMod_deleteVfModuleResponse")\r
313                         utils.logAudit("DeleteAAIVfModule - deleteVfModuleResponse" + errorResponse)\r
314                         errorCode = 5000\r
315                 } else {\r
316                         if (execution.getVariable("DAAIVfMod_isBaseModule", true) == true &&\r
317                                         execution.getVariable("DAAIVfMod_isLastModule") == false) {\r
318                                 // attempt to delete a Base Module that is not the last Vf Module\r
319                                 utils.log("DEBUG", execution.getVariable("DAAIVfMod_parseModuleResponse"), isDebugEnabled)\r
320                                 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")\r
321                                 utils.logAudit("DeleteAAIVfModule - parseModuleResponse" + errorResponse)\r
322                                 errorCode = 1002\r
323                         } else {\r
324                                 // attempt to delete a non-existant Vf Module\r
325                                 if (execution.getVariable("DAAIVfMod_moduleExists") == false) {\r
326                                         utils.log("DEBUG", execution.getVariable("DAAIVfMod_parseModuleResponse"), isDebugEnabled)\r
327                                         errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")\r
328                                         utils.logAudit("DeleteAAIVfModule - parseModuleResponse" + errorResponse)\r
329                                         errorCode = 1002\r
330                                 } else {\r
331                                         // if the responses get populated corerctly, we should never get here\r
332                                         errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"\r
333                                 }\r
334                         }\r
335                 }\r
336 \r
337                 utils.log("ERROR", "Error occurred during DeleteAAIVfModule flow: " + errorResponse, isDebugEnabled)\r
338                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)\r
339 \r
340         }\r
341 \r
342         // generates a WorkflowException if\r
343         //              - the A&AI Generic Vnf DELETE returns a response code other than 200\r
344         public void handleDeleteGenericVnfFailure(Execution execution) {\r
345                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
346                 utils.log("ERROR", "AAI error occurred deleting the Generic Vnf: "\r
347                         + execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"), isDebugEnabled)\r
348                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"))\r
349         }\r
350 }