2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.bpmn.common.scripts
23 import static org.apache.commons.lang3.StringUtils.*;
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.apache.commons.codec.binary.Base64
28 import org.apache.commons.lang3.*
30 import org.openecomp.mso.bpmn.core.json.JsonUtils
31 import org.openecomp.mso.bpmn.core.WorkflowException
32 import org.openecomp.mso.rest.APIResponse;
33 import org.openecomp.mso.rest.RESTClient
34 import org.openecomp.mso.rest.RESTConfig
36 import java.net.URLEncoder;
37 import org.springframework.web.util.UriUtils
40 * This class supports the GenericPutService Sub Flow.
41 * This Generic sub flow can be used by any flow for the
42 * goal of creating a Service Instance or Service-Subscription in AAI. Upon successful completion of
43 * this sub flow the GENPS_SuccessIndicator will be true.
44 * The calling flow must set the GENPS_type variable as "service-instance"
45 * or "service-subscription".
46 * A MSOWorkflowException will be thrown Upon unsuccessful
47 * completion or if an error occurs within this flow.
48 * Please map variables to the corresponding variable names
52 * Incoming Required Variables:
53 * @param - GENPS_requestId
54 * @param - GENPS_type - Required field. This will be required field populated as service-instance or service-subscription
55 * @param - GENPS_globalSubscriberId - Required field
56 * @param - GENPS_serviceType - Required Field
57 * @param - GENPS_payload - Required Field This will be the payload that needs to be sent.
59 * @param - GENPS_serviceInstanceId - Conditional Field. Required for service-instance.
60 * @param - GENPS_allottedResourceId - Conditional Field. Required for allotted-resource.
61 * @param - GENPS_tunnelXconnectId - Conditional Field. Required for tunnel-xconnect.
63 * @param - GENPS_serviceResourceVersion - Conditional Field. Needs to be provided only in case of update for both service-instance and service subscription. The calling flows
64 * should check if a service-instance or servic-subscription exists by calling the subflow GenericGetService. if it exists then resourceversion should be
65 * obtained from aai and sent as an input parameter.
68 * @param - GENPS_SuccessIndicator
69 * @param - WorkflowException
75 class CustomE2EPutService extends AbstractServiceTaskProcessor{
77 String Prefix = "GENPS_"
78 ExceptionUtil exceptionUtil = new ExceptionUtil()
81 public void preProcessRequest(DelegateExecution execution) {
82 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
83 execution.setVariable("prefix",Prefix)
84 utils.log("DEBUG", " *** STARTED GenericPutService PreProcessRequest Process*** ", isDebugEnabled)
86 execution.setVariable("GENPS_SuccessIndicator", false)
90 String requestId = execution.getVariable("GENPS_requestId")
91 utils.log("DEBUG", "Incoming GENPS_requestId is: " + requestId, isDebugEnabled)
93 String globalSubscriberId = execution.getVariable("GENPS_globalSubscriberId")
94 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
95 String serviceType = execution.getVariable("GENPS_serviceType")
96 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
97 String tunnelXconnectId = execution.getVariable("GENPS_tunnelXconnectId")
98 String type = execution.getVariable("GENPS_type")
101 utils.log("DEBUG", "Incoming GENPS_type is: " + type, isDebugEnabled)
102 if(type.equalsIgnoreCase("service-instance")){
103 if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId)){
104 utils.log("DEBUG", "Incoming Required Variable is missing or null!", isDebugEnabled)
105 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
107 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
108 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
109 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
111 }else if(type.equalsIgnoreCase("service-subscription")){
112 if(isBlank(serviceType) || isBlank(globalSubscriberId)){
113 utils.log("DEBUG", "Incoming ServiceType or GlobalSubscriberId is null. These variables are required to create a service-subscription.", isDebugEnabled)
114 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
116 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
117 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
119 }else if(type.equalsIgnoreCase("allotted-resource")){
120 if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId) || isBlank(allottedResourceId)){
121 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
122 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
123 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
124 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
125 utils.log("DEBUG", "Incoming Required Variable is missing or null!", isDebugEnabled)
126 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
128 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
129 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
130 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
131 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
133 }else if(type.equalsIgnoreCase("tunnel-xconnect")){
134 if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId) || isBlank(allottedResourceId) || isBlank(tunnelXconnectId)){
135 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
136 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
137 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
138 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
139 utils.log("DEBUG", "Incoming Tunnel Xconnect Id is: " + tunnelXconnectId, isDebugEnabled)
140 utils.log("DEBUG", "Incoming Required Variable is missing or null!", isDebugEnabled)
141 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
143 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
144 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
145 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
146 utils.log("DEBUG", "Incoming Allotted Resource Id is: " + allottedResourceId, isDebugEnabled)
147 utils.log("DEBUG", "Incoming Tunnel Xconnect Id is: " + tunnelXconnectId, isDebugEnabled)
150 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
153 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENPS_type is null. Variable is Required.")
157 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
160 utils.log("ERROR", " Error encountered within GenericPutService PreProcessRequest method!" + e, isDebugEnabled)
161 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutService PreProcessRequest")
164 utils.log("DEBUG", "*** COMPLETED GenericPutService PreProcessRequest Process ***", isDebugEnabled)
171 * This method executes a Put call to AAI for the provided
177 public void putServiceInstance(DelegateExecution execution){
178 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
179 execution.setVariable("prefix",Prefix)
180 utils.log("DEBUG", " *** STARTED GenericPutService PutServiceInstance method*** ", isDebugEnabled)
182 String type = execution.getVariable("GENPS_type")
184 AaiUtil aaiUriUtil = new AaiUtil(this)
185 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
186 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
187 String namespace = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
188 logDebug('AAI namespace is: ' + namespace, isDebugEnabled)
190 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
191 String serviceAaiPath = ""
192 String payload = execution.getVariable("GENPS_payload")
193 execution.setVariable("GENPS_payload", payload)
194 utils.log("DEBUG", "Incoming GENPS_payload is: " + payload, isDebugEnabled)
195 utils.logAudit(payload)
197 String serviceType = execution.getVariable("GENPS_serviceType")
198 utils.log("DEBUG", " Incoming GENPS_serviceType is: " + serviceType, isDebugEnabled)
199 String globalSubscriberId = execution.getVariable("GENPS_globalSubscriberId")
200 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
202 // This IF clause is if we need to create a new Service Instance
203 if(type.equalsIgnoreCase("service-instance")){
205 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
206 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
208 // 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")
209 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")
211 }else if(type.equalsIgnoreCase("service-subscription")){
213 // serviceAaiPath = "${aai_endpoint}${aai_uri}/" + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
214 serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
215 }else if(type.equalsIgnoreCase("allotted-resource")){
217 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
218 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
219 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
220 utils.log("DEBUG", " Incoming GENPS_allottedResourceId is: " + allottedResourceId, isDebugEnabled)
222 // serviceAaiPath = "${aai_endpoint}${aai_uri}/" + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
223 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")
224 }else if(type.equalsIgnoreCase("tunnel-xconnect")){
226 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
227 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
228 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
229 utils.log("DEBUG", " Incoming GENPS_allottedResourceId is: " + allottedResourceId, isDebugEnabled)
230 String tunnelXconnectId = execution.getVariable("GENPS_tunnelXconnectId")
231 utils.log("DEBUG", " Incoming GENPS_tunnelXconnectId is: " + tunnelXconnectId, isDebugEnabled)
233 // serviceAaiPath = "${aai_endpoint}${aai_uri}/" + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
234 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")
237 String resourceVersion = execution.getVariable("GENPS_serviceResourceVersion")
238 utils.log("DEBUG", "Incoming Resource Version is: " + resourceVersion, isDebugEnabled)
239 if(resourceVersion !=null){
240 serviceAaiPath = serviceAaiPath +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")
243 execution.setVariable("GENPS_putServiceInstanceAaiPath", serviceAaiPath)
244 utils.log("DEBUG", "PUT Service Instance AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)
246 APIResponse response = aaiUriUtil.executeAAIPutCall(execution, serviceAaiPath, payload)
247 int responseCode = response.getStatusCode()
248 execution.setVariable("GENPS_putServiceInstanceResponseCode", responseCode)
249 utils.log("DEBUG", " Put Service Instance response code is: " + responseCode, isDebugEnabled)
251 String aaiResponse = response.getResponseBodyAsString()
252 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
253 execution.setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
257 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
258 //200 OK 201 CREATED 202 ACCEPTED
260 utils.log("DEBUG", "PUT Service Instance Received a Good Response", isDebugEnabled)
261 execution.setVariable("GENPS_SuccessIndicator", true)
265 utils.log("DEBUG", "Put Generic Service Instance Received a Bad Response Code. Response Code is: " + responseCode, isDebugEnabled)
266 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
267 throw new BpmnError("MSOWorkflowException")
270 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
273 utils.log("ERROR", " Error encountered within GenericPutService PutServiceInstance method!" + e, isDebugEnabled)
274 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Put Service Instance")
276 utils.log("DEBUG", " *** COMPLETED GenericPutService PutServiceInstance Process*** ", isDebugEnabled)