Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / CatalogDbUtils.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.common.scripts
22
23 import org.json.JSONObject;
24 import org.json.JSONArray;
25 import org.json.XML
26 import org.onap.so.bpmn.core.UrnPropertiesReader;
27 import org.springframework.web.util.UriUtils;
28
29 import org.onap.so.bpmn.core.json.JsonUtils
30
31
32 import groovy.json.JsonBuilder
33 import groovy.json.JsonSlurper
34 import groovy.util.slurpersupport.GPathResult
35 import groovy.xml.QName;
36
37 import org.camunda.bpm.engine.delegate.DelegateExecution
38
39 import org.onap.so.logger.MsoLogger;
40 import org.onap.so.rest.APIResponse;
41 import org.onap.so.rest.RESTClient
42 import org.onap.so.rest.RESTConfig
43 import org.onap.so.logger.MessageEnum
44
45
46
47 /***
48  * Utilities for accessing Catalog DB Adapter to retrieve Networks, VNF/VFModules, AllottedResources and complete ServiceResources information
49  *
50  */
51
52 class CatalogDbUtils {
53         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CatalogDbUtils.class);
54
55
56         MsoUtils utils = new MsoUtils()
57         JsonUtils jsonUtils = new JsonUtils()
58         static private String defaultDbAdapterVersion = "v2"
59
60         public JSONArray getAllNetworksByServiceModelUuid(DelegateExecution execution, String serviceModelUuid) {
61                 JSONArray networksList = null
62                 String endPoint = "/serviceNetworks?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
63                 try {
64                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
65
66                         if (catalogDbResponse != null) {
67                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", "v1")
68                         }
69
70                 }
71                 catch (Exception e) {
72                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
73                         throw e
74                 }
75
76                 return networksList
77         }
78
79         public JSONArray getAllNetworksByServiceModelUuid(DelegateExecution execution, String serviceModelUuid, String catalogUtilsVersion) {
80                 JSONArray networksList = null
81                 String endPoint = "/serviceNetworks?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
82                 try {
83                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
84
85                         if (catalogDbResponse != null) {
86                                 if (!catalogUtilsVersion.equals("v1")) {
87                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
88                                         networksList = responseJson.getJSONArray("serviceNetworks")
89                                 }
90                                 else {
91                                         networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", catalogUtilsVersion)
92                                 }
93                         }
94
95                 }
96                 catch (Exception e) {
97                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
98                         throw e
99                 }
100
101                 return networksList
102         }
103
104         public JSONArray getAllNetworksByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid) {
105                 JSONArray networksList = null
106                 String endPoint = "/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
107                 try {
108                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
109
110                         if (catalogDbResponse != null) {
111                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", "v1")
112                         }
113
114                 }
115                 catch (Exception e) {
116                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
117                         throw e
118                 }
119
120                 return networksList
121         }
122
123         public JSONArray getAllNetworksByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
124                 JSONArray networksList = null
125                 String endPoint = "/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
126                 try {
127                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
128
129                         if (catalogDbResponse != null) {
130                                 if (!catalogUtilsVersion.equals("v1")) {
131                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
132                                         networksList = responseJson.getJSONArray("serviceNetworks")
133                                 }
134                                 else {
135                                         networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", catalogUtilsVersion)
136                                 }
137                         }
138
139                 }
140                 catch (Exception e) {
141                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
142                         throw e
143                 }
144
145                 return networksList
146         }
147
148         public JSONArray getAllNetworksByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion) {
149                 JSONArray networksList = null
150                 String endPoint = "/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
151                 try {
152                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
153
154                         if (catalogDbResponse != null) {
155                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", "v1")
156                         }
157
158                 }
159                 catch (Exception e) {
160                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
161                         throw e
162                 }
163
164                 return networksList
165         }
166
167         public JSONArray getAllNetworksByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion, String catalogUtilsVersion) {
168                 JSONArray networksList = null
169                 String endPoint = "/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
170                 try {
171                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
172
173                         if (catalogDbResponse != null) {
174                                 if (!catalogUtilsVersion.equals("v1")) {
175                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
176                                         networksList = responseJson.getJSONArray("serviceNetworks")
177                                 }
178                                 else {
179                                         networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", catalogUtilsVersion)
180                                 }
181                         }
182
183                 }
184                 catch (Exception e) {
185                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
186                         throw e
187                 }
188
189                 return networksList
190         }
191
192         public JSONArray getAllNetworksByNetworkModelCustomizationUuid(DelegateExecution execution, String networkModelCustomizationUuid) {
193                 JSONArray networksList = null
194                 String endPoint = "/serviceNetworks?networkModelCustomizationUuid=" + UriUtils.encode(networkModelCustomizationUuid, "UTF-8")
195                 try {
196                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
197
198                         if (catalogDbResponse != null) {
199                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", "v1")
200                         }
201
202                 }
203                 catch (Exception e) {
204                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
205                         throw e
206                 }
207
208                 return networksList
209         }
210
211         public JSONArray getAllNetworksByNetworkModelCustomizationUuid(DelegateExecution execution, String networkModelCustomizationUuid, String catalogUtilsVersion) {
212                 JSONArray networksList = null
213                 String endPoint = "/serviceNetworks?networkModelCustomizationUuid=" + UriUtils.encode(networkModelCustomizationUuid, "UTF-8")
214                 try {
215                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
216
217                         if (catalogDbResponse != null) {
218                                 if (!catalogUtilsVersion.equals("v1")) {
219                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
220                                         networksList = responseJson.getJSONArray("serviceNetworks")
221                                 }
222                                 else {
223                                         networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", catalogUtilsVersion)
224                                 }
225                         }
226
227                 }
228                 catch (Exception e) {
229                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
230                         throw e
231                 }
232
233                 return networksList
234         }
235
236         public JSONArray getAllNetworksByNetworkType(DelegateExecution execution, String networkType) {
237                 JSONArray networksList = null
238                 String endPoint = "/serviceNetworks?networkType=" + UriUtils.encode(networkType, "UTF-8")
239                 try {
240                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
241
242                         if (catalogDbResponse != null) {
243                                 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", "v1")
244                         }
245
246                 }
247                 catch (Exception e) {
248                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
249                         throw e
250                 }
251
252                 return networksList
253         }
254
255         public JSONArray getAllNetworksByNetworkType(DelegateExecution execution, String networkType, String catalogUtilsVersion) {
256                 JSONArray networksList = null
257                 String endPoint = "/serviceNetworks?networkType=" + UriUtils.encode(networkType, "UTF-8")
258                 try {
259                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
260
261                         if (catalogDbResponse != null) {
262                                 if (!catalogUtilsVersion.equals("v1")) {
263                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
264                                         networksList = responseJson.getJSONArray("serviceNetworks")
265                                 }
266                                 else {
267                                         networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks", catalogUtilsVersion)
268                                 }
269                         }
270
271                 }
272                 catch (Exception e) {
273                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
274                         throw e
275                 }
276
277                 return networksList
278         }
279
280
281         public JSONArray getAllVnfsByServiceModelUuid(DelegateExecution execution, String serviceModelUuid) {
282                 JSONArray vnfsList = null
283                 String endPoint = "/serviceVnfs?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
284                 try {
285                         msoLogger.debug("ENDPOINT: " + endPoint)
286                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
287
288                         if (catalogDbResponse != null) {
289                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", "v1")
290                         }
291
292                 }
293                 catch (Exception e) {
294                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
295                         throw e
296                 }
297
298                 return vnfsList
299         }
300
301         public JSONArray getAllVnfsByServiceModelUuid(DelegateExecution execution, String serviceModelUuid, String catalogUtilsVersion) {
302                 JSONArray vnfsList = null
303                 String endPoint = "/serviceVnfs?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
304                 try {
305                         msoLogger.debug("ENDPOINT: " + endPoint)
306                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
307
308                         if (catalogDbResponse != null) {
309                                 if (!catalogUtilsVersion.equals("v1")) {
310                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
311                                         vnfsList = responseJson.getJSONArray("serviceVnfs")
312                                 }
313                                 else {
314                                         vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", catalogUtilsVersion)
315                                 }
316                         }
317
318                 }
319                 catch (Exception e) {
320                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
321                         throw e
322                 }
323
324                 return vnfsList
325         }
326
327         public JSONArray getAllVnfsByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid) {
328                 JSONArray vnfsList = null
329                 String endPoint ="/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
330                 try {
331                         msoLogger.debug("ENDPOINT: " + endPoint)
332                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
333
334                         if (catalogDbResponse != null) {
335                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", "v1")
336                         }
337
338                 }
339                 catch (Exception e) {
340                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
341                         throw e
342                 }
343
344                 return vnfsList
345         }
346
347         public JSONArray getAllVnfsByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
348                 JSONArray vnfsList = null
349                 String endPoint = "/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
350                 try {
351                         msoLogger.debug("ENDPOINT: " + endPoint)
352                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
353
354                         if (catalogDbResponse != null) {
355                                 if (!catalogUtilsVersion.equals("v1")) {
356                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
357                                         vnfsList = responseJson.getJSONArray("serviceVnfs")
358                                 }
359                                 else {
360                                         vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", catalogUtilsVersion)
361                                 }
362                         }
363
364                 }
365                 catch (Exception e) {
366                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
367                         throw e
368                 }
369
370                 return vnfsList
371         }
372
373         public JSONArray getAllVnfsByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion) {
374                 JSONArray vnfsList = null
375                 String endPoint =  "/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
376                 try {
377                         msoLogger.debug("ENDPOINT: " + endPoint)
378                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
379
380                         if (catalogDbResponse != null) {
381                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", "v1")
382                         }
383
384                 }
385                 catch (Exception e) {
386                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
387                         throw e
388                 }
389
390                 return vnfsList
391         }
392
393         public JSONArray getAllVnfsByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion, String catalogUtilsVersion) {
394                 JSONArray vnfsList = null
395                 String endPoint = "/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
396                 try {
397                         msoLogger.debug("ENDPOINT: " + endPoint)
398                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
399
400                         if (catalogDbResponse != null) {
401                                 if (!catalogUtilsVersion.equals("v1")) {
402                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
403                                         vnfsList = responseJson.getJSONArray("serviceVnfs")
404                                 }
405                                 else {
406                                         vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", catalogUtilsVersion)
407                                 }
408                         }
409
410                 }
411                 catch (Exception e) {
412                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
413                         throw e
414                 }
415
416                 return vnfsList
417         }
418
419         public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid) {
420                 JSONArray vnfsList = null
421                 String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
422                 try {
423                         msoLogger.debug("ENDPOINT: " + endPoint)
424                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
425
426                         if (catalogDbResponse != null) {
427                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", "v1")
428                         }
429
430                 }
431                 catch (Exception e) {
432                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
433                         throw e
434                 }
435
436                 return vnfsList
437         }
438
439         /**
440          * This method gets a all vnfs for a particular
441          * service from the catalog database using the
442          * service model's model name.
443          *
444          * @param catalogDbEndpoint
445          * @param serviceModelModelName
446          * @return vnfsList      *
447          *
448          */
449         public JSONArray getAllVnfsByServiceModelModelName(DelegateExecution execution, String serviceModelModelName) {
450                 JSONArray vnfsList = null
451                 String endPoint = "/serviceVnfs?serviceModelName=" + UriUtils.encode(serviceModelModelName, "UTF-8")
452                 try {
453                         msoLogger.debug("ENDPOINT: " + endPoint)
454                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
455
456                         if (catalogDbResponse != null) {
457                                 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", defaultDbAdapterVersion)
458                         }
459                 }catch (Exception e) {
460                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
461                         throw e
462                 }
463                 return vnfsList
464         }
465
466         public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid, String catalogUtilsVersion) {
467                 JSONArray vnfsList = null
468                 String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
469                 try {
470                         msoLogger.debug("ENDPOINT: " + endPoint)
471                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
472
473                         if (catalogDbResponse != null) {
474                                 if (!catalogUtilsVersion.equals("v1")) {
475                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
476                                         vnfsList = responseJson.getJSONArray("serviceVnfs")
477                                 }
478                                 else {
479                                         vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", catalogUtilsVersion)
480                                 }
481                         }
482
483                 }
484                 catch (Exception e) {
485                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
486                         throw e
487                 }
488
489                 return vnfsList
490         }
491
492         /**
493          * This method gets a single vf module from
494          * the catalog database using the vf module's
495          * model name. It returns that vf module as
496          * a JSONObject
497          *
498          * @param catalogDbEndpoint
499          * @param vfModuleModelName
500          * @return vfModule
501          */
502         public JSONObject getVfModuleByVfModuleModelName(DelegateExecution execution, String vfModuleModelName) {
503                 JSONObject vfModule = null
504                 String endPoint = "/vfModules?vfModuleModelName=" + UriUtils.encode(vfModuleModelName, "UTF-8")
505                 try{
506                         msoLogger.debug("Get VfModule By VfModule ModelName Endpoint is: " + endPoint)
507                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
508
509                         if (catalogDbResponse != null) {
510                                 vfModule = parseVfModuleJson(catalogDbResponse, "vfModules", "v1")
511                         }
512                 }
513                 catch(Exception e){
514                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
515                         throw e
516                 }
517
518                 return vfModule
519         }
520
521         /**
522          * This method gets a single vf module from
523          * the catalog database using the vf module's
524          * model name. It returns that vf module as
525          * a JSONObject
526          *
527          * @param catalogDbEndpoint
528          * @param vfModuleModelName
529          * @param catalogUtilsVersion
530          * @return vfModules
531          */
532         public JSONObject getVfModuleByVfModuleModelName(DelegateExecution execution, String vfModuleModelName, String catalogUtilsVersion)  {
533                 JSONObject vfModule = null
534                 String endPoint = "/vfModules?vfModuleModelName=" + UriUtils.encode(vfModuleModelName, "UTF-8")
535                 try{
536                         msoLogger.debug("Get VfModule By VfModule ModelName Endpoint is: " + endPoint)
537                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
538
539                         if (catalogDbResponse != null) {
540                                 vfModule = parseVfModuleJson(catalogDbResponse, "vfModules", "v1")
541                         }
542                 }
543                 catch(Exception e){
544                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
545                         throw e
546                 }
547
548                 return vfModule
549         }
550
551
552         public JSONArray getAllottedResourcesByServiceModelUuid(DelegateExecution execution, String serviceModelUuid) {
553                 JSONArray vnfsList = null
554                 String endPoint = "/ServiceAllottedResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
555                 try {
556                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
557
558                         if (catalogDbResponse != null) {
559                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", "v1")
560                         }
561
562                 }
563                 catch (Exception e) {
564                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
565                         throw e
566                 }
567
568                 return vnfsList
569         }
570
571         public JSONArray getAllottedResourcesByServiceModelUuid(DelegateExecution execution, String serviceModelUuid, String catalogUtilsVersion) {
572                 JSONArray vnfsList = null
573                 String endPoint = "/ServiceAllottedResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
574                 try {
575                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
576
577                         if (catalogDbResponse != null) {
578                                 if (!catalogUtilsVersion.equals("v1")) {
579                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
580                                         vnfsList = responseJson.getJSONArray("serviceAllottedResources")
581                                 }
582                                 else {
583                                         vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", catalogUtilsVersion)
584                                 }
585                         }
586
587                 }
588                 catch (Exception e) {
589                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
590                         throw e
591                 }
592
593                 return vnfsList
594         }
595
596         public JSONArray getAllottedResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid) {
597                 JSONArray vnfsList = null
598                 String endPoint = "/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
599                 try {
600                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
601
602                         if (catalogDbResponse != null) {
603                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", "v1")
604                         }
605
606                 }
607                 catch (Exception e) {
608                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
609                         throw e
610                 }
611
612                 return vnfsList
613         }
614
615         public JSONArray getAllottedResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
616                 JSONArray vnfsList = null
617                 String endPoint = "/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
618                 try {
619                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
620
621                         if (catalogDbResponse != null) {
622                                 if (!catalogUtilsVersion.equals("v1")) {
623                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
624                                         vnfsList = responseJson.getJSONArray()
625                                 }
626                                 else {
627                                         vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", catalogUtilsVersion)
628                                 }
629                         }
630
631                 }
632                 catch (Exception e) {
633                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.getStackTrace());
634                         throw e
635                 }
636
637                 return vnfsList
638         }
639
640         public JSONArray getAllottedResourcesByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion) {
641                 JSONArray vnfsList = null
642                 String endPoint = "/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
643                 try {
644                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
645
646                         if (catalogDbResponse != null) {
647                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", "v1")
648                         }
649
650                 }
651                 catch (Exception e) {
652                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
653                         throw e
654                 }
655
656                 return vnfsList
657         }
658
659         public JSONArray getAllottedResourcesByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion, String catalogUtilsVersion) {
660                 JSONArray vnfsList = null
661                 String endPoint = "/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
662                 try {
663                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
664
665                         if (catalogDbResponse != null) {
666                                 if (!catalogUtilsVersion.equals("v1")) {
667                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
668                                         vnfsList = responseJson.getJSONArray("serviceAllottedResources")
669                                 }
670                                 else {
671                                         vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", catalogUtilsVersion)
672                                 }
673                         }
674
675                 }
676                 catch (Exception e) {
677                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
678                         throw e
679                 }
680
681                 return vnfsList
682         }
683
684
685         public JSONArray getAllottedResourcesByArModelCustomizationUuid(DelegateExecution execution, String arModelCustomizationUuid) {
686                 JSONArray vnfsList = null
687                 String endPoint = "/serviceAllottedResources?serviceModelCustomizationUuid=" + UriUtils.encode(arModelCustomizationUuid, "UTF-8")
688                 try {
689                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
690
691                         if (catalogDbResponse != null) {
692                                 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", "v1")
693                         }
694
695                 }
696                 catch (Exception e) {
697                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
698                         throw e
699                 }
700
701                 return vnfsList
702         }
703
704         public JSONArray getAllottedResourcesByArModelCustomizationUuid(DelegateExecution execution, String arModelCustomizationUuid, String catalogUtilsVersion) {
705                 JSONArray vnfsList = null
706                 String endPoint = "/serviceAllottedResources?serviceModelCustomizationUuid=" + UriUtils.encode(arModelCustomizationUuid, "UTF-8")
707                 try {
708                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
709
710                         if (catalogDbResponse != null) {
711                                 if (!catalogUtilsVersion.equals("v1")) {
712                                         JSONObject responseJson = new JSONObject(catalogDbResponse)
713                                         vnfsList = responseJson.getJSONArray("serviceAllottedResources")
714                                 }
715                                 else {
716                                         vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources", catalogUtilsVersion)
717                                 }
718                         }
719
720                 }
721                 catch (Exception e) {
722                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
723                         throw e
724                 }
725
726                 return vnfsList
727         }
728
729         public JSONObject getServiceResourcesByServiceModelUuid(DelegateExecution execution, String serviceModelUuid) {
730                 JSONObject resources = null
731                 String endPoint = "/serviceResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
732                 try {
733                     String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
734         
735                     if (catalogDbResponse != null) {
736         
737                         resources = parseServiceResourcesJson(catalogDbResponse, "v1")
738                     }
739         
740                 }
741                 catch (Exception e) {
742                     utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
743                         throw e
744                 }
745
746                 return resources
747         }
748
749         public JSONObject getServiceResourcesByServiceModelUuid(DelegateExecution execution, String serviceModelUuid, String catalogUtilsVersion) {
750                 JSONObject resources = null
751                 String endPoint = "/serviceResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
752                 try {
753                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
754
755                         if (catalogDbResponse != null) {
756                                 if (!catalogUtilsVersion.equals("v1")) {
757                                         resources = new JSONObject(catalogDbResponse)
758                                 }
759                                 else {
760                                         resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
761                                 }
762                         }
763                 }
764                 catch (Exception e) {
765                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
766                         throw e
767                 }
768
769                 return resources
770         }
771
772         public JSONObject getServiceResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid) {
773                 JSONObject resources = null
774                 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
775                 try {
776                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
777
778                         if (catalogDbResponse != null) {
779
780                                 resources = parseServiceResourcesJson(catalogDbResponse, "v1")
781                         }
782
783                 }
784                 catch (Exception e) {
785                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
786                         throw e
787                 }
788
789                 return resources
790         }
791
792         public String getServiceResourcesByServiceModelInvariantUuidString(DelegateExecution execution, String serviceModelInvariantUuid) {
793                 String resources = null
794                 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
795                 try {
796                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
797
798                         if (catalogDbResponse != null) {
799
800                                 resources = catalogDbResponse
801                         }
802
803                 }
804                 catch (Exception e) {
805                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
806                         throw e
807                 }
808
809                 return resources
810         }
811
812         public JSONObject getServiceResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
813                 JSONObject resources = null
814                 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
815                 try {
816                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
817
818                         if (catalogDbResponse != null) {
819                                 if (!catalogUtilsVersion.equals("v1")) {
820                                         resources = new JSONObject(catalogDbResponse)
821                                 }
822                                 else {
823                                         resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
824                                 }
825                         }
826
827                 }
828                 catch (Exception e) {
829                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
830                         throw e
831                 }
832
833                 return resources
834         }
835
836
837         public JSONObject getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion) {
838                 JSONObject resources = null
839                 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
840                 try {
841                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
842
843                         if (catalogDbResponse != null) {
844                                 //TODO this is wrong
845                                 resources = parseServiceResourcesJson(catalogDbResponse)
846                         }
847
848                 }
849                 catch (Exception e) {
850                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
851                         throw e
852                 }
853
854                 return resources
855         }
856
857         public JSONObject getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion, String catalogUtilsVersion) {
858                 JSONObject resources = null
859                 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
860                 try {
861                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
862
863                         if (catalogDbResponse != null) {
864                                 if (!catalogUtilsVersion.equals("v1")) {
865                                         resources = new JSONObject(catalogDbResponse)
866                                 }
867                                 else {
868                                         resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
869                                 }
870                         }
871
872                 }
873                 catch (Exception e) {
874                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
875                         throw e
876                 }
877
878                 return resources
879         }
880
881
882
883         private JSONArray parseNetworksJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
884                 JSONArray modelInfos = null
885
886                 msoLogger.debug("parseNetworksJson - catalogUtilsVersion is " + catalogUtilsVersion)
887                 try {
888                         // Create array of jsons
889
890                         JSONObject responseJson = new JSONObject(catalogDbResponse)
891                         JSONArray networks = responseJson.getJSONArray(arrayName)
892                         modelInfos = new JSONArray()
893
894                         for (int i = 0; i < networks.length(); i++) {
895
896                                 JSONObject network = networks.getJSONObject(i)
897                                 JSONObject modelJson = new JSONObject()
898                                 JSONObject modelInfo = buildModelInfo("network", network, catalogUtilsVersion)
899                                 modelJson.put("modelInfo", modelInfo)
900                                 String networkType = jsonUtils.getJsonValueForKey(network, "networkType")
901                                 modelJson.put("networkType", networkType)
902
903                                 switch (catalogUtilsVersion) {
904                                         case "v1":
905                                                 break
906                                         default:
907                                                 String toscaNodeType = jsonUtils.getJsonValueForKey(network, "toscaNodeType")
908                                                 modelJson.put("toscaNodeType", toscaNodeType)
909                                                 String networkTechnology = jsonUtils.getJsonValueForKey(network, "networkTechnology")
910                                                 modelJson.put("networkTechnology", networkTechnology)
911                                                 String networkRole = jsonUtils.getJsonValueForKey(network, "networkRole")
912                                                 modelJson.put("networkRole", networkRole)
913                                                 String networkScope = jsonUtils.getJsonValueForKey(network, "networkScope")
914                                                 modelJson.put("networkScope", networkScope)
915                                                 break
916                                 }
917                                 modelInfos.put(modelJson)
918                         }
919
920                         String modelInfosString = modelInfos.toString()
921                         msoLogger.debug("Returning networks JSON: " + modelInfosString)
922
923                 } catch (Exception e) {
924                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
925                 }
926
927                 return modelInfos
928         }
929
930         private JSONArray parseVnfsJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
931                 JSONArray modelInfos = null
932
933                 msoLogger.debug("parseVnfsJson - catalogUtilsVersion is " + catalogUtilsVersion)
934
935                 try {
936                         // Create array of jsons
937
938                         JSONObject responseJson = new JSONObject(catalogDbResponse)
939                         JSONArray vnfs = responseJson.getJSONArray(arrayName)
940                         modelInfos = new JSONArray()
941
942                         for (int i = 0; i < vnfs.length(); i++) {
943                                 JSONObject vnf = vnfs.getJSONObject(i)
944
945                                 msoLogger.debug(vnf.toString(2))
946                                 JSONObject modelInfo = buildModelInfo("vnf", vnf, catalogUtilsVersion)
947                                 JSONObject modelJson = new JSONObject()
948                                 modelJson.put("modelInfo", modelInfo)
949                                 switch(catalogUtilsVersion) {
950                                         case "v1":
951                                                 break
952                                         default:
953                                                 String toscaNodeType = jsonUtils.getJsonValueForKey(vnf, "toscaNodeType")
954                                                 modelJson.put("toscaNodeType", toscaNodeType)
955                                                 String nfType = jsonUtils.getJsonValueForKey(vnf, "nfType")
956                                                 modelJson.put("nfType", nfType)
957                                                 String nfRole = jsonUtils.getJsonValueForKey(vnf, "nfRole")
958                                                 modelJson.put("nfRole", nfRole)
959                                                 String nfCode = jsonUtils.getJsonValueForKey(vnf, "nfCode")
960                                                 modelJson.put("nfNamingCode", nfCode)
961                                                 String nfFunction = jsonUtils.getJsonValueForKey(vnf, "nfFunction")
962                                                 modelJson.put("nfFunction", nfFunction)
963                                                 String multiStageDesign = jsonUtils.getJsonValueForKey(vnf, "multiStageDesign")
964                                                 modelJson.put("multiStageDesign", multiStageDesign)
965                                                 break
966                                 }
967
968                                 JSONArray vfModules = null
969                                 try {
970                                         vfModules = vnf.getJSONArray("vfModules")
971                                 } catch (Exception e)
972                                 {
973                                         msoLogger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
974                                 }
975
976                                 if (vfModules != null) {
977                                         JSONArray vfModuleInfo = new JSONArray()
978                                         for (int j = 0; j < vfModules.length(); j++) {
979                                                 JSONObject vfModule = vfModules.getJSONObject(j)
980                                                 JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule, catalogUtilsVersion)
981                                                 JSONObject vfModuleModelJson = new JSONObject()
982                                                 vfModuleModelJson.put("modelInfo", vfModuleModelInfo)
983                                                 String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
984                                                 vfModuleModelJson.put("vfModuleType", vfModuleType)
985                                                 switch(catalogUtilsVersion) {
986                                                         case "v1":
987                                                         //TODO this does not work, isBase is not a integer.
988                                                                 Integer isBase = jsonUtils.getJsonIntValueForKey(vfModule, "isBase")
989                                                                 if (isBase.intValue() == 1) {
990                                                                         vfModuleModelJson.put("isBase", "true")
991                                                                 }
992                                                                 else {
993                                                                         vfModuleModelJson.put("isBase", "false")
994                                                                 }
995                                                                 break
996                                                         default:
997                                                                 boolean isBase = jsonUtils.getJsonBooleanValueForKey(vfModule, "isBase")
998                                                                 vfModuleModelJson.put("isBase", isBase)
999                                                                 break
1000                                                 }
1001                                                 String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
1002                                                 vfModuleModelJson.put("vfModuleLabel", vfModuleLabel)
1003                                                 Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
1004                                                 vfModuleModelJson.put("initialCount", initialCount.intValue())
1005                                                 vfModuleInfo.put(vfModuleModelJson)
1006                                         }
1007                                         modelJson.put("vfModules", vfModuleInfo)
1008                                 }
1009                                 modelInfos.put(modelJson)
1010                         }
1011
1012                         String modelInfosString = modelInfos.toString()
1013                         msoLogger.debug("Returning vnfs JSON: " + modelInfosString)
1014
1015                 } catch (Exception e) {
1016                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1017                 }
1018
1019                 return modelInfos
1020         }
1021
1022         /**
1023          * This method parses a Vf Module from the
1024          * Vf Modules array
1025          *
1026          * @param catalogDbResponse
1027          * @param arrayName
1028          * @param catalogUtilsVersion
1029          * @return vfModulelJson
1030          */
1031         private JSONObject parseVfModuleJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
1032                 JSONObject vfModulelJson = new JSONObject()
1033                 msoLogger.debug("Started Parse Vf Module Json")
1034                 try {
1035                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1036                         JSONArray vfModules = responseJson.getJSONArray(arrayName)
1037                         if(vfModules != null){
1038                                 JSONObject vfModuleInfo = new JSONObject()
1039                                 for (int i = 0; i < vfModules.length(); i++) {
1040                                         JSONObject vfModule = vfModules.getJSONObject(i)
1041                                         JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule, catalogUtilsVersion)
1042                                         vfModulelJson.put("modelInfo", vfModuleModelInfo)
1043                                         String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
1044                                         vfModulelJson.put("vfModuleType", vfModuleType)
1045                                         switch(catalogUtilsVersion) {
1046                                                 case "v1":
1047                                                         Integer isBase = jsonUtils.getJsonIntValueForKey(vfModule, "isBase")
1048                                                         if (isBase.intValue() == 1) {
1049                                                                 vfModulelJson.put("isBase", "true")
1050                                                         }
1051                                                         else {
1052                                                                 vfModulelJson.put("isBase", "false")
1053                                                         }
1054                                                         break
1055                                                 default:
1056                                                         boolean isBase = jsonUtils.getJsonBooleanValueForKey(vfModule, "isBase")
1057                                                         vfModulelJson.put("isBase", isBase)
1058                                                         break
1059                                         }
1060                                         String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
1061                                         vfModulelJson.put("vfModuleLabel", vfModuleLabel)
1062                                         Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
1063                                         vfModulelJson.put("initialCount", initialCount.intValue())
1064                                 }
1065                         }
1066                         msoLogger.debug("Completed Parsing Vf Module: " + vfModulelJson.toString())
1067                 }catch (Exception e){
1068                         msoLogger.debug("Exception while parsing Vf Modules from Catalog DB Response: " + e.message)
1069                 }
1070
1071                 return vfModulelJson
1072         }
1073
1074         private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
1075                 JSONArray modelInfos = null
1076
1077                 msoLogger.debug("parseAllottedResourcesJson - catalogUtilsVersion is " + catalogUtilsVersion)
1078
1079                 try {
1080                         // Create array of jsons
1081
1082                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1083                         JSONArray allottedResources = responseJson.getJSONArray(arrayName)
1084                         modelInfos = new JSONArray()
1085
1086                         for (int i = 0; i < allottedResources.length(); i++) {
1087                                 JSONObject allottedResource = allottedResources.getJSONObject(i)
1088                                 JSONObject modelInfo = buildModelInfo("allottedResource", allottedResource, catalogUtilsVersion)
1089                                 JSONObject modelJson = new JSONObject()
1090                                 modelJson.put("modelInfo", modelInfo)
1091                                 switch(catalogUtilsVersion) {
1092                                         case "v1":
1093                                                 break
1094                                         default:
1095                                                 String toscaNodeType = jsonUtils.getJsonValueForKey(allottedResource, "toscaNodeType")
1096                                                 modelJson.put("toscaNodeType", toscaNodeType)
1097                                                 String nfType = jsonUtils.getJsonValueForKey(allottedResource, "nfType")
1098                                                 modelJson.put("nfType", nfType)
1099                                                 String nfRole = jsonUtils.getJsonValueForKey(allottedResource, "nfRole")
1100                                                 modelJson.put("nfRole", nfRole)
1101                                                 String nfCode = jsonUtils.getJsonValueForKey(allottedResource, "nfCode")
1102                                                 modelJson.put("nfNamingCode", nfCode)
1103                                                 String nfFunction = jsonUtils.getJsonValueForKey(allottedResource, "nfFunction")
1104                                                 modelJson.put("nfFunction", nfFunction)
1105                                                 String providingServiceModelName = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelName")
1106                                                 modelJson.put("providingServiceModelName", providingServiceModelName)
1107                                                 String providingServiceModelUuid = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelUuid")
1108                                                 modelJson.put("providingServiceModelUuid", providingServiceModelUuid)
1109                                                 break
1110                                 }
1111
1112
1113                                 modelInfos.put(modelJson)
1114                         }
1115
1116                         String modelInfosString = modelInfos.toString()
1117                         msoLogger.debug("Returning allottedResources JSON: " + modelInfosString)
1118
1119                 } catch (Exception e) {
1120                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1121                 }
1122
1123                 return modelInfos
1124         }
1125
1126         //TODO this is wrong
1127         private JSONObject parseServiceResourcesJson (String catalogDbResponse) {
1128                 JSONObject serviceResources = new JSONObject()
1129                 String catalogUtilsVersion = "v1"
1130
1131                 try {
1132                         // Create array of jsons
1133
1134                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1135                         JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
1136                         JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "vnfResources", catalogUtilsVersion)
1137                         serviceResources.put("vnfs", vnfsArray)
1138                         JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "networkResourceCustomization", catalogUtilsVersion)
1139                         serviceResources.put("networks", networksArray)
1140                         JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "allottedResourceCustomization", catalogUtilsVersion)
1141                         serviceResources.put("allottedResources", allottedResourcesArray)
1142
1143                         String serviceResourcesString = serviceResources.toString()
1144                         msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesString)
1145
1146                 } catch (Exception e) {
1147                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1148                 }
1149
1150                 return serviceResources
1151         }
1152
1153         private JSONObject parseServiceResourcesJson (String catalogDbResponse, String catalogUtilsVersion) {
1154                 JSONObject serviceResources = new JSONObject()
1155                 JSONObject serviceResourcesObject = new JSONObject()
1156                 String serviceResourcesString = ""
1157
1158                 try {
1159                         // Create array of jsons
1160
1161                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1162                         JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
1163                         JSONObject modelInfo = buildModelInfo("", serviceResourcesRoot, catalogUtilsVersion)
1164                         serviceResources.put("modelInfo", modelInfo)
1165                         JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "serviceVnfs", catalogUtilsVersion)
1166                         serviceResources.put("serviceVnfs", vnfsArray)
1167                         JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "serviceNetworks", catalogUtilsVersion)
1168                         serviceResources.put("serviceNetworks", networksArray)
1169                         JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "serviceAllottedResources", catalogUtilsVersion)
1170                         serviceResources.put("serviceAllottedResources", allottedResourcesArray)
1171                         serviceResourcesObject.put("serviceResources", serviceResources)
1172
1173                         serviceResourcesString = serviceResourcesObject.toString()
1174                         msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesString)
1175
1176                 } catch (Exception e) {
1177                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1178                 }
1179
1180                 return serviceResourcesObject
1181         }
1182
1183         private JSONObject buildModelInfo(String modelType, JSONObject modelFromDb, String catalogUtilsVersion) {
1184                 JSONObject modelInfo = null
1185                 try {
1186                         modelInfo = new JSONObject()
1187                         modelInfo.put("modelType", modelType)
1188                         String modelInvariantId = jsonUtils.getJsonValueForKey(modelFromDb, "modelInvariantUuid")
1189                         modelInfo.put("modelInvariantId", modelInvariantId)
1190                         if(modelType.equalsIgnoreCase("allottedResource") || modelType.equalsIgnoreCase("vnf")){
1191                                 String modelInstanceName = jsonUtils.getJsonValueForKey(modelFromDb, "modelInstanceName")
1192                                 modelInfo.put("modelInstanceName", modelInstanceName)
1193                         }
1194                         if ((!"vfModule".equals(modelType) && !"vnf".equals(modelType)) || !catalogUtilsVersion.equals("v1")) {
1195                                 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "modelUuid")
1196                                 modelInfo.put("modelVersionId", modelVersionId)
1197                         }
1198                         else {
1199                                 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "asdcUuid")
1200                                 modelInfo.put("modelVersionId", modelVersionId)
1201                         }
1202                         String modelName = jsonUtils.getJsonValueForKey(modelFromDb, "modelName")
1203                         modelInfo.put("modelName", modelName)
1204                         String modelVersion = jsonUtils.getJsonValueForKey(modelFromDb, "modelVersion")
1205                         modelInfo.put("modelVersion", modelVersion)
1206                         if (!"vfModule".equals(modelType)) {
1207                                 String modelCustomizationName = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationName")
1208                                 modelInfo.put("modelCustomizationName", modelCustomizationName)
1209                         }
1210                         String modelCustomizationId = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationUuid")
1211                         switch (catalogUtilsVersion) {
1212                                 case "v1":
1213                                         modelInfo.put("modelCustomizationId", modelCustomizationId)
1214                                         break
1215                                 default:
1216                                         modelInfo.put("modelCustomizationUuid", modelCustomizationId)
1217                                         break
1218                         }
1219                         JSONObject modelJson = new JSONObject()
1220                         modelJson.put("modelInfo", modelInfo)
1221                 }
1222                 catch (Exception e) {
1223                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception while parsing model information", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1224                 }
1225                 return modelInfo
1226         }
1227
1228         private String getResponseFromCatalogDb (DelegateExecution execution, String endPoint) {
1229                 try {
1230
1231                         String catalogDbEndpoint = UrnPropertiesReader.getVariable("mso.catalog.db.endpoint",execution)
1232                         String queryEndpoint = catalogDbEndpoint + "/" + defaultDbAdapterVersion + endPoint
1233                         RESTConfig config = new RESTConfig(queryEndpoint);
1234                         def responseData = ''
1235                         def bpmnRequestId = UUID.randomUUID().toString()
1236                         RESTClient client = new RESTClient(config).
1237                                         addHeader('X-TransactionId', bpmnRequestId).
1238                                         addHeader('X-FromAppId', 'BPMN').
1239                                         addHeader('Content-Type', 'application/json').
1240                                         addHeader('Accept','application/json');
1241
1242                         String basicAuthCred = execution.getVariable("BasicAuthHeaderValueDB")
1243                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {
1244                                         client.addAuthorizationHeader(basicAuthCred)
1245                         }else {
1246                                 client.addAuthorizationHeader(getBasicDBAuthHeader(execution))
1247                         }
1248                         msoLogger.debug('sending GET to Catalog DB endpoint: ' + endPoint)
1249                         APIResponse response = client.httpGet()
1250
1251                         responseData = response.getResponseBodyAsString()
1252                         if (responseData != null) {
1253                                 msoLogger.debug("Received data from Catalog DB: " + responseData)
1254                         }
1255
1256                         msoLogger.debug('Response code:' + response.getStatusCode())
1257                         msoLogger.debug('Response:' + System.lineSeparator() + responseData)
1258                         if (response.getStatusCode() == 200) {
1259                                 // parse response as needed
1260                                 return responseData
1261                         }
1262                         else {
1263                                 return null
1264                         }
1265                 }
1266                 catch (Exception e) {
1267                         msoLogger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
1268                         throw e
1269                 }
1270
1271         }
1272
1273         /**
1274          * get resource recipe by resource model uuid and action
1275          */
1276         public JSONObject getResourceRecipe(DelegateExecution execution, String resourceModelUuid, String action) {
1277                 String endPoint = "/resourceRecipe?resourceModelUuid=" + UriUtils.encode(resourceModelUuid, "UTF-8")+ "&action=" + UriUtils.encode(action, "UTF-8")
1278                 JSONObject responseJson = null
1279                 try {
1280                         msoLogger.debug("ENDPOINT: " + endPoint)
1281                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
1282
1283                         if (catalogDbResponse != null) {
1284                                 responseJson = new JSONObject(catalogDbResponse)
1285                         }
1286                 }
1287                 catch (Exception e) {
1288                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
1289                         throw e
1290                 }
1291
1292                 return responseJson
1293         }
1294         
1295         private String getBasicDBAuthHeader(DelegateExecution execution) {
1296                 
1297                 String encodedString = null
1298                 try {
1299                         String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
1300                         utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB)
1301                         
1302                         encodedString = utils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
1303                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)
1304                 } catch (IOException ex) {
1305                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
1306                         utils.log("ERROR", dataErrorMessage)
1307                 }
1308                 return encodedString
1309         }
1310         
1311 }