[MSO-8] Second step of the rebase for MSO
[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 = 'http://org.openecomp.aai.inventory/'\r
33         public static final String DEFAULT_VERSION_KEY = 'URN_mso_workflow_global_default_aai_version'\r
34         \r
35         private AbstractServiceTaskProcessor taskProcessor\r
36 \r
37         public AaiUtil(AbstractServiceTaskProcessor taskProcessor) {\r
38                 this.taskProcessor = taskProcessor\r
39         }\r
40         \r
41         public String getNetworkGenericVnfEndpoint(Execution execution) {\r
42                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
43                 String endpoint = execution.getVariable("URN_aai_endpoint")\r
44                 def uri = getNetworkGenericVnfUri(execution)\r
45                 taskProcessor.logDebug('AaiUtil.getNetworkGenericVnfEndpoint() - AAI endpoint: ' + endpoint + uri, isDebugLogEnabled)\r
46                 return endpoint + uri\r
47         }\r
48 \r
49         public String getNetworkGenericVnfUri(Execution execution) {\r
50                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
51                 def uri = getUri(execution, 'generic_vnf')\r
52                 taskProcessor.logDebug('AaiUtil.getNetworkGenericVnfUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
53                 return uri\r
54         }\r
55                 \r
56         public String getNetworkVpnBindingUri(Execution execution) {\r
57                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
58                 def uri = getUri(execution, 'vpn_binding')\r
59                 taskProcessor.logDebug('AaiUtil.getNetworkVpnBindingUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
60                 return uri\r
61         }\r
62 \r
63         public String getNetworkPolicyUri(Execution execution) {\r
64                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
65                 def uri = getUri(execution, 'network_policy')\r
66                 taskProcessor.logDebug('AaiUtil.getNetworkPolicyUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
67                 return uri\r
68         }\r
69 \r
70         public String getNetworkTableReferencesUri(Execution execution) {\r
71                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
72                 def uri = getUri(execution, 'route_table_reference')\r
73                 taskProcessor.logDebug('AaiUtil.getNetworkTableReferencesUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
74                 return uri\r
75         }\r
76         \r
77         public String getNetworkVceUri(Execution execution) {\r
78                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
79                 def uri = getUri(execution, 'vce')\r
80                 taskProcessor.logDebug('AaiUtil.getNetworkVceUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
81                 return uri\r
82         }\r
83 \r
84         public String getNetworkL3NetworkUri(Execution execution) {\r
85                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
86                 def uri = getUri(execution, 'l3_network')\r
87                 taskProcessor.logDebug('AaiUtil.getNetworkL3NetworkUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
88                 return uri\r
89         }\r
90 \r
91         public String getBusinessCustomerUri(Execution execution) {\r
92                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
93                 def uri = getUri(execution, 'customer')\r
94                 taskProcessor.logDebug('AaiUtil.getBusinessCustomerUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
95                 return uri\r
96         }\r
97 \r
98         //public String getBusinessCustomerUriv7(Execution execution) {\r
99         //      def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
100         //      //def uri = getUri(execution, BUSINESS_CUSTOMERV7)\r
101         //      def uri = getUri(execution, 'Customer')\r
102         //      taskProcessor.logDebug('AaiUtil.getBusinessCustomerUriv7() - AAI URI: ' + uri, isDebugLogEnabled)\r
103         //      return uri\r
104         //}\r
105 \r
106         public String getCloudInfrastructureCloudRegionEndpoint(Execution execution) {\r
107                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
108                 String endpoint = execution.getVariable("URN_aai_endpoint")\r
109                 def uri = getCloudInfrastructureCloudRegionUri(execution)\r
110                 taskProcessor.logDebug('AaiUtil.getCloudInfrastructureCloudRegionEndpoint() - AAI endpoint: ' + endpoint + uri, isDebugLogEnabled)\r
111                 return endpoint + uri\r
112         }\r
113         \r
114         public String getCloudInfrastructureCloudRegionUri(Execution execution) {\r
115                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
116                 def uri = getUri(execution, 'cloud_region')\r
117                 taskProcessor.logDebug('AaiUtil.getCloudInfrastructureCloudRegionUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
118                 return uri\r
119         }\r
120 \r
121         public String getCloudInfrastructureTenantUri(Execution execution) {\r
122                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
123                 def uri = getUri(execution, 'tenant')\r
124                 taskProcessor.logDebug('AaiUtil.getCloudInfrastructureTenantUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
125                 return uri\r
126         }\r
127 \r
128         public String getSearchNodesQueryUri(Execution execution) {\r
129                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
130                 def uri = getUri(execution, 'nodes_query')\r
131                 taskProcessor.logDebug('AaiUtil.getSearchNodesQueryUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
132                 return uri\r
133         }\r
134         \r
135         public String getSearchNodesQueryEndpoint(Execution execution) {\r
136                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
137                 String endpoint = execution.getVariable("URN_aai_endpoint")\r
138                 def uri = getSearchNodesQueryUri(execution)\r
139                 taskProcessor.logDebug('AaiUtil.getSearchNodesQueryEndpoint() - AAI endpoint: ' + endpoint + uri, isDebugLogEnabled)\r
140                 return endpoint + uri\r
141         }\r
142         \r
143         public String getSearchGenericQueryUri(Execution execution) {\r
144                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
145                 def uri = getUri(execution, 'generic_query')\r
146                 taskProcessor.logDebug('AaiUtil.getSearchGenericQueryUri() - AAI URI: ' + uri, isDebugLogEnabled)\r
147                 return uri\r
148         }\r
149 \r
150         public String getVersion(Execution execution, resourceName, processKey) {\r
151                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
152 \r
153                 resourceName = resourceName.replaceAll('-', '_')\r
154                 \r
155                 def versionWithResourceKey = "URN_mso_workflow_default_aai_${resourceName}_version"\r
156                 def versionWithProcessKey = "URN_mso_workflow_custom_${processKey}_aai_version"\r
157 \r
158                 def version = execution.getVariable(versionWithProcessKey)\r
159                 if (version) {\r
160                         taskProcessor.logDebug("AaiUtil.getVersion() - using flow specific ${versionWithProcessKey}=${version}", isDebugLogEnabled)\r
161                         return version \r
162                 }\r
163                 \r
164                 version = execution.getVariable(versionWithResourceKey)\r
165                 if (version) {\r
166                         taskProcessor.logDebug("AaiUtil.getVersion() - using resource specific ${versionWithResourceKey}=${version}", isDebugLogEnabled)\r
167                         return version\r
168                 }\r
169                 \r
170                 version = execution.getVariable(DEFAULT_VERSION_KEY)\r
171                 if (version) {\r
172                         taskProcessor.logDebug("AaiUtil.getVersion() - using default version ${DEFAULT_VERSION_KEY}=${version}", isDebugLogEnabled)\r
173                         return version\r
174                 }\r
175                 \r
176                 (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
177         }\r
178         \r
179         public String getUri(Execution execution, resourceName) {\r
180 \r
181                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
182                 def processKey = taskProcessor.getMainProcessKey(execution)\r
183                 \r
184                 resourceName = resourceName.replaceAll('-', '_')\r
185                 \r
186                 // Check for flow+resource specific first\r
187                 def key = "URN_mso_workflow_${processKey}_aai_${resourceName}_uri"\r
188                 def uri = execution.getVariable(key)\r
189                 if(uri) {\r
190                         taskProcessor.logDebug("AaiUtil.getUri() - using flow+resource specific key: ${key}=${uri}", isDebugLogEnabled)\r
191                         return uri\r
192                 }\r
193                 \r
194                 // Check for versioned key\r
195                 def version = getVersion(execution, resourceName, processKey)\r
196                 key = "URN_mso_workflow_default_aai_v${version}_${resourceName}_uri"\r
197                 uri = execution.getVariable(key)\r
198 \r
199                 if(uri) {\r
200                         taskProcessor.logDebug("AaiUtil.getUri() - using versioned URI key: ${key}=${uri}", isDebugLogEnabled)\r
201                         return uri\r
202                 }\r
203 \r
204                 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, 'Internal Error: AAI URI entry for ' + key + ' not defined in the MSO URN properties file')\r
205         }\r
206 \r
207         public String getNamespaceFromUri(String uri) {\r
208                 String namespace = AAI_NAMESPACE_STRING\r
209                 if(uri!=null){\r
210                         return namespace + uri.substring(uri.indexOf("v"),  uri.indexOf("v")+2)\r
211                 }else{\r
212                         return namespace\r
213                 }\r
214         }\r
215 \r
216 \r
217         \r
218         /**\r
219          * This reusable method can be used for making AAI Get Calls. The url should\r
220          * be passed as a parameter along with the execution.  The method will\r
221          * return an APIResponse.\r
222          *\r
223          * @param execution\r
224          * @param url\r
225          *\r
226          * @return APIResponse\r
227          */\r
228         public APIResponse executeAAIGetCall(Execution execution, String url){\r
229                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
230                 taskProcessor.logDebug(" ======== STARTED Execute AAI Get Process ======== ", isDebugEnabled)\r
231                 try{\r
232                         String uuid = UUID.randomUUID()\r
233                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
234                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
235                                         \r
236                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
237 \r
238                         RESTConfig config = new RESTConfig(url);\r
239                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
240 \r
241                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
242                                 client.addAuthorizationHeader(basicAuthCred)\r
243                         }\r
244 \r
245                         APIResponse apiResponse = client.get()\r
246                         return apiResponse\r
247 \r
248                 }catch(Exception e){\r
249                         taskProcessor.logDebug("Exception occured while executing AAI Get Call. Exception is: \n" + e, isDebugEnabled)\r
250                         return e\r
251                 }\r
252                 taskProcessor.logDebug( "======== COMPLETED Execute AAI Get Process ======== ", isDebugEnabled)\r
253         }\r
254 \r
255         /**\r
256          * This reusable method can be used for making AAI httpPut Calls. The url should\r
257          * be passed as a parameter along with the execution and payload.  The method will\r
258          * return an APIResponse.\r
259          *\r
260          * @param execution\r
261          * @param url\r
262          * @param payload\r
263          *\r
264          * @return APIResponse\r
265          */\r
266         public APIResponse executeAAIPutCall(Execution execution, String url, String payload){\r
267                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
268                 taskProcessor.logDebug( " ======== Started Execute AAI Put Process ======== ", isDebugEnabled)\r
269                 try{\r
270                         String uuid = UUID.randomUUID()\r
271                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
272                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
273                         \r
274                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
275 \r
276                         RESTConfig config = new RESTConfig(url);\r
277                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Content-Type", "application/xml").addHeader("Accept","application/xml");\r
278                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
279                                 client.addAuthorizationHeader(basicAuthCred)\r
280                         }\r
281                         APIResponse apiResponse = client.httpPut(payload)\r
282 \r
283                         return apiResponse\r
284                 }catch(Exception e){\r
285                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Put Call. Exception is: \n" + e, isDebugEnabled)\r
286                         return e\r
287                 }\r
288                 taskProcessor.logDebug( "======== Completed Execute AAI Put Process ======== ", isDebugEnabled)\r
289         }\r
290         \r
291         /**\r
292          * This reusable method can be used for making AAI httpPatch Calls. The url should\r
293          * be passed as a parameter along with the execution and payload.  The method will\r
294          * return an APIResponse.\r
295          *\r
296          * @param execution\r
297          * @param url\r
298          * @param payload\r
299          *\r
300          * @return APIResponse\r
301          */\r
302         public APIResponse executeAAIPatchCall(Execution execution, String url, String payload){\r
303                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
304                 taskProcessor.logDebug( " ======== Started Execute AAI Patch Process ======== ", isDebugEnabled)\r
305                 try{\r
306                         String uuid = UUID.randomUUID()\r
307                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
308 \r
309                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
310 \r
311                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
312                         \r
313                         RESTConfig config = new RESTConfig(url);\r
314                         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
315                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
316                                 client.addAuthorizationHeader(basicAuthCred)\r
317                         }\r
318                         APIResponse apiResponse = client.httpPatch(payload)\r
319 \r
320                         return apiResponse\r
321                 }catch(Exception e){\r
322                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Patch Call. Exception is: \n" + e, isDebugEnabled)\r
323                         return e\r
324                 }\r
325                 taskProcessor.logDebug( "======== Completed Execute AAI Patch Process ======== ", isDebugEnabled)\r
326         }\r
327 \r
328 \r
329         /**\r
330          * This reusable method can be used for making AAI Delete Calls. The url should\r
331          * be passed as a parameter along with the execution.  The method will\r
332          * return an APIResponse.\r
333          *\r
334          * @param execution\r
335          * @param url\r
336          *\r
337          * @return APIResponse\r
338          */\r
339         public APIResponse executeAAIDeleteCall(Execution execution, String url){\r
340                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
341                 taskProcessor.logDebug( " ======== Started Execute AAI Delete Process ======== ", isDebugEnabled)\r
342                 try{\r
343                         String uuid = UUID.randomUUID()\r
344                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
345                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
346 \r
347                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
348                         \r
349                         RESTConfig config = new RESTConfig(url);\r
350                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
351                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
352                                 client.addAuthorizationHeader(basicAuthCred)\r
353                         }\r
354                         APIResponse apiResponse = client.delete()\r
355 \r
356                         return apiResponse\r
357 \r
358                 }catch(Exception e){\r
359                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Delete Call. Exception is: \n" + e, isDebugEnabled)\r
360                         return e\r
361                 }\r
362                 taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
363         }\r
364 \r
365         /**\r
366          * This reusable method can be used for making AAI Delete Calls. The url should\r
367          * be passed as a parameter along with the execution.  The method will\r
368          * return an APIResponse.\r
369          *\r
370          * @param execution\r
371          * @param url\r
372          * @param payload\r
373          *\r
374          * @return APIResponse\r
375          */\r
376         public APIResponse executeAAIDeleteCall(Execution execution, String url, String payload, String authHeader){\r
377                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
378                 taskProcessor.logDebug( " ======== Started Execute AAI Delete Process ======== ", isDebugEnabled)\r
379                 try{\r
380                         String uuid = UUID.randomUUID()\r
381                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\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                         RESTConfig config = new RESTConfig(url);\r
386                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml").addAuthorizationHeader(authHeader);\r
387                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
388                                 client.addAuthorizationHeader(basicAuthCred)\r
389                         }\r
390                         \r
391                         APIResponse apiResponse = client.httpDelete(payload)\r
392 \r
393                         return apiResponse\r
394 \r
395                 }catch(Exception e){\r
396                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Delete Call. Exception is: \n" + e, isDebugEnabled)\r
397                         return e\r
398                 }\r
399                 taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
400         }\r
401         \r
402         /**\r
403          * This reusable method can be used for making AAI Post 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          * @param payload\r
410          *\r
411          * @return APIResponse\r
412          */\r
413         public APIResponse executeAAIPostCall(Execution execution, String url, String payload){\r
414                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
415                 taskProcessor.logDebug( " ======== Started Execute AAI Post Process ======== ", isDebugEnabled)\r
416                 try{\r
417                         String uuid = UUID.randomUUID()\r
418                         taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
419                         taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
420 \r
421                         String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
422                         RESTConfig config = new RESTConfig(url);\r
423                         RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
424                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
425                                 client.addAuthorizationHeader(basicAuthCred)\r
426                         }\r
427                         APIResponse apiResponse = client.httpPost(payload)\r
428 \r
429                         return apiResponse\r
430 \r
431                 }catch(Exception e){\r
432                         taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Post Call. Exception is: \n" + e, isDebugEnabled)\r
433                         return e\r
434                 }\r
435                 taskProcessor.logDebug( "======== Completed Execute AAI Post Process ======== ", isDebugEnabled)\r
436         }\r
437 \r
438         /** Utilitty to get the Cloud Region from AAI\r
439          * Returns String cloud region id, (ie, cloud-region-id)\r
440          * @param execution\r
441          * @param url  - url for AAI get cloud region\r
442          * @param backend - "PO" - real region, or "SDNC" - v2.5 (fake region).\r
443          */\r
444 \r
445         //TODO: We should refactor this method to return WorkflowException instead of Error. Also to throw MSOWorkflowException which the calling flow will then catch.\r
446 \r
447         public String getAAICloudReqion(Execution execution, String url, String backend, inputCloudRegion){\r
448                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
449 \r
450                 try {\r
451                   APIResponse apiResponse = executeAAIGetCall(execution, url)\r
452                   String returnCode = apiResponse.getStatusCode()\r
453                   String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
454                   taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Return code: " + returnCode, isDebugEnabled)\r
455                   execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionReturnCode", returnCode)\r
456                   //taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Response: " + aaiResponseAsString, isDebugEnabled)\r
457                   //execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionResponse", aaiResponseAsString)\r
458                   String regionId = ""\r
459                   if (returnCode == "200") {\r
460                          taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region is Successful.", isDebugEnabled)\r
461                            try {\r
462                            String regionVersion = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-version")\r
463                            taskProcessor.utils.log("DEBUG", "Cloud Region Version from AAI for " + backend + " is: " + regionVersion, isDebugEnabled)\r
464                            if (backend == "PO") {\r
465                                   regionId = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-id")\r
466                            } else { // backend not "PO"\r
467                                   if (regionVersion == "2.5" ) {\r
468                                           regionId = "AAIAIC25"\r
469                                   } else {\r
470                                           regionId = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-id")\r
471                                   }\r
472                            }\r
473 \r
474                            taskProcessor.utils.log("DEBUG", "Cloud Region Id from AAI " + backend + " is: " + regionId, isDebugEnabled)\r
475                            return regionId\r
476 \r
477                          } catch (Exception e) {\r
478                                   taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)\r
479                                   return "ERROR"\r
480                          }\r
481                   } else { // not 200\r
482                       if (returnCode == "404") {\r
483                                  if (backend == "PO") {\r
484                                           regionId = inputCloudRegion\r
485                                  } else  {  // backend not "PO"\r
486                                           regionId = "AAIAIC25"\r
487                                  }\r
488                                  taskProcessor.utils.log("DEBUG", "Cloud Region value for code='404' of " + backend + " is: " + regionId, isDebugEnabled)\r
489                                   return regionId\r
490                       } else {\r
491                               taskProcessor.utils.log("ERROR", "Call AAI Cloud Region is NOT Successful.", isDebugEnabled)\r
492                               return "ERROR"\r
493                       }\r
494                   }\r
495                 }catch(Exception e) {\r
496                    taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)\r
497                    return "ERROR"\r
498                 }\r
499         }\r
500 \r
501         /* returns xml Node with service-type of searchValue */\r
502         def searchServiceType(xmlInput, searchValue){\r
503                 def fxml= new XmlSlurper().parseText(xmlInput)\r
504                 def ret = fxml.'**'.find {it.'service-type' == searchValue}\r
505                 return ret\r
506         }\r
507 \r
508         /* returns xml Node with service-instance-id of searchValue */\r
509         def searchServiceInstanceId(xmlInput, searchValue){\r
510                 def ret = xmlInput.'**'.find {it.'service-instance-id' == searchValue}\r
511                 return ret\r
512         }\r
513 \r
514         \r
515         /**\r
516          * Get the lowest unused VF Module index from AAI response for a given module type. The criteria for \r
517          * determining module type is specified by "key" parameter (for example, "persona-model-id"),\r
518          * the value for filtering is specified in "value" parameter\r
519          * \r
520          * @param execution\r
521          * @param aaiVnfResponse\r
522          * @param key\r
523          * @param value\r
524          *\r
525          * @return moduleIndex\r
526          * \r
527          */\r
528         public int getLowestUnusedVfModuleIndexFromAAIVnfResponse(Execution execution, String aaiVnfResponse, String key, String value) {\r
529                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
530                 if (aaiVnfResponse != null) {\r
531                         String vfModulesText = taskProcessor.utils.getNodeXml(aaiVnfResponse, "vf-modules")\r
532                         if (vfModulesText == null || vfModulesText.isEmpty()) {\r
533                                 taskProcessor.utils.log("DEBUG", "There are no VF modules in this VNF yet", isDebugEnabled)\r
534                                 return 0                                \r
535                         }\r
536                         else {\r
537                                 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)\r
538                                 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}\r
539                                 int vfModulesSize = 0\r
540                                 if (vfModules != null) {\r
541                                         vfModulesSize = vfModules.size()\r
542                                 }\r
543                                 String matchingVfModules = "<vfModules>"\r
544                                 for (i in 0..vfModulesSize-1) {\r
545                                         def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])\r
546                                         def keyFromAAI = taskProcessor.utils.getNodeText(vfModuleXml, key)\r
547                                         if (keyFromAAI != null && keyFromAAI.equals(value)) {\r
548                                                 matchingVfModules = matchingVfModules + taskProcessor.utils.removeXmlPreamble(vfModuleXml)\r
549                                         }\r
550                                 }\r
551                                 matchingVfModules = matchingVfModules + "</vfModules>"                          \r
552                                 taskProcessor.utils.log("DEBUG", "Matching VF Modules: " + matchingVfModules, isDebugEnabled)\r
553                                 String lowestUnusedIndex = taskProcessor.utils.getLowestUnusedIndex(matchingVfModules)\r
554                                 return Integer.parseInt(lowestUnusedIndex)\r
555                         }                       \r
556                 }\r
557                 else {\r
558                         return 0\r
559                 }\r
560         }\r
561 \r
562 }