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=========================================================
20 package org.openecomp.mso.bpmn.common.scripts
22 import static org.apache.commons.lang3.StringUtils.*;
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.runtime.Execution;
26 import org.apache.commons.codec.binary.Base64
27 import org.apache.commons.lang3.*
29 import org.openecomp.mso.bpmn.core.json.JsonUtils
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 java.net.URLEncoder;
36 import org.springframework.web.util.UriUtils
39 * This class supports the GenericPutService Sub Flow.
40 * This Generic sub flow can be used by any flow for the
41 * goal of creating a Service Instance or Service-Subscription in AAI. Upon successful completion of
42 * this sub flow the GENPS_SuccessIndicator will be true.
43 * The calling flow must set the GENPS_type variable as "service-instance"
44 * or "service-subscription".
45 * A MSOWorkflowException will be thrown Upon unsuccessful
46 * completion or if an error occurs within this flow.
47 * Please map variables to the corresponding variable names
51 * Incoming Required Variables:
52 * @param - GENPS_requestId
53 * @param - GENPS_type - Required field. This will be required field populated as service-instance or service-subscription
54 * @param - GENPS_globalSubscriberId - Required field
55 * @param - GENPS_serviceType - Required Field
56 * @param - GENPS_payload - Required Field This will be the payload that needs to be sent.
58 * @param - GENPS_serviceInstanceId - Conditional Field. Required for service-instance.
59 * @param - GENPS_allottedResourceId - Conditional Field. Required for allotted-resource.
60 * @param - GENPS_tunnelXconnectId - Conditional Field. Required for tunnel-xconnect.
62 * @param - GENPS_serviceResourceVersion - Conditional Field. Needs to be provided only in case of update for both service-instance and service subscription. The calling flows
63 * should check if a service-instance or servic-subscription exists by calling the subflow GenericGetService. if it exists then resourceversion should be
64 * obtained from aai and sent as an input parameter.
67 * @param - GENPS_SuccessIndicator
68 * @param - WorkflowException
74 class CustomE2EPutService extends AbstractServiceTaskProcessor{
76 String Prefix = "GENPS_"
77 ExceptionUtil exceptionUtil = new ExceptionUtil()
80 public void preProcessRequest(Execution execution) {
81 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
82 execution.setVariable("prefix",Prefix)
83 utils.log("DEBUG", " *** STARTED GenericPutService PreProcessRequest Process*** ", isDebugEnabled)
85 execution.setVariable("GENPS_SuccessIndicator", false)
89 String requestId = execution.getVariable("GENPS_requestId")
90 utils.log("DEBUG", "Incoming GENPS_requestId is: " + requestId, isDebugEnabled)
92 String globalSubscriberId = execution.getVariable("GENPS_globalSubscriberId")
93 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
94 String serviceType = execution.getVariable("GENPS_serviceType")
95 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
96 String tunnelXconnectId = execution.getVariable("GENPS_tunnelXconnectId")
97 String type = execution.getVariable("GENPS_type")
100 utils.log("DEBUG", "Incoming GENPS_type is: " + type, isDebugEnabled)
101 if(type.equalsIgnoreCase("service-instance")){
102 if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId)){
103 utils.log("DEBUG", "Incoming Required Variable is missing or null!", isDebugEnabled)
104 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
106 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
107 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
108 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
110 }else if(type.equalsIgnoreCase("service-subscription")){
111 if(isBlank(serviceType) || isBlank(globalSubscriberId)){
112 utils.log("DEBUG", "Incoming ServiceType or GlobalSubscriberId is null. These variables are required to create a service-subscription.", isDebugEnabled)
113 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
115 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
116 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
118 }else if(type.equalsIgnoreCase("allotted-resource")){
119 if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId) || isBlank(allottedResourceId)){
120 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
121 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
122 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
123 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
124 utils.log("DEBUG", "Incoming Required Variable is missing or null!", isDebugEnabled)
125 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
127 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
128 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
129 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
130 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
132 }else if(type.equalsIgnoreCase("tunnel-xconnect")){
133 if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId) || isBlank(allottedResourceId) || isBlank(tunnelXconnectId)){
134 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
135 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
136 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
137 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
138 utils.log("DEBUG", "Incoming Tunnel Xconnect Id is: " + tunnelXconnectId, isDebugEnabled)
139 utils.log("DEBUG", "Incoming Required Variable is missing or null!", isDebugEnabled)
140 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
142 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
143 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
144 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
145 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
146 utils.log("DEBUG", "Incoming Tunnel Xconnect Id is: " + tunnelXconnectId, isDebugEnabled)
149 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
152 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENPS_type is null. Variable is Required.")
156 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
159 utils.log("ERROR", " Error encountered within GenericPutService PreProcessRequest method!" + e, isDebugEnabled)
160 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutService PreProcessRequest")
163 utils.log("DEBUG", "*** COMPLETED GenericPutService PreProcessRequest Process ***", isDebugEnabled)
170 * This method executes a Put call to AAI for the provided
176 public void putServiceInstance(Execution execution){
177 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
178 execution.setVariable("prefix",Prefix)
179 utils.log("DEBUG", " *** STARTED GenericPutService PutServiceInstance method*** ", isDebugEnabled)
181 String type = execution.getVariable("GENPS_type")
183 AaiUtil aaiUriUtil = new AaiUtil(this)
184 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
185 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
186 String namespace = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
187 logDebug('AAI namespace is: ' + namespace, isDebugEnabled)
189 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
190 String serviceAaiPath = ""
191 String payload = execution.getVariable("GENPS_payload")
192 execution.setVariable("GENPS_payload", payload)
193 utils.log("DEBUG", "Incoming GENPS_payload is: " + payload, isDebugEnabled)
194 utils.logAudit(payload)
196 String serviceType = execution.getVariable("GENPS_serviceType")
197 utils.log("DEBUG", " Incoming GENPS_serviceType is: " + serviceType, isDebugEnabled)
198 String globalSubscriberId = execution.getVariable("GENPS_globalSubscriberId")
199 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
201 // This IF clause is if we need to create a new Service Instance
202 if(type.equalsIgnoreCase("service-instance")){
204 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
205 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
207 // serviceAaiPath = "${aai_endpoint}${aai_uri}/" + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + URLEncoder.encode(serviceInstanceId,"UTF-8")
208 serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + UriUtils.encode(serviceInstanceId,"UTF-8")
210 }else if(type.equalsIgnoreCase("service-subscription")){
212 // serviceAaiPath = "${aai_endpoint}${aai_uri}/" + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
213 serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
214 }else if(type.equalsIgnoreCase("allotted-resource")){
216 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
217 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
218 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
219 utils.log("DEBUG", " Incoming GENPS_allottedResourceId is: " + allottedResourceId, isDebugEnabled)
221 // serviceAaiPath = "${aai_endpoint}${aai_uri}/" + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
222 serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + UriUtils.encode(serviceInstanceId,"UTF-8") + "/allotted-resources/allotted-resource/" + UriUtils.encode(allottedResourceId,"UTF-8")
223 }else if(type.equalsIgnoreCase("tunnel-xconnect")){
225 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
226 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
227 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
228 utils.log("DEBUG", " Incoming GENPS_allottedResourceId is: " + allottedResourceId, isDebugEnabled)
229 String tunnelXconnectId = execution.getVariable("GENPS_tunnelXconnectId")
230 utils.log("DEBUG", " Incoming GENPS_tunnelXconnectId is: " + tunnelXconnectId, isDebugEnabled)
232 // serviceAaiPath = "${aai_endpoint}${aai_uri}/" + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
233 serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + UriUtils.encode(serviceInstanceId,"UTF-8") + "/allotted-resources/allotted-resource/" + UriUtils.encode(allottedResourceId,"UTF-8") + "/tunnel-xconnects/tunnel-xconnect/" + UriUtils.encode(tunnelXconnectId,"UTF-8")
236 String resourceVersion = execution.getVariable("GENPS_serviceResourceVersion")
237 utils.log("DEBUG", "Incoming Resource Version is: " + resourceVersion, isDebugEnabled)
238 if(resourceVersion !=null){
239 serviceAaiPath = serviceAaiPath +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")
242 execution.setVariable("GENPS_putServiceInstanceAaiPath", serviceAaiPath)
243 utils.log("DEBUG", "PUT Service Instance AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)
245 APIResponse response = aaiUriUtil.executeAAIPutCall(execution, serviceAaiPath, payload)
246 int responseCode = response.getStatusCode()
247 execution.setVariable("GENPS_putServiceInstanceResponseCode", responseCode)
248 utils.log("DEBUG", " Put Service Instance response code is: " + responseCode, isDebugEnabled)
250 String aaiResponse = response.getResponseBodyAsString()
251 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
252 execution.setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
256 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
257 //200 OK 201 CREATED 202 ACCEPTED
259 utils.log("DEBUG", "PUT Service Instance Received a Good Response", isDebugEnabled)
260 execution.setVariable("GENPS_SuccessIndicator", true)
264 utils.log("DEBUG", "Put Generic Service Instance Received a Bad Response Code. Response Code is: " + responseCode, isDebugEnabled)
265 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
266 throw new BpmnError("MSOWorkflowException")
269 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
272 utils.log("ERROR", " Error encountered within GenericPutService PutServiceInstance method!" + e, isDebugEnabled)
273 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Put Service Instance")
275 utils.log("DEBUG", " *** COMPLETED GenericPutService PutServiceInstance Process*** ", isDebugEnabled)