[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / CatalogDbUtils.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 org.json.JSONObject;
24 import org.json.JSONArray;
25 import org.json.XML;
26 import org.springframework.web.util.UriUtils;
27
28 import org.openecomp.mso.bpmn.core.json.JsonUtils
29
30
31 import groovy.json.JsonBuilder
32 import groovy.json.JsonSlurper
33 import groovy.util.slurpersupport.GPathResult
34 import groovy.xml.QName;
35
36 import org.openecomp.mso.logger.MsoLogger;
37 import org.openecomp.mso.rest.APIResponse;
38 import org.openecomp.mso.rest.RESTClient
39 import org.openecomp.mso.rest.RESTConfig
40
41
42 /***
43  * Utilities for accessing Catalog DB Adapter to retrieve Networks, VNF/VFModules, AllottedResources and complete ServiceResources information
44  *
45  */
46
47 class CatalogDbUtils {
48
49         MsoUtils utils = new MsoUtils()
50         JsonUtils jsonUtils = new JsonUtils()
51         MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
52
53         public JSONArray getAllNetworksByServiceModelUuid(String catalogDbEndpoint, String serviceModelUuid) {
54                 JSONArray networksList = null
55                 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
56                 try {
57                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
58
59                         if (catalogDbResponse != null) {
60                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
61                         }
62
63                 }
64                 catch (Exception e) {
65                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
66                 }
67
68                 return networksList
69         }
70
71         public JSONArray getAllNetworksByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
72                 JSONArray networksList = null
73                 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
74                 try {
75                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
76
77                         if (catalogDbResponse != null) {
78                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
79                         }
80
81                 }
82                 catch (Exception e) {
83                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
84                 }
85
86                 return networksList
87         }
88
89         public JSONArray getAllNetworksByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
90                 JSONArray networksList = null
91                 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
92                 try {
93                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
94
95                         if (catalogDbResponse != null) {
96                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
97                         }
98
99                 }
100                 catch (Exception e) {
101                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
102                 }
103
104                 return networksList
105         }
106
107         public JSONArray getAllNetworksByNetworkModelCustomizationUuid(String catalogDbEndpoint, String networkModelCustomizationUuid) {
108                 JSONArray networksList = null
109                 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?networkModelCustomizationUuid=" + UriUtils.encode(networkModelCustomizationUuid, "UTF-8")
110                 try {
111                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
112
113                         if (catalogDbResponse != null) {
114                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
115                         }
116
117                 }
118                 catch (Exception e) {
119                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
120                 }
121
122                 return networksList
123         }
124
125         public JSONArray getAllNetworksByNetworkType(String catalogDbEndpoint, String networkType) {
126                 JSONArray networksList = null
127                 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?networkType=" + UriUtils.encode(networkType, "UTF-8")
128                 try {
129                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
130
131                         if (catalogDbResponse != null) {
132                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
133                         }
134
135                 }
136                 catch (Exception e) {
137                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
138                 }
139
140                 return networksList
141         }
142
143
144         public JSONArray getAllVnfsByServiceModelUuid(String catalogDbEndpoint, String serviceModelUuid) {
145                 JSONArray vnfsList = null
146                 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
147                 try {
148                         msoLogger.debug("ENDPOINT: " + endPoint)
149                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
150
151                         if (catalogDbResponse != null) {
152                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
153                         }
154
155                 }
156                 catch (Exception e) {
157                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
158                 }
159
160                 return vnfsList
161         }
162
163         public JSONArray getAllVnfsByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
164                 JSONArray vnfsList = null
165                 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
166                 try {
167                         msoLogger.debug("ENDPOINT: " + endPoint)
168                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
169
170                         if (catalogDbResponse != null) {
171                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
172                         }
173
174                 }
175                 catch (Exception e) {
176                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
177                 }
178
179                 return vnfsList
180         }
181
182         public JSONArray getAllVnfsByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
183                 JSONArray vnfsList = null
184                 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
185                 try {
186                         msoLogger.debug("ENDPOINT: " + endPoint)
187                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
188
189                         if (catalogDbResponse != null) {
190                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
191                         }
192
193                 }
194                 catch (Exception e) {
195                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
196                 }
197
198                 return vnfsList
199         }
200
201         public JSONArray getAllVnfsByVnfModelCustomizationUuid(String catalogDbEndpoint, String vnfModelCustomizationUuid) {
202                 JSONArray vnfsList = null
203                 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
204                 try {
205                         msoLogger.debug("ENDPOINT: " + endPoint)
206                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
207
208                         if (catalogDbResponse != null) {
209                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
210                         }
211
212                 }
213                 catch (Exception e) {
214                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
215                 }
216
217                 return vnfsList
218         }
219
220
221         public JSONArray getAllottedResourcesByServiceModelUuid(String catalogDbEndpoint, String serviceModelUuid) {
222                 JSONArray vnfsList = null
223                 String endPoint = catalogDbEndpoint + "/v1/ServiceAllottedResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
224                 try {
225                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
226
227                         if (catalogDbResponse != null) {
228                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
229                         }
230
231                 }
232                 catch (Exception e) {
233                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
234                 }
235
236                 return vnfsList
237         }
238
239         public JSONArray getAllottedResourcesByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
240                 JSONArray vnfsList = null
241                 String endPoint = catalogDbEndpoint + "/v1/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
242                 try {
243                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
244
245                         if (catalogDbResponse != null) {
246                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
247                         }
248
249                 }
250                 catch (Exception e) {
251                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
252                 }
253
254                 return vnfsList
255         }
256
257         public JSONArray getAllottedResourcesByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
258                 JSONArray vnfsList = null
259                 String endPoint = catalogDbEndpoint + "/v1/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
260                 try {
261                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
262
263                         if (catalogDbResponse != null) {
264                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
265                         }
266
267                 }
268                 catch (Exception e) {
269                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
270                 }
271
272                 return vnfsList
273         }
274
275         public JSONArray getAllottedResourcesByArModelCustomizationUuid(String catalogDbEndpoint, String arModelCustomizationUuid) {
276                 JSONArray vnfsList = null
277                 String endPoint = catalogDbEndpoint + "/v1/serviceAllottedResources?serviceModelCustomizationUuid=" + UriUtils.encode(arModelCustomizationUuid, "UTF-8")
278                 try {
279                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
280
281                         if (catalogDbResponse != null) {
282                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
283                         }
284
285                 }
286                 catch (Exception e) {
287                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
288                 }
289
290                 return vnfsList
291         }
292
293         public JSONObject getServiceResourcesByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
294                 JSONObject resources = null
295                 String endPoint = catalogDbEndpoint + "/v1/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
296                 try {
297                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
298
299                         if (catalogDbResponse != null) {
300                                 resources = parseServiceResourcesJson(catalogDbResponse)
301                         }
302
303                 }
304                 catch (Exception e) {
305                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
306                 }
307
308                 return resources
309         }
310
311         public JSONObject getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
312                 JSONObject resources = null
313                 String endPoint = catalogDbEndpoint + "/v1/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
314                 try {
315                         String catalogDbResponse = getResponseFromCatalogDb(endPoint)
316
317                         if (catalogDbResponse != null) {
318                                 resources = parseServiceResourcesJson(catalogDbResponse)
319                         }
320
321                 }
322                 catch (Exception e) {
323                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
324                 }
325
326                 return resources
327         }
328
329
330
331         private JSONArray parseNetworksJson (String catalogDbResponse, String arrayName) {
332                 JSONArray modelInfos = null
333
334                 try {
335                         // Create array of jsons
336
337                         JSONObject responseJson = new JSONObject(catalogDbResponse)
338                         JSONArray networks = responseJson.getJSONArray(arrayName)
339                         modelInfos = new JSONArray()
340
341                         for (int i = 0; i < networks.length(); i++) {
342
343                                 JSONObject network = networks.getJSONObject(i)
344                                 JSONObject modelInfo = buildModelInfo("network", network)
345                                 JSONObject modelJson = new JSONObject()
346                                 modelJson.put("modelInfo", modelInfo)
347                                 String networkType = jsonUtils.getJsonValueForKey(network, "networkType")
348                                 modelJson.put("networkType", networkType)
349                                 modelInfos.put(modelJson)
350                         }
351
352                         String modelInfosString = modelInfos.toString()
353                         msoLogger.debug("Returning networks JSON: " + modelInfosString)
354
355                 } catch (Exception e) {
356                         utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
357                 }
358
359                 return modelInfos
360         }
361
362         private JSONArray parseVnfsJson (String catalogDbResponse, String arrayName) {
363                 JSONArray modelInfos = null
364
365                 try {
366                         // Create array of jsons
367
368                         JSONObject responseJson = new JSONObject(catalogDbResponse)
369                         JSONArray vnfs = responseJson.getJSONArray(arrayName)
370                         modelInfos = new JSONArray()
371
372                         for (int i = 0; i < vnfs.length(); i++) {
373                                 JSONObject vnf = vnfs.getJSONObject(i)
374
375                                 msoLogger.debug(vnf.toString(2))
376                                 JSONObject modelInfo = buildModelInfo("vnf", vnf)
377                                 JSONObject modelJson = new JSONObject()
378                                 modelJson.put("modelInfo", modelInfo)
379
380                                 JSONArray vfModules = null
381                                 try {
382                                         vfModules = vnf.getJSONArray("vfModules")
383                                 } catch (Exception e)
384                                 {
385                                         msoLogger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
386                                 }
387
388                                 if (vfModules != null) {
389                                         JSONArray vfModuleInfo = new JSONArray()
390                                         for (int j = 0; j < vfModules.length(); j++) {
391                                                 JSONObject vfModule = vfModules.getJSONObject(j)
392                                                 JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule)
393                                                 JSONObject vfModuleModelJson = new JSONObject()
394                                                 vfModuleModelJson.put("modelInfo", vfModuleModelInfo)
395                                                 String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
396                                                 vfModuleModelJson.put("vfModuleType", vfModuleType)
397                                                 Integer isBase = jsonUtils.getJsonIntValueForKey(vfModule, "isBase")
398                                                 if (isBase.intValue() == 1) {
399                                                         vfModuleModelJson.put("isBase", "true")
400                                                 }
401                                                 else {
402                                                         vfModuleModelJson.put("isBase", "false")
403                                                 }
404                                                 String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
405                                                 vfModuleModelJson.put("vfModuleLabel", vfModuleLabel)
406                                                 Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
407                                                 vfModuleModelJson.put("initialCount", initialCount.intValue())
408                                                 vfModuleInfo.put(vfModuleModelJson)
409                                         }
410                                         modelJson.put("vfModules", vfModuleInfo)
411                                 }
412                                 modelInfos.put(modelJson)
413                         }
414
415                         String modelInfosString = modelInfos.toString()
416                         msoLogger.debug("Returning vnfs JSON: " + modelInfosString)
417
418                 } catch (Exception e) {
419                         utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
420                 }
421
422                 return modelInfos
423         }
424
425         private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName) {
426                 JSONArray modelInfos = null
427
428                 try {
429                         // Create array of jsons
430
431                         JSONObject responseJson = new JSONObject(catalogDbResponse)
432                         JSONArray allottedResources = responseJson.getJSONArray(arrayName)
433                         modelInfos = new JSONArray()
434
435                         for (int i = 0; i < allottedResources.length(); i++) {
436                                 JSONObject allottedResource = allottedResources.getJSONObject(i)
437                                 JSONObject modelInfo = buildModelInfo("allottedResource", allottedResource)
438                                 JSONObject modelJson = new JSONObject()
439                                 modelJson.put("modelInfo", modelInfo)
440                                 modelInfos.put(modelJson)
441                         }
442
443                         String modelInfosString = modelInfos.toString()
444                         msoLogger.debug("Returning allottedResources JSON: " + modelInfosString)
445
446                 } catch (Exception e) {
447                         utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
448                 }
449
450                 return modelInfos
451         }
452
453         private JSONObject parseServiceResourcesJson (String catalogDbResponse) {
454                 JSONObject serviceResources = new JSONObject()
455
456                 try {
457                         // Create array of jsons
458
459                         JSONObject responseJson = new JSONObject(catalogDbResponse)
460                         JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
461                         JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "vnfResources")
462                         serviceResources.put("vnfs", vnfsArray)
463                         JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "networkResourceCustomization")
464                         serviceResources.put("networks", networksArray)
465                         JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "allottedResourceCustomization")
466                         serviceResources.put("allottedResources", allottedResourcesArray)
467
468                         String serviceResourcesString = serviceResources.toString()
469                         msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesString)
470
471                 } catch (Exception e) {
472                         utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
473                 }
474
475                 return serviceResources
476         }
477
478         private JSONObject buildModelInfo(String modelType, JSONObject modelFromDb) {
479                 JSONObject modelInfo = null
480                 try {
481                         modelInfo = new JSONObject()
482                         modelInfo.put("modelType", modelType)
483                         String modelInvariantId = jsonUtils.getJsonValueForKey(modelFromDb, "modelInvariantUuid")
484                         modelInfo.put("modelInvariantId", modelInvariantId)
485                         if(modelType.equalsIgnoreCase("allottedResource")){
486                                 String modelInstanceName = jsonUtils.getJsonValueForKey(modelFromDb, "modelInstanceName")
487                                 modelInfo.put("modelInstanceName", modelInstanceName)
488                         }
489                         if (!"vfModule".equals(modelType) && !"vnf".equals(modelType)) {
490                                 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "modelUuid")
491                                 modelInfo.put("modelVersionId", modelVersionId)
492                         }
493                         else {
494                                 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "asdcUuid")
495                                 modelInfo.put("modelVersionId", modelVersionId)
496                         }
497                         String modelName = jsonUtils.getJsonValueForKey(modelFromDb, "modelName")
498                         modelInfo.put("modelName", modelName)
499                         String modelVersion = jsonUtils.getJsonValueForKey(modelFromDb, "modelVersion")
500                         modelInfo.put("modelVersion", modelVersion)
501                         if (!"vfModule".equals(modelType)) {
502                                 String modelCustomizationName = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationName")
503                                 modelInfo.put("modelCustomizationName", modelCustomizationName)
504                         }
505                         String modelCustomizationId = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationUuid")
506                         modelInfo.put("modelCustomizationId", modelCustomizationId)
507                         JSONObject modelJson = new JSONObject()
508                         modelJson.put("modelInfo", modelInfo)
509                 }
510                 catch (Exception e) {
511                         utils.log("ERROR", "Exception while parsing model information: " + e.message)
512                 }
513                 return modelInfo
514         }
515
516         private String getResponseFromCatalogDb (String endPoint) {
517                 try {
518                         RESTConfig config = new RESTConfig(endPoint);
519                         def responseData = ''
520                         def bpmnRequestId = UUID.randomUUID().toString()
521                         RESTClient client = new RESTClient(config).
522                                 addHeader('X-TransactionId', bpmnRequestId).
523                                 addHeader('X-FromAppId', 'BPMN').
524                                 addHeader('Content-Type', 'application/json').
525                                 addHeader('Accept','application/json');
526                         msoLogger.debug('sending GET to Catalog DB endpoint' + endPoint)
527                         APIResponse response = client.httpGet()
528
529                         responseData = response.getResponseBodyAsString()
530                         if (responseData != null) {
531                                 msoLogger.debug("Received data from Catalog DB: " + responseData)
532                         }
533
534                         msoLogger.debug('Response code:' + response.getStatusCode())
535                         msoLogger.debug('Response:' + System.lineSeparator() + responseData)
536                         if (response.getStatusCode() == 200) {
537                                 // parse response as needed
538                                 return responseData
539                         }
540                         else {
541                                 return null
542                         }
543                 }
544                 catch (Exception e) {
545                         msoLogger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
546                         return null
547                 }
548
549         }
550 }