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