Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / 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
22 package org.onap.so.bpmn.common.scripts
23
24 import static org.apache.commons.lang3.StringUtils.*;
25
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.apache.commons.codec.binary.Base64
29 import org.apache.commons.lang3.*
30
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.rest.APIResponse;
34 import org.onap.so.rest.RESTClient
35 import org.onap.so.rest.RESTConfig
36
37 import java.net.URLEncoder;
38 import org.springframework.web.util.UriUtils
39 import org.onap.so.bpmn.core.UrnPropertiesReader
40 import org.onap.so.logger.MessageEnum
41 import org.onap.so.logger.MsoLogger
42
43 /**
44  * This class supports the GenericPutService Sub Flow.
45  * This Generic sub flow can be used by any flow for the
46  * goal of creating a Service Instance or Service-Subscription in AAI. Upon successful completion of
47  * this sub flow the GENPS_SuccessIndicator will be true.
48  * The calling flow must set the GENPS_type variable as "service-instance"
49  * or "service-subscription".
50  *  A MSOWorkflowException will be thrown Upon unsuccessful
51  * completion or if an error occurs within this flow.
52  * Please map variables to the corresponding variable names
53  * below.
54  *
55  *
56  * Incoming Required Variables:
57  * @param - GENPS_requestId
58  * @param - GENPS_type - Required field. This will be required field populated as service-instance or service-subscription
59  * @param - GENPS_globalSubscriberId - Required field
60  * @param - GENPS_serviceType - Required Field
61  * @param - GENPS_payload - Required Field This will be the payload that needs to be sent.
62  *
63  * @param - GENPS_serviceInstanceId - Conditional Field. Required for service-instance.
64  * @param - GENPS_allottedResourceId - Conditional Field. Required for allotted-resource.
65  * @param - GENPS_tunnelXconnectId - Conditional Field. Required for tunnel-xconnect.
66  *
67  * @param - GENPS_serviceResourceVersion - Conditional Field. Needs to be provided only in case of update for both service-instance and service subscription. The calling flows
68  *          should check if a service-instance or servic-subscription exists by calling the subflow GenericGetService. if it exists then resourceversion should be
69  *          obtained from aai and sent as an input parameter.
70  *
71  * Outgoing Variables:
72  * @param - GENPS_SuccessIndicator
73  * @param - WorkflowException
74  *
75  *
76  */
77
78
79 class CustomE2EPutService extends AbstractServiceTaskProcessor{
80         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CustomE2EPutService.class);
81
82         String Prefix = "GENPS_"
83         ExceptionUtil exceptionUtil = new ExceptionUtil()
84
85
86         public void preProcessRequest(DelegateExecution execution) {
87                 execution.setVariable("prefix",Prefix)
88                 msoLogger.trace("STARTED GenericPutService PreProcessRequest Process")
89
90                 execution.setVariable("GENPS_SuccessIndicator", false)
91
92                 try{
93                         // Get Variables
94                         String requestId = execution.getVariable("GENPS_requestId")
95                         msoLogger.debug("Incoming GENPS_requestId is: " + requestId)
96
97                         String globalSubscriberId = execution.getVariable("GENPS_globalSubscriberId")
98                         String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
99                         String serviceType = execution.getVariable("GENPS_serviceType")
100                         String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
101                         String tunnelXconnectId = execution.getVariable("GENPS_tunnelXconnectId")
102                         String type = execution.getVariable("GENPS_type")
103
104                         if(type != null){
105                                 msoLogger.debug("Incoming GENPS_type is: " + type)
106                                 if(type.equalsIgnoreCase("service-instance")){
107                                         if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId)){
108                                                 msoLogger.debug("Incoming Required Variable is missing or null!")
109                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
110                                         }else{
111                                                 msoLogger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
112                                                 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
113                                                 msoLogger.debug("Incoming Service Type is: " + serviceType)
114                                         }
115                                 }else if(type.equalsIgnoreCase("service-subscription")){
116                                         if(isBlank(serviceType) || isBlank(globalSubscriberId)){
117                                                 msoLogger.debug("Incoming ServiceType or GlobalSubscriberId is null. These variables are required to create a service-subscription.")
118                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
119                                         }else{
120                                                 msoLogger.debug("Incoming Service Type is: " + serviceType)
121                                                 msoLogger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
122                                         }
123                                 }else if(type.equalsIgnoreCase("allotted-resource")){
124                                         if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId) || isBlank(allottedResourceId)){
125                                                 msoLogger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
126                                                 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
127                                                 msoLogger.debug("Incoming Service Type is: " + serviceType)
128                                                 msoLogger.debug("Incoming Allotted Resource Id is: " + allottedResourceId)
129                                                 msoLogger.debug("Incoming Required Variable is missing or null!")
130                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
131                                         }else{
132                                                 msoLogger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
133                                                 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
134                                                 msoLogger.debug("Incoming Service Type is: " + serviceType)
135                                                 msoLogger.debug("Incoming Allotted Resource Id is: " + allottedResourceId)
136                                         }
137                                 }else if(type.equalsIgnoreCase("tunnel-xconnect")){
138                                         if(isBlank(globalSubscriberId) || isBlank(serviceType) || isBlank(serviceInstanceId) || isBlank(allottedResourceId) || isBlank(tunnelXconnectId)){
139                                                 msoLogger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
140                                                 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
141                                                 msoLogger.debug("Incoming Service Type is: " + serviceType)
142                                                 msoLogger.debug("Incoming Allotted Resource Id is: " + allottedResourceId)
143                                                 msoLogger.debug("Incoming Tunnel Xconnect Id is: " + tunnelXconnectId)
144                                                 msoLogger.debug("Incoming Required Variable is missing or null!")
145                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
146                                         }else{
147                                                 msoLogger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
148                                                 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
149                                                 msoLogger.debug("Incoming Service Type is: " + serviceType)
150                                                 msoLogger.debug("Incoming Allotted Resource Id is: " + allottedResourceId)
151                                                 msoLogger.debug("Incoming Tunnel Xconnect Id is: " + tunnelXconnectId)
152                                         }
153                                 }else{
154                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
155                                 }
156                         }else{
157                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENPS_type is null. Variable is Required.")
158                         }
159
160                 }catch(BpmnError b){
161                         msoLogger.debug("Rethrowing MSOWorkflowException")
162                         throw b
163                 }catch(Exception e){
164                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericPutService PreProcessRequest method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
165                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutService PreProcessRequest")
166
167                 }
168                 msoLogger.trace("COMPLETED GenericPutService PreProcessRequest Process ")
169
170         }
171
172
173
174         /**
175          * This method executes a Put call to AAI for the provided
176          * service instance.
177          *
178          * @param - execution
179          *
180          */
181         public void putServiceInstance(DelegateExecution execution){
182                 execution.setVariable("prefix",Prefix)
183                 msoLogger.trace("STARTED GenericPutService PutServiceInstance method")
184                 try {
185                         String type = execution.getVariable("GENPS_type")
186
187                         AaiUtil aaiUriUtil = new AaiUtil(this)
188                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
189                         msoLogger.debug('AAI URI is: ' + aai_uri)
190                         String namespace = aaiUriUtil.getNamespaceFromUri(execution, aai_uri)
191                         msoLogger.debug('AAI namespace is: ' + namespace)
192
193                         String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
194                         String serviceAaiPath = ""
195                         String payload = execution.getVariable("GENPS_payload")
196                         execution.setVariable("GENPS_payload", payload)
197                         msoLogger.debug("Incoming GENPS_payload is: " + payload)
198                         msoLogger.debug(payload)
199
200                         String serviceType = execution.getVariable("GENPS_serviceType")
201                         msoLogger.debug(" Incoming GENPS_serviceType is: " + serviceType)
202                         String globalSubscriberId = execution.getVariable("GENPS_globalSubscriberId")
203                         msoLogger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
204
205                         // This IF clause is if we need to create a new Service Instance
206                         if(type.equalsIgnoreCase("service-instance")){
207
208                                 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
209                                 msoLogger.debug(" Incoming GENPS_serviceInstanceId is: " + serviceInstanceId)
210
211                                 //      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")
212                                 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")
213
214                         }else if(type.equalsIgnoreCase("service-subscription")){
215
216                                 //      serviceAaiPath = "${aai_endpoint}${aai_uri}/"  + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
217                                 serviceAaiPath = "${aai_endpoint}${aai_uri}/"  + UriUtils.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
218                         }else if(type.equalsIgnoreCase("allotted-resource")){
219
220                                 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
221                                 msoLogger.debug(" Incoming GENPS_serviceInstanceId is: " + serviceInstanceId)
222                                 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
223                                 msoLogger.debug(" Incoming GENPS_allottedResourceId is: " + allottedResourceId)
224
225                                 //      serviceAaiPath = "${aai_endpoint}${aai_uri}/"  + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
226                                 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")
227                         }else if(type.equalsIgnoreCase("tunnel-xconnect")){
228
229                                 String serviceInstanceId = execution.getVariable("GENPS_serviceInstanceId")
230                                 msoLogger.debug(" Incoming GENPS_serviceInstanceId is: " + serviceInstanceId)
231                                 String allottedResourceId = execution.getVariable("GENPS_allottedResourceId")
232                                 msoLogger.debug(" Incoming GENPS_allottedResourceId is: " + allottedResourceId)
233                                 String tunnelXconnectId = execution.getVariable("GENPS_tunnelXconnectId")
234                                 msoLogger.debug(" Incoming GENPS_tunnelXconnectId is: " + tunnelXconnectId)
235
236                                 //      serviceAaiPath = "${aai_endpoint}${aai_uri}/"  + URLEncoder.encode(globalSubscriberId,"UTF-8") + "/service-subscriptions/service-subscription/" + URLEncoder.encode(serviceType,"UTF-8")
237                                 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")
238                         }
239
240                         String resourceVersion = execution.getVariable("GENPS_serviceResourceVersion")
241                         msoLogger.debug("Incoming Resource Version is: " + resourceVersion)
242                         if(resourceVersion !=null){
243                                 serviceAaiPath = serviceAaiPath +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")
244                         }
245
246                         execution.setVariable("GENPS_putServiceInstanceAaiPath", serviceAaiPath)
247                         msoLogger.debug("PUT Service Instance AAI Path is: " + "\n" + serviceAaiPath)
248
249                         APIResponse response = aaiUriUtil.executeAAIPutCall(execution, serviceAaiPath, payload)
250                         int responseCode = response.getStatusCode()
251                         execution.setVariable("GENPS_putServiceInstanceResponseCode", responseCode)
252                         msoLogger.debug("  Put Service Instance response code is: " + responseCode)
253
254                         String aaiResponse = response.getResponseBodyAsString()
255                         execution.setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
256
257
258                         //Process Response
259                         if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
260                                 //200 OK 201 CREATED 202 ACCEPTED
261                         {
262                                 msoLogger.debug("PUT Service Instance Received a Good Response")
263                                 execution.setVariable("GENPS_SuccessIndicator", true)
264                         }
265
266                         else{
267                                 msoLogger.debug("Put Generic Service Instance Received a Bad Response Code. Response Code is: " + responseCode)
268                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
269                                 throw new BpmnError("MSOWorkflowException")
270                         }
271                 }catch(BpmnError b){
272                         msoLogger.debug("Rethrowing MSOWorkflowException")
273                         throw b
274                 }catch(Exception e){
275                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericPutService PutServiceInstance method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
276                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Put Service Instance")
277                 }
278                 msoLogger.trace("COMPLETED GenericPutService PutServiceInstance Process")
279         }
280
281
282
283 }