[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericPutService.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.mso.bpmn.common.scripts
21
22 import static org.apache.commons.lang3.StringUtils.*;
23
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.*
28
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
34
35 import java.net.URLEncoder;
36 import org.springframework.web.util.UriUtils
37
38 /**
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
48  * below.
49  *
50  *
51  * Incoming Required Variables:
52  * @param - GENPS_type - Required field. This will be required field populated as service-instance or service-subscription
53  * @param - GENPS_globalSubscriberId - Required field
54  * @param - GENPS_serviceType - Required Field
55  * @param - GENPS_payload - Required Field This will be the payload that needs to be sent.
56  *
57  * @param - GENPS_serviceInstanceId - Conditional Field. Required for service-instance.
58  * @param - GENPS_allottedResourceId - Conditional Field. Required for allotted-resource.
59  * @param - GENPS_tunnelXconnectId - Conditional Field. Required for tunnel-xconnect.
60  *
61  * @param - GENPS_requestId to trace the request id
62  * @param - GENPS_serviceResourceVersion optional 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.
65  *
66  * Outgoing Variables:
67  * @param - GENPS_SuccessIndicator
68  * @param - WorkflowException
69  *
70  *
71  */
72
73
74 class GenericPutService extends AbstractServiceTaskProcessor{
75
76         String Prefix = "GENPS_"
77         ExceptionUtil exceptionUtil = new ExceptionUtil()
78
79
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)
84
85                 execution.setVariable("GENPS_SuccessIndicator", false)
86
87                 try{
88                         // Get Variables
89                         String requestId = execution.getVariable("GENPS_requestId")
90                         utils.log("DEBUG", "Incoming GENPS_requestId is: " + requestId, isDebugEnabled)
91
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")
98
99                         if(type != null){
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!")
105                                         }else{
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)
109                                         }
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.")
114                                         }else{
115                                                 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
116                                                 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
117                                         }
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!")
126                                         }else{
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)
131                                         }
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!")
141                                         }else{
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)
147                                         }
148                                 }else{
149                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
150                                 }
151                         }else{
152                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENPS_type is null. Variable is Required.")
153                         }
154
155                 }catch(BpmnError b){
156                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
157                         throw b
158                 }catch(Exception e){
159                         utils.log("ERROR", " Error encountered within GenericPutService PreProcessRequest method!" + e, isDebugEnabled)
160                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutService PreProcessRequest")
161
162                 }
163                 utils.log("DEBUG", "*** COMPLETED GenericPutService PreProcessRequest Process ***", isDebugEnabled)
164
165         }
166
167
168
169         /**
170          * This method executes a Put call to AAI for the provided
171          * service instance.
172          *
173          * @param - execution
174          *
175          */
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)
180                 try {
181                         String type = execution.getVariable("GENPS_type")
182
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(aai_uri)
187                         logDebug('AAI namespace is: ' + namespace, isDebugEnabled)
188
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)
195
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)
200
201                         // This IF clause is if we need to create a new Service Instance
202                         if(type.equalsIgnoreCase("service-instance")){
203
204                                 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
205                                 utils.log("DEBUG", " Incoming GENPS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
206
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")
209
210                         }else if(type.equalsIgnoreCase("service-subscription")){
211
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")){
215
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)
220
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")){
224
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)
231
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")
234                         }
235
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")
240                         }
241
242                         execution.setVariable("GENPS_putServiceInstanceAaiPath", serviceAaiPath)
243                         utils.log("DEBUG", "PUT Service Instance AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)
244
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)
249
250                         String aaiResponse = response.getResponseBodyAsString()
251                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
252                         execution.setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
253
254
255                         //Process Response
256                         if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
257                                 //200 OK 201 CREATED 202 ACCEPTED
258                         {
259                                 utils.log("DEBUG", "PUT Service Instance Received a Good Response", isDebugEnabled)
260                                 execution.setVariable("GENPS_SuccessIndicator", true)
261                         }
262
263                         else{
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")
267                         }
268                 }catch(BpmnError b){
269                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
270                         throw b
271                 }catch(Exception e){
272                         utils.log("ERROR", " Error encountered within GenericPutService PutServiceInstance method!" + e, isDebugEnabled)
273                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Put Service Instance")
274                 }
275                 utils.log("DEBUG", " *** COMPLETED GenericPutService PutServiceInstance Process*** ", isDebugEnabled)
276         }
277
278
279
280 }