Added Groovy for E2E svc
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / CustomE2EPutService.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.openecomp.mso.bpmn.common.scripts
22
23 import static org.apache.commons.lang3.StringUtils.*;
24
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.*
29
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
35
36 import java.net.URLEncoder;
37 import org.springframework.web.util.UriUtils
38
39 /**
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
49  * below.
50  *
51  *
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.
58  *
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.
62  *
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.
66  *
67  * Outgoing Variables:
68  * @param - GENPS_SuccessIndicator
69  * @param - WorkflowException
70  *
71  *
72  */
73
74
75 class CustomE2EPutService extends AbstractServiceTaskProcessor{
76
77         String Prefix = "GENPS_"
78         ExceptionUtil exceptionUtil = new ExceptionUtil()
79
80
81         public void preProcessRequest(Execution execution) {
82                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
83                 execution.setVariable("prefix",Prefix)
84                 utils.log("DEBUG", " *** STARTED GenericPutService PreProcessRequest Process*** ", isDebugEnabled)
85
86                 execution.setVariable("GENPS_SuccessIndicator", false)
87
88                 try{
89                         // Get Variables
90                         String requestId = execution.getVariable("GENPS_requestId")
91                         utils.log("DEBUG", "Incoming GENPS_requestId is: " + requestId, isDebugEnabled)
92
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")
99
100                         if(type != null){
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!")
106                                         }else{
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)
110                                         }
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.")
115                                         }else{
116                                                 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
117                                                 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
118                                         }
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!")
127                                         }else{
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)
132                                         }
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!")
142                                         }else{
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)
148                                         }
149                                 }else{
150                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
151                                 }
152                         }else{
153                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENPS_type is null. Variable is Required.")
154                         }
155
156                 }catch(BpmnError b){
157                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
158                         throw b
159                 }catch(Exception e){
160                         utils.log("ERROR", " Error encountered within GenericPutService PreProcessRequest method!" + e, isDebugEnabled)
161                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutService PreProcessRequest")
162
163                 }
164                 utils.log("DEBUG", "*** COMPLETED GenericPutService PreProcessRequest Process ***", isDebugEnabled)
165
166         }
167
168
169
170         /**
171          * This method executes a Put call to AAI for the provided
172          * service instance.
173          *
174          * @param - execution
175          *
176          */
177         public void putServiceInstance(Execution execution){
178                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
179                 execution.setVariable("prefix",Prefix)
180                 utils.log("DEBUG", " *** STARTED GenericPutService PutServiceInstance method*** ", isDebugEnabled)
181                 try {
182                         String type = execution.getVariable("GENPS_type")
183
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)
189
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)
196
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)
201
202                         // This IF clause is if we need to create a new Service Instance
203                         if(type.equalsIgnoreCase("service-instance")){
204
205                                 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
206                                 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
207
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")
210
211                         }else if(type.equalsIgnoreCase("service-subscription")){
212
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")){
216
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)
221
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")){
225
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)
232
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")
235                         }
236
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")
241                         }
242
243                         execution.setVariable("GENPS_putServiceInstanceAaiPath", serviceAaiPath)
244                         utils.log("DEBUG", "PUT Service Instance AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)
245
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)
250
251                         String aaiResponse = response.getResponseBodyAsString()
252                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
253                         execution.setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
254
255
256                         //Process Response
257                         if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
258                                 //200 OK 201 CREATED 202 ACCEPTED
259                         {
260                                 utils.log("DEBUG", "PUT Service Instance Received a Good Response", isDebugEnabled)
261                                 execution.setVariable("GENPS_SuccessIndicator", true)
262                         }
263
264                         else{
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")
268                         }
269                 }catch(BpmnError b){
270                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
271                         throw b
272                 }catch(Exception e){
273                         utils.log("ERROR", " Error encountered within GenericPutService PutServiceInstance method!" + e, isDebugEnabled)
274                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Put Service Instance")
275                 }
276                 utils.log("DEBUG", " *** COMPLETED GenericPutService PutServiceInstance Process*** ", isDebugEnabled)
277         }
278
279
280
281 }