VNF Recreate
[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.logging.ref.slf4j.ONAPLogConstants
27 import org.onap.so.bpmn.core.UrnPropertiesReader;
28 import org.springframework.web.util.UriUtils;
29
30 import org.onap.so.bpmn.core.json.JsonUtils
31 import org.onap.so.client.HttpClient
32 import groovy.json.JsonBuilder
33 import groovy.json.JsonSlurper
34 import groovy.util.slurpersupport.GPathResult
35 import groovy.xml.QName;
36
37 import javax.ws.rs.core.MediaType
38 import javax.ws.rs.core.Response
39 import org.camunda.bpm.engine.delegate.DelegateExecution
40
41 import org.onap.so.logger.MsoLogger;
42 import org.onap.so.utils.TargetEntity
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                                                 vfModuleModelJson.put("isBase", jsonUtils.getJsonBooleanValueForKey(vfModule, "isBase"))
986                                                 String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
987                                                 vfModuleModelJson.put("vfModuleLabel", vfModuleLabel)
988                                                 Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
989                                                 vfModuleModelJson.put("initialCount", initialCount.intValue())
990                                                 vfModuleInfo.put(vfModuleModelJson)
991                                         }
992                                         modelJson.put("vfModules", vfModuleInfo)
993                                 }
994                                 modelInfos.put(modelJson)
995                         }
996
997                         String modelInfosString = modelInfos.toString()
998                         msoLogger.debug("Returning vnfs JSON: " + modelInfosString)
999
1000                 } catch (Exception e) {
1001                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1002                 }
1003
1004                 return modelInfos
1005         }
1006
1007         /**
1008          * This method parses a Vf Module from the
1009          * Vf Modules array
1010          *
1011          * @param catalogDbResponse
1012          * @param arrayName
1013          * @param catalogUtilsVersion
1014          * @return vfModulelJson
1015          */
1016         private JSONObject parseVfModuleJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
1017                 JSONObject vfModulelJson = new JSONObject()
1018                 msoLogger.debug("Started Parse Vf Module Json")
1019                 try {
1020                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1021                         JSONArray vfModules = responseJson.getJSONArray(arrayName)
1022                         if(vfModules != null){
1023                                 JSONObject vfModuleInfo = new JSONObject()
1024                                 for (int i = 0; i < vfModules.length(); i++) {
1025                                         JSONObject vfModule = vfModules.getJSONObject(i)
1026                                         JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule, catalogUtilsVersion)
1027                                         vfModulelJson.put("modelInfo", vfModuleModelInfo)
1028                                         String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
1029                                         vfModulelJson.put("vfModuleType", vfModuleType)
1030                                         switch(catalogUtilsVersion) {
1031                                                 case "v1":
1032                                                         Integer isBase = jsonUtils.getJsonIntValueForKey(vfModule, "isBase")
1033                                                         if (isBase.intValue() == 1) {
1034                                                                 vfModulelJson.put("isBase", "true")
1035                                                         }
1036                                                         else {
1037                                                                 vfModulelJson.put("isBase", "false")
1038                                                         }
1039                                                         break
1040                                                 default:
1041                                                         boolean isBase = jsonUtils.getJsonBooleanValueForKey(vfModule, "isBase")
1042                                                         vfModulelJson.put("isBase", isBase)
1043                                                         break
1044                                         }
1045                                         String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
1046                                         vfModulelJson.put("vfModuleLabel", vfModuleLabel)
1047                                         Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
1048                                         vfModulelJson.put("initialCount", initialCount.intValue())
1049                                 }
1050                         }
1051                         msoLogger.debug("Completed Parsing Vf Module: " + vfModulelJson.toString())
1052                 }catch (Exception e){
1053                         msoLogger.debug("Exception while parsing Vf Modules from Catalog DB Response: " + e.message)
1054                 }
1055
1056                 return vfModulelJson
1057         }
1058
1059         private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
1060                 JSONArray modelInfos = null
1061
1062                 msoLogger.debug("parseAllottedResourcesJson - catalogUtilsVersion is " + catalogUtilsVersion)
1063
1064                 try {
1065                         // Create array of jsons
1066
1067                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1068                         JSONArray allottedResources = responseJson.getJSONArray(arrayName)
1069                         modelInfos = new JSONArray()
1070
1071                         for (int i = 0; i < allottedResources.length(); i++) {
1072                                 JSONObject allottedResource = allottedResources.getJSONObject(i)
1073                                 JSONObject modelInfo = buildModelInfo("allottedResource", allottedResource, catalogUtilsVersion)
1074                                 JSONObject modelJson = new JSONObject()
1075                                 modelJson.put("modelInfo", modelInfo)
1076                                 switch(catalogUtilsVersion) {
1077                                         case "v1":
1078                                                 break
1079                                         default:
1080                                                 String toscaNodeType = jsonUtils.getJsonValueForKey(allottedResource, "toscaNodeType")
1081                                                 modelJson.put("toscaNodeType", toscaNodeType)
1082                                                 String nfType = jsonUtils.getJsonValueForKey(allottedResource, "nfType")
1083                                                 modelJson.put("nfType", nfType)
1084                                                 String nfRole = jsonUtils.getJsonValueForKey(allottedResource, "nfRole")
1085                                                 modelJson.put("nfRole", nfRole)
1086                                                 String nfCode = jsonUtils.getJsonValueForKey(allottedResource, "nfCode")
1087                                                 modelJson.put("nfNamingCode", nfCode)
1088                                                 String nfFunction = jsonUtils.getJsonValueForKey(allottedResource, "nfFunction")
1089                                                 modelJson.put("nfFunction", nfFunction)
1090                                                 String providingServiceModelName = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelName")
1091                                                 modelJson.put("providingServiceModelName", providingServiceModelName)
1092                                                 String providingServiceModelUuid = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelUuid")
1093                                                 modelJson.put("providingServiceModelUuid", providingServiceModelUuid)
1094                                                 break
1095                                 }
1096
1097
1098                                 modelInfos.put(modelJson)
1099                         }
1100
1101                         String modelInfosString = modelInfos.toString()
1102                         msoLogger.debug("Returning allottedResources JSON: " + modelInfosString)
1103
1104                 } catch (Exception e) {
1105                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1106                 }
1107
1108                 return modelInfos
1109         }
1110
1111         //TODO this is wrong
1112         private JSONObject parseServiceResourcesJson (String catalogDbResponse) {
1113                 JSONObject serviceResources = new JSONObject()
1114                 String catalogUtilsVersion = "v1"
1115
1116                 try {
1117                         // Create array of jsons
1118
1119                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1120                         JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
1121                         JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "vnfResources", catalogUtilsVersion)
1122                         serviceResources.put("vnfs", vnfsArray)
1123                         JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "networkResourceCustomization", catalogUtilsVersion)
1124                         serviceResources.put("networks", networksArray)
1125                         JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "allottedResourceCustomization", catalogUtilsVersion)
1126                         serviceResources.put("allottedResources", allottedResourcesArray)
1127
1128                         String serviceResourcesString = serviceResources.toString()
1129                         msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesString)
1130
1131                 } catch (Exception e) {
1132                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1133                 }
1134
1135                 return serviceResources
1136         }
1137
1138         private JSONObject parseServiceResourcesJson (String catalogDbResponse, String catalogUtilsVersion) {
1139                 JSONObject serviceResources = new JSONObject()
1140                 JSONObject serviceResourcesObject = new JSONObject()
1141                 String serviceResourcesString = ""
1142
1143                 try {
1144                         // Create array of jsons
1145
1146                         JSONObject responseJson = new JSONObject(catalogDbResponse)
1147                         JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
1148                         JSONObject modelInfo = buildModelInfo("", serviceResourcesRoot, catalogUtilsVersion)
1149                         serviceResources.put("modelInfo", modelInfo)
1150                         JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "serviceVnfs", catalogUtilsVersion)
1151                         serviceResources.put("serviceVnfs", vnfsArray)
1152                         JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "serviceNetworks", catalogUtilsVersion)
1153                         serviceResources.put("serviceNetworks", networksArray)
1154                         JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "serviceAllottedResources", catalogUtilsVersion)
1155                         serviceResources.put("serviceAllottedResources", allottedResourcesArray)
1156                         serviceResourcesObject.put("serviceResources", serviceResources)
1157
1158                         serviceResourcesString = serviceResourcesObject.toString()
1159                         msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesString)
1160
1161                 } catch (Exception e) {
1162                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1163                 }
1164
1165                 return serviceResourcesObject
1166         }
1167
1168         private JSONObject buildModelInfo(String modelType, JSONObject modelFromDb, String catalogUtilsVersion) {
1169                 JSONObject modelInfo = null
1170                 try {
1171                         modelInfo = new JSONObject()
1172                         modelInfo.put("modelType", modelType)
1173                         String modelInvariantId = jsonUtils.getJsonValueForKey(modelFromDb, "modelInvariantUuid")
1174                         modelInfo.put("modelInvariantId", modelInvariantId)
1175                         if(modelType.equalsIgnoreCase("allottedResource") || modelType.equalsIgnoreCase("vnf")){
1176                                 String modelInstanceName = jsonUtils.getJsonValueForKey(modelFromDb, "modelInstanceName")
1177                                 modelInfo.put("modelInstanceName", modelInstanceName)
1178                         }
1179                         if ((!"vfModule".equals(modelType) && !"vnf".equals(modelType)) || !catalogUtilsVersion.equals("v1")) {
1180                                 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "modelUuid")
1181                                 modelInfo.put("modelVersionId", modelVersionId)
1182                         }
1183                         else {
1184                                 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "asdcUuid")
1185                                 modelInfo.put("modelVersionId", modelVersionId)
1186                         }
1187                         String modelName = jsonUtils.getJsonValueForKey(modelFromDb, "modelName")
1188                         modelInfo.put("modelName", modelName)
1189                         String modelVersion = jsonUtils.getJsonValueForKey(modelFromDb, "modelVersion")
1190                         modelInfo.put("modelVersion", modelVersion)
1191                         if (!"vfModule".equals(modelType)) {
1192                                 String modelCustomizationName = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationName")
1193                                 modelInfo.put("modelCustomizationName", modelCustomizationName)
1194                         }
1195                         String modelCustomizationId = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationUuid")
1196                         switch (catalogUtilsVersion) {
1197                                 case "v1":
1198                                         modelInfo.put("modelCustomizationId", modelCustomizationId)
1199                                         break
1200                                 default:
1201                                         modelInfo.put("modelCustomizationUuid", modelCustomizationId)
1202                                         break
1203                         }
1204                         JSONObject modelJson = new JSONObject()
1205                         modelJson.put("modelInfo", modelInfo)
1206                 }
1207                 catch (Exception e) {
1208                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception while parsing model information", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
1209                 }
1210                 return modelInfo
1211         }
1212
1213         private String getResponseFromCatalogDb (DelegateExecution execution, String endPoint) {
1214                 try {
1215
1216                         String catalogDbEndpoint = UrnPropertiesReader.getVariable("mso.catalog.db.endpoint",execution)
1217                         String queryEndpoint = catalogDbEndpoint + "/" + defaultDbAdapterVersion + endPoint
1218                         def responseData = ''
1219                         def bpmnRequestId = UUID.randomUUID().toString()
1220
1221                         URL url = new URL(queryEndpoint)
1222                         HttpClient client = new HttpClient(url, MediaType.APPLICATION_JSON, TargetEntity.CATALOG_DB)
1223                         client.addAdditionalHeader(ONAPLogConstants.Headers.REQUEST_ID, bpmnRequestId)
1224                         client.addAdditionalHeader('X-FromAppId', "BPMN")
1225                         client.addAdditionalHeader('Accept', MediaType.APPLICATION_JSON)
1226                         String basicAuthCred = execution.getVariable("BasicAuthHeaderValueDB")
1227                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {
1228                                 client.addAdditionalHeader("Authorization", basicAuthCred)
1229                         }else {
1230                                 client.addAdditionalHeader("Authorization", getBasicDBAuthHeader(execution))
1231                         }
1232
1233                         msoLogger.debug('sending GET to Catalog DB endpoint: ' + endPoint)
1234                         Response response = client.get()
1235
1236                         responseData = response.readEntity(String.class)
1237                         if (responseData != null) {
1238                                 msoLogger.debug("Received data from Catalog DB: " + responseData)
1239                         }
1240
1241                         msoLogger.debug('Response code:' + response.getStatus())
1242                         msoLogger.debug('Response:' + System.lineSeparator() + responseData)
1243                         if (response.getStatus() == 200) {
1244                                 // parse response as needed
1245                                 return responseData
1246                         }
1247                         else {
1248                                 return null
1249                         }
1250                 }
1251                 catch (Exception e) {
1252                         msoLogger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
1253                         throw e
1254                 }
1255
1256         }
1257
1258         /**
1259          * get resource recipe by resource model uuid and action
1260          */
1261         public JSONObject getResourceRecipe(DelegateExecution execution, String resourceModelUuid, String action) {
1262                 String endPoint = "/resourceRecipe?resourceModelUuid=" + UriUtils.encode(resourceModelUuid, "UTF-8")+ "&action=" + UriUtils.encode(action, "UTF-8")
1263                 JSONObject responseJson = null
1264                 try {
1265                         msoLogger.debug("ENDPOINT: " + endPoint)
1266                         String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
1267
1268                         if (catalogDbResponse != null) {
1269                                 responseJson = new JSONObject(catalogDbResponse)
1270                         }
1271                 }
1272                 catch (Exception e) {
1273                         utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
1274                         throw e
1275                 }
1276
1277                 return responseJson
1278         }
1279
1280         private String getBasicDBAuthHeader(DelegateExecution execution) {
1281
1282                 String encodedString = null
1283                 try {
1284                         String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
1285                         utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB)
1286
1287                         encodedString = utils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
1288                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)
1289                 } catch (IOException ex) {
1290                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
1291                         utils.log("ERROR", dataErrorMessage)
1292                 }
1293                 return encodedString
1294         }
1295
1296 }