2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.common.scripts
23 import static org.apache.commons.lang3.StringUtils.*
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
32 * TODO: Support Putting vnf type = vpe and vce
34 * This class supports the GenericPutVnf Sub Flow.
35 * This Generic sub flow can be used by any flow for accomplishing
36 * the goal of Creating/Updating(PUT) a Vnf Object (in AAI). The flow
37 * supports the Creating/Updating of 3 types of Vnfs (generic-vnf, vce, and vpe).
38 * The "type" must be provided by the calling flow and this type should
39 * be mapped to the variable GENPV_type. The type should either be
40 * "generic-vnf", "vce", or "vpe". In addition, the Vnf Id and
41 * payload should be provided.
43 * Upon successful completion of this sub flow the
44 * GENPV_SuccessIndicator. An MSOWorkflowException will
45 * be thrown if an error occurs at any time during this
46 * sub flow. Please map input variables to the corresponding
47 * variable names below.
50 * Incoming Required Variables:
51 * @param - GENPV_vnfId
52 * @param - GENPV_vnfPayload
57 * @param - GENPV_SuccessIndicator
58 * @param - WorkflowException
60 class GenericPutVnf extends AbstractServiceTaskProcessor{
62 String Prefix = "GENPV_"
63 ExceptionUtil exceptionUtil = new ExceptionUtil()
66 * This method validates the incoming variables and
67 * generates a Vnf Id if one is not provided.
71 public void preProcessRequest(Execution execution) {
72 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
73 execution.setVariable("prefix",Prefix)
74 utils.log("DEBUG", " *** STARTED GenericPutVnf PreProcessRequest Process*** ", isDebugEnabled)
76 execution.setVariable("GENPV_SuccessIndicator", false)
77 execution.setVariable("GENPV_FoundIndicator", false)
81 String payload = execution.getVariable("GENPV_vnfPayload")
82 utils.log("DEBUG", "Incoming Vnf Payload is: " + payload, isDebugEnabled)
83 String type = execution.getVariable("GENPV_type")
84 utils.log("DEBUG", "Incoming Type of Vnf is: " + type, isDebugEnabled)
86 if(isBlank(payload) || isBlank(type)){
87 utils.log("ERROR", "Incoming Vnf Payload and/or Type is null. These Variables are required!", isDebugEnabled)
88 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Payload and/or Type is null. These Variables are required!")
90 String vnfId = execution.getVariable("GENPV_vnfId")
92 vnfId = UUID.randomUUID().toString()
93 utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
94 execution.setVariable("GENPV_vnfId", vnfId)
96 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
100 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
103 utils.log("ERROR", " Error encountered within GenericPutVnf PreProcessRequest method!" + e, isDebugEnabled)
104 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutVnf PreProcessRequest")
107 utils.log("DEBUG", "*** COMPLETED GenericPutVnf PreProcessRequest Process ***", isDebugEnabled)
111 * This method executes a Put call to AAI to create
112 * or update a Vnf Object using the provided payload
116 public void putVnf(Execution execution){
117 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
118 execution.setVariable("prefix",Prefix)
119 utils.log("DEBUG", " *** STARTED GenericPutVnf PutVnf Process*** ", isDebugEnabled)
121 String vnfId = execution.getVariable("GENPV_vnfId")
122 String payload = execution.getVariable("GENPV_vnfPayload")
123 String type = execution.getVariable("GENPV_type")
125 AaiUtil aaiUtil = new AaiUtil(this)
127 if(type.equals("generic-vnf")){
128 aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
129 }else if(type.equals("vce")){
130 aai_uri = aaiUtil.getNetworkVceUri(execution)
131 }else if(type.equals("vpe")){
132 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericPutVnf does not yet support getting type of vnf = vpe")
134 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
135 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENPV_type")
137 utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)
139 String path = "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")
140 utils.log("DEBUG", "PUT Vnf Endpoint is: " + path, isDebugEnabled)
142 String putVnfAAIPath = execution.getVariable("URN_aai_endpoint") + path
143 execution.setVariable("GENPV_putVnfAAIPath", putVnfAAIPath)
144 utils.logAudit("PUT Vnf Url is: " + putVnfAAIPath)
146 APIResponse apiResponse = aaiUtil.executeAAIPutCall(execution, putVnfAAIPath, payload)
147 int responseCode = apiResponse.getStatusCode()
148 execution.setVariable("GENPV_putVnfResponseCode", responseCode)
149 utils.logAudit("AAI Response Code: " + responseCode)
150 String aaiResponse = apiResponse.getResponseBodyAsString()
151 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
152 execution.setVariable("GENPV_putVnfResponse", aaiResponse)
153 utils.logAudit("AAI Response: " + aaiResponse)
155 if(responseCode == 200 || responseCode == 201){
156 utils.log("DEBUG", "PUT Vnf Received a Good Response Code.", isDebugEnabled)
158 utils.log("DEBUG", "PUT Vnf Received a Bad Response Code. Response Code is: " + responseCode, isDebugEnabled)
159 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
160 throw new BpmnError("MSOWorkflowException")
163 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
166 utils.log("ERROR", " Error encountered within GenericPutVnf PutVnf method!" + e, isDebugEnabled)
167 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During PutVnf")
169 utils.log("DEBUG", " *** COMPLETED GenericPutVnf PutVnf Process*** ", isDebugEnabled)