82d58cfe7accea37550ec17657c81938df8c3f38
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / AaiUtil.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 import org.camunda.bpm.engine.delegate.BpmnError\r
23 import org.camunda.bpm.engine.runtime.Execution\r
24 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;\r
25 import org.openecomp.mso.rest.APIResponse;\r
26 import org.openecomp.mso.rest.RESTClient\r
27 import org.openecomp.mso.rest.RESTConfig\r
28 \r
29 class AaiUtil {\r
30 \r
31         public MsoUtils utils = new MsoUtils()\r
32         public static final String AAI_NAMESPACE_STRING_KEY = 'URN_mso_workflow_global_default_aai_namespace'\r
33         public static final String DEFAULT_VERSION_KEY = 'URN_mso_workflow_global_default_aai_version'\r
34 \r
35         private String aaiNamespace = null;\r
36 \r
37         private AbstractServiceTaskProcessor taskProcessor\r
38 \r
39         public AaiUtil(AbstractServiceTaskProcessor taskProcessor) {\r
40                 this.taskProcessor = taskProcessor\r
41         }\r
42 \r
43         public String getNetworkGenericVnfEndpoint(Execution execution) {\r
44                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
45                 String endpoint = execution.getVariable("URN_aai_endpoint")\r
46                 def uri = getNetworkGenericVnfUri(execution)\r
47                 taskProcessor.logDebug('AaiUtil.getNetworkGenericVnfEndpoint() - AAI endpoint: ' + endpoint + uri, isDebugLogEnabled)\r
48                 return endpoint + uri\r
49         }\r
50 \r
51         public String getNetworkGenericVnfUri(Execution execution) {\r
52                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
53                 def uri = getUri(execution, 'generic_vnf')\r
54                 taskProcessor.logDebug('AaiUtil.getNetworkGenericVnfUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
55                 return uri\r
56         }\r
57 \r
58         public String getNetworkVpnBindingUri(Execution execution) {\r
59                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
60                 def uri = getUri(execution, 'vpn_binding')\r
61                 taskProcessor.logDebug('AaiUtil.getNetworkVpnBindingUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
62                 return uri\r
63         }\r
64 \r
65         public String getNetworkPolicyUri(Execution execution) {\r
66                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
67                 def uri = getUri(execution, 'network_policy')\r
68                 taskProcessor.logDebug('AaiUtil.getNetworkPolicyUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
69                 return uri\r
70         }\r
71 \r
72         public String getNetworkTableReferencesUri(Execution execution) {\r
73                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
74                 def uri = getUri(execution, 'route_table_reference')\r
75                 taskProcessor.logDebug('AaiUtil.getNetworkTableReferencesUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
76                 return uri\r
77         }\r
78 \r
79         public String getNetworkVceUri(Execution execution) {\r
80                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
81                 def uri = getUri(execution, 'vce')\r
82                 taskProcessor.logDebug('AaiUtil.getNetworkVceUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
83                 return uri\r
84         }\r
85 \r
86         public String getNetworkL3NetworkUri(Execution execution) {\r
87                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
88                 def uri = getUri(execution, 'l3_network')\r
89                 taskProcessor.logDebug('AaiUtil.getNetworkL3NetworkUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
90                 return uri\r
91         }\r
92 \r
93         public String getBusinessCustomerUri(Execution execution) {\r
94                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
95                 def uri = getUri(execution, 'customer')\r
96                 taskProcessor.logDebug('AaiUtil.getBusinessCustomerUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
97                 return uri\r
98         }\r
99 \r
100         //public String getBusinessCustomerUriv7(Execution execution) {\r
101         //      def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
102         //      //def uri = getUri(execution, BUSINESS_CUSTOMERV7)\r
103         //      def uri = getUri(execution, 'Customer')\r
104         //      taskProcessor.logDebug('AaiUtil.getBusinessCustomerUriv7() - AAI URI: ' + uri, isDebugLogEnabled)\r
105         //      return uri\r
106         //}\r
107 \r
108         public String getCloudInfrastructureCloudRegionEndpoint(Execution execution) {\r
109                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
110                 String endpoint = execution.getVariable("URN_aai_endpoint")\r
111                 def uri = getCloudInfrastructureCloudRegionUri(execution)\r
112                 taskProcessor.logDebug('AaiUtil.getCloudInfrastructureCloudRegionEndpoint() - AAI endpoint: ' + endpoint + uri, isDebugLogEnabled)\r
113                 return endpoint + uri\r
114         }\r
115 \r
116         public String getCloudInfrastructureCloudRegionUri(Execution execution) {\r
117                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
118                 def uri = getUri(execution, 'cloud_region')\r
119                 taskProcessor.logDebug('AaiUtil.getCloudInfrastructureCloudRegionUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
120                 return uri\r
121         }\r
122 \r
123         public String getCloudInfrastructureTenantUri(Execution execution) {\r
124                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
125                 def uri = getUri(execution, 'tenant')\r
126                 taskProcessor.logDebug('AaiUtil.getCloudInfrastructureTenantUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
127                 return uri\r
128         }\r
129 \r
130         public String getSearchNodesQueryUri(Execution execution) {\r
131                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
132                 def uri = getUri(execution, 'nodes_query')\r
133                 taskProcessor.logDebug('AaiUtil.getSearchNodesQueryUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
134                 return uri\r
135         }\r
136 \r
137         public String getSearchNodesQueryEndpoint(Execution execution) {\r
138                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
139                 String endpoint = execution.getVariable("URN_aai_endpoint")\r
140                 def uri = getSearchNodesQueryUri(execution)\r
141                 taskProcessor.logDebug('AaiUtil.getSearchNodesQueryEndpoint() - AAI endpoint: ' + endpoint + uri, isDebugLogEnabled)\r
142                 return endpoint + uri\r
143         }\r
144 \r
145         public String getSearchGenericQueryUri(Execution execution) {\r
146                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
147                 def uri = getUri(execution, 'generic_query')\r
148                 taskProcessor.logDebug('AaiUtil.getSearchGenericQueryUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
149                 return uri\r
150         }\r
151 \r
152         public String getVersion(Execution execution, resourceName, processKey) {\r
153                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
154 \r
155                 resourceName = resourceName.replaceAll('-', '_')\r
156 \r
157                 def versionWithResourceKey = "URN_mso_workflow_default_aai_${resourceName}_version"\r
158                 def versionWithProcessKey = "URN_mso_workflow_custom_${processKey}_aai_version"\r
159 \r
160                 def version = execution.getVariable(versionWithProcessKey)\r
161                 if (version) {\r
162                         taskProcessor.logDebug("AaiUtil.getVersion() - using flow specific ${versionWithProcessKey}=${version}", isDebugLogEnabled)\r
163                         return version\r
164                 }\r
165 \r
166                 version = execution.getVariable(versionWithResourceKey)\r
167                 if (version) {\r
168                         taskProcessor.logDebug("AaiUtil.getVersion() - using resource specific ${versionWithResourceKey}=${version}", isDebugLogEnabled)\r
169                         return version\r
170                 }\r
171 \r
172                 version = execution.getVariable(DEFAULT_VERSION_KEY)\r
173                 if (version) {\r
174                         taskProcessor.logDebug("AaiUtil.getVersion() - using default version ${DEFAULT_VERSION_KEY}=${version}", isDebugLogEnabled)\r
175                         return version\r
176                 }\r
177 \r
178                 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, "Internal Error: One of the following should be defined in MSO URN properties file: ${versionWithResourceKey}, ${versionWithProcessKey}, ${DEFAULT_VERSION_KEY}")\r
179         }\r
180 \r
181         public String getUri(Execution execution, resourceName) {\r
182 \r
183                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
184                 def processKey = taskProcessor.getMainProcessKey(execution)\r
185                 resourceName = resourceName.replaceAll('-', '_')\r
186 \r
187                 //set namespace\r
188                 setNamespace(execution)\r
189 \r
190                 // Check for flow+resource specific first\r
191                 def key = "URN_mso_workflow_${processKey}_aai_${resourceName}_uri"\r
192                 def uri = execution.getVariable(key)\r
193                 if(uri) {\r
194                         taskProcessor.logDebug("AaiUtil.getUri() - using flow+resource specific key: ${key}=${uri}", isDebugLogEnabled)\r
195                         return uri\r
196                 }\r
197 \r
198                 // Check for versioned key\r
199                 def version = getVersion(execution, resourceName, processKey)\r
200                 key = "URN_mso_workflow_default_aai_v${version}_${resourceName}_uri"\r
201                 uri = execution.getVariable(key)\r
202 \r
203                 if(uri) {\r
204                         taskProcessor.logDebug("AaiUtil.getUri() - using versioned URI key: ${key}=${uri}", isDebugLogEnabled)\r
205                         return uri\r
206                 }\r
207 \r
208                 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, 'Internal Error: AAI URI entry for ' + key + ' not defined in the MSO URN properties file')\r
209         }\r
210 \r
211         public String setNamespace(Execution execution) {\r
212                 def key = AAI_NAMESPACE_STRING_KEY\r
213                 aaiNamespace = execution.getVariable(key)\r
214                 if (aaiNamespace == null ) {\r
215                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, 'Internal Error: AAI URI entry for ' + key + ' not defined in the MSO URN properties file')\r
216                 }\r
217         }\r
218 \r
219         /**\r
220          * This method can be used for getting the building namespace out of uri.\r
221          *  NOTE: A getUri() method needs to be invoked first.\r
222          *        Alternative method is the getNamespaceFromUri(Execution execution, String uri)\r
223          * return namespace (plus version from uri)\r
224          *\r
225          * @param url\r
226          *\r
227          * @return namespace\r
228          */\r
229 \r
230         public String getNamespaceFromUri(String uri) {\r
231                  if (aaiNamespace == null) {\r
232                         throw new Exception('Internal Error: AAI Namespace has not been set yet. A getUri() method needs to be invoked first.')\r
233                 }\r
234                 String namespace = aaiNamespace\r
235                 if(uri!=null){\r
236                         String version = getVersionFromUri(uri)\r
237                         return namespace + "v"+version\r
238                 }else{\r
239                         return namespace\r
240                 }\r
241         }\r
242 \r
243         /**\r
244          * This method can be used for building namespace with aai version out of uri.\r
245          *   NOTE: 2 arguments: Execution execution & String uri\r
246          * @param execution\r
247          * @param url\r
248          *\r
249          * @return namespace\r
250          */\r
251         public String getNamespaceFromUri(Execution execution, String uri) {\r
252            String namespace = execution.getVariable(AAI_NAMESPACE_STRING_KEY)\r
253            if (namespace == null ) {\r
254                    (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, 'Internal Error: AAI URI entry for ' + AAI_NAMESPACE_STRING_KEY + ' not defined in the MSO URN properties file')\r
255            }\r
256            if(uri!=null){\r
257                    String version = getVersionFromUri(uri)\r
258                    return namespace + "v"+version\r
259            }else{\r
260                    return namespace\r
261            }\r
262    }\r
263 \r
264         /**\r
265          * This is used to extract the version from uri.\r
266          *\r
267          * @param uri\r
268          *\r
269          * @return version\r
270          */\r
271         private String getVersionFromUri(String uri) {\r
272                 def version = ""\r
273                 def savedVersion = ""\r
274                 for (int x=2; x<6; x++) {\r
275                         version = uri.substring(uri.indexOf("v")+1,  uri.indexOf("v")+x)\r
276                         if (!Character.isDigit(version.charAt(version.size()-1))) {\r
277                                 break\r
278                         }\r
279                         savedVersion = version\r
280                 }\r
281                 return savedVersion\r
282         }\r
283 \r
284 \r
285         /**\r
286          * This reusable method can be used for making AAI Get Calls. The url should\r
287          * be passed as a parameter along with the execution.  The method will\r
288          * return an APIResponse.\r
289          *\r
290          * @param execution\r
291          * @param url\r
292          *\r
293          * @return APIResponse\r
294          *\r
295          */\r
296         public APIResponse executeAAIGetCall(Execution execution, String url){\r
297                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
298                 taskProcessor.logDebug(" ======== STARTED Execute AAI Get Process ======== ", isDebugEnabled)\r
299                 APIResponse apiResponse = null\r
300                 try{\r
301                         String uuid = utils.getRequestID()\r
302                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
303                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
304 \r
305                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
306 \r
307                         RESTConfig config = new RESTConfig(url);\r
308                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
309 \r
310                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
311                                 client.addAuthorizationHeader(basicAuthCred)\r
312                         }\r
313                         apiResponse = client.get()\r
314 \r
315                         taskProcessor.logDebug( "======== COMPLETED Execute AAI Get Process ======== ", isDebugEnabled)\r
316                 }catch(Exception e){\r
317                         taskProcessor.logDebug("Exception occured while executing AAI Get Call. Exception is: \n" + e, isDebugEnabled)\r
318                         throw new BpmnError("MSOWorkflowException")\r
319                 }\r
320                 return apiResponse\r
321         }\r
322 \r
323 \r
324         /**\r
325          * This reusable method can be used for making AAI httpPut Calls. The url should\r
326          * be passed as a parameter along with the execution and payload.  The method will\r
327          * return an APIResponse.\r
328          *\r
329          * @param execution\r
330          * @param url\r
331          * @param payload\r
332          *\r
333          * @return APIResponse\r
334          *\r
335          */\r
336         public APIResponse executeAAIPutCall(Execution execution, String url, String payload){\r
337                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
338                 taskProcessor.logDebug( " ======== Started Execute AAI Put Process ======== ", isDebugEnabled)\r
339                 APIResponse apiResponse = null\r
340                 try{\r
341                         String uuid = utils.getRequestID()\r
342                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
343                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
344 \r
345                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
346 \r
347                         RESTConfig config = new RESTConfig(url);\r
348                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Content-Type", "application/xml").addHeader("Accept","application/xml");\r
349                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
350                                 client.addAuthorizationHeader(basicAuthCred)\r
351                         }\r
352                         apiResponse = client.httpPut(payload)\r
353 \r
354                         taskProcessor.logDebug( "======== Completed Execute AAI Put Process ======== ", isDebugEnabled)\r
355                 }catch(Exception e){\r
356                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Put Call. Exception is: \n" + e, isDebugEnabled)\r
357                         throw new BpmnError("MSOWorkflowException")\r
358                 }\r
359                 return apiResponse\r
360         }\r
361 \r
362         /**\r
363          * This reusable method can be used for making AAI httpPatch Calls. The url should\r
364          * be passed as a parameter along with the execution and payload.  The method will\r
365          * return an APIResponse.\r
366          *\r
367          * @param execution\r
368          * @param url\r
369          * @param payload\r
370          *\r
371          * @return APIResponse\r
372          *\r
373          */\r
374         public APIResponse executeAAIPatchCall(Execution execution, String url, String payload){\r
375                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
376                 taskProcessor.logDebug( " ======== Started Execute AAI Patch Process ======== ", isDebugEnabled)\r
377                 APIResponse apiResponse = null\r
378                 try{\r
379                         String uuid = utils.getRequestID()\r
380                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
381 \r
382                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
383 \r
384                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
385 \r
386                         RESTConfig config = new RESTConfig(url);\r
387                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Content-Type", "application/merge-patch+json").addHeader("Accept","application/json");\r
388                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
389                                 client.addAuthorizationHeader(basicAuthCred)\r
390                         }\r
391                         apiResponse = client.httpPatch(payload)\r
392 \r
393                         taskProcessor.logDebug( "======== Completed Execute AAI Patch Process ======== ", isDebugEnabled)\r
394                 }catch(Exception e){\r
395                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Patch Call. Exception is: \n" + e, isDebugEnabled)\r
396                         throw new BpmnError("MSOWorkflowException")\r
397                 }\r
398                 return apiResponse\r
399         }\r
400 \r
401 \r
402         /**\r
403          * This reusable method can be used for making AAI Delete Calls. The url should\r
404          * be passed as a parameter along with the execution.  The method will\r
405          * return an APIResponse.\r
406          *\r
407          * @param execution\r
408          * @param url\r
409          *\r
410          * @return APIResponse\r
411          *\r
412          */\r
413         public APIResponse executeAAIDeleteCall(Execution execution, String url){\r
414                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
415                 taskProcessor.logDebug( " ======== Started Execute AAI Delete Process ======== ", isDebugEnabled)\r
416                 APIResponse apiResponse = null\r
417                 try{\r
418                         String uuid = utils.getRequestID()\r
419                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
420                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
421 \r
422                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
423 \r
424                         RESTConfig config = new RESTConfig(url);\r
425                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
426                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
427                                 client.addAuthorizationHeader(basicAuthCred)\r
428                         }\r
429                         apiResponse = client.delete()\r
430 \r
431                         taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
432                 }catch(Exception e){\r
433                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Delete Call. Exception is: \n" + e, isDebugEnabled)\r
434                         throw new BpmnError("MSOWorkflowException")\r
435                 }\r
436                 return apiResponse\r
437         }\r
438 \r
439         /**\r
440          * This reusable method can be used for making AAI Delete Calls. The url should\r
441          * be passed as a parameter along with the execution.  The method will\r
442          * return an APIResponse.\r
443          *\r
444          * @param execution\r
445          * @param url\r
446          * @param payload\r
447          *\r
448          * @return APIResponse\r
449          *\r
450          */\r
451         public APIResponse executeAAIDeleteCall(Execution execution, String url, String payload, String authHeader){\r
452                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
453                 taskProcessor.logDebug( " ======== Started Execute AAI Delete Process ======== ", isDebugEnabled)\r
454                 APIResponse apiResponse = null\r
455                 try{\r
456                         String uuid = utils.getRequestID()\r
457                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
458 \r
459                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
460 \r
461                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
462                         RESTConfig config = new RESTConfig(url);\r
463                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml").addAuthorizationHeader(authHeader);\r
464                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
465                                 client.addAuthorizationHeader(basicAuthCred)\r
466                         }\r
467                         apiResponse = client.httpDelete(payload)\r
468 \r
469                         taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
470                 }catch(Exception e){\r
471                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Delete Call. Exception is: \n" + e, isDebugEnabled)\r
472                         throw new BpmnError("MSOWorkflowException")\r
473                 }\r
474                 return apiResponse\r
475         }\r
476 \r
477         /**\r
478          * This reusable method can be used for making AAI Post Calls. The url\r
479          * and payload should be passed as a parameters along with the execution.\r
480          * The method will return an APIResponse.\r
481          *\r
482          * @param execution\r
483          * @param url\r
484          * @param payload\r
485          *\r
486          * @return APIResponse\r
487          *\r
488          */\r
489         public APIResponse executeAAIPostCall(Execution execution, String url, String payload){\r
490                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
491                 taskProcessor.logDebug( " ======== Started Execute AAI Post Process ======== ", isDebugEnabled)\r
492                 APIResponse apiResponse = null\r
493                 try{\r
494                         String uuid = utils.getRequestID()\r
495                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
496                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
497 \r
498                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
499                         RESTConfig config = new RESTConfig(url);\r
500                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
501 \r
502                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
503                                 client.addAuthorizationHeader(basicAuthCred)\r
504                         }\r
505                         apiResponse = client.httpPost(payload)\r
506 \r
507                         taskProcessor.logDebug( "======== Completed Execute AAI Post Process ======== ", isDebugEnabled)\r
508                 }catch(Exception e){\r
509                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Post Call. Exception is: \n" + e, isDebugEnabled)\r
510                         throw new BpmnError("MSOWorkflowException")\r
511                 }\r
512                 return apiResponse\r
513         }\r
514 \r
515         /**\r
516          * This reusable method can be used for making AAI Post Calls. The url\r
517          * and payload should be passed as a parameters along with the execution.\r
518          * The method will return an APIResponse.\r
519          *\r
520          * @param execution\r
521          * @param url\r
522          * @param payload\r
523          * @param authenticationHeader - addAuthenticationHeader value\r
524          * @param headerName - name of header you want to add, i.e. addHeader(headerName, headerValue)\r
525          * @param headerValue - the header's value, i.e. addHeader(headerName, headerValue)\r
526          *\r
527          * @return APIResponse\r
528          *\r
529          */\r
530         public APIResponse executeAAIPostCall(Execution execution, String url, String payload, String authenticationHeaderValue, String headerName, String headerValue){\r
531                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
532                 taskProcessor.logDebug( " ======== Started Execute AAI Post Process ======== ", isDebugEnabled)\r
533                 APIResponse apiResponse = null\r
534                 try{\r
535                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
536 \r
537                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
538 \r
539                         RESTConfig config = new RESTConfig(url);\r
540                         RESTClient client = new RESTClient(config).addAuthorizationHeader(authenticationHeaderValue).addHeader(headerName, headerValue)\r
541                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
542                                 client.addAuthorizationHeader(basicAuthCred)\r
543                         }\r
544                         apiResponse = client.httpPost(payload)\r
545 \r
546                         taskProcessor.logDebug( "======== Completed Execute AAI Post Process ======== ", isDebugEnabled)\r
547                 }catch(Exception e){\r
548                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Post Call. Exception is: \n" + e, isDebugEnabled)\r
549                         throw new BpmnError("MSOWorkflowException")\r
550                 }\r
551                 return apiResponse\r
552         }\r
553 \r
554 \r
555         /* Utility to get the Cloud Region from AAI\r
556          * Returns String cloud region id, (ie, cloud-region-id)\r
557          * @param execution\r
558          * @param url  - url for AAI get cloud region\r
559          * @param backend - "PO" - real region, or "SDNC" - v2.5 (fake region).\r
560          */\r
561 \r
562         public String getAAICloudReqion(Execution execution, String url, String backend, inputCloudRegion){\r
563                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
564                 String regionId = ""\r
565                 try{\r
566                         APIResponse apiResponse = executeAAIGetCall(execution, url)\r
567                         String returnCode = apiResponse.getStatusCode()\r
568                         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
569                         taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Return code: " + returnCode, isDebugEnabled)\r
570                         execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionReturnCode", returnCode)\r
571 \r
572                         if(returnCode == "200"){\r
573                                 taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region is Successful.", isDebugEnabled)\r
574 \r
575                                 String regionVersion = taskProcessor.utils.getNodeText1(aaiResponseAsString, "cloud-region-version")\r
576                                 taskProcessor.utils.log("DEBUG", "Cloud Region Version from AAI for " + backend + " is: " + regionVersion, isDebugEnabled)\r
577                                 if (backend == "PO") {\r
578                                         regionId = taskProcessor.utils.getNodeText1(aaiResponseAsString, "cloud-region-id")\r
579                                 } else { // backend not "PO"\r
580                                         if (regionVersion == "2.5" ) {\r
581                                                 regionId = "AAIAIC25"\r
582                                         } else {\r
583                                                 regionId = taskProcessor.utils.getNodeText1(aaiResponseAsString, "cloud-region-id")\r
584                                         }\r
585                                 }\r
586                                 if(regionId == null){\r
587                                         throw new BpmnError("MSOWorkflowException")\r
588                                 }\r
589                                 taskProcessor.utils.log("DEBUG", "Cloud Region Id from AAI " + backend + " is: " + regionId, isDebugEnabled)\r
590                         }else if (returnCode == "404"){ // not 200\r
591                                 if (backend == "PO") {\r
592                                         regionId = inputCloudRegion\r
593                                 }else{  // backend not "PO"\r
594                                         regionId = "AAIAIC25"\r
595                                 }\r
596                                 taskProcessor.utils.log("DEBUG", "Cloud Region value for code='404' of " + backend + " is: " + regionId, isDebugEnabled)\r
597                         }else{\r
598                                 taskProcessor.utils.log("ERROR", "Call AAI Cloud Region is NOT Successful.", isDebugEnabled)\r
599                                 throw new BpmnError("MSOWorkflowException")\r
600                         }\r
601                 }catch(Exception e) {\r
602                         taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)\r
603                         throw new BpmnError("MSOWorkflowException")\r
604                 }\r
605                 return regionId\r
606         }\r
607 \r
608         /* returns xml Node with service-type of searchValue */\r
609         def searchServiceType(xmlInput, searchValue){\r
610                 def fxml= new XmlSlurper().parseText(xmlInput)\r
611                 def ret = fxml.'**'.find {it.'service-type' == searchValue}\r
612                 return ret\r
613         }\r
614 \r
615         /* returns xml Node with service-instance-id of searchValue */\r
616         def searchServiceInstanceId(xmlInput, searchValue){\r
617                 def ret = xmlInput.'**'.find {it.'service-instance-id' == searchValue}\r
618                 return ret\r
619         }\r
620 \r
621         /**\r
622          * Get the lowest unused VF Module index from AAI response for a given module type. The criteria for\r
623          * determining module type is specified by "key" parameter (for example, "persona-model-id"),\r
624          * the value for filtering is specified in "value" parameter\r
625          *\r
626          * @param execution\r
627          * @param aaiVnfResponse\r
628          * @param key\r
629          * @param value\r
630          *\r
631          * @return moduleIndex\r
632          *\r
633          */\r
634         public int getLowestUnusedVfModuleIndexFromAAIVnfResponse(Execution execution, String aaiVnfResponse, String key, String value) {\r
635                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
636                 if (aaiVnfResponse != null) {\r
637                         String vfModulesText = taskProcessor.utils.getNodeXml(aaiVnfResponse, "vf-modules")\r
638                         if (vfModulesText == null || vfModulesText.isEmpty()) {\r
639                                 taskProcessor.utils.log("DEBUG", "There are no VF modules in this VNF yet", isDebugEnabled)\r
640                                 return 0\r
641                         }\r
642                         else {\r
643                                 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)\r
644                                 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}\r
645                                 int vfModulesSize = 0\r
646                                 if (vfModules != null) {\r
647                                         vfModulesSize = vfModules.size()\r
648                                 }\r
649                                 String matchingVfModules = "<vfModules>"\r
650                                 for (i in 0..vfModulesSize-1) {\r
651                                         def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])\r
652                                         def keyFromAAI = taskProcessor.utils.getNodeText(vfModuleXml, key)\r
653                                         if (keyFromAAI != null && keyFromAAI.equals(value)) {\r
654                                                 matchingVfModules = matchingVfModules + taskProcessor.utils.removeXmlPreamble(vfModuleXml)\r
655                                         }\r
656                                 }\r
657                                 matchingVfModules = matchingVfModules + "</vfModules>"\r
658                                 taskProcessor.utils.log("DEBUG", "Matching VF Modules: " + matchingVfModules, isDebugEnabled)\r
659                                 String lowestUnusedIndex = taskProcessor.utils.getLowestUnusedIndex(matchingVfModules)\r
660                                 return Integer.parseInt(lowestUnusedIndex)\r
661                         }\r
662                 }\r
663                 else {\r
664                         return 0\r
665                 }\r
666         }\r
667 }