Groovy scripts header correction
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericDeleteVnf.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 \r
23 import static org.apache.commons.lang3.StringUtils.*\r
24 \r
25 import org.apache.commons.lang3.*\r
26 import org.camunda.bpm.engine.delegate.BpmnError\r
27 import org.camunda.bpm.engine.runtime.Execution\r
28 import org.openecomp.mso.rest.APIResponse\r
29 import org.springframework.web.util.UriUtils\r
30 \r
31 \r
32 /**\r
33  * TODO: Support getting vnf type = vpe\r
34  *\r
35  * This class supports the GenericGetVnf Sub Flow.\r
36  * This Generic sub flow can be used by any flow for accomplishing\r
37  * the goal of getting a Vnf Object (from AAI).  The flow currently\r
38  * supports the querying of 2 types of Vnfs, generic-vnf and vce. The\r
39  * type must be provided by the calling flow and the type should\r
40  * be mapped to the variable GENDV_type. The type should either be\r
41  * "generic-vnf" or "vce".  If the Vnf Id is not provided by the calling\r
42  * flow then this sub flow will execute the query to get the\r
43  * Vnf using the Vnf Name. Therefore, the calling flow must provide\r
44  * either the Vnf Id or Vnf Name.\r
45  *\r
46  * Upon successful completion of this sub flow the\r
47  * GENDV_SuccessIndicator will be true and the query response payload\r
48  * will be set to GENDV_vnf.  An MSOWorkflowException will\r
49  * be thrown upon unsuccessful completion or if an error occurs\r
50  * at any time during this sub flow. Please map variables\r
51  * to the corresponding variable names below.\r
52  *\r
53  * Note - if this sub flow receives a Not Found (404) response\r
54  * from AAI at any time this will be considered an acceptable\r
55  * successful response however the GENDV_FoundIndicator\r
56  * set to false.  This will allow the calling flow to distinguish\r
57  * between the two success scenarios, "Success where Vnf is found"\r
58  * and "Success where Vnf is NOT found".\r
59  *\r
60  *\r
61  * Variable Mapping Below\r
62  *\r
63  * In Mapping Variables:\r
64  *   @param - GENDV_vnfId\r
65  *   @param - GENDV_type\r
66  *   @param (Optional) - GENDV_resourceVersion\r
67  *\r
68  *\r
69  * Out Mapping Variables:\r
70  *   @param - GENDV_SuccessIndicator\r
71  *   @param - GENDV_FoundIndicator\r
72  *   @param - WorkflowException\r
73  *\r
74  *\r
75  */\r
76 class GenericDeleteVnf extends AbstractServiceTaskProcessor{\r
77 \r
78         String Prefix = "GENDV_"\r
79         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
80 \r
81         /**\r
82          * This method validates the incoming variables and\r
83          * determines if the resource version was provided\r
84          *\r
85          * @param - execution\r
86          */\r
87         public void preProcessRequest(Execution execution) {\r
88                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
89                 execution.setVariable("prefix",Prefix)\r
90                 utils.log("DEBUG", " *** STARTED GenericDeleteVnf PreProcessRequest Process*** ", isDebugEnabled)\r
91 \r
92                 execution.setVariable("GENDV_resourceVersionProvided", true)\r
93                 execution.setVariable("GENDV_SuccessIndicator", false)\r
94                 execution.setVariable("GENDV_FoundIndicator", false)\r
95 \r
96                 try{\r
97                         // Get Variables\r
98                         String vnfId = execution.getVariable("GENDV_vnfId")\r
99                         String type = execution.getVariable("GENDV_type")\r
100 \r
101                         if(isBlank(type) || isBlank(vnfId)){\r
102                                 utils.log("ERROR", "Incoming Required Variable is null!", isDebugEnabled)\r
103                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")\r
104                         }else{\r
105                                 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)\r
106 \r
107                                 String resourceVersion = execution.getVariable("GENDV_resourceVersion")\r
108                                 if(isBlank(resourceVersion)){\r
109                                         utils.log("DEBUG", "Vnf Resource Version is NOT Provided", isDebugEnabled)\r
110                                         execution.setVariable("GENDV_resourceVersionProvided", false)\r
111                                 }else{\r
112                                         utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)\r
113                                 }\r
114                         }\r
115                 }catch(BpmnError b){\r
116                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
117                         throw b\r
118                 }catch(Exception e){\r
119                         utils.log("ERROR", " Error encountered within GenericDeleteVnf PreProcessRequest method!" + e, isDebugEnabled)\r
120                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericDeleteVnf PreProcessRequest")\r
121 \r
122                 }\r
123                 utils.log("DEBUG", "*** COMPLETED GenericDeleteVnf PreProcessRequest Process ***", isDebugEnabled)\r
124         }\r
125 \r
126         /**\r
127          * This method executes a GET call to AAI for the Vnf\r
128          * so that the Vnf's resource-version can be\r
129          * obtained.\r
130          *\r
131          * @param - execution\r
132          */\r
133         public void getVnfResourceVersion(Execution execution){\r
134                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
135                 execution.setVariable("prefix",Prefix)\r
136                 utils.log("DEBUG", " *** STARTED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)\r
137                 try {\r
138                         String vnfId = execution.getVariable("GENDV_vnfId")\r
139                         String type = execution.getVariable("GENDV_type")\r
140                         utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)\r
141 \r
142                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
143                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
144 \r
145                         //Determine Type of Vnf Querying For\r
146                         def aai_uri = ""\r
147                         if(type.equals("generic-vnf")){\r
148                                 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
149                         }else if(type.equals("vce")){\r
150                                 aai_uri = aaiUriUtil.getNetworkVceUri(execution)\r
151                         }else{\r
152                                 utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)\r
153                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")\r
154                         }\r
155 \r
156                         String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")\r
157 \r
158                         execution.setVariable("GENDV_getVnfPath", getVnfPath)\r
159                         utils.logAudit("Get Vnf Resource Version Url is: " + getVnfPath)\r
160 \r
161                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)\r
162                         int responseCode = response.getStatusCode()\r
163                         execution.setVariable("GENDV_getVnfResponseCode", responseCode)\r
164                         utils.log("DEBUG", "  GET Vnf response code is: " + responseCode, isDebugEnabled)\r
165 \r
166                         utils.logAudit("GenericDeleteVnf Get VNF Response Code: " + responseCode)\r
167                         String aaiResponse = response.getResponseBodyAsString()\r
168                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
169                         execution.setVariable("GENDV_getVnfResponse", aaiResponse)\r
170 \r
171                         utils.logAudit("GenericDeleteVnf Get VNF Response: " + aaiResponse)\r
172                         //Process Response\r
173                         if(responseCode == 200 || responseCode == 202){\r
174                                 utils.log("DEBUG", "GET Vnf Received a Good Response: \n" + aaiResponse, isDebugEnabled)\r
175                                 execution.setVariable("GENDV_FoundIndicator", true)\r
176                                 if(utils.nodeExists(aaiResponse, "resource-version")){\r
177                                         String resourceVersion = utils.getNodeText1(aaiResponse, "resource-version")\r
178                                         execution.setVariable("GENDV_resourceVersion", resourceVersion)\r
179                                         utils.log("DEBUG", "SI Resource Version is: " + resourceVersion, isDebugEnabled)\r
180                                 }else{\r
181                                         utils.log("DEBUG", "GET Vnf for Resource Version Response Does NOT Contain a resource-version", isDebugEnabled)\r
182                                         exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Unable to obtain Vnf resource-version. GET Vnf Response Does NOT Contain a resource-version")\r
183                                 }\r
184 \r
185                         }else if(responseCode == 404){\r
186                                 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)\r
187                                 execution.setVariable("GENDV_SuccessIndicator", true)\r
188                                 execution.setVariable("WorkflowResponse", "  ") // for junits\r
189                         }\r
190                         else{\r
191                                 utils.log("DEBUG", "  GET Vnf Received a Bad Response: \n" + aaiResponse, isDebugEnabled)\r
192                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
193                                 throw new BpmnError("MSOWorkflowException")\r
194                         }\r
195                 }catch(BpmnError b){\r
196                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
197                         throw b\r
198                 }catch(Exception e){\r
199                         utils.log("DEBUG", " Error encountered within GenericDeleteVnf GetVnfResourceVersion method!" + e, isDebugEnabled)\r
200                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfResourceVersion")\r
201                 }\r
202                 utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)\r
203         }\r
204 \r
205         /**\r
206          * This method executes a DELETE call to AAI for the provided\r
207          * Vnf.\r
208          *\r
209          * @param - execution\r
210          */\r
211         public void deleteVnf(Execution execution){\r
212                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
213                 execution.setVariable("prefix",Prefix)\r
214                 utils.log("DEBUG", " *** STARTED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)\r
215                 try {\r
216                         String vnfId = execution.getVariable("GENDV_vnfId")\r
217                         String type = execution.getVariable("GENDV_type")\r
218                         utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)\r
219                         String resourceVersion = execution.getVariable("GENDV_resourceVersion")\r
220                         utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)\r
221 \r
222                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
223                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
224 \r
225                         //Determine Type of Vnf Querying For\r
226                         def aai_uri = ""\r
227                         if(type.equals("generic-vnf")){\r
228                                 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
229                         }else if(type.equals("vce")){\r
230                                 aai_uri = aaiUriUtil.getNetworkVceUri(execution)\r
231                         }else{\r
232                                 utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)\r
233                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")\r
234                         }\r
235 \r
236                         String deleteVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")\r
237 \r
238                         execution.setVariable("GENDV_deleteVnfPath", deleteVnfPath)\r
239                         utils.logAudit("Delete Vnf Url is: " + deleteVnfPath)\r
240 \r
241                         APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, deleteVnfPath)\r
242                         int responseCode = response.getStatusCode()\r
243                         execution.setVariable("GENDV_deleteVnfResponseCode", responseCode)\r
244                         utils.log("DEBUG", "  DELETE Vnf response code is: " + responseCode, isDebugEnabled)\r
245 \r
246                         utils.logAudit("GenericDeleteVnf Delete VNF Response Code: " + responseCode)\r
247                         String aaiResponse = response.getResponseBodyAsString()\r
248                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
249                         execution.setVariable("GENDV_deleteVnfResponse", aaiResponse)\r
250 \r
251                         utils.logAudit("GenericDeleteVnf Delete VNF Response: " + aaiResponse)\r
252                         //Process Response\r
253                         if(responseCode == 204){\r
254                                 utils.log("DEBUG", "  DELETE Vnf Received a Good Response", isDebugEnabled)\r
255                                 execution.setVariable("GENDV_FoundIndicator", true)\r
256                         }else if(responseCode == 404){\r
257                                 utils.log("DEBUG", "  DELETE Vnf Received a Not Found (404) Response", isDebugEnabled)\r
258                         }else if(responseCode == 412){\r
259                                 utils.log("DEBUG", "DELETE Vnf Received a Resource Version Mismatch Error: \n" + aaiResponse, isDebugEnabled)\r
260                                 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "Delete Vnf Received a resource-version Mismatch Error Response from AAI")\r
261                         }else{\r
262                                 utils.log("DEBUG", "DELETE Vnf Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)\r
263                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
264                                 throw new BpmnError("MSOWorkflowException")\r
265                         }\r
266                 }catch(BpmnError b){\r
267                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
268                         throw b\r
269                 }catch(Exception e){\r
270                         utils.log("DEBUG", " Error encountered within GenericDeleteVnf DeleteVnf method!" + e, isDebugEnabled)\r
271                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete Vnf")\r
272                 }\r
273                 utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)\r
274         }\r
275 \r
276 \r
277 }\r