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