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 com.att.bpm.scripts
23 import static org.apache.commons.lang3.StringUtils.*;
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.*
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
35 import org.springframework.web.util.UriUtils
38 * TODO: Support Putting vnf type = vpe and vce
40 * This class supports the GenericPutVnf Sub Flow.
41 * This Generic sub flow can be used by any flow for accomplishing
42 * the goal of Creating/Updating(PUT) a Vnf Object (in AAI). The flow
43 * supports the Creating/Updating of 3 types of Vnfs (generic-vnf, vce, and vpe).
44 * The "type" must be provided by the calling flow and this type should
45 * be mapped to the variable GENPV_type. The type should either be
46 * "generic-vnf", "vce", or "vpe". In addition, the Vnf Id and
47 * payload should be provided.
49 * Upon successful completion of this sub flow the
50 * GENPV_SuccessIndicator. An MSOWorkflowException will
51 * be thrown if an error occurs at any time during this
52 * sub flow. Please map input variables to the corresponding
53 * variable names below.
56 * Incoming Required Variables:
57 * @param - GENPV_vnfId
58 * @param - GENPV_vnfPayload
63 * @param - GENPV_SuccessIndicator
64 * @param - WorkflowException
66 class GenericPutVnf extends AbstractServiceTaskProcessor{
68 String Prefix = "GENPV_"
69 ExceptionUtil exceptionUtil = new ExceptionUtil()
72 * This method validates the incoming variables and
73 * generates a Vnf Id if one is not provided.
77 public void preProcessRequest(Execution execution) {
78 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
79 execution.setVariable("prefix",Prefix)
80 utils.log("DEBUG", " *** STARTED GenericPutVnf PreProcessRequest Process*** ", isDebugEnabled)
82 execution.setVariable("GENPV_SuccessIndicator", false)
83 execution.setVariable("GENPV_FoundIndicator", false)
87 String payload = execution.getVariable("GENPV_vnfPayload")
88 utils.log("DEBUG", "Incoming Vnf Payload is: " + payload, isDebugEnabled)
89 String type = execution.getVariable("GENPV_type")
90 utils.log("DEBUG", "Incoming Type of Vnf is: " + type, isDebugEnabled)
92 if(isBlank(payload) || isBlank(type)){
93 utils.log("ERROR", "Incoming Vnf Payload and/or Type is null. These Variables are required!", isDebugEnabled)
94 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Payload and/or Type is null. These Variables are required!")
96 String vnfId = execution.getVariable("GENPV_vnfId")
98 vnfId = UUID.randomUUID().toString()
99 utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
100 execution.setVariable("GENPV_vnfId", vnfId)
102 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
106 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
109 utils.log("ERROR", " Error encountered within GenericPutVnf PreProcessRequest method!" + e, isDebugEnabled)
110 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutVnf PreProcessRequest")
113 utils.log("DEBUG", "*** COMPLETED GenericPutVnf PreProcessRequest Process ***", isDebugEnabled)
117 * This method executes a Put call to AAI to create
118 * or update a Vnf Object using the provided payload
122 public void putVnf(Execution execution){
123 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
124 execution.setVariable("prefix",Prefix)
125 utils.log("DEBUG", " *** STARTED GenericPutVnf PutVnf Process*** ", isDebugEnabled)
127 String vnfId = execution.getVariable("GENPV_vnfId")
128 String payload = execution.getVariable("GENPV_vnfPayload")
129 String type = execution.getVariable("GENPV_type")
131 AaiUtil aaiUtil = new AaiUtil(this)
133 if(type.equals("generic-vnf")){
134 aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
135 }else if(type.equals("vce")){
136 aai_uri = aaiUtil.getNetworkVceUri(execution)
137 }else if(type.equals("vpe")){
138 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericPutVnf does not yet support getting type of vnf = vpe")
140 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
141 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENPV_type")
143 utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)
145 String path = "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")
146 utils.log("DEBUG", "PUT Vnf Endpoint is: " + path, isDebugEnabled)
148 String putVnfAAIPath = execution.getVariable("URN_aai_endpoint") + path
149 execution.setVariable("GENPV_putVnfAAIPath", putVnfAAIPath)
150 utils.logAudit("PUT Vnf Url is: " + putVnfAAIPath)
152 APIResponse apiResponse = aaiUtil.executeAAIPutCall(execution, putVnfAAIPath, payload)
153 int responseCode = apiResponse.getStatusCode()
154 execution.setVariable("GENPV_putVnfResponseCode", responseCode)
155 utils.logAudit("AAI Response Code: " + responseCode)
156 String aaiResponse = apiResponse.getResponseBodyAsString()
157 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
158 execution.setVariable("GENPV_putVnfResponse", aaiResponse)
159 utils.logAudit("AAI Response: " + aaiResponse)
161 if(responseCode == 200 || responseCode == 201){
162 utils.log("DEBUG", "PUT Vnf Received a Good Response Code.", isDebugEnabled)
164 utils.log("DEBUG", "PUT Vnf Received a Bad Response Code. Response Code is: " + responseCode, isDebugEnabled)
165 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
166 throw new BpmnError("MSOWorkflowException")
169 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
172 utils.log("ERROR", " Error encountered within GenericPutVnf PutVnf method!" + e, isDebugEnabled)
173 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During PutVnf")
175 utils.log("DEBUG", " *** COMPLETED GenericPutVnf PutVnf Process*** ", isDebugEnabled)