aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/GenericPutVnf.groovy
blob: 9a7aa5ab9fbe180e9601aa55708b7b6821aaf9ec (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
/*-
 * ============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 Putting vnf type = vpe and vce
 *
 * This class supports the GenericPutVnf Sub Flow.
 * This Generic sub flow can be used by any flow for accomplishing
 * the goal of Creating/Updating(PUT) a Vnf Object (in AAI).  The flow
 * supports the Creating/Updating of 3 types of Vnfs (generic-vnf, vce, and vpe).
 * The "type" must be provided by the calling flow and this type should
 * be mapped to the variable GENPV_type. The type should either be
 * "generic-vnf", "vce", or "vpe".  In addition, the Vnf Id and
 * payload should be provided.
 *
 * Upon successful completion of this sub flow the
 * GENPV_SuccessIndicator.  An MSOWorkflowException will
 * be thrown if an error occurs at any time during this
 * sub flow. Please map input variables to the corresponding
 * variable names below.
 *
 *
 * Incoming Required Variables:
 * @param - GENPV_vnfId
 * @param - GENPV_vnfPayload
 * @param - GENPV_type
 *
 *
 * Outgoing Variables:
 * @param - GENPV_SuccessIndicator
 * @param - WorkflowException
 */
class GenericPutVnf  extends AbstractServiceTaskProcessor{

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

	/**
	 * This method validates the incoming variables and
	 * generates a Vnf Id if one is not provided.
	 *
	 * @param - execution
	 */
	public void preProcessRequest(Execution execution) {
		def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericPutVnf PreProcessRequest Process*** ", isDebugEnabled)

		execution.setVariable("GENPV_SuccessIndicator", false)
		execution.setVariable("GENPV_FoundIndicator", false)

		try{
			// Get Variables
			String payload = execution.getVariable("GENPV_vnfPayload")
			utils.log("DEBUG", "Incoming Vnf Payload is: " + payload, isDebugEnabled)
			String type = execution.getVariable("GENPV_type")
			utils.log("DEBUG", "Incoming Type of Vnf is: " + type, isDebugEnabled)

			if(isBlank(payload) || isBlank(type)){
				utils.log("ERROR", "Incoming Vnf Payload and/or Type is null. These Variables are required!", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Payload and/or Type is null. These Variables are required!")
			}else{
				String vnfId = execution.getVariable("GENPV_vnfId")
				if(isBlank(vnfId)){
					vnfId = UUID.randomUUID().toString()
					utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
					execution.setVariable("GENPV_vnfId", vnfId)
				}else{
					utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
				}
			}
		}catch(BpmnError b){
			utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
			throw b
		}catch(Exception e){
			utils.log("ERROR", " Error encountered within GenericPutVnf PreProcessRequest method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutVnf PreProcessRequest")

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

	/**
	 * This method executes a Put call to AAI to create
	 * or update a Vnf Object using the provided payload
	 *
	 * @param - execution
	 */
	public void putVnf(Execution execution){
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		execution.setVariable("prefix",Prefix)
		utils.log("DEBUG", " *** STARTED GenericPutVnf PutVnf Process*** ", isDebugEnabled)
		try {
			String vnfId = execution.getVariable("GENPV_vnfId")
			String payload = execution.getVariable("GENPV_vnfPayload")
			String type = execution.getVariable("GENPV_type")

			AaiUtil aaiUtil = new AaiUtil(this)
			def aai_uri = ""
			if(type.equals("generic-vnf")){
				aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
			}else if(type.equals("vce")){
				aai_uri = aaiUtil.getNetworkVceUri(execution)
			}else if(type.equals("vpe")){
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericPutVnf does not yet support getting type of vnf = vpe")
			}else{
				utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENPV_type")
			}
			utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)

			String path = "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")
			utils.log("DEBUG", "PUT Vnf Endpoint is: " + path, isDebugEnabled)

			String putVnfAAIPath = execution.getVariable("URN_aai_endpoint") + path
			execution.setVariable("GENPV_putVnfAAIPath", putVnfAAIPath)
			utils.logAudit("PUT Vnf Url is: " + putVnfAAIPath)

			APIResponse apiResponse = aaiUtil.executeAAIPutCall(execution, putVnfAAIPath, payload)
			int responseCode = apiResponse.getStatusCode()
			execution.setVariable("GENPV_putVnfResponseCode", responseCode)
			utils.logAudit("AAI Response Code: " + responseCode)
			String aaiResponse = apiResponse.getResponseBodyAsString()
			aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
			execution.setVariable("GENPV_putVnfResponse", aaiResponse)
			utils.logAudit("AAI Response: " + aaiResponse)

			if(responseCode == 200 || responseCode == 201){
				utils.log("DEBUG", "PUT Vnf Received a Good Response Code.", isDebugEnabled)
			}else{
				utils.log("DEBUG", "PUT Vnf Received a Bad Response Code. Response Code is: " + responseCode, 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 GenericPutVnf PutVnf method!" + e, isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During PutVnf")
		}
		utils.log("DEBUG", " *** COMPLETED GenericPutVnf PutVnf Process*** ", isDebugEnabled)
	}

}