aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/GenericGetVnf.groovy
blob: bc787df577fb4c8b337a29365fc2d06dba0bde03 (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
/*-
 * ============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.openecomp.mso.bpmn.common.scripts.GenericUtils.*;

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 GENGV_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
 * GENGV_SuccessIndicator will be true and the query response payload
 * will be set to GENGV_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 GENGV_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 - GENGV_vnfId  or  @param - GENGV_vnfName
 *   @param - GENGV_type
 *
 * Out Mapping Variables:
 *   @param - GENGV_vnf
 *   @param - GENGV_SuccessIndicator
 *   @param - GENGV_FoundIndicator
 *   @param - WorkflowException
 *
 *
 */
class GenericGetVnf extends AbstractServiceTaskProcessor{

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

	/**
	 * This method validates the incoming variables and
	 * determines the subsequent event based on which
	 * variables the calling flow provided.
	 *
	 * @param - execution
	 */
	public void preProcessRequest(Execution execution) {
		def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericGetVnf PreProcessRequest Process*** ", isDebugEnabled)

		execution.setVariable("GENGV_getVnfByName", false)
		execution.setVariable("GENGV_SuccessIndicator", false)
		execution.setVariable("GENGV_FoundIndicator", false)

		try{
			// Get Variables
			String vnfId = execution.getVariable("GENGV_vnfId")
			utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
			String vnfName = execution.getVariable("GENGV_vnfName")
			utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)

			if(isBlank(vnfId) && isBlank(vnfName)){
				utils.log("DEBUG", "Incoming Vnf Name and Vnf Id are null. At least one is required!", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Name and Vnf Id are null. At least one is required.")
			}else{
				if(isBlank(vnfId)){
					execution.setVariable("GENGV_getVnfByName", true)
				}
			}

		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
			throw b
		}catch(Exception e){
			utils.log("DEBUG", " Error encountered within GenericGetVnf PreProcessRequest method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetVnf PreProcessRequest")

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

	/**
	 * This method executes a GET call to AAI to obtain the
	 * Vnf using the Vnf Name
	 *
	 * @param - execution
	 */
	public void getVnfByName(Execution execution){
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)
		try {
			String vnfName = execution.getVariable("GENGV_vnfName")
			utils.log("DEBUG", "Getting Vnf by Vnf Name: " + vnfName, isDebugEnabled)
			String type = execution.getVariable("GENGV_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 GENGV_type", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
			}

			String getVnfPath = "${aai_endpoint}${aai_uri}?vnf-name=" + UriUtils.encode(vnfName, "UTF-8") + "&depth=1"

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

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

			String aaiResponse = response.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			execution.setVariable("GENGV_getVnfResponse", aaiResponse)

			//Process Response
			if(responseCode == 200){
				utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)
					if(utils.nodeExists(aaiResponse, type)){
						utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)
						execution.setVariable("GENGV_FoundIndicator", true)
						execution.setVariable("GENGV_vnf", aaiResponse)
						execution.setVariable("WorkflowResponse", aaiResponse)
					}else{
						utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)
					}

			}else if(responseCode == 404){
				utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
			}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("ERROR", " Error encountered within GenericGetVnf GetVnfByName method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfByName")
		}
		utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)
	}

	/**
	 * This method executes a GET call to AAI to obtain the
	 * Vnf using the Vnf Id
	 *
	 * @param - execution
	 */
	public void getVnfById(Execution execution){
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)
		try {
			String vnfId = execution.getVariable("GENGV_vnfId")
			utils.log("DEBUG", "Getting Vnf by Vnf Id: " + vnfId, isDebugEnabled)
			String type = execution.getVariable("GENGV_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 if(type.equals("vpe")){
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericGetVnf does not yet support getting type of vnf = vpe")
			}else{
				utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
			}
			utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)

			String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
			utils.log("DEBUG", "GET Vnf Endpoint is: " + getVnfPath, isDebugEnabled)

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

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

			String aaiResponse = response.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			execution.setVariable("GENGV_getVnfResponse", aaiResponse)

			//Process Response
			if(responseCode == 200){
				utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)
					if(utils.nodeExists(aaiResponse, type)){
						utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)
						execution.setVariable("GENGV_FoundIndicator", true)
						execution.setVariable("GENGV_vnf", aaiResponse)
						execution.setVariable("WorkflowResponse", aaiResponse)
					}else{
						utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)
					}

			}else if(responseCode == 404){
				utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
			}else{
				utils.log("DEBUG", "GET 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("ERROR", " Error encountered within GenericGetVnf GetVnfById method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfById")
		}
		utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)
	}

}