Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericPutService.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.delegate.DelegateExecution;
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_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.
57  *
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.
61  *
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.
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(DelegateExecution execution) {
81                 execution.setVariable("isDebugLogEnabled","true")
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          * This method executes a Put call to AAI for the provided
170          * service instance.
171          *
172          * @param - execution
173          *
174          */
175         public void putServiceInstance(DelegateExecution execution){
176                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")           
177                 execution.setVariable("prefix",Prefix)
178                 utils.log("DEBUG", " *** STARTED GenericPutService PutServiceInstance method*** ", isDebugEnabled)
179                 try {
180                         String type = execution.getVariable("GENPS_type")
181
182                         AaiUtil aaiUriUtil = new AaiUtil(this)
183                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
184                         logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
185                         String namespace = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
186                         logDebug('AAI namespace is: ' + namespace, isDebugEnabled)
187
188                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
189                         String serviceAaiPath = ""
190                         String payload = execution.getVariable("GENPS_payload")
191                         execution.setVariable("GENPS_payload", payload)
192                         utils.log("DEBUG", "Incoming GENPS_payload is: " + payload, isDebugEnabled)
193                         utils.logAudit(payload)
194
195                         String serviceType = execution.getVariable("GENPS_serviceType")
196                         utils.log("DEBUG", " Incoming GENPS_serviceType is: " + serviceType, isDebugEnabled)
197                         
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                         APIResponse response = aaiUriUtil.executeAAIPutCall(execution, serviceAaiPath, payload)
245                         int responseCode = response.getStatusCode()
246                         execution.setVariable("GENPS_putServiceInstanceResponseCode", responseCode)
247                         utils.log("DEBUG", "  Put Service Instance response code is: " + responseCode, isDebugEnabled)
248
249                         String aaiResponse = response.getResponseBodyAsString()
250                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
251                         execution.setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
252
253                         //Process Response
254                         if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
255                                 //200 OK 201 CREATED 202 ACCEPTED
256                         {
257                                 utils.log("DEBUG", "PUT Service Instance Received a Good Response", isDebugEnabled)
258                                 execution.setVariable("GENPS_SuccessIndicator", true)
259                         }
260
261                         else{
262                                 utils.log("DEBUG", "Put Generic Service Instance Received a Bad Response Code. Response Code is: " + responseCode, isDebugEnabled)
263                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
264                                 throw new BpmnError("MSOWorkflowException")
265                         }
266                 }catch(BpmnError b){
267                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
268                         throw b
269                 }catch(Exception e){
270                         utils.log("ERROR", " Error encountered within GenericPutService PutServiceInstance method!" + e, isDebugEnabled)
271                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Put Service Instance")
272                 }
273                 utils.log("DEBUG", " *** COMPLETED GenericPutService PutServiceInstance Process*** ", isDebugEnabled)
274         }
275
276 }