aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/GenericDeleteVnf.groovy
blob: 1bd4383b74429780672f9c7200ab2cd25d623c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.bpmn.common.scripts

import static org.apache.commons.lang3.StringUtils.*

import org.apache.commons.lang3.*
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.runtime.Execution
import org.openecomp.mso.rest.APIResponse
import org.springframework.web.util.UriUtils


/**
 * TODO: Support getting vnf type = vpe
 *
 * This class supports the GenericGetVnf Sub Flow.
 * This Generic sub flow can be used by any flow for accomplishing
 * the goal of getting a Vnf Object (from AAI).  The flow currently
 * supports the querying of 2 types of Vnfs, generic-vnf and vce. The
 * type must be provided by the calling flow and the type should
 * be mapped to the variable GENDV_type. The type should either be
 * "generic-vnf" or "vce".  If the Vnf Id is not provided by the calling
 * flow then this sub flow will execute the query to get the
 * Vnf using the Vnf Name. Therefore, the calling flow must provide
 * either the Vnf Id or Vnf Name.
 *
 * Upon successful completion of this sub flow the
 * GENDV_SuccessIndicator will be true and the query response payload
 * will be set to GENDV_vnf.  An MSOWorkflowException will
 * be thrown upon unsuccessful completion or if an error occurs
 * at any time during this sub flow. Please map variables
 * to the corresponding variable names below.
 *
 * Note - if this sub flow receives a Not Found (404) response
 * from AAI at any time this will be considered an acceptable
 * successful response however the GENDV_FoundIndicator
 * set to false.  This will allow the calling flow to distinguish
 * between the two success scenarios, "Success where Vnf is found"
 * and "Success where Vnf is NOT found".
 *
 *
 * Variable Mapping Below
 *
 * In Mapping Variables:
 *   @param - GENDV_vnfId
 *   @param - GENDV_type
 *   @param (Optional) - GENDV_resourceVersion
 *
 *
 * Out Mapping Variables:
 *   @param - GENDV_vnf
 *   @param - GENDV_SuccessIndicator
 *   @param - GENDV_FoundIndicator
 *   @param - WorkflowException
 *
 *
 */
class GenericDeleteVnf extends AbstractServiceTaskProcessor{

	String Prefix = "GENDV_"
	ExceptionUtil exceptionUtil = new ExceptionUtil()

	/**
	 * This method validates the incoming variables and
	 * determines if the resource version was provided
	 *
	 * @param - execution
	 */
	public void preProcessRequest(Execution execution) {
		def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericDeleteVnf PreProcessRequest Process*** ", isDebugEnabled)

		execution.setVariable("GENDV_resourceVersionProvided", true)
		execution.setVariable("GENDV_SuccessIndicator", false)
		execution.setVariable("GENDV_FoundIndicator", false)

		try{
			// Get Variables
			String vnfId = execution.getVariable("GENDV_vnfId")
			String type = execution.getVariable("GENDV_type")

			if(isBlank(type) || isBlank(vnfId)){
				utils.log("ERROR", "Incoming Required Variable is null!", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
			}else{
				utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)

				String resourceVersion = execution.getVariable("GENDV_resourceVersion")
				if(isBlank(resourceVersion)){
					utils.log("DEBUG", "Vnf Resource Version is NOT Provided", isDebugEnabled)
					execution.setVariable("GENDV_resourceVersionProvided", false)
				}else{
					utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)
				}
			}
		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
			throw b
		}catch(Exception e){
			utils.log("ERROR", " Error encountered within GenericDeleteVnf PreProcessRequest method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericDeleteVnf PreProcessRequest")

		}
		utils.log("DEBUG", "*** COMPLETED GenericDeleteVnf PreProcessRequest Process ***", isDebugEnabled)
	}

	/**
	 * This method executes a GET call to AAI for the Vnf
	 * so that the Vnf's resource-version can be
	 * obtained.
	 *
	 * @param - execution
	 */
	public void getVnfResourceVersion(Execution execution){
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)
		try {
			String vnfId = execution.getVariable("GENDV_vnfId")
			String type = execution.getVariable("GENDV_type")
			utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)

			String aai_endpoint = execution.getVariable("URN_aai_endpoint")
			AaiUtil aaiUriUtil = new AaiUtil(this)

			//Determine Type of Vnf Querying For
			def aai_uri = ""
			if(type.equals("generic-vnf")){
				aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
			}else if(type.equals("vce")){
				aai_uri = aaiUriUtil.getNetworkVceUri(execution)
			}else{
				utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")
			}

			String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")

			execution.setVariable("GENDV_getVnfPath", getVnfPath)
			utils.logAudit("Get Vnf Resource Version Url is: " + getVnfPath)

			APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
			int responseCode = response.getStatusCode()
			execution.setVariable("GENDV_getVnfResponseCode", responseCode)
			utils.log("DEBUG", "  GET Vnf response code is: " + responseCode, isDebugEnabled)

			utils.logAudit("GenericDeleteVnf Get VNF Response Code: " + responseCode)
			String aaiResponse = response.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			execution.setVariable("GENDV_getVnfResponse", aaiResponse)

			utils.logAudit("GenericDeleteVnf Get VNF Response: " + aaiResponse)
			//Process Response
			if(responseCode == 200 || responseCode == 202){
				utils.log("DEBUG", "GET Vnf Received a Good Response: \n" + aaiResponse, isDebugEnabled)
				execution.setVariable("GENDV_FoundIndicator", true)
				if(utils.nodeExists(aaiResponse, "resource-version")){
					String resourceVersion = utils.getNodeText1(aaiResponse, "resource-version")
					execution.setVariable("GENDV_resourceVersion", resourceVersion)
					utils.log("DEBUG", "SI Resource Version is: " + resourceVersion, isDebugEnabled)
				}else{
					utils.log("DEBUG", "GET Vnf for Resource Version Response Does NOT Contain a resource-version", isDebugEnabled)
					exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Unable to obtain Vnf resource-version. GET Vnf Response Does NOT Contain a resource-version")
				}

			}else if(responseCode == 404){
				utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
				execution.setVariable("GENDV_SuccessIndicator", true)
				execution.setVariable("WorkflowResponse", "  ") // for junits
			}
			else{
				utils.log("DEBUG", "  GET Vnf Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
				exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
				throw new BpmnError("MSOWorkflowException")
			}
		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
			throw b
		}catch(Exception e){
			utils.log("DEBUG", " Error encountered within GenericDeleteVnf GetVnfResourceVersion method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfResourceVersion")
		}
		utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)
	}

	/**
	 * This method executes a DELETE call to AAI for the provided
	 * Vnf.
	 *
	 * @param - execution
	 */
	public void deleteVnf(Execution execution){
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)
		try {
			String vnfId = execution.getVariable("GENDV_vnfId")
			String type = execution.getVariable("GENDV_type")
			utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
			String resourceVersion = execution.getVariable("GENDV_resourceVersion")
			utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)

			String aai_endpoint = execution.getVariable("URN_aai_endpoint")
			AaiUtil aaiUriUtil = new AaiUtil(this)

			//Determine Type of Vnf Querying For
			def aai_uri = ""
			if(type.equals("generic-vnf")){
				aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
			}else if(type.equals("vce")){
				aai_uri = aaiUriUtil.getNetworkVceUri(execution)
			}else{
				utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")
			}

			String deleteVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")

			execution.setVariable("GENDV_deleteVnfPath", deleteVnfPath)
			utils.logAudit("Delete Vnf Url is: " + deleteVnfPath)

			APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, deleteVnfPath)
			int responseCode = response.getStatusCode()
			execution.setVariable("GENDV_deleteVnfResponseCode", responseCode)
			utils.log("DEBUG", "  DELETE Vnf response code is: " + responseCode, isDebugEnabled)

			utils.logAudit("GenericDeleteVnf Delete VNF Response Code: " + responseCode)
			String aaiResponse = response.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			execution.setVariable("GENDV_deleteVnfResponse", aaiResponse)

			utils.logAudit("GenericDeleteVnf Delete VNF Response: " + aaiResponse)
			//Process Response
			if(responseCode == 204){
				utils.log("DEBUG", "  DELETE Vnf Received a Good Response", isDebugEnabled)
				execution.setVariable("GENDV_FoundIndicator", true)
			}else if(responseCode == 404){
				utils.log("DEBUG", "  DELETE Vnf Received a Not Found (404) Response", isDebugEnabled)
			}else if(responseCode == 412){
				utils.log("DEBUG", "DELETE Vnf Received a Resource Version Mismatch Error: \n" + aaiResponse, isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 412, "Delete Vnf Received a resource-version Mismatch Error Response from AAI")
			}else{
				utils.log("DEBUG", "DELETE Vnf Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
				exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
				throw new BpmnError("MSOWorkflowException")
			}
		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
			throw b
		}catch(Exception e){
			utils.log("DEBUG", " Error encountered within GenericDeleteVnf DeleteVnf method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete Vnf")
		}
		utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)
	}


}