Remove unnecessary use of Calendar.getInstance()
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericGetService.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
21 package org.openecomp.mso.bpmn.common.scripts
22
23 import static org.apache.commons.lang3.StringUtils.*
24
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.runtime.Execution
28 import org.openecomp.mso.rest.APIResponse
29 import org.springframework.web.util.UriUtils
30
31
32 /**
33  * This class supports the GenericGetService Sub Flow.
34  * This Generic sub flow can be used by any flow for accomplishing
35  * the goal of getting a Service-Instance or Service-Subscription (from AAI).
36  * The calling flow must set the GENGS_type variable as "service-instance"
37  * or "service-subscription".
38  *
39  * When using to Get a Service-Instance:
40  * If the global-customer-id and service-type are not provided
41  * this flow executes a query to get the service- Url using the
42  * Service  Id or Name (whichever is provided).
43  *
44  * When using to Get a Service-Subscription:
45  * The global-customer-id and service-type must be
46  * provided.
47  *
48  * Upon successful completion of this sub flow the
49  * GENGS_SuccessIndicator will be true and the query response payload
50  * will be set to GENGS_service.  An MSOWorkflowException will
51  * be thrown upon unsuccessful completion or if an error occurs
52  * at any time during this sub flow. Please map variables
53  * to the corresponding variable names below.
54  *
55  * Note - If this sub flow receives a Not Found (404) response
56  * from AAI at any time this will be considered an acceptable
57  * successful response however the GENGS_FoundIndicator
58  * will be set to false. This variable will allow the calling flow
59  * to distinguish between the two Success scenarios,
60  * "Success where service- is found" and
61  * "Success where service- is NOT found".
62  *
63  *
64  * Incoming Variables (Service-Instance):
65  * @param - GENGS_serviceInstanceId or @param - GENGS_serviceInstanceName
66  * @param - GENGS_type
67  * @param (Optional) - GENGS_serviceType
68  * @param (Optional) - GENGS_globalCustomerId
69  *
70  * Incoming Variables (Service-Subscription):
71  * @param - GENGS_type
72  * @param - GENGS_serviceType
73  * @param - GENGS_globalCustomerId
74  *
75  *
76  * Outgoing Variables:
77  * @param - GENGS_service
78  * @param - GENGS_SuccessIndicator
79  * @param - GENGS_FoundIndicator
80  * @param - WorkflowException
81  */
82 class GenericGetService extends AbstractServiceTaskProcessor{
83
84         String Prefix = "GENGS_"
85         ExceptionUtil exceptionUtil = new ExceptionUtil()
86
87         /**
88          * This method validates the incoming variables and
89          * determines the subsequent event based on which
90          * variables the calling flow provided.
91          *
92          * @param - execution
93          */
94         public void preProcessRequest(Execution execution) {
95                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
96                 execution.setVariable("prefix",Prefix)
97                 utils.log("DEBUG", " *** STARTED GenericGetService PreProcessRequest Process*** ", isDebugEnabled)
98
99                 execution.setVariable("GENGS_obtainServiceInstanceUrl", false)
100                 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", false)
101                 execution.setVariable("GENGS_SuccessIndicator", false)
102                 execution.setVariable("GENGS_FoundIndicator", false)
103                 execution.setVariable("GENGS_siResourceLink", null)
104
105                 try{
106                         // Get Variables
107                         String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
108                         String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
109                         String serviceType = execution.getVariable("GENGS_serviceType")
110                         String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
111                         String type = execution.getVariable("GENGS_type")
112
113                         if(type != null){
114                                 utils.log("DEBUG", "Incoming GENGS_type is: " + type, isDebugEnabled)
115                                 if(type.equalsIgnoreCase("service-instance")){
116                                         if(isBlank(serviceInstanceId) && isBlank(serviceInstanceName)){
117                                                 utils.log("DEBUG", "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.", isDebugEnabled)
118                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")
119                                         }else{
120                                                 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
121                                                 utils.log("DEBUG", "Incoming Service Instance Name is: " + serviceInstanceName, isDebugEnabled)
122                                                 if(isBlank(globalCustomerId) || isBlank(serviceType)){
123                                                         execution.setVariable("GENGS_obtainServiceInstanceUrl", true)
124                                                         if(isBlank(serviceInstanceId)){
125                                                                 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", true)
126                                                         }
127                                                 }else{
128                                                         utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
129                                                         utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
130                                                 }
131                                         }
132                                 }else if(type.equalsIgnoreCase("service-subscription")){
133                                         if(isBlank(serviceType) || isBlank(globalCustomerId)){
134                                                 utils.log("DEBUG", "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.", isDebugEnabled)
135                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
136                                         }else{
137                                                 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
138                                                 utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
139                                         }
140                                 }else{
141                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
142                                 }
143                         }else{
144                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENGS_type is null. Variable is Required.")
145                         }
146
147                 }catch(BpmnError b){
148                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
149                         throw b
150                 }catch(Exception e){
151                         utils.log("DEBUG", "Internal Error encountered within GenericGetService PreProcessRequest method!" + e, isDebugEnabled)
152                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetService PreProcessRequest")
153
154                 }
155                 utils.log("DEBUG", "*** COMPLETED GenericGetService PreProcessRequest Process ***", isDebugEnabled)
156         }
157
158         /**
159          * This method obtains the Url to the provided service instance
160          * using the Service Instance Id.
161          *
162          * @param - execution
163          */
164         public void obtainServiceInstanceUrlById(Execution execution){
165                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
166                 execution.setVariable("prefix",Prefix)
167                 utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)
168                 try {
169                         String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
170                         utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Id: " + serviceInstanceId, isDebugEnabled)
171
172                         AaiUtil aaiUriUtil = new AaiUtil(this)
173                         String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
174                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
175                         
176                         utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)
177                         String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-id:EQUALS:${serviceInstanceId}"
178
179                         //String url = "${aai_endpoint}${path}"  host name needs to be removed from property
180                         String url = "${path}"
181                         execution.setVariable("GENGS_obtainSIUrlPath", url)
182
183                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
184                         int responseCode = response.getStatusCode()
185                         execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
186                         utils.log("DEBUG", "  GET Service Instance response code is: " + responseCode, isDebugEnabled)
187                         utils.logAudit("GenericGetService AAI GET Response Code: " + responseCode)
188
189                         String aaiResponse = response.getResponseBodyAsString()
190                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
191                         execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
192                         utils.logAudit("GenericGetService AAI Response: " + aaiResponse)
193                         //Process Response
194                         if(responseCode == 200){
195                                 utils.log("DEBUG", "  Query for Service Instance Url Received a Good Response Code", isDebugEnabled)
196                                 execution.setVariable("GENGS_SuccessIndicator", true)
197                                 if(utils.nodeExists(aaiResponse, "result-data")){
198                                         utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)
199                                         execution.setVariable("GENGS_FoundIndicator", true)
200                                         String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")
201                                         execution.setVariable("GENGS_siResourceLink", resourceLink)
202                                 }else{
203                                         utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)
204                                         execution.setVariable("WorkflowResponse", "  ") //for junits
205                                 }
206                         }else if(responseCode == 404){
207                                 utils.log("DEBUG", "  Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)
208                                 execution.setVariable("GENGS_SuccessIndicator", true)
209                                 execution.setVariable("WorkflowResponse", "  ") //for junits
210                         }else{
211                                 utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
212                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
213                                 throw new BpmnError("MSOWorkflowException")
214                         }
215                 }catch(BpmnError b){
216                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
217                         throw b
218                 }catch(Exception e){
219                         utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlById method!" + e, isDebugEnabled)
220                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlById")
221                 }
222                 utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)
223         }
224
225         /**
226          * This method obtains the Url to the provided service instance
227          * using the Service Instance Name.
228          *
229          * @param - execution
230          */
231         public void obtainServiceInstanceUrlByName(Execution execution){
232                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
233                 execution.setVariable("prefix",Prefix)
234                 utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)
235                 try {
236                         String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
237                         utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Name " + serviceInstanceName, isDebugEnabled)
238
239                         AaiUtil aaiUriUtil = new AaiUtil(this)
240                         String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
241                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
242                         String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-name:EQUALS:${serviceInstanceName}"
243
244                         //String url = "${aai_endpoint}${path}"  host name needs to be removed from property
245                         String url = "${path}"
246                         execution.setVariable("GENGS_obtainSIUrlPath", url)
247
248                         utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)
249                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
250                         int responseCode = response.getStatusCode()
251                         execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
252                         utils.log("DEBUG", "  GET Service Instance response code is: " + responseCode, isDebugEnabled)
253                         utils.logAudit("GenericGetService AAI Response Code: " + responseCode)
254                         
255                         String aaiResponse = response.getResponseBodyAsString()
256                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
257                         execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
258                         utils.logAudit("GenericGetService AAI Response: " + aaiResponse) 
259                         //Process Response
260                         if(responseCode == 200){
261                                 utils.log("DEBUG", "  Query for Service Instance Url Received a Good Response Code", isDebugEnabled)
262                                 execution.setVariable("GENGS_SuccessIndicator", true)
263                                 if(utils.nodeExists(aaiResponse, "result-data")){
264                                         utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)
265                                         execution.setVariable("GENGS_FoundIndicator", true)
266                                         String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")
267                                         execution.setVariable("GENGS_siResourceLink", resourceLink)
268                                 }else{
269                                         utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)
270                                         execution.setVariable("WorkflowResponse", "  ") //for junits
271                                 }
272                         }else if(responseCode == 404){
273                                 utils.log("DEBUG", "  Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)
274                                 execution.setVariable("GENGS_SuccessIndicator", true)
275                                 execution.setVariable("WorkflowResponse", "  ") //for junits
276                         }else{
277                                 utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
278                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
279                                 throw new BpmnError("MSOWorkflowException")
280                         }
281                 }catch(BpmnError b){
282                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
283                         throw b
284                 }catch(Exception e){
285                         utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlByName method!" + e, isDebugEnabled)
286                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlByName")
287                 }
288                 utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)
289         }
290
291
292         /**
293          * This method executes a GET call to AAI to obtain the
294          * service-instance or service-subscription
295          *
296          * @param - execution
297          */
298         public void getServiceObject(Execution execution){
299                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
300                 execution.setVariable("prefix",Prefix)
301                 utils.log("DEBUG", " *** STARTED GenericGetService GetServiceObject Process*** ", isDebugEnabled)
302                 try {
303                         String type = execution.getVariable("GENGS_type")
304                         AaiUtil aaiUriUtil = new AaiUtil(this)
305                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
306                         String serviceEndpoint = ""
307                         
308                         utils.logAudit("GenericGetService getServiceObject AAI Endpoint: " + aai_endpoint)
309                         if(type.equalsIgnoreCase("service-instance")){
310                                 String siResourceLink = execution.getVariable("GENGS_siResourceLink")
311                                 if(isBlank(siResourceLink)){
312                                         String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
313                                         utils.log("DEBUG", " Incoming GENGS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
314                                         String serviceType = execution.getVariable("GENGS_serviceType")
315                                         utils.log("DEBUG", " Incoming GENGS_serviceType is: " + serviceType, isDebugEnabled)
316                                         String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
317                                         utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
318
319                                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
320                                         logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
321                                         serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + UriUtils.encode(serviceInstanceId,"UTF-8")
322                                 }else{
323                                         utils.log("DEBUG", "Incoming Service Instance Resource Link is: " + siResourceLink, isDebugEnabled)
324                                         String[] split = siResourceLink.split("/aai/")
325                                         serviceEndpoint = "/aai/" + split[1]
326                                 }
327                         }else if(type.equalsIgnoreCase("service-subscription")){
328                                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
329                                 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
330                                 String serviceType = execution.getVariable("GENGS_serviceType")
331                                 serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
332                         }
333
334                         String serviceUrl = "${aai_endpoint}" + serviceEndpoint
335
336                         execution.setVariable("GENGS_getServiceUrl", serviceUrl)
337                         utils.log("DEBUG", "GET Service AAI Path is: \n" + serviceUrl, isDebugEnabled)
338
339                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceUrl)
340                         int responseCode = response.getStatusCode()
341                         execution.setVariable("GENGS_getServiceResponseCode", responseCode)
342                         utils.log("DEBUG", "  GET Service response code is: " + responseCode, isDebugEnabled)
343                         utils.logAudit("GenericGetService AAI Response Code: " + responseCode)
344                         
345                         String aaiResponse = response.getResponseBodyAsString()
346                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
347                         execution.setVariable("GENGS_getServiceResponse", aaiResponse)
348                         utils.logAudit("GenericGetService AAI Response: " + aaiResponse)
349                         
350                         //Process Response
351                         if(responseCode == 200 || responseCode == 202){
352                                 utils.log("DEBUG", "GET Service Received a Good Response Code", isDebugEnabled)
353                                 if(utils.nodeExists(aaiResponse, "service-instance") || utils.nodeExists(aaiResponse, "service-subscription")){
354                                         utils.log("DEBUG", "GET Service Response Contains a service-instance" , isDebugEnabled)
355                                         execution.setVariable("GENGS_FoundIndicator", true)
356                                         execution.setVariable("GENGS_service", aaiResponse)
357                                         execution.setVariable("WorkflowResponse", aaiResponse)
358
359                                 }else{
360                                         utils.log("DEBUG", "GET Service Response Does NOT Contain Data" , isDebugEnabled)
361                                 }
362                         }else if(responseCode == 404){
363                                 utils.log("DEBUG", "GET Service Received a Not Found (404) Response", isDebugEnabled)
364                                 execution.setVariable("WorkflowResponse", "  ") //for junits
365                         }
366                         else{
367                                 utils.log("DEBUG", "  GET Service Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
368                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
369                                 throw new BpmnError("MSOWorkflowException")
370                         }
371                 }catch(BpmnError b){
372                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
373                         throw b
374                 }catch(Exception e){
375                         utils.log("DEBUG", " Error encountered within GenericGetService GetServiceObject method!" + e, isDebugEnabled)
376                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GenericGetService")
377                 }
378                 utils.log("DEBUG", " *** COMPLETED GenericGetService GetServiceObject Process*** ", isDebugEnabled)
379         }
380
381 }