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