Merge "Modify readme of VFC Adapter"
[so.git] / mso-catalog-db / src / main / java / org / openecomp / mso / db / catalog / CatalogDatabase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.mso.db.catalog;
23
24 import java.io.Closeable;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32
33 import org.hibernate.HibernateException;
34 import org.hibernate.Query;
35 import org.hibernate.Session;
36 import org.openecomp.mso.db.AbstractSessionFactoryManager;
37 import org.openecomp.mso.db.catalog.beans.AllottedResource;
38 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
39 import org.openecomp.mso.db.catalog.beans.ArRecipe;
40 import org.openecomp.mso.db.catalog.beans.HeatEnvironment;
41 import org.openecomp.mso.db.catalog.beans.HeatFiles;
42 import org.openecomp.mso.db.catalog.beans.HeatNestedTemplate;
43 import org.openecomp.mso.db.catalog.beans.HeatTemplate;
44 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
45 import org.openecomp.mso.db.catalog.beans.Model;
46 import org.openecomp.mso.db.catalog.beans.ModelRecipe;
47 import org.openecomp.mso.db.catalog.beans.NetworkRecipe;
48 import org.openecomp.mso.db.catalog.beans.NetworkResource;
49 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
50 import org.openecomp.mso.db.catalog.beans.Service;
51 import org.openecomp.mso.db.catalog.beans.ServiceMacroHolder;
52 import org.openecomp.mso.db.catalog.beans.ServiceRecipe;
53 import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;
54 import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;
55 import org.openecomp.mso.db.catalog.beans.ServiceToResourceCustomization;
56 import org.openecomp.mso.db.catalog.beans.TempNetworkHeatTemplateLookup;
57 import org.openecomp.mso.db.catalog.beans.ToscaCsar;
58 import org.openecomp.mso.db.catalog.beans.VfModule;
59 import org.openecomp.mso.db.catalog.beans.VfModuleCustomization;
60 import org.openecomp.mso.db.catalog.beans.VfModuleToHeatFiles;
61 import org.openecomp.mso.db.catalog.beans.VnfComponent;
62 import org.openecomp.mso.db.catalog.beans.VnfComponentsRecipe;
63 import org.openecomp.mso.db.catalog.beans.VnfRecipe;
64 import org.openecomp.mso.db.catalog.beans.VnfResCustomToVfModuleCustom;
65 import org.openecomp.mso.db.catalog.beans.VnfResource;
66 import org.openecomp.mso.db.catalog.beans.VnfResourceCustomization;
67 import org.openecomp.mso.db.catalog.utils.MavenLikeVersioningComparator;
68 import org.openecomp.mso.db.catalog.utils.RecordNotFoundException;
69 import org.openecomp.mso.logger.MessageEnum;
70 import org.openecomp.mso.logger.MsoLogger;
71
72 /**
73  * This class encapsulates all of the objects that can be queried from a Catalog database.
74  * Clients must use these methods to retrieve catalog objects. The session is not
75  * available for clients to do their own direct queries to the database.
76  *
77  *
78  */
79 public class CatalogDatabase implements Closeable {
80
81     protected final AbstractSessionFactoryManager sessionFactoryCatalogDB;
82
83     private static final String NETWORK_TYPE = "networkType";
84     private static final String ACTION = "action";
85     private static final String VNF_TYPE = "vnfType";
86     private static final String SERVICE_TYPE = "serviceType";
87     private static final String MODEL_UUID= "modelUUID";
88     private static final String VNF_COMPONENT_TYPE = "vnfComponentType";
89     private static final String MODEL_ID = "modelId";
90     private static final String MODEL_NAME = "modelName";
91     private static final String MODEL_VERSION = "version";
92     private static final String TYPE = "type";
93     private static final String MODEL_TYPE = "modelType";
94     private static final String MODEL_VERSION_ID = "modelVersionId";
95     private static final String MODEL_CUSTOMIZATION_UUID = "modelCustomizationUuid";
96         private static final String VF_MODULE_MODEL_UUID = "vfModuleModelUUId";
97
98     protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.GENERAL);
99
100     protected Session session = null;
101
102     protected CatalogDatabase (AbstractSessionFactoryManager sessionFactoryCatalog) {
103         sessionFactoryCatalogDB = sessionFactoryCatalog;
104     }
105     
106     public static CatalogDatabase getInstance() {
107         return new CatalogDatabase(new CatalogDbSessionFactoryManager ());
108     }
109     
110     private Session getSession () {
111
112              if (session == null) {
113             try {
114                 session = sessionFactoryCatalogDB.getSessionFactory ().openSession ();
115                 session.beginTransaction ();
116             } catch (HibernateException he) {
117                 LOGGER.error (MessageEnum.GENERAL_EXCEPTION_ARG, "Error creating Hibernate Session: " + he, "", "", MsoLogger.ErrorCode.DataError, "Error creating Hibernate Session: " + he);
118                 throw he;
119             }
120         }
121
122         return session;
123     }
124
125     /**
126      * Close an open Catalog Database session.
127      * This method should always be called when a client is finished using a
128      * CatalogDatabase instance.
129      */
130     @Override
131     public void close () {
132         if (session != null) {
133             session.close ();
134             session = null;
135         }
136     }
137
138     /**
139      * Commits the current transaction on this session and starts a fresh one.
140      */
141     public void commit () {
142         getSession ().getTransaction ().commit ();
143         getSession ().beginTransaction ();
144     }
145
146     /**
147      * Rolls back current transaction and starts a fresh one.
148      */
149     public void rollback () {
150         getSession ().getTransaction ().rollback ();
151         getSession ().beginTransaction ();
152     }
153
154     /**
155      * Return all Heat Templates in the Catalog DB
156      *
157      * @return A list of HeatTemplate objects
158      */
159     @SuppressWarnings("unchecked")
160     public List <HeatTemplate> getAllHeatTemplates() {
161         long startTime = System.currentTimeMillis();
162         LOGGER.debug("Catalog database - get all Heat templates");
163         String hql = "FROM HeatTemplate";
164         Query query = getSession().createQuery(hql);
165
166         List <HeatTemplate> result = query.list();
167         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllHeatTemplates", null);
168         return result;
169     }
170
171     /**
172      * Fetch a specific Heat Template by ID.
173      *
174      * @param templateId template id
175      * @return HeatTemplate object or null if none found
176      */
177     @Deprecated
178     public HeatTemplate getHeatTemplate(int templateId) {
179         long startTime = System.currentTimeMillis();
180         LOGGER.debug ("Catalog database - get Heat template with id " + templateId);
181
182         HeatTemplate template = (HeatTemplate) getSession().get(HeatTemplate.class, templateId);
183         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
184         return template;
185     }
186
187     /**
188      * Return the newest version of a specific Heat Template (queried by Name).
189      *
190      * @param templateName template name
191      * @return HeatTemplate object or null if none found
192      */
193     public HeatTemplate getHeatTemplate(String templateName) {
194
195         long startTime = System.currentTimeMillis();
196         LOGGER.debug("Catalog database - get Heat template with name " + templateName);
197
198         String hql = "FROM HeatTemplate WHERE templateName = :template_name";
199         Query query = getSession().createQuery (hql);
200         query.setParameter("template_name", templateName);
201
202         @SuppressWarnings("unchecked")
203         List <HeatTemplate> resultList = query.list();
204
205         // See if something came back. Name is unique, so
206         if (resultList.isEmpty ()) {
207             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No template found", "CatalogDB", "getHeatTemplate", null);
208             return null;
209         }
210         resultList.sort(new MavenLikeVersioningComparator());
211         Collections.reverse(resultList);
212
213         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
214         return resultList.get(0);
215     }
216
217     /**
218      * Return a specific version of a specific Heat Template (queried by Name).
219      *
220      * @param templateName
221      * @param version
222      * @return HeatTemplate object or null if none found
223      */
224     public HeatTemplate getHeatTemplate(String templateName, String version) {
225
226         long startTime = System.currentTimeMillis();
227         LOGGER.debug("Catalog database - get Heat template with name " + templateName
228                                       + " and version "
229                                       + version);
230
231         String hql = "FROM HeatTemplate WHERE templateName = :template_name AND version = :version";
232         Query query = getSession().createQuery(hql);
233         query.setParameter("template_name", templateName);
234         query.setParameter("version", version);
235
236         @SuppressWarnings("unchecked")
237         List <HeatTemplate> resultList = query.list();
238
239         // See if something came back.
240         if (resultList.isEmpty()) {
241             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No template found.", "CatalogDB", "getHeatTemplate", null);
242             return null;
243         }
244         // Name + Version is unique, so should only be one element
245         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
246         return resultList.get(0);
247     }
248
249     /**
250      * Return a specific Heat Template by ARTIFACT_UUID).
251      *
252      * @param artifactUuid
253      * @return HeatTemplate object or null if none found
254      */    
255     
256     public HeatTemplate getHeatTemplateByArtifactUuid(String artifactUuid) {
257         long startTime = System.currentTimeMillis ();
258         LOGGER.debug ("Catalog database - get Heat template with artifactUuid " + artifactUuid);
259
260         // Will this work if the id is a string? 
261         HeatTemplate template = (HeatTemplate) getSession ().get (HeatTemplate.class, artifactUuid);
262         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
263         return template;
264     }
265     
266     /**
267      * Return a specific Heat Template by ARTIFACT_UUID using standard query method. unique record expected.
268      *
269      * @param artifactUuid
270      * @return HeatTemplate object or null if none found
271      */
272     public HeatTemplate getHeatTemplateByArtifactUuidRegularQuery(String artifactUuid) {
273         long startTime = System.currentTimeMillis();
274         LOGGER.debug("Catalog database - get Heat template (regular query) with artifactUuid " + artifactUuid);
275
276         String hql = "FROM HeatTemplate WHERE artifactUuid = :artifactUuidValue";
277         HashMap<String, String> variables = new HashMap<>();
278         variables.put("artifactUuidValue", artifactUuid);
279         HeatTemplate template = (HeatTemplate) this.executeQuerySingleRow(hql, variables, true);
280
281         if (template == null) {
282             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getHeatTemplateByArtifactUuidRegularQuery", null);
283         } else {
284             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplateByArtifactUuidRegularQuery", null);
285         }
286         return template;
287     }
288     
289     public List<HeatTemplateParam> getParametersForHeatTemplate(String heatTemplateArtifactUuid) {
290         LOGGER.debug ("Catalog database - getParametersForHeatTemplate with artifactUuid " + heatTemplateArtifactUuid);
291
292         String hql = "FROM HeatTemplateParams WHERE artifactUuid = :artifactUuidValue";
293         Query query = getSession().createQuery(hql);
294         query.setParameter ("artifactUuidValue", heatTemplateArtifactUuid);
295         List<HeatTemplateParam> resultList = new ArrayList<>();
296         try {
297                 resultList = query.list ();
298         } catch (org.hibernate.HibernateException he) {
299                 LOGGER.debug("Hibernate Exception - while searching HeatTemplateParams for: heatTemplateArtifactUuid='" + heatTemplateArtifactUuid + "'" + he.getMessage());
300                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching HeatTemplateParams for artifactUuid=" + heatTemplateArtifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for artifactUuid=" + heatTemplateArtifactUuid);
301                 throw he;
302         } catch (Exception e) {
303                 LOGGER.debug("Generic Exception - while searching HeatTemplateParam for: artifactUuid='" + heatTemplateArtifactUuid + "'" + e.getMessage());
304                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching HeatTemplate for artifactUuid=" + heatTemplateArtifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for artifactUuid=" + heatTemplateArtifactUuid);
305                 throw e;
306         }
307         
308         return resultList;
309         
310     }
311     
312     /**
313      * Return a specific Heat Environment by ARTIFACT_UUID using standard query method. unique record expected.
314      *
315      * @param artifactUuid
316      * @return HeatEnvironment object or null if none found
317      */    
318     public HeatEnvironment getHeatEnvironmentByArtifactUuid(String artifactUuid) {
319         long startTime = System.currentTimeMillis();
320         LOGGER.debug("Catalog database - get Heat Environment with artifactUuid " + artifactUuid);
321
322         String hql = "FROM HeatEnvironment WHERE artifactUuid = :artifactUuidValue";
323         Query query = getSession().createQuery(hql);
324         query.setParameter("artifactUuidValue", artifactUuid);
325         HeatEnvironment environment = null;
326         try {
327             environment = (HeatEnvironment) query.uniqueResult();
328         } catch (org.hibernate.NonUniqueResultException nure) {
329                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row for Envt - data integrity error: artifactUuid='" + artifactUuid +"'", nure);
330                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for heatEnvironment artifactUuid=" + artifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for artifactUuid==" + artifactUuid);
331         } catch (org.hibernate.HibernateException he) {
332                 LOGGER.debug("Hibernate Exception - while searching for envt: artifactUuid='" + artifactUuid + "' " + he.getMessage());
333                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching envt for artifactUuid=" + artifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching envt for artifactUuid=" + artifactUuid);
334
335                 throw he;
336         } catch (Exception e) {
337                 LOGGER.debug("Generic Exception - while searching for: artifactUuid='" + artifactUuid + "' " + e.getMessage());
338                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching envt for artifactUuid=" + artifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching envt for artifactUuid=" + artifactUuid);
339
340                 throw e;
341         }
342
343         if (environment == null) {
344                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getHeatEnvironmentByArtifactUuid", null);
345         } else {
346                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatEnvironmentByArtifactUuid", null);
347         }
348         return environment;
349     }
350     
351     /**
352      * Fetch a Service definition by InvariantUUID
353      */
354     public Service getServiceByInvariantUUID (String modelInvariantUUID) {
355
356         long startTime = System.currentTimeMillis();
357         LOGGER.debug("Catalog database - get service with Invariant UUID " + modelInvariantUUID);
358
359         String hql = "FROM Service WHERE modelInvariantUUID = :model_invariant_uuid";
360         Query query = getSession().createQuery(hql);
361         query.setParameter ("model_invariant_uuid", modelInvariantUUID);
362
363         @SuppressWarnings("unchecked")
364         List <Service> resultList = query.list ();
365
366         // See if something came back.
367         if (resultList.isEmpty ()) {
368             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByName", null);
369             return null;
370         }
371         resultList.sort(new MavenLikeVersioningComparator());
372         Collections.reverse (resultList);
373
374         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByName", null);
375         return resultList.get (0);
376     }
377
378     /**
379      * Fetch a Service definition
380      */
381     public Service getService (String modelName) {
382
383         long startTime = System.currentTimeMillis();
384         LOGGER.debug("Catalog database - get service with name " + modelName);
385
386         String hql = "FROM Service WHERE modelName = :MODEL_NAME";
387         Query query = getSession().createQuery(hql);
388         query.setParameter("MODEL_NAME", modelName);
389
390         Service service = null;
391         try {
392                 service = (Service) query.uniqueResult();
393         } catch (org.hibernate.NonUniqueResultException nure) {
394                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelName='" + modelName + "'");
395                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelName=" + modelName, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelName=" + modelName);
396                 throw nure;
397         } catch (org.hibernate.HibernateException he) {
398                 LOGGER.debug("Hibernate Exception - while searching for: modelName='" + modelName + "' " + he.getMessage());
399                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelName=" + modelName, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelName=" + modelName);
400                 throw he;
401         } catch (Exception e) {
402                 LOGGER.debug("Generic Exception - while searching for: modelName='" + modelName + " " + e.getMessage());
403                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelName=" + modelName, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelName=" + modelName);
404                 throw e;
405         }
406         if (service == null) {
407                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getService", null);
408         } else {
409                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getService", null);
410         }
411
412         return service;
413     }
414
415     public Service getServiceByModelUUID (String modelUUID) {
416
417         long startTime = System.currentTimeMillis();
418         LOGGER.debug("Catalog database - get service with Model UUID " + modelUUID);
419
420         String hql = "FROM Service WHERE modelUUID = :MODEL_UUID";
421         HashMap<String, String> parameters = new HashMap<>();
422         parameters.put("MODEL_UUID", modelUUID);
423
424         
425         Service service = this.executeQuerySingleRow(hql, parameters, true);
426
427         /*
428         try {
429                 service = (Service) query.uniqueResult ();
430         } catch (org.hibernate.NonUniqueResultException nure) {
431                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelUUID='" + modelUUID + "'");
432                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelUUID=" + modelUUID, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelUUID=" + modelUUID);
433                 throw nure;
434         } catch (org.hibernate.HibernateException he) {
435                 LOGGER.debug("Hibernate Exception - while searching for: modelUUID='" + modelUUID + "' " + he.getMessage());
436                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelUUID=" + modelUUID, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelUUID=" + modelUUID);
437                 throw he;
438         } catch (Exception e) {
439                 LOGGER.debug("Generic Exception - while searching for: modelUUID='" + modelUUID + " " + e.getMessage());
440                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelUUID=" + modelUUID, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelUUID=" + modelUUID);
441                 throw e;
442         }
443         */
444         if (service == null) {
445                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getServiceByModelUUID", null);
446         } else {
447                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByModelUUID", null);
448         }
449
450         return service;
451     }
452
453     /**
454      * Fetch the Common Service API definition using Http Method + serviceNameVersionId
455      */
456     public Service getService(Map<String, String> map, String httpMethod) {
457
458         String serviceNameVersionId = map.get("serviceNameVersionId");
459         Query query;
460         String serviceId = "not_set";
461         String serviceVersion = "not_set";
462
463         if(serviceNameVersionId != null && serviceNameVersionId.length() > 0){
464                 LOGGER.debug ("Catalog database - get service modelUUID with id " + serviceNameVersionId);
465
466                 String hql = "FROM Service WHERE MODEL_UUID = :MODEL_UUID and http_method = :http_method";
467                 query = getSession().createQuery(hql);
468             query.setParameter("MODEL_UUID", serviceNameVersionId);
469          } else {
470                 serviceId = map.get("serviceId");
471                 serviceVersion = map.get("serviceVersion");
472             LOGGER.debug("Catalog database - get serviceId with id " + serviceId + " and serviceVersion with " + serviceVersion);
473
474             String hql = "FROM Service WHERE service_id = :service_id and service_version = :service_version and http_method = :http_method";
475             query = getSession().createQuery(hql);
476             query.setParameter("service_id", serviceId);
477             query.setParameter("service_version", serviceVersion);
478          }
479
480         query.setParameter("http_method", httpMethod);
481
482         long startTime = System.currentTimeMillis();
483         Service service = null;
484         try {
485                 service = (Service) query.uniqueResult();
486         } catch (org.hibernate.NonUniqueResultException nure) {
487                 LOGGER.debug("Non Unique Result Exception - data integrity error: service_id='" + serviceId + "', serviceVersion='" + serviceVersion + "'");
488                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for service_id=" + serviceId + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for service_id=" + serviceId);
489
490                 throw nure;
491         } catch (org.hibernate.HibernateException he) {
492                 LOGGER.debug("Hibernate Exception - while searching for: service_id='" + serviceId + "', serviceVersion='" + serviceVersion + "' " + he.getMessage());
493                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for service_id=" + serviceId + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for service_id=" + serviceId);
494
495                 throw he;
496         } catch (Exception e) {
497                 LOGGER.debug("Generic Exception - while searching for: service_id='" + serviceId + "', serviceVersion='" + serviceVersion + "' " + e.getMessage());
498                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for service_id=" + serviceId + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for service_id=" + serviceId);
499
500                 throw e;
501         }
502         if (service == null) {
503                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getService", null);
504         } else {
505                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getService", null);
506         }
507         return service;
508     }
509
510     /**
511      * Return the newest version of a Service (queried by Name).
512      *
513      * @param modelName
514      * @return Service object or null if none found
515      */
516     public Service getServiceByModelName(String modelName){
517
518         long startTime = System.currentTimeMillis();
519         LOGGER.debug("Catalog database - get service with name " + modelName);
520
521         String hql = "FROM Service WHERE modelName = :MODEL_NAME";
522         Query query = getSession().createQuery(hql);
523         query.setParameter("MODEL_NAME", modelName);
524
525         @SuppressWarnings("unchecked")
526         List <Service> resultList = query.list();
527
528         // See if something came back.
529         if (resultList.isEmpty()) {
530             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByModelName", null);
531             return null;
532         }
533         resultList.sort(new MavenLikeVersioningComparator());
534         Collections.reverse(resultList);
535
536         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByModelName", null);
537         return resultList.get(0);
538     }
539
540     public Service getServiceByVersionAndInvariantId(String modelInvariantId, String modelVersion) throws Exception {
541         long startTime = System.currentTimeMillis();
542         LOGGER.debug("Catalog database - get service with modelInvariantId: " + modelInvariantId + " and modelVersion: " + modelVersion);
543
544         String hql = "FROM Service WHERE modelInvariantUUID = :MODEL_INVARIANT_UUID AND version = :VERSION_STR";
545         Query query = getSession().createQuery(hql);
546         query.setParameter("MODEL_INVARIANT_UUID", modelInvariantId);
547         query.setParameter("VERSION_STR", modelVersion);
548
549         Service result = null;
550         try {
551             result = (Service) query.uniqueResult();
552         } catch (org.hibernate.NonUniqueResultException nure) {
553             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelInvariantId='" + modelInvariantId + "', modelVersion='" + modelVersion + "'", nure);
554             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelInvariantId=" + modelInvariantId + " and modelVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelInvariantId=" + modelInvariantId);
555             throw new Exception("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelInvariantId='" + modelInvariantId + "', modelVersion='" + modelVersion + "'");
556         }
557         // See if something came back.
558         if (result==null) {
559             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByVersionAndInvariantId", null);
560             return null;
561         }
562
563         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByVersionAndInvariantId", null);
564         return result;
565     }
566
567     /**
568      * Return a newest version of Service recipe that matches a given SERVICE_ID and ACTION
569      *
570      * @param serviceModelUUID
571      * @param action     * 
572      * @return ServiceRecipe object or null if none found
573      */
574     @Deprecated
575     public ServiceRecipe getServiceRecipe (int serviceModelUUID, String action) {
576        
577         StringBuilder hql;
578
579         if(action == null){
580                 hql = new StringBuilder ("FROM ServiceRecipe WHERE serviceModelUUID = :serviceModelUUID");
581         }else {
582                 hql = new StringBuilder ("FROM ServiceRecipe WHERE serviceModelUUID = :serviceModelUUID AND action = :action ");
583         }
584
585         long startTime = System.currentTimeMillis ();
586         LOGGER.debug ("Catalog database - get Service recipe with serviceModelUUID " + Integer.toString(serviceModelUUID)
587                                       + " and action "
588                                       + action
589                                       );
590
591         Query query = getSession ().createQuery (hql.toString ());
592         query.setParameter ("serviceModelUUID", serviceModelUUID);
593         if(action != null){
594                 query.setParameter (ACTION, action);
595         }
596
597                         @SuppressWarnings("unchecked")
598         List <ServiceRecipe> resultList = query.list ();
599
600         if (resultList.isEmpty ()) {
601             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getServiceRecipe", null);
602                                 return null;
603                         }
604
605         resultList.sort(new MavenLikeVersioningComparator());
606         Collections.reverse (resultList);
607
608         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipe", null);
609         return resultList.get (0);
610     }
611
612     /**
613      * Return a newest version of Service recipe that matches a given SERVICE_MODEL_UUID and ACTION
614      *
615      * @param serviceModelUuid
616      * @param action     *
617      * @return ServiceRecipe object or null if none found
618      */
619     public ServiceRecipe getServiceRecipeByServiceModelUuid(String serviceModelUuid, String action) {
620
621         StringBuilder hql;
622
623         if(action == null){
624             hql = new StringBuilder("FROM ServiceRecipe WHERE serviceModelUuid = :serviceModelUuid");
625         }else {
626             hql = new StringBuilder("FROM ServiceRecipe WHERE serviceModelUuid = :serviceModelUuid AND action = :action ");
627         }
628
629         long startTime = System.currentTimeMillis ();
630         LOGGER.debug("Catalog database - get Service recipe with serviceModelUuid " + serviceModelUuid
631                                       + " and action "
632                                       + action
633                                       );
634
635         Query query = getSession().createQuery(hql.toString());
636         query.setParameter("serviceModelUuid", serviceModelUuid);
637         if(action != null){
638             query.setParameter(ACTION, action);
639         }
640
641         @SuppressWarnings("unchecked")
642         List <ServiceRecipe> resultList = query.list();
643
644         if (resultList.isEmpty()) {
645             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getServiceRecipe", null);
646             return null;
647         }
648
649         resultList.sort(new MavenLikeVersioningComparator());
650         Collections.reverse(resultList);
651
652         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipe", null);
653         return resultList.get(0);
654     }
655
656     public List<ServiceRecipe> getServiceRecipes(String serviceModelUuid) {
657
658         StringBuilder hql;
659
660         hql = new StringBuilder("FROM ServiceRecipe WHERE serviceModelUUID = :serviceModelUUID");
661
662         long startTime = System.currentTimeMillis();
663         LOGGER.debug("Catalog database - get Service recipe with serviceModelUUID " + serviceModelUuid);
664
665         Query query = getSession().createQuery(hql.toString());
666         query.setParameter("serviceModelUUID", serviceModelUuid);
667
668         @SuppressWarnings("unchecked")
669         List <ServiceRecipe> resultList = query.list();
670
671         if (resultList.isEmpty()) {
672             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getServiceRecipes", null);
673             return Collections.EMPTY_LIST;
674         }
675
676         resultList.sort(new MavenLikeVersioningComparator());
677         Collections.reverse(resultList);
678
679         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipes", null);
680         return resultList;
681     }
682
683     
684     /**
685      * Return the VNF component data - queried by the VNFs ID and the component type.
686      *
687      * @param vnfId
688      * @param type
689      * @return VnfComponent object or null if none found
690      */
691     @Deprecated
692     public VnfComponent getVnfComponent (int vnfId, String type) {
693
694         long startTime = System.currentTimeMillis();
695         LOGGER.debug ("Catalog database - get VnfComponent where vnfId="+ vnfId+ " AND componentType="+ type);
696
697         String hql = "FROM VnfComponent WHERE vnfId = :vnf_id AND componentType = :type";
698         Query query = getSession ().createQuery (hql);
699         query.setParameter ("vnf_id", vnfId);
700         query.setParameter ("type", type);
701
702         VnfComponent result = null;
703         try {
704                 result = (VnfComponent) query.uniqueResult();
705         } catch (org.hibernate.NonUniqueResultException nure) {
706                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: vnf_id='" + vnfId + "', componentType='" + type + "'");
707                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnf_id=" + vnfId + " and componentType=" + type, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnf_id=" + vnfId);
708
709                 throw nure;
710         } catch (org.hibernate.HibernateException he) {
711                 LOGGER.debug("Hibernate Exception - while searching for: vnf_id='" + vnfId + "', componentType='" + type + "' " + he.getMessage());
712                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for vnf_id=" + vnfId + " and componentType=" + type, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnf_id=" + vnfId);
713
714                 throw he;
715         } catch (Exception e) {
716                 LOGGER.debug("Generic Exception - while searching for: vnf_id='" + vnfId + "', componentType='" + type + "' " + e.getMessage());
717                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for vnf_id=" + vnfId + " and componentType=" + type, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for vnf_id=" + vnfId);
718
719                 throw e;
720         }
721
722         if (result != null) {
723             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfComponent", null);
724         } else {
725             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No VNFComponent found", "CatalogDB", "getVnfComponent", null);
726         }
727         return result;
728     }
729
730     /**
731      * Return the newest version of a specific VNF resource (queried by Name).
732      *
733      * @param vnfType
734      * @return VnfResource object or null if none found
735      */
736     public VnfResource getVnfResource (String vnfType) {
737
738         long startTime = System.currentTimeMillis();
739         LOGGER.debug("Catalog database - get vnf resource with model_name " + vnfType);
740
741         String hql = "FROM VnfResource WHERE modelName = :vnf_name";
742         Query query = getSession().createQuery(hql);
743         query.setParameter("vnf_name", vnfType);
744
745         @SuppressWarnings("unchecked")
746         List <VnfResource> resultList = query.list();
747
748         // See if something came back. Name is unique, so
749         if (resultList.isEmpty()) {
750             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF not found", "CatalogDB", "getVnfResource", null);
751             return null;
752         }
753         resultList.sort(new MavenLikeVersioningComparator());
754         Collections.reverse(resultList);
755
756         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResource", null);
757         return resultList.get(0);
758     }
759
760     /**
761      * Return the newest version of a specific VNF resource (queried by Name).
762      *
763      * @param vnfType
764      * @param serviceVersion
765      * @return VnfResource object or null if none found
766      */
767     public VnfResource getVnfResource (String vnfType, String serviceVersion) {
768
769         long startTime = System.currentTimeMillis();
770         LOGGER.debug("Catalog database - get VNF resource with model_name " + vnfType + " and version=" + serviceVersion);
771
772         String hql = "FROM VnfResource WHERE modelName = :vnfName and version = :serviceVersion";
773         Query query = getSession().createQuery(hql);
774         query.setParameter("vnfName", vnfType);
775         query.setParameter("serviceVersion", serviceVersion);
776
777         VnfResource resource = null;
778         try {
779                 resource = (VnfResource) query.uniqueResult();
780         } catch (org.hibernate.NonUniqueResultException nure) {
781                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: vnfType='" + vnfType + "', serviceVersion='" + serviceVersion + "'");
782                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfType=" + vnfType + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnfType=" + vnfType);
783
784                 throw nure;
785         } catch (org.hibernate.HibernateException he) {
786                 LOGGER.debug("Hibernate Exception - while searching for: vnfType='" + vnfType + "', asdc_service_model_version='" + serviceVersion + "' " + he.getMessage());
787                 LOGGER.debug(Arrays.toString(he.getStackTrace()));
788                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for vnfType=" + vnfType + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfType=" + vnfType);
789
790                 throw he;
791         } catch (Exception e) {
792                 LOGGER.debug("Generic Exception - while searching for: vnfType='" + vnfType + "', serviceVersion='" + serviceVersion + "' " + e.getMessage());
793                 LOGGER.debug(Arrays.toString(e.getStackTrace()));
794                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for vnfType=" + vnfType + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for vnfType=" + vnfType);
795
796                 throw e;
797         }
798         if (resource == null) {
799                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResource", null);
800         } else {
801                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResource", null);
802         }
803         return resource;
804     }
805
806     /**
807      * Return VnfResource (queried by modelCustomizationId).
808      *
809      * @param modelCustomizationId
810      * @return VnfResource object or null if none found
811      */
812     public VnfResource getVnfResourceByModelCustomizationId(String modelCustomizationId) {
813
814         long startTime = System.currentTimeMillis();
815         LOGGER.debug("Catalog database - get VNF resource with modelCustomizationId " + modelCustomizationId);
816
817         String hql = "SELECT vr "
818                                         + "FROM VnfResource as vr JOIN vr.vnfResourceCustomizations as vrc "
819                                         + "WHERE vrc.modelCustomizationUuid = :modelCustomizationId";
820                 
821         Query query = getSession().createQuery(hql);
822         query.setParameter("modelCustomizationId", modelCustomizationId);
823
824         VnfResource resource = null;
825         try {
826             resource = (VnfResource) query.uniqueResult();
827         } catch(org.hibernate.NonUniqueResultException nure) {
828                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelCustomizationUuid='" + modelCustomizationId + "'");
829                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelCustomizationUuid=" + modelCustomizationId, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationId=" + modelCustomizationId);
830
831                 throw nure;
832         } catch (org.hibernate.HibernateException he) {
833                 LOGGER.debug("Hibernate Exception - while searching for: modelCustomizationId='" + modelCustomizationId + "' " + he.getMessage());
834                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelCustomizationId=" + modelCustomizationId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationId=" + modelCustomizationId);
835
836                 throw he;
837         } catch (Exception e) {
838                 LOGGER.debug("Generic Exception - while searching for: modelCustomizationId='" + modelCustomizationId + "' " + e.getMessage());
839                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelCustomizationId=" + modelCustomizationId, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationId=" + modelCustomizationId);
840
841                 throw e;
842         }
843         if (resource == null) {
844             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResourceByModelCustomizationId", null);
845         } else {
846             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceByModelCustomizationId", null);
847         }
848         return resource;
849     }
850     
851     
852     /**
853      * Return the newest version of a specific VNF resource Customization (queried by modelCustomizationName and modelVersionId).
854      *
855      * @return {@link VnfResourceCustomization} object or null if none found
856      */
857     public VnfResourceCustomization getVnfResourceCustomizationByModelCustomizationName (String modelCustomizationName, String modelVersionId) {
858
859         long startTime = System.currentTimeMillis();
860         LOGGER.debug("Catalog database - get VNF resource Customization with modelCustomizationName " + modelCustomizationName + " serviceModelUUID " + modelVersionId);
861
862         String hql = "SELECT vrc FROM VnfResourceCustomization as vrc WHERE vrc.modelCustomizationUuid IN "
863                                         + "(SELECT src.resourceModelCustomizationUUID FROM ServiceToResourceCustomization src "
864                                         + "WHERE src.serviceModelUUID = :modelVersionId)"
865                                         + "AND vrc.modelInstanceName = :modelCustomizationName";
866                 
867         Query query = getSession().createQuery(hql);
868         query.setParameter("modelCustomizationName", modelCustomizationName);
869         query.setParameter("modelVersionId", modelVersionId);
870
871         @SuppressWarnings("unchecked")
872         List<VnfResourceCustomization> resultList = query.list();
873
874         if (resultList.isEmpty()) {
875             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VnfResourceCustomization not found", "CatalogDB", "getVnfResourceCustomizationByModelCustomizationName", null);
876             return null;
877         }
878         
879         resultList.sort(new MavenLikeVersioningComparator());
880         Collections.reverse(resultList);
881
882         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceCustomizationByModelCustomizationName", null);
883         return resultList.get(0);
884     }
885     
886     
887     /**
888      * Return the newest version of a specific VNF resource (queried by modelInvariantId).
889      *
890      * @param modelInvariantUuid model invariant ID
891      * @param modelVersion model version
892      * @return VnfResource object or null if none found
893      */
894     public VnfResource getVnfResourceByModelInvariantId(String modelInvariantUuid, String modelVersion) {
895
896         long startTime = System.currentTimeMillis();
897         LOGGER.debug("Catalog database - get VNF resource with modelInvariantUuid " + modelInvariantUuid);
898
899         String hql = "FROM VnfResource WHERE modelInvariantUuid = :modelInvariantUuid and version = :serviceVersion";
900         Query query = getSession().createQuery(hql);
901         query.setParameter("modelInvariantUuid", modelInvariantUuid);
902         query.setParameter("serviceVersion", modelVersion);
903
904         VnfResource resource = null;
905         try {
906                 resource = (VnfResource) query.uniqueResult();
907         } catch (org.hibernate.NonUniqueResultException nure) {
908                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelInvariantUuid='" + modelInvariantUuid + "', serviceVersion='" + modelVersion + "'");
909                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelInvariantUuid=" + modelInvariantUuid + " and serviceVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelInvariantUuid=" + modelInvariantUuid);
910
911                 throw nure;
912         } catch (org.hibernate.HibernateException he) {
913                 LOGGER.debug("Hibernate Exception - while searching for: modelInvariantUuid='" + modelInvariantUuid + "', asdc_service_model_version='" + modelVersion + "' " + he.getMessage());
914                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelInvariantUuid=" + modelInvariantUuid + " and serviceVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelInvariantUuid=" + modelInvariantUuid);
915
916                 throw he;
917         } catch (Exception e) {
918                 LOGGER.debug("Generic Exception - while searching for: modelInvariantUuid='" + modelInvariantUuid + "', serviceVersion='" + modelVersion + "' " + e.getMessage());
919                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelInvariantUuid=" + modelInvariantUuid + " and serviceVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelInvariantUuid=" + modelInvariantUuid);
920
921                 throw e;
922         }
923         if (resource == null) {
924             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResource", null);
925         } else {
926             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResource", null);
927         }
928         return resource;
929     }
930
931     /**
932      * Return the newest version of a specific VNF resource (queried by ID).
933      *
934      * @param id The vnf id
935      * @return VnfResource object or null if none found
936      */
937     @Deprecated
938     public VnfResource getVnfResourceById (int id) {
939
940         long startTime = System.currentTimeMillis ();
941         LOGGER.debug ("Catalog database - get VNF resource with id " + id);
942
943         String hql = "FROM VnfResource WHERE id = :id";
944         Query query = getSession ().createQuery (hql);
945         query.setParameter ("id", id);
946
947         @SuppressWarnings("unchecked")
948         List <VnfResource> resultList = query.list ();
949
950         // See if something came back. Name is unique, so
951         if (resultList.isEmpty ()) {
952             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VnfResource not found", "CatalogDB", "getVnfResourceById", null);
953             return null;
954         }
955         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceById", null);
956         return resultList.get (0);
957     }
958
959     /**
960      * Return the newest version of a vfModule - 1607
961      *
962      */
963     public VfModule getVfModuleModelName(String modelName) {
964
965         long startTime = System.currentTimeMillis();
966         LOGGER.debug("Catalog database - get vfModuleModelName with name " + modelName);
967
968         String hql = "FROM VfModule WHERE modelName = :model_name";
969         Query query = getSession().createQuery(hql);
970         query.setParameter("model_name", modelName);
971
972         @SuppressWarnings("unchecked")
973         List<VfModule> resultList = query.list();
974
975         // See if something came back. Name is unique, so
976         if (resultList.isEmpty()) {
977             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF not found", "CatalogDB", "getVfModuleModelName", null);
978             return null;
979         }
980         resultList.sort(new MavenLikeVersioningComparator());
981         Collections.reverse(resultList);
982
983         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleModelName", null);
984         return resultList.get(0);
985     }
986
987     public VfModule getVfModuleModelName(String modelName, String model_version) {
988
989         long startTime = System.currentTimeMillis();
990         LOGGER.debug("Catalog database - get vfModuleModelName with type='" + modelName + "' and asdc_service_model_version='" + model_version + "'");
991
992         String hql = "FROM VfModule WHERE Name = :model_name and version = :model_version";
993         Query query = getSession().createQuery(hql);
994         query.setParameter("modelName", modelName);
995         query.setParameter("model_version", model_version);
996
997         VfModule module = null;
998         try {
999             module = (VfModule) query.uniqueResult();
1000         } catch (org.hibernate.NonUniqueResultException nure) {
1001                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: type='" + modelName + "', asdc_service_model_version='" + model_version + "'");
1002                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for type=" + modelName + " and version=" + model_version, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for type=" + modelName);
1003
1004                 throw nure;
1005         } catch (org.hibernate.HibernateException he) {
1006                 LOGGER.debug("Hibernate Exception - while searching for: type='" + modelName + "', asdc_service_model_version='" + model_version + "' " + he.getMessage());
1007                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for type=" + modelName + " and version=" + model_version, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for type=" + modelName);
1008
1009                 throw he;
1010         } catch (Exception e) {
1011                 LOGGER.debug("Generic Exception - while searching for: type='" + modelName + "', asdc_service_model_version='" + model_version + "' " + e.getMessage());
1012                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for type=" + modelName + " and version=" + model_version, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for type=" + modelName);
1013
1014                 throw e;
1015         }
1016         if (module == null) {
1017             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleModelName", null);
1018         } else {
1019             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleModelName", null);
1020         }
1021         return module;
1022     }
1023     
1024     /**
1025      * Need this for 1707 DHV. This may be a temporary solution. May
1026      * change it to get resources using service's model name.
1027      * 
1028      *@author cb645j
1029      *
1030      */
1031     public VfModuleCustomization getVfModuleCustomizationByModelName(String modelName) {
1032
1033         long startTime = System.currentTimeMillis();
1034         LOGGER.debug("Catalog database - get VfModuleCustomization By VfModule's ModelName: " + modelName);
1035
1036         String hql = "SELECT VfModuleCustomization FROM VfModuleCustomization as vfmc LEFT OUTER JOIN VfModule as vfm on vfm.modelUUID = vfmc.vfModuleModelUuid where vfm.modelName = :model_name";
1037         Query query = getSession().createQuery(hql);
1038         query.setParameter("model_name", modelName);
1039
1040         @SuppressWarnings("unchecked")
1041         List<VfModuleCustomization> resultList = query.list();
1042
1043         // See if something came back. Name is unique, so
1044         if (resultList.isEmpty()) {
1045             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful query but Vf module NOT found", "CatalogDB", "getVfModuleCustomizationByModelName", null);
1046             return null;
1047         }
1048
1049         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful query ", "CatalogDB", "getVfModuleCustomizationByModelName", null);
1050         return resultList.get(0);
1051     }
1052
1053
1054     /**
1055      * Return the newest version of a specific Network resource (queried by Type).
1056      *
1057      * @param networkType
1058      * @return NetworkResource object or null if none found
1059      */
1060     public NetworkResource getNetworkResource(String networkType) {
1061
1062         long startTime = System.currentTimeMillis();
1063         LOGGER.debug("Catalog database - get network resource with type " + networkType);
1064
1065         String hql = "FROM NetworkResource WHERE model_name = :network_type";
1066         Query query = getSession().createQuery(hql);
1067         query.setParameter("network_type", networkType);
1068
1069         @SuppressWarnings("unchecked")
1070         List <NetworkResource> resultList = query.list();
1071
1072         // See if something came back. Name is unique, so
1073         if (resultList.isEmpty()) {
1074             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Network Resource not found", "CatalogDB", "getNetworkResource", null);
1075             return null;
1076         }
1077
1078         resultList.sort(new MavenLikeVersioningComparator());
1079         Collections.reverse(resultList);
1080         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResource", null);
1081         return resultList.get(0);
1082     }
1083
1084     /**
1085      * Return a VNF recipe that matches a given VNF_TYPE, ACTION, and, if specified, SERVICE_TYPE
1086      *
1087      * @param vnfType
1088      * @param action
1089      * @param serviceType The service Name, if null or empty is provided, it won't be taken into account
1090      * @return VnfRecipe object or null if none found
1091      */
1092     public VnfRecipe getVnfRecipe(String vnfType, String action, String serviceType) {
1093         boolean withServiceType = false;
1094
1095         StringBuilder hql = new StringBuilder("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action ");
1096
1097         // If query c
1098         if (serviceType == null || serviceType.isEmpty()) {
1099             hql.append("AND serviceType is NULL ");
1100         } else {
1101             hql.append("AND serviceType = :serviceType ");
1102             withServiceType = true;
1103         }
1104
1105         long startTime = System.currentTimeMillis();
1106         LOGGER.debug("Catalog database - get VNF recipe with name " + vnfType
1107                                       + " and action "
1108                                       + action
1109                                       + " and service type "
1110                                       + serviceType);
1111
1112         Query query = getSession().createQuery(hql.toString());
1113         query.setParameter(VNF_TYPE, vnfType);
1114         query.setParameter(ACTION, action);
1115         if (withServiceType) {
1116             query.setParameter(SERVICE_TYPE, serviceType);
1117         }
1118
1119         @SuppressWarnings("unchecked")
1120         List <VnfRecipe> resultList = query.list();
1121
1122         if (resultList.isEmpty()) {
1123             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null);
1124             return null;
1125         }
1126
1127         resultList.sort(new MavenLikeVersioningComparator());
1128         Collections.reverse(resultList);
1129
1130         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null);
1131         return resultList.get(0);
1132     }
1133
1134     
1135     
1136     /**
1137      * Return a VNF recipe that matches a given VNF_TYPE and ACTION
1138      *
1139      * @param vnfType
1140      * @param action
1141      * @return VnfRecipe object or null if none found
1142      */
1143     public VnfRecipe getVnfRecipe(String vnfType, String action) {
1144
1145         long startTime = System.currentTimeMillis();
1146         LOGGER.debug("Catalog database - get VNF recipe with name " + vnfType
1147                                       + " and action "
1148                                       + action);
1149
1150         Query query = getSession().createQuery("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action ");
1151         query.setParameter(VNF_TYPE, vnfType);
1152         query.setParameter(ACTION, action);
1153
1154         @SuppressWarnings("unchecked")
1155         List <VnfRecipe> resultList = query.list();
1156
1157         if (resultList.isEmpty()) {
1158             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null);
1159             return null;
1160         }
1161
1162         resultList.sort(new MavenLikeVersioningComparator());
1163         Collections.reverse(resultList);
1164
1165         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null);
1166         return resultList.get(0);
1167     }
1168     
1169     /**
1170      * Return a VNF recipe that matches a given ModelName and Modelversion and ACTION
1171      *
1172      * @param modelName
1173      * @param modelVersion
1174      * @param action
1175      * @return VnfRecipe object or null if none found
1176      */
1177     public VnfRecipe getVnfRecipeByNameVersion(String modelName, String modelVersion, String action) {
1178
1179         long startTime = System.currentTimeMillis();
1180         LOGGER.debug("Catalog database - get VNF recipe with name " + modelName + " and action " + action);
1181
1182         Query query = getSession().createQuery("FROM VnfRecipe WHERE vnfType = :vnfType AND version= :version AND action = :action ");
1183         query.setParameter(VNF_TYPE, modelName);
1184         query.setParameter(MODEL_VERSION, modelVersion);
1185         query.setParameter(ACTION, action);
1186
1187         @SuppressWarnings("unchecked")
1188         List <VnfRecipe> resultList = query.list();
1189
1190         if (resultList.isEmpty()) {
1191             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null);
1192             return null;
1193         }
1194
1195         resultList.sort(new MavenLikeVersioningComparator());
1196         Collections.reverse(resultList);
1197
1198         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null);
1199         return resultList.get(0);
1200     }
1201     
1202     /**
1203      * Return a Network recipe that matches a given MODEL_UUID and ACTION
1204      *
1205      * @param modelName
1206      * @param action
1207      * @return NetworkRecipe object or null if none found
1208      */
1209     public VnfRecipe getVnfRecipeByModuleUuid (String vnfModelUuid, String action) {
1210         LOGGER.debug ("Catalog database - get vnf recipe with vnf resource model uuid " + vnfModelUuid
1211                 + " and action "
1212                 + action
1213                 );
1214         VnfResource vnfResource = getVnfResourceByModelUuid(vnfModelUuid);
1215         if(null == vnfResource){
1216             return null;
1217         }
1218         
1219         VnfRecipe recipe = this.getVnfRecipeByNameVersion(vnfResource.getModelName(), vnfResource.getVersion(), action);
1220         return recipe;        
1221     }
1222
1223     /**
1224      * Return a VNF recipe that matches a given VF_MODULE_ID and ACTION
1225      *
1226      * @param vfModuleId
1227      * @param action
1228      * @return VnfRecipe object or null if none found
1229      */
1230     public VnfRecipe getVnfRecipeByVfModuleId(String vnfType, String vfModuleId, String action) {
1231
1232         long startTime = System.currentTimeMillis();
1233         LOGGER.debug("Catalog database - get VNF Recipe with vfModuleId " + vfModuleId);
1234
1235         Query query = getSession().createQuery("FROM VnfRecipe WHERE vfModuleId = :vfModuleId and action = :action  ");
1236         query.setParameter(VF_MODULE_MODEL_UUID, vfModuleId);
1237         query.setParameter(ACTION, action);
1238
1239         @SuppressWarnings("unchecked")
1240         List <VnfRecipe> resultList = query.list();
1241
1242         if (resultList.isEmpty()) {
1243             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe Entry not found", "CatalogDB", "getVnfRecipeByVfModuleId", null);
1244             return null;
1245         }
1246
1247         resultList.sort(new MavenLikeVersioningComparator());
1248         Collections.reverse(resultList);
1249
1250         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF Recipe Entry found", "CatalogDB", "getVnfRecipeByVfModuleId", null);
1251         return resultList.get(0);
1252     }
1253
1254     public VfModule getVfModuleTypeByUuid(String modelCustomizationUuid) {
1255         long startTime = System.currentTimeMillis();
1256         LOGGER.debug("Catalog database - get vfModuleTypeByUuid with uuid=" + modelCustomizationUuid);
1257
1258         String hql = "FROM VfModule WHERE modelCustomizationUuid = :modelCustomizationUuid";
1259         Query query = getSession().createQuery(hql);
1260         query.setParameter("modelCustomizationUuid", modelCustomizationUuid);
1261
1262         VfModule module = null;
1263         try {
1264             module = (VfModule) query.uniqueResult();
1265         } catch (org.hibernate.NonUniqueResultException nure) {
1266             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelCustomizationUuid='" + modelCustomizationUuid + "'");
1267             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationUuid==" + modelCustomizationUuid);
1268
1269                 throw nure;
1270         } catch (org.hibernate.HibernateException he) {
1271                 LOGGER.debug("Hibernate Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + he.getMessage());
1272             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1273
1274                 throw he;
1275         } catch (Exception e) {
1276                 LOGGER.debug("Generic Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + e.getMessage());
1277             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1278
1279                 throw e;
1280         }
1281         if (module == null) {
1282             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleTypeByUuid", null);
1283         } else {
1284             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleTypeByUuid", null);
1285         }
1286         return module;
1287     }
1288
1289     @Deprecated
1290     public VfModule getVfModuleType(String type) {
1291         long startTime = System.currentTimeMillis();
1292         LOGGER.debug("Catalog database - get vfModuleType with type " + type);
1293
1294         String hql = "FROM VfModule WHERE type = :type";
1295         Query query = getSession().createQuery(hql);
1296         query.setParameter("type",  type);
1297
1298         @SuppressWarnings("unchecked")
1299         List<VfModule> resultList = query.list();
1300         if (resultList.isEmpty()) {
1301             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF not found", "CatalogDB", "getVfModuleType", null);
1302             return null;
1303         }
1304         resultList.sort(new MavenLikeVersioningComparator());
1305         Collections.reverse (resultList);
1306
1307         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
1308         return resultList.get (0);
1309     }
1310
1311     @Deprecated
1312     public VfModule getVfModuleType(String type, String version) {
1313
1314         long startTime = System.currentTimeMillis();
1315         LOGGER.debug ("Catalog database - get vfModuleType with type " + type + " and model_version " + version);
1316
1317         String hql = "FROM VfModule WHERE type = :type and version = :version";
1318         Query query = getSession().createQuery(hql);
1319         query.setParameter ("type", type);
1320         query.setParameter ("version", version);
1321         VfModule module = null;
1322         try {
1323                 module = (VfModule) query.uniqueResult ();
1324         } catch (org.hibernate.NonUniqueResultException nure) {
1325                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: type='" + type + "', asdc_service_model_version='" + version + "'");
1326                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for type=" + type + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for type==" + type);
1327
1328                 throw nure;
1329         } catch (org.hibernate.HibernateException he) {
1330                 LOGGER.debug("Hibernate Exception - while searching for: type='" + type + "', asdc_service_model_version='" + version + "' " + he.getMessage());
1331                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for type=" + type + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for type=" + type);
1332
1333                 throw he;
1334         } catch (Exception e) {
1335                 LOGGER.debug("Generic Exception - while searching for: type='" + type + "', asdc_service_model_version='" + version + "' " + e.getMessage());
1336                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for type=" + type + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for type=" + type);
1337
1338                 throw e;
1339         }
1340         if (module == null) {
1341                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null);
1342         } else {
1343                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
1344         }
1345         return module;
1346     }
1347
1348     public VnfResource getVnfResourceByServiceUuid(String serviceModelInvariantUuid) {
1349         long startTime = System.currentTimeMillis();
1350         LOGGER.debug ("Catalog database - get vfModuleType with serviceModelInvariantUuid " + serviceModelInvariantUuid);
1351
1352         String hql = "FROM VnfResource WHERE serviceModelInvariantUuid = :serviceModelInvariantUuid";
1353         Query query = getSession().createQuery(hql);
1354         query.setParameter ("serviceModelInvariantUuid", serviceModelInvariantUuid);
1355         VnfResource vnfResource = null;
1356         try {
1357             vnfResource = (VnfResource) query.uniqueResult();
1358         } catch (org.hibernate.NonUniqueResultException nure) {
1359             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: serviceModelInvariantUuid='" + serviceModelInvariantUuid);
1360             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for serviceModelInvariantUuid==" + serviceModelInvariantUuid);
1361
1362                 throw nure;
1363         } catch (org.hibernate.HibernateException he) {
1364                 LOGGER.debug("Hibernate Exception - while searching for: serviceModelInvariantUuid='" + serviceModelInvariantUuid + "' " + he.getMessage());
1365             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1366
1367                 throw he;
1368         } catch (Exception e) {
1369                 LOGGER.debug("Generic Exception - while searching for: serviceModelInvariantUuid='" + serviceModelInvariantUuid + "' " + e.getMessage());
1370             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1371
1372                 throw e;
1373         }
1374         if (vnfResource == null) {
1375             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null);
1376         } else {
1377             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
1378         }
1379         return vnfResource;
1380     }
1381
1382     public VnfResource getVnfResourceByVnfUuid(String vnfResourceModelInvariantUuid) {
1383         long startTime = System.currentTimeMillis();
1384         LOGGER.debug("Catalog database - get vfModuleType with vnfResourceModelInvariantUuid " + vnfResourceModelInvariantUuid);
1385
1386         String hql = "FROM VnfResource WHERE vnfResourceModelInvariantUuid = :vnfResourceModelInvariantUuid";
1387         Query query = getSession().createQuery(hql);
1388         query.setParameter("vnfResourceModelInvariantUuid", vnfResourceModelInvariantUuid);
1389         VnfResource vnfResource = null;
1390         try {
1391             vnfResource = (VnfResource) query.uniqueResult();
1392         } catch (org.hibernate.NonUniqueResultException nure) {
1393             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: vnfResourceModelInvariantUuid='" + vnfResourceModelInvariantUuid);
1394             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnfResourceModelInvariantUuid==" + vnfResourceModelInvariantUuid);
1395
1396                 throw nure;
1397         } catch (org.hibernate.HibernateException he) {
1398                 LOGGER.debug("Hibernate Exception - while searching for: vnfResourceModelInvariantUuid='" + vnfResourceModelInvariantUuid + "' " + he.getMessage());
1399             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid);
1400
1401                 throw he;
1402         } catch (Exception e) {
1403                 LOGGER.debug("Generic Exception - while searching for: vnfResourceModelInvariantUuid='" + vnfResourceModelInvariantUuid + "' " + e.getMessage());
1404             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid);
1405
1406                 throw e;
1407         }
1408         if (vnfResource == null) {
1409             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null);
1410         } else {
1411             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
1412         }
1413         return vnfResource;
1414     }
1415
1416     public VnfResource getVnfResourceByType(String vnfType) {
1417         return this.getVnfResource(vnfType);
1418     }
1419
1420     public VfModule getVfModuleByModelInvariantUuid(String modelInvariantUUID) {
1421         long startTime = System.currentTimeMillis();
1422         LOGGER.debug ("Catalog database - get vfModuleTypeByModelInvariantUuid with uuid " + modelInvariantUUID);
1423
1424         String hql = "FROM VfModule WHERE modelInvariantUUID = :modelInvariantUUID ";
1425         HashMap<String, String> parameters = new HashMap<>();
1426         parameters.put("modelInvariantUUID", modelInvariantUUID);
1427         List<VfModule> modules = this.executeQueryMultipleRows(hql, parameters, true);
1428         VfModule module = null;
1429         
1430         if (modules != null && ! modules.isEmpty()) {
1431                 modules.sort(new MavenLikeVersioningComparator());
1432                 Collections.reverse (modules);
1433                 module =  modules.get(0);
1434         }
1435   
1436         if (module == null) {
1437             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelInvariantUuid", null);
1438         } else {
1439             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelInvariantUuid", null);
1440         }
1441         return module;
1442     }
1443
1444     public VfModuleCustomization getVfModuleByModelCustomizationUuid(String modelCustomizationUuid) {
1445         long startTime = System.currentTimeMillis();
1446         LOGGER.debug ("Catalog database - get vfModuleTypeByModelCustomizationUuid with uuid " + modelCustomizationUuid);
1447
1448         String hql = "FROM VfModuleCustomization WHERE modelCustomizationUuid = :modelCustomizationUuid ";
1449         Query query = getSession().createQuery(hql);
1450         query.setParameter ("modelCustomizationUuid", modelCustomizationUuid);
1451         VfModuleCustomization module = null;
1452         try {
1453                 module = (VfModuleCustomization) query.uniqueResult ();
1454         } catch (org.hibernate.NonUniqueResultException nure) {
1455             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelCustomizationUuid='" + modelCustomizationUuid + "'");
1456             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vfModuleModelInvariantUuid=" + modelCustomizationUuid , "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationUuid==" + modelCustomizationUuid);
1457
1458                 throw nure;
1459         } catch (org.hibernate.HibernateException he) {
1460                 LOGGER.debug("Hibernate Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + he.getMessage());
1461             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1462
1463                 throw he;
1464         } catch (Exception e) {
1465                 LOGGER.debug("Generic Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + e.getMessage());
1466             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1467
1468                 throw e;
1469         }
1470         if (module == null) {
1471             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelCustomizationUuid", null);
1472         } else {
1473             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelCustomizationUuid", null);
1474         }
1475         return module;
1476     }
1477
1478     
1479     public VfModule getVfModuleByModelInvariantUuidAndModelVersion(String modelInvariantUuid, String modelVersion) {
1480         long startTime = System.currentTimeMillis();
1481         LOGGER.debug ("Catalog database - get getVfModuleByModelInvariantUuidAndModelVersion with modelInvariantUuid: " + modelInvariantUuid + ", modelVersion: " + modelVersion);
1482
1483         String hql = "FROM VfModule WHERE modelInvariantUUID = :modelInvariantUuid and version = :modelVersion";
1484         Query query = getSession().createQuery(hql);
1485         query.setParameter ("modelInvariantUuid", modelInvariantUuid);
1486         query.setParameter("modelVersion", modelVersion);
1487         VfModule module = null;
1488         try {
1489                 module = (VfModule) query.uniqueResult ();
1490         } catch (org.hibernate.NonUniqueResultException nure) {
1491                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelInvariantUuid='" + modelInvariantUuid + "', modelVersion='" +modelVersion + "'");
1492                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vfModule ModelInvariantUuid=" + modelInvariantUuid + " modelVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for ModelInvariantUuid==" + modelInvariantUuid + " modelVersion==" + modelVersion);
1493                 throw nure;
1494         } catch (org.hibernate.HibernateException he) {
1495                 LOGGER.debug("Hibernate Exception - while searching for: modelInvariantUuid='" + modelInvariantUuid + "', modelVersion='" +modelVersion + "' " + he.getMessage());
1496                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelInvariantUuid=" + modelInvariantUuid + " modelVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelInvariantUuid=" + modelInvariantUuid + " modelVersion=" + modelVersion);
1497                 throw he;
1498         } catch (Exception e) {
1499                 LOGGER.debug("Generic Exception - while searching for: modelInvariantUuid='" + modelInvariantUuid + "', modelVersion='" +modelVersion + "' " + e.getMessage());
1500                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelInvariantUuid=" + modelInvariantUuid + " modelVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelInvariantUuid=" + modelInvariantUuid + " modelVersion=" + modelVersion);
1501                 throw e;
1502         }
1503         if (module == null) {
1504                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelInvariantUuidAndModelVersion", null);
1505         } else {
1506                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelInvariantUuidAndModelVersion", null);
1507         }
1508         return module;
1509     }
1510     
1511     /**
1512      * Return the VfModuleCustomization object identified by the given modelCustomizationUuid 1707
1513      *
1514      * @param modelCustomizationUuid
1515      * @return VfModuleCustomization or null if not found
1516      */
1517     public VfModuleCustomization getVfModuleCustomizationByModelCustomizationId(String modelCustomizationUuid) {
1518         long startTime = System.currentTimeMillis();
1519         LOGGER.debug ("Catalog database - get getVfModuleCustomizationByModelCustomizationId with modelCustomizationUuid: " + modelCustomizationUuid);
1520
1521         String hql = "FROM VfModuleCustomization WHERE modelCustomizationUuid = :modelCustomizationUuid";
1522         Query query = getSession().createQuery(hql);
1523         query.setParameter ("modelCustomizationUuid", modelCustomizationUuid);
1524         VfModuleCustomization VfModuleCustomization = null;
1525         try {
1526                 VfModuleCustomization = (VfModuleCustomization) query.uniqueResult ();
1527         } catch (org.hibernate.NonUniqueResultException nure) {
1528                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelCustomizationUuid='" + modelCustomizationUuid +"'");
1529                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vfModuleCustomization modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationUuid==" + modelCustomizationUuid);
1530                 throw nure;
1531         } catch (org.hibernate.HibernateException he) {
1532                 LOGGER.debug("Hibernate Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + he.getMessage());
1533                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1534                 throw he;
1535         } catch (Exception e) {
1536                 LOGGER.debug("Generic Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + e.getMessage());
1537                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1538                 throw e;
1539         }
1540         if (VfModuleCustomization != null) {
1541                 LOGGER.debug("Found VMC of " + VfModuleCustomization.getModelCustomizationUuid() + ", now looking for vfModule=" + VfModuleCustomization.getVfModuleModelUuid());
1542                 VfModuleCustomization.setVfModule(this.getVfModuleByModelUuid(VfModuleCustomization.getVfModuleModelUuid()));
1543         }
1544
1545         if (VfModuleCustomization == null) {
1546                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleCustomizationByModelCustomizationId", null);
1547         } else {
1548                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleCustomizationByModelCustomizationId", null);
1549         }
1550         return VfModuleCustomization;
1551     }
1552     
1553     /**
1554      * Return the VfModule object identified by the given modelUuid 1707
1555      * per Mike Z. - this may return more than one row - and even if it does, 
1556      * the heat template will be the same - so just return any of the rows.
1557      *
1558      * @param modelUuid
1559      * @return VfModule or null if not found
1560      */
1561     public VfModule getVfModuleByModelUuid(String modelUuid) {
1562         long startTime = System.currentTimeMillis();
1563         LOGGER.debug ("Catalog database - get getVfModuleByModelUuid with modelUuid: " + modelUuid);
1564
1565         String hql = "FROM VfModule WHERE modelUUID = :modelUuidValue";
1566         Query query = getSession().createQuery(hql);
1567         query.setParameter ("modelUuidValue", modelUuid);
1568         List<VfModule> vfModules = null;
1569         try {
1570                 vfModules = query.list ();
1571         } catch (org.hibernate.HibernateException he) {
1572                 LOGGER.debug("Hibernate Exception - while searching VfModule for: modelUuid='" + modelUuid + "' " + he.getMessage());
1573                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching VfModule for modelUuid=" + modelUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelUuid=" + modelUuid);
1574                 throw he;
1575         } catch (Exception e) {
1576                 LOGGER.debug("Generic Exception - while searching VfModule for: modelUuid='" + modelUuid + "' " + e.getMessage());
1577                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelUuid=" + modelUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelUuid=" + modelUuid);
1578                 throw e;
1579         }
1580
1581         if (vfModules == null || vfModules.isEmpty()) {
1582                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelUuid", null);
1583                 return null;
1584         } else {
1585                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelUuid", null);
1586         }
1587         return vfModules.get(0);
1588     }
1589     /**
1590      * Return the VnfResourceCustomization object identified by the given modelCustomizationUuid 1707
1591      * Note that the corresponding VnfResource Object will be put in the VnfResourceCustomization bean
1592      *
1593      * @param modelCustomizationUuid
1594      * @return VnfResourceCustomization or null if not found
1595      */
1596     public VnfResourceCustomization getVnfResourceCustomizationByModelCustomizationUuid(String modelCustomizationUuid) {
1597         long startTime = System.currentTimeMillis();
1598         LOGGER.debug ("Catalog database - get getVnfResourceByModelCustomizatonUuid with modelCustomizationUuid: " + modelCustomizationUuid);
1599
1600         String hql = "FROM VnfResourceCustomization WHERE modelCustomizationUuid = :modelCustomizationUuid";
1601         Query query = getSession().createQuery(hql);
1602         query.setParameter ("modelCustomizationUuid", modelCustomizationUuid);
1603         VnfResourceCustomization vnfResourceCustomization = null;
1604         try {
1605                 vnfResourceCustomization = (VnfResourceCustomization) query.uniqueResult ();
1606         } catch (org.hibernate.NonUniqueResultException nure) {
1607                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row in VRC - data integrity error: modelCustomizationUuid='" + modelCustomizationUuid +"'");
1608                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceCustomization modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationUuid==" + modelCustomizationUuid);
1609                 throw nure;
1610         } catch (org.hibernate.HibernateException he) {
1611                 LOGGER.debug("Hibernate Exception - while searching VRC for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + he.getMessage());
1612                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching VRC for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1613                 throw he;
1614         } catch (Exception e) {
1615                 LOGGER.debug("Generic Exception - while searching VRC for: modelCustomizationUuid='" + modelCustomizationUuid + "' " + e.getMessage());
1616                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching VRC for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1617                 throw e;
1618         }
1619         if (vnfResourceCustomization != null) {
1620                 LOGGER.debug("Found VRC of " + vnfResourceCustomization.getModelCustomizationUuid() + ", now looking for vnfResource=" + vnfResourceCustomization.getVnfResourceModelUuid() );
1621                 vnfResourceCustomization.setVnfResource(this.getVnfResourceByModelUuid(vnfResourceCustomization.getVnfResourceModelUuid()));
1622                 LOGGER.debug("Now looking for vfModules for " + vnfResourceCustomization.getModelCustomizationUuid());
1623                 vnfResourceCustomization.setVfModuleCustomizations(this.getAllVfModuleCustomizations(vnfResourceCustomization.getModelCustomizationUuid()));
1624         }
1625
1626         if (vnfResourceCustomization == null) {
1627                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResourceCustomizationByModelCustomizationUuid", null);
1628         } else {
1629                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceCustomizationByModelCustomizationUuid", null);
1630         }
1631         return vnfResourceCustomization;
1632     }
1633     
1634     /**
1635      * Return the VnfResourceCustomization object identified by the given modelCustomizationUuid 1707
1636      * Note that the corresponding VnfResource Object will be put in the VnfResourceCustomization bean
1637      *
1638      * @param modelVersionId
1639      * @return VnfResourceCustomization or null if not found
1640      */
1641     public VnfResourceCustomization getVnfResourceCustomizationByModelVersionId(String modelVersionId) {
1642         long startTime = System.currentTimeMillis();
1643         LOGGER.debug ("Catalog database - get getVnfResourceCustomizationByModelVersionId with modelVersionId: " + modelVersionId);
1644
1645         String hql = "FROM VnfResourceCustomization WHERE vnfResourceModelUuid = :modelVersionId";
1646         Query query = getSession().createQuery(hql);
1647         query.setParameter ("modelVersionId", modelVersionId);
1648         VnfResourceCustomization vnfResourceCustomization = null;
1649         try {
1650                 vnfResourceCustomization = (VnfResourceCustomization) query.uniqueResult ();
1651         } catch (org.hibernate.NonUniqueResultException nure) {
1652                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row in VRC - data integrity error: modelVersionId='" + modelVersionId +"'");
1653                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceCustomization modelVersionId=" + modelVersionId, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelVersionId==" + modelVersionId);
1654                 throw nure;
1655         } catch (org.hibernate.HibernateException he) {
1656                 LOGGER.debug("Hibernate Exception - while searching VRC for: modelVersionId='" + modelVersionId + "' " + he.getMessage());
1657                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching VRC for modelVersionId=" + modelVersionId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelVersionId=" + modelVersionId);
1658                 throw he;
1659         } catch (Exception e) {
1660                 LOGGER.debug("Generic Exception - while searching VRC for: modelVersionId='" + modelVersionId + "' " + e.getMessage());
1661                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching VRC for modelVersionId=" + modelVersionId, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelVersionId=" + modelVersionId);
1662                 throw e;
1663         }
1664         if (vnfResourceCustomization != null) {
1665                 LOGGER.debug("Found VRC of " + vnfResourceCustomization.getModelCustomizationUuid() + ", now looking for vnfResource=" + vnfResourceCustomization.getVnfResourceModelUuid() );
1666                 vnfResourceCustomization.setVnfResource(this.getVnfResourceByModelUuid(vnfResourceCustomization.getVnfResourceModelUuid()));
1667                 LOGGER.debug("Now looking for vfModules for " + vnfResourceCustomization.getModelCustomizationUuid());
1668                 vnfResourceCustomization.setVfModuleCustomizations(this.getAllVfModuleCustomizations(vnfResourceCustomization.getModelCustomizationUuid()));
1669         }
1670
1671         if (vnfResourceCustomization == null) {
1672                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResourceCustomizationByModelVersionId", null);
1673         } else {
1674                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceCustomizationByModelVersionId", null);
1675         }
1676         return vnfResourceCustomization;
1677     }
1678     
1679     /**
1680      * Return the VfModule object identified by the given modelCustomizationId, modelVersionId 1707
1681      *
1682      * @param modelVersionId, modelCustomizationId
1683      * @return VfModule or null if not found
1684      */
1685     public VfModule getVfModuleByModelCustomizationIdAndVersion(String modelCustomizationId, String modelVersionId) {
1686         long startTime = System.currentTimeMillis();
1687         LOGGER.debug ("Catalog database - get getVfModuleByModelCustomizationIdAndVersion with modelVersionId: " + modelVersionId + " modelCustomizationId: " + modelCustomizationId);
1688
1689 //      select * from vf_module vfm where vfm.MODEL_UUID IN (
1690 //      select vfmc.VF_MODULE_MODEL_UUID from vf_module_customization vfmc where vfmc.MODEL_CUSTOMIZATION_UUID='222bd8f2-341d-4419-aa0e-98398fa34050')
1691 //      and vfm.MODEL_UUID = 'fa1c8558-006c-4fb6-82f2-4fc0646d6b06';
1692         
1693         String hql = "Select vfm FROM VfModule as vfm WHERE vfm.modelUUID IN ("
1694                         + "SELECT vfmc.vfModuleModelUuid FROM VfModuleCustomization as vfmc "
1695                         + "WHERE vfmc.modelCustomizationUuid = :modelCustomizationId) "
1696                         + "AND vfm.modelUUID = :modelVersionId";
1697         Query query = getSession().createQuery(hql);
1698         query.setParameter ("modelVersionId", modelVersionId);
1699         query.setParameter ("modelCustomizationId", modelCustomizationId);
1700         VfModule vfModule = null;
1701         try {
1702                 vfModule = (VfModule) query.uniqueResult ();
1703         } catch (org.hibernate.NonUniqueResultException nure) {
1704                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row in VRC - data integrity error: modelVersionId='" + modelVersionId +"' modelCustomizationId='" + modelCustomizationId + "'");
1705                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceCustomization modelVersionId=" + modelVersionId, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelVersionId=" + modelVersionId + " modelCustomizationId=" + modelCustomizationId);
1706                 throw nure;
1707         } catch (org.hibernate.HibernateException he) {
1708                 LOGGER.debug("Hibernate Exception - while searching VRC for: modelVersionId='" + modelVersionId + "' modelCustomizationId='" + modelCustomizationId + "' " + he.getMessage());
1709                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching VRC for modelVersionId=" + modelVersionId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelVersionId=" + modelVersionId + " modelCustomizationId=" + modelCustomizationId);
1710                 throw he;
1711         } catch (Exception e) {
1712                 LOGGER.debug("Generic Exception - while searching VRC for: modelVersionId='" + modelVersionId + "' modelCustomizationId='" + modelCustomizationId + "' " + e.getMessage());
1713                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching VRC for modelVersionId=" + modelVersionId, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelVersionId=" + modelVersionId + " modelCustomizationId=" + modelCustomizationId);
1714                 throw e;
1715         }
1716
1717         if (vfModule == null) {
1718                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelCustomizationIdAndVersion", null);
1719         } else {
1720                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelCustomizationIdAndVersion", null);
1721         }
1722         return vfModule;
1723     }
1724     
1725     /**
1726      * Return the VfModule object identified by the given modelCustomizationId, modelVersion, modelInvariantId 1707
1727      *
1728      * @param modelCustomizationId, modelVersion, modelInvariantId
1729      * @return VfModule or null if not found
1730      */
1731     public VfModule getVfModuleByModelCustomizationIdModelVersionAndModelInvariantId(String modelCustomizationId, String modelVersion, String modelInvariantId) {
1732         long startTime = System.currentTimeMillis();
1733         LOGGER.debug ("Catalog database - get getVfModuleByModelCustomizationIdModelVersionAndModelInvariantId with modelVersionId: " + modelVersion);
1734
1735         //select * from vf_module vfm left outer join vf_module_customization vfmc on vfmc.VF_MODULE_MODEL_UUID = vfm.MODEL_UUID 
1736 //        where vfmc.MODEL_CUSTOMIZATION_UUID='52643a8e-7953-4e48-8eab-97165b2b3a4b' and vfm.MODEL_UUID = ''
1737         
1738         String hql = "Select vfm FROM VfModule as vfm LEFT OUTER JOIN VfModuleCustomization as vfmc on vfmc.vfModuleModelUuid = vfm.modelUUID"
1739                         + "WHERE vfmc.modelCustomizationUuid = :modelCustomizationId AND vfm.modelInvariantUUID = :modelInvariantId AND vfm.modelVersion = :modelVersion";
1740         Query query = getSession().createQuery(hql);
1741         query.setParameter ("modelInvariantId", modelInvariantId);
1742         query.setParameter ("modelCustomizationId", modelCustomizationId);
1743         query.setParameter ("modelVersion", modelVersion);
1744         VfModule vfModule = null;
1745         try {
1746                 vfModule = (VfModule) query.uniqueResult ();
1747         } catch (org.hibernate.NonUniqueResultException nure) {
1748                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row in VRC - data integrity error: modelInvariantId='" + modelInvariantId +"' modelVersion='" + modelVersion + "' modelCustomizationId='" + modelCustomizationId +"'");
1749                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceCustomization modelInvariantId=" + modelInvariantId, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelInvariantId=" + modelInvariantId + " modelVersion=" + modelVersion + " modelCustomizationId=" + modelCustomizationId);
1750                 throw nure;
1751         } catch (org.hibernate.HibernateException he) {
1752                 LOGGER.debug("Hibernate Exception - while searching VRC for: modelInvariantId='" + modelInvariantId + "' modelVersion='" + modelVersion + "' modelCustomizationId='" + modelCustomizationId + "' " + he.getMessage());
1753                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching VRC for modelInvariantId=" + modelInvariantId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelInvariantId=" + modelInvariantId + " modelVersion=" + modelVersion + " modelCustomizationId=" + modelCustomizationId);
1754                 throw he;
1755         } catch (Exception e) {
1756                 LOGGER.debug("Generic Exception - while searching VRC for: modelInvariantId='" + modelInvariantId + "' modelVersion='" + modelVersion + "' modelCustomizationId='" + modelCustomizationId + "' " + e.getMessage());
1757                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching VRC for modelInvariantId=" + modelInvariantId, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelInvariantId=" + modelInvariantId + " modelVersion=" + modelVersion + " modelCustomizationId=" + modelCustomizationId);
1758                 throw e;
1759         }
1760
1761         if (vfModule == null) {
1762                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelCustomizationIdModelVersionAndModelInvariantId", null);
1763         } else {
1764                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelCustomizationIdModelVersionAndModelInvariantId", null);
1765         }
1766         return vfModule;
1767     }
1768     
1769     /**
1770      * Return the VnfResourceCustomization object identified by the given modelCustomizationName, modelInvariantId and modelVersion 1707
1771      *
1772      * @param modelInvariantId, modelVersion, modelCustomizationName
1773      * @return VnfResourceCustomization or null if not found
1774      */
1775     public VnfResourceCustomization getVnfResourceCustomizationByModelInvariantId(String modelInvariantId, String modelVersion, String modelCustomizationName) {
1776         long startTime = System.currentTimeMillis();
1777         LOGGER.debug ("Catalog database - get getVnfResourceCustomizationByModelInvariantId with modelInvariantId: " + modelInvariantId + ", modelVersion: " 
1778                                                 + modelVersion + ", modelCustomizationName: " + modelCustomizationName);
1779         
1780         String hql = "SELECT VnfResourceCustomization FROM VnfResourceCustomization as vrc "
1781                                 + "LEFT OUTER JOIN VnfResource as vr "
1782                                 + "on vr.modelUuid =vrc.vnfResourceModelUuid "
1783                                 + "WHERE vr.modelInvariantUuid = :modelInvariantId AND vr.modelVersion = :modelVersion AND vrc.modelInstanceName = :modelCustomizationName";
1784         
1785         Query query = getSession().createQuery(hql);
1786         query.setParameter ("modelInvariantId", modelInvariantId);
1787         query.setParameter("modelVersion", modelVersion);
1788         query.setParameter("modelCustomizationName", modelCustomizationName);
1789         VnfResourceCustomization vnfResourceCustomization = null;
1790         try {
1791                 vnfResourceCustomization = (VnfResourceCustomization) query.uniqueResult ();
1792         } catch (org.hibernate.NonUniqueResultException nure) {
1793                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row in VRC - data integrity error: modelInvariantId='" + modelInvariantId +"' and modelVersion='" + modelVersion + "' modelCustomizationName='" + modelCustomizationName + "'");
1794                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceCustomization modelInvariantId=" + modelInvariantId, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelInvariantId==" + modelInvariantId+"' and modelVersion='" + modelVersion + "'modelCustomizationName='" + modelCustomizationName + "'");
1795                 throw nure;
1796         } catch (org.hibernate.HibernateException he) {
1797                 LOGGER.debug("Hibernate Exception - while searching VRC for: modelInvariantId='" + modelInvariantId +"' and modelVersion='" + modelVersion + "'modelCustomizationName='" + modelCustomizationName + "' " + he.getMessage());
1798                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching VRC for modelInvariantId=" + modelInvariantId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelInvariantId=" + modelInvariantId+"' and modelVersion='" + modelVersion + "'modelCustomizationName='" + modelCustomizationName + "'");
1799                 throw he;
1800         } catch (Exception e) {
1801                 LOGGER.debug("Generic Exception - while searching VRC for: modelInvariantId='" + modelInvariantId +"' and modelVersion='" + modelVersion + "' " + e.getMessage());
1802                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching VRC for modelInvariantId=" + modelInvariantId, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelInvariantId=" + modelInvariantId+"' and modelVersion='" + modelVersion + "'modelCustomizationName='" + modelCustomizationName + "'");
1803                 throw e;
1804         }
1805         if (vnfResourceCustomization != null) {
1806                 LOGGER.debug("Found VRC of " + vnfResourceCustomization.getModelCustomizationUuid() + ", now looking for vnfResource=" + vnfResourceCustomization.getVnfResourceModelUuid() );
1807                 vnfResourceCustomization.setVnfResource(this.getVnfResourceByModelUuid(vnfResourceCustomization.getVnfResourceModelUUID()));
1808                 LOGGER.debug("Now looking for vfModules for " + vnfResourceCustomization.getModelCustomizationUuid());
1809                 vnfResourceCustomization.setVfModuleCustomizations(this.getAllVfModuleCustomizations(vnfResourceCustomization.getModelCustomizationUuid()));
1810         }
1811
1812         if (vnfResourceCustomization == null) {
1813                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResourceCustomizationByModelInvariantId", null);
1814         } else {
1815                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceCustomizationByModelInvariantId", null);
1816         }
1817         return vnfResourceCustomization;
1818     }
1819     
1820     /**
1821      * Return list of VnfResourceCustomization objects identified by the given modelCustomizationUuid 1707
1822      *
1823      * @param modelCustomizationUuid
1824      * @return List<VfModuleCustomization> or null if not found
1825      */
1826     public List<VfModuleCustomization> getVfModuleCustomizationByVnfModuleCustomizationUuid(String modelCustomizationUuid) {
1827         long startTime = System.currentTimeMillis();
1828         LOGGER.debug ("Catalog database - get getVfModuleCustomizationByVnfModuleCustomizationUuid with modelCustomizationUuid: " + modelCustomizationUuid);
1829         
1830 //      select * from vf_module_customization as vfmc where vfmc.MODEL_CUSTOMIZATION_UUID IN(
1831 //      select vrcmc.VF_MODULE_CUST_MODEL_CUSTOMIZATION_UUID from vnf_res_custom_to_vf_module_custom as vrcmc
1832 //      where vrcmc.VNF_RESOURCE_CUST_MODEL_CUSTOMIZATION_UUID = 'd279139c-4b85-48ff-8ac4-9b83a6fc6da7') 
1833         
1834         String hql = "SELECT vfmc FROM VfModuleCustomization as vfmc where vfmc.modelCustomizationUuid "
1835                                 + "IN(select vrcmc.vfModuleCustModelCustomizationUuid from VnfResCustomToVfModuleCustom as vrcmc "
1836                                                 + "WHERE vrcmc.vnfResourceCustModelCustomizationUuid = :modelCustomizationUuid)";
1837         
1838         Query query = getSession().createQuery(hql);
1839         query.setParameter ("modelCustomizationUuid", modelCustomizationUuid);
1840         List<VfModuleCustomization> resultList = null;
1841         try {
1842                 resultList = query.list();
1843         } catch (org.hibernate.HibernateException he) {
1844                 LOGGER.debug("Hibernate Exception - getVfModuleCustomizationByVnfModuleCustomizationUuid - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + " " + he.getMessage());
1845                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception - getVfModuleCustomizationByVnfModuleCustomizationUuid - searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1846                 throw he;
1847         } catch (Exception e) {
1848                 LOGGER.debug("Exception - getVfModuleCustomizationByVnfModuleCustomizationUuid - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + " " + e.getMessage());
1849                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception - getVfModuleCustomizationByVnfModuleCustomizationUuid - searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1850                 throw e;
1851         }
1852
1853         if (resultList == null) {
1854                 resultList = new ArrayList<>();
1855         }
1856         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleCustomizationByVnfModuleCustomizationUuid", null);
1857         return resultList;
1858     }
1859     
1860     /**
1861      * Return the newest version of a specific VNF resource Customization (queried by modelCustomizationName and modelVersionId).
1862      *
1863      * @return {@link VnfResourceCustomization} object or null if none found
1864      */
1865     public VnfResourceCustomization getVnfResourceCustomizationByVnfModelCustomizationNameAndModelVersionId (String modelCustomizationName, String modelVersionId) {
1866
1867         long startTime = System.currentTimeMillis ();
1868         LOGGER.debug ("Catalog database - get VNF resource Customization with modelCustomizationName " + modelCustomizationName + " modelUUID " + modelVersionId);
1869
1870         String hql = "SELECT vrc FROM VnfResourceCustomization as vrc WHERE vrc.vnfResourceModelUuid IN "
1871                                         + "(SELECT vr.modelUuid FROM VnfResource vr "
1872                                         + "WHERE vr.modelUuid = :modelVersionId)"
1873                                         + "AND vrc.modelInstanceName = :modelCustomizationName";
1874                 
1875         Query query = getSession ().createQuery (hql);
1876         query.setParameter ("modelCustomizationName", modelCustomizationName);
1877         query.setParameter ("modelVersionId", modelVersionId);
1878
1879         @SuppressWarnings("unchecked")
1880         List <VnfResourceCustomization> resultList = query.list ();
1881
1882         if (resultList.isEmpty ()) {
1883             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VnfResourceCustomization not found", "CatalogDB", "getVnfResourceCustomizationByVnfModelCustomizationNameAndModelVersionId", null);
1884             return null;
1885         }
1886         
1887         resultList.sort(new MavenLikeVersioningComparator());
1888         Collections.reverse (resultList);
1889
1890         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceCustomizationByVnfModelCustomizationNameAndModelVersionId", null);
1891         return resultList.get (0);
1892     }
1893     
1894     public ArrayList<VfModuleCustomization> getAllVfModuleCustomizations(String vnfResourceCustomizationMCU) {
1895         LOGGER.debug ("Catalog database - getAllVfModuleCustomizations with vnfResourceCustomizationMCU " + vnfResourceCustomizationMCU);
1896         
1897         List<VnfResCustomToVfModuleCustom> matches = this.getVRCtoVFMC(vnfResourceCustomizationMCU, null); 
1898         if (matches == null || matches.isEmpty()) {
1899                 LOGGER.debug("Found no vf modules for " + vnfResourceCustomizationMCU);
1900                 return new ArrayList<>();
1901         }
1902         ArrayList<VfModuleCustomization> list = new ArrayList<>();
1903         for (VnfResCustomToVfModuleCustom v : matches) {
1904                 String m = v.getVfModuleCustModelCustomizationUuid();
1905                 LOGGER.debug("VfModule to match: " + m);
1906                 VfModuleCustomization c = this.getVfModuleCustomizationByModelCustomizationId(m);
1907                 if (c != null) {
1908                         list.add(c);
1909                 } else {
1910                         LOGGER.debug("**UNABLE to find vfModule " + m);
1911                 }
1912         }
1913         return list;
1914     }
1915     
1916     /**
1917      * Return the VnfResourceCustomization object identified by the given modelCustomizationUuid 1707
1918      * Note that the corresponding VnfResource Object will be put in the VnfResourceCustomization bean
1919      *
1920      * @param modelUuid
1921      * @return VnfResourceCustomization or null if not found
1922      */
1923     public VnfResource getVnfResourceByModelUuid(String modelUuid) {
1924         long startTime = System.currentTimeMillis();
1925         LOGGER.debug ("Catalog database - get VnfResource with modelUuid " + modelUuid);
1926
1927         String hql = "FROM VnfResource WHERE modelUuid = :modelUuid";
1928         Query query = getSession().createQuery(hql);
1929         query.setParameter ("modelUuid", modelUuid);
1930         VnfResource vnfResource = null;
1931         try {
1932                 vnfResource = (VnfResource) query.uniqueResult ();
1933         } catch (org.hibernate.NonUniqueResultException nure) {
1934                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique Vnf_Resource row - data integrity error: modelUuid='" + modelUuid);
1935                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for Vnf Resource modelUuid=" + modelUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnfResource modelUuid==" + modelUuid);
1936                 throw nure;
1937         } catch (org.hibernate.HibernateException he) {
1938                 LOGGER.debug("Hibernate Exception - while searching for: VnfResource modelUuid='" + modelUuid + "' " + he.getMessage());
1939                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for vnfResource ModelUuid=" + modelUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfResource modelUuid=" + modelUuid);
1940                 throw he;
1941         } catch (Exception e) {
1942                 LOGGER.debug("Generic Exception - while searching for: vnfResource ModelUuid='" + modelUuid + "'");
1943                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for vnfResource ModelUuid=" + modelUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for vnfResource modelUuid=" + modelUuid);
1944                 throw e;
1945         }
1946         if (vnfResource == null) {
1947                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResourceByModelUuid", null);
1948         } else {
1949                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceByModelUuid", null);
1950         }
1951         return vnfResource;
1952     }
1953
1954     public VnfResCustomToVfModuleCustom getVnfResCustomToVfModule(String vnfId, String vfId) {
1955         long startTime = System.currentTimeMillis();
1956         LOGGER.debug("Catalog database - getVnfResCustomToVfModule - vnfResourceCustModelCustUuid: " + vnfId + ", vfModuleCustModelCustomUuid=" + vfId);
1957         HashMap<String, String> parameters = new HashMap<>();
1958         parameters.put("vnfIdValue", vnfId);
1959         parameters.put("vfIdValue", vfId);
1960         VnfResCustomToVfModuleCustom vrctvmc = this.executeQuerySingleRow(
1961             "FROM VnfResCustomToVfModuleCustom where vnfResourceCustModelCustomizationUuid = :vnfIdValue and vfModuleCustModelCustomizationUuid = :vfIdValue", parameters, true);
1962         if (vrctvmc == null) {
1963                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResCustomToVfModule", null);
1964         } else {
1965                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResCustomToVfModule", null);
1966         }
1967         return vrctvmc;
1968     }
1969
1970     public List<VfModule> getVfModulesForVnfResource(VnfResource vnfResource) {
1971         if (vnfResource == null)
1972             return Collections.EMPTY_LIST;
1973         String vnfResourceModelUuid = vnfResource.getModelUuid();
1974
1975         LOGGER.debug("Catalog database - getVfModulesForVnfResource - vnfResource: " + vnfResource.toString());
1976
1977         return this.getVfModulesForVnfResource(vnfResourceModelUuid);
1978
1979     }
1980
1981     public List<VfModule> getVfModulesForVnfResource(String vnfResourceModelUuid) {
1982         long startTime = System.currentTimeMillis();
1983         LOGGER.debug("Catalog database - getVfModulesForVnfResource - vnfResourceModelUuid: " + vnfResourceModelUuid);
1984         Query query = getSession().createQuery("FROM VfModule where vnfResourceModelUUId = :vnfResourceModelUUId");
1985         query.setParameter("vnfResourceModelUUId", vnfResourceModelUuid);
1986         List<VfModule> resultList = null;
1987         try {
1988             resultList = query.list();
1989             if (resultList != null)
1990                 LOGGER.debug("\tQuery found " + resultList.size() + " records.");
1991             else
1992                 LOGGER.debug("\tQuery found no records.");
1993         } catch (org.hibernate.HibernateException he) {
1994                 LOGGER.debug("Hibernate Exception - getVfModulesForVnfResource - while searching for: vnfResourceModelUUId='" + vnfResourceModelUuid + " " + he.getMessage());
1995                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception - getVfModulesForVnfResource - searching for vnfResourceModelUUId=" + vnfResourceModelUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfResourceModelUUId=" + vnfResourceModelUuid);
1996                 throw he;
1997         } catch (Exception e) {
1998                 LOGGER.debug("Exception - getVfModulesForVnfResource - while searching for: vnfResourceModelUUId='" + vnfResourceModelUuid + " " + e.getMessage());
1999                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception - getVfModulesForVnfResource - searching for vnfResourceModelUUId=" + vnfResourceModelUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfResourceModelUUId=" + vnfResourceModelUuid);
2000                 throw e;
2001         }
2002         if (resultList == null) {
2003             resultList = new ArrayList<>();
2004         }
2005         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModulesForVnfResource", null);
2006         return resultList;
2007     }
2008
2009     public Service getServiceByUuid (String serviceModelInvariantUuid) {
2010
2011         long startTime = System.currentTimeMillis ();
2012         LOGGER.debug ("Catalog database - get service with ModelInvariantUuid " + serviceModelInvariantUuid);
2013
2014         String hql = "FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid";
2015         Query query = getSession ().createQuery (hql);
2016         query.setParameter ("serviceModelInvariantUuid", serviceModelInvariantUuid);
2017
2018         Service service = null;
2019         try {
2020             service = (Service) query.uniqueResult ();
2021         } catch (org.hibernate.NonUniqueResultException nure) {
2022             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: serviceModelInvariantUuid='" + serviceModelInvariantUuid + "'");
2023             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
2024                 throw nure;
2025         } catch (org.hibernate.HibernateException he) {
2026                 LOGGER.debug("Hibernate Exception - while searching for: serviceName='" + serviceModelInvariantUuid + "' " + he.getMessage());
2027             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
2028                 throw he;
2029         } catch (Exception e) {
2030                 LOGGER.debug("Generic Exception - while searching for: serviceModelInvariantUuid='" + serviceModelInvariantUuid + " " + e.getMessage());
2031             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
2032                 throw e;
2033         }
2034         if (service == null) {
2035             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getService", null);
2036         } else {
2037             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getService", null);
2038         }
2039
2040         return service;
2041     }
2042
2043     public NetworkResource getNetworkResourceById(Integer id) {
2044         long startTime = System.currentTimeMillis ();
2045         LOGGER.debug ("Catalog database - getNetworkResource with id " + id);
2046
2047         String hql = "FROM NetworkResource WHERE id = :id";
2048         Query query = getSession ().createQuery (hql);
2049         query.setParameter ("id", id);
2050
2051         NetworkResource networkResource = null;
2052         try {
2053             networkResource = (NetworkResource) query.uniqueResult ();
2054         } catch (org.hibernate.NonUniqueResultException nure) {
2055             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: NETWORK_RESOURCE.id='" + id + "'");
2056             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for NETWORK_RESOURCE.id=" + id, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for NETWORK_RESOURCE.id=" + id);
2057                 throw nure;
2058         } catch (org.hibernate.HibernateException he) {
2059                 LOGGER.debug("Hibernate Exception - while searching for: NETWORK_RESOURCE.id='" + id + "' " + he.getMessage());
2060             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for NETWORK_RESOURCE.id=" + id, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for NETWORK_RESOURCE.id=" + id);
2061                 throw he;
2062         } catch (Exception e) {
2063                 LOGGER.debug("Generic Exception - while searching for: NETWORK_RESOURCE.id='" + id + " " + e.getMessage());
2064             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for NETWORK_RESOURCE.id=" + id, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for NETWORK_RESOURCE.id=" + id);
2065                 throw e;
2066         }
2067
2068         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResourceById", null);
2069         return networkResource;
2070
2071     }
2072
2073     public NetworkResource getNetworkResourceById(String id) {
2074         long startTime = System.currentTimeMillis ();
2075         LOGGER.debug ("Catalog database - getNetworkResource with model_uuid " + id);
2076
2077         String hql = "FROM NetworkResource WHERE modelUUID = :model_uuid";
2078         Query query = getSession ().createQuery (hql);
2079         query.setParameter ("model_uuid", id);
2080         
2081         List<NetworkResource> networkResources = null;
2082         try {
2083                 networkResources = query.list ();
2084         } catch (org.hibernate.HibernateException he) {
2085                 LOGGER.debug("Hibernate Exception - while searching for: NETWORK_RESOURCE.id='" + id + "' " + he.getMessage());
2086                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for NETWORK_RESOURCE.model_uuid=" + id, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for NETWORK_RESOURCE.model_uuid=" + id);
2087                 throw he;
2088         } catch (Exception e) {
2089                 LOGGER.debug("Generic Exception - while searching for: NETWORK_RESOURCE.id='" + id + " " + e.getMessage());
2090                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for NETWORK_RESOURCE.model_uuid=" + id, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for NETWORK_RESOURCE.model_uuid=" + id);
2091                 throw e;
2092         }
2093         
2094         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResourceById", null);
2095         if (networkResources == null || networkResources.isEmpty())
2096                 return null;
2097         else
2098                 return networkResources.get(0);
2099     }
2100     
2101     // 1707 API Spec
2102     
2103     public static boolean isEmptyOrNull(String str) {
2104         if (str == null) 
2105                 return true;
2106         if ("null".equals(str))
2107                 return true;
2108         if ("".equals(str))
2109                 return true;
2110         return false;
2111     }
2112     
2113     public List<ServiceToResourceCustomization> getSTR(String serviceModelUuid, String resourceModelCustomizationUuid, String modelType) {
2114         LOGGER.debug("Catalog database: getSTR - smu=" + serviceModelUuid + ", rmcu=" + resourceModelCustomizationUuid + ", modelType = " + modelType);
2115         
2116         if (isEmptyOrNull(serviceModelUuid) && isEmptyOrNull(resourceModelCustomizationUuid) && isEmptyOrNull(modelType)) 
2117                 return null;
2118         
2119         StringBuilder hql = new StringBuilder("FROM ServiceToResourceCustomization WHERE ");
2120         boolean first = true;
2121         if (serviceModelUuid != null && !serviceModelUuid.equals("")) {
2122                 hql.append("serviceModelUUID = :smu");
2123                 first = false;
2124         }
2125         if (resourceModelCustomizationUuid != null && !resourceModelCustomizationUuid.equals("")) {
2126                 if (!first) {
2127                         hql.append(" AND ");
2128                 }
2129                 hql.append("resourceModelCustomizationUUID = :rmcu");
2130                 first = false;
2131         }
2132         if (modelType != null && !modelType.equals("")) {
2133                 if (!first) {
2134                         hql.append(" AND ");
2135                         first = false;
2136                 }
2137                 hql.append("modelType = :modelType");
2138                 first = false;
2139         }
2140         Query query = getSession().createQuery(hql.toString());
2141         if (hql.toString().contains(":smu")) 
2142                 query.setParameter("smu", serviceModelUuid);
2143         if (hql.toString().contains(":rmcu")) 
2144                 query.setParameter("rmcu", resourceModelCustomizationUuid);
2145         if (hql.toString().contains(":modelType")) 
2146                 query.setParameter("modelType", modelType);
2147         LOGGER.debug("query - " + hql.toString());
2148         
2149         @SuppressWarnings("unchecked")
2150         List<ServiceToResourceCustomization> resultList = query.list();
2151         if (resultList == null || resultList.isEmpty()) {
2152                 LOGGER.debug("Found no matches to the query - " + hql.toString());
2153                 return new ArrayList<>();
2154         }
2155         return resultList;
2156     }
2157     
2158     public List<VnfResCustomToVfModuleCustom> getVRCtoVFMC (String vrc_mcu, String vfmc_mcu) {
2159         LOGGER.debug("Catalog database: getVRCtoVFMC - vrc_mcu=" + vrc_mcu + ", vfmc_mcu=" + vfmc_mcu);
2160         
2161         if (isEmptyOrNull(vrc_mcu) && isEmptyOrNull(vfmc_mcu))
2162                 return null;
2163         
2164         StringBuilder hql = new StringBuilder("FROM VnfResCustomToVfModuleCustom WHERE ");
2165         boolean first = true;
2166         if (vrc_mcu != null && !vrc_mcu.equals("")) {
2167                 hql.append("vnfResourceCustModelCustomizationUuid = :vrc_mcu");
2168                 first = false;
2169         }
2170         if (vfmc_mcu != null && !vfmc_mcu.equals("")) {
2171                 if (!first) {
2172                         hql.append(" AND ");
2173                 }
2174                 hql.append("vfModuleCustModelCustomizationUuid = :vfmc_mcu");
2175                 first = false;
2176         }
2177         Query query = getSession().createQuery(hql.toString());
2178         if (hql.toString().contains(":vrc_mcu")) 
2179                 query.setParameter("vrc_mcu", vrc_mcu);
2180         if (hql.toString().contains(":vfmc_mcu")) 
2181                 query.setParameter("vfmc_mcu", vfmc_mcu);
2182         @SuppressWarnings("unchecked")
2183         List<VnfResCustomToVfModuleCustom> resultList = query.list();
2184         if (resultList == null || resultList.isEmpty()) {
2185                 LOGGER.debug("Found no matches to the query - " + hql.toString());
2186                 return new ArrayList<>();
2187         }
2188         return resultList;
2189     }
2190     
2191     @SuppressWarnings("unchecked")
2192     public List <TempNetworkHeatTemplateLookup> getTempNetworkHeatTemplateLookup (String networkResourceModelName) {
2193
2194         long startTime = System.currentTimeMillis ();
2195         LOGGER.debug ("Catalog database - GetTempNetworkHeatTemplateLookup for Network Name " + networkResourceModelName);
2196
2197         String hql = "FROM TempNetworkHeatTemplateLookup where networkResourceModelName = :networkResourceModelName";
2198         Query query = getSession ().createQuery (hql);
2199         query.setParameter ("networkResourceModelName", networkResourceModelName);
2200
2201         List <TempNetworkHeatTemplateLookup> result = query.list ();
2202         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getTempNetworkHeatTemplateLookup", null);
2203         return result;
2204     }
2205     
2206     // 1702 API Spec - Query for all networks in a Service:
2207     public List<NetworkResourceCustomization> getAllNetworksByServiceModelUuid(String serviceModelUuid) {
2208         long startTime = System.currentTimeMillis();
2209         LOGGER.debug("Catalog database: getServiceNetworksByServiceModelUuid - " + serviceModelUuid);
2210
2211         List<ServiceToResourceCustomization> strMappings = this.getSTR(serviceModelUuid, null, "network");
2212         if (strMappings == null || strMappings.isEmpty()) {
2213                 LOGGER.debug("Found NO matches for NRC with ServiceModelUuid=" + serviceModelUuid);
2214             return new ArrayList<>();
2215         }
2216         LOGGER.debug("Found " + strMappings.size() + " entries in ServiceToResourceCustomizations.network with smu=" + serviceModelUuid); 
2217
2218         ArrayList<NetworkResourceCustomization> masterList = new ArrayList<>();
2219         for (ServiceToResourceCustomization stn : strMappings) {
2220                 String networkModelCustomizationUuid = stn.getResourceModelCustomizationUUID();
2221             LOGGER.debug("Now searching for NetworkResourceCustomization for " + networkModelCustomizationUuid);
2222             List<NetworkResourceCustomization> resultSet = this.getAllNetworksByNetworkModelCustomizationUuid(networkModelCustomizationUuid);
2223             masterList.addAll(resultSet);
2224         }
2225         LOGGER.debug("Returning " + masterList.size() + " NRC records");
2226         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworksByServiceModelUuid", null);
2227         return masterList;
2228     }
2229     public List<NetworkResourceCustomization> getAllNetworksByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
2230         LOGGER.debug("Catalog database: getServiceNetworksByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
2231
2232         Query query = getSession().createQuery("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
2233         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2234         @SuppressWarnings("unchecked")
2235         List<Service> serviceList = query.list();
2236
2237         if (serviceList.isEmpty()) {
2238             LOGGER.debug("Could not find Service for " + serviceModelInvariantUuid);
2239             return new ArrayList<>();
2240         }
2241
2242         serviceList.sort(new MavenLikeVersioningComparator());
2243         Collections.reverse (serviceList);
2244         Service service = serviceList.get(0);
2245
2246         String serviceNameVersionId = service.getModelUUID();
2247         LOGGER.debug("The highest version for the Service " + serviceModelInvariantUuid + " is " + serviceNameVersionId);
2248
2249         // Service.serviceNameVersionId == ServiceToNetworks.serviceModelUuid
2250         return this.getAllNetworksByServiceModelUuid(serviceNameVersionId);
2251     }
2252     public List<NetworkResourceCustomization> getAllNetworksByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
2253         LOGGER.debug("Catalog database: getServiceNetworksByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
2254
2255         Query query = getSession().createQuery(
2256             "FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
2257         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2258         query.setParameter("serviceModelVersion", serviceModelVersion);
2259
2260         //TODO
2261         //can fix this later - no time - could do a unique query here - but this should work
2262         @SuppressWarnings("unchecked")
2263         List<Service> serviceList = query.list();
2264
2265         if (serviceList.isEmpty()) {
2266             LOGGER.debug("No Service found with smu=" + serviceModelInvariantUuid + " and smv=" + serviceModelVersion);
2267             return new ArrayList<>();
2268         }
2269
2270         serviceList.sort(new MavenLikeVersioningComparator());
2271         Collections.reverse (serviceList);
2272         Service service = serviceList.get(0);
2273
2274         String serviceNameVersionId = service.getModelUUID();
2275
2276         // Service.serviceNameVersionId == ServiceToNetworks.serviceModelUuid
2277         return this.getAllNetworksByServiceModelUuid(serviceNameVersionId);
2278
2279     }
2280     public List<NetworkResourceCustomization> getAllNetworksByNetworkModelCustomizationUuid(String networkModelCustomizationUuid) {
2281         long startTime = System.currentTimeMillis();
2282         LOGGER.debug("Catalog database: getAllNetworksByNetworkModelCustomizationUuid - " + networkModelCustomizationUuid);
2283
2284         //Query query = getSession().createQuery(hql.toString());
2285         //query.setParameter("networkModelCustomizationUuid", networkModelCustomizationUuid);
2286         //LOGGER.debug("QUERY: " + hql.toString() + ", networkModelCustomizationUuid=" + networkModelCustomizationUuid);
2287
2288         //@SuppressWarnings("unchecked")
2289         //List<NetworkResourceCustomization> resultList = query.list();
2290
2291         HashMap<String, String> params = new HashMap<>();
2292         params.put("networkModelCustomizationUuid", networkModelCustomizationUuid);
2293
2294         List<NetworkResourceCustomization> resultList = this.executeQueryMultipleRows(
2295             "FROM NetworkResourceCustomization WHERE modelCustomizationUuid = :networkModelCustomizationUuid", params, true);
2296
2297         if (resultList.isEmpty()) {
2298                 LOGGER.debug("Unable to find an NMC with nmcu=" + networkModelCustomizationUuid);
2299                 return new ArrayList<>();
2300         }
2301         for (NetworkResourceCustomization nrc : resultList) {
2302                 nrc.setNetworkResource(this.getNetworkResourceById(nrc.getNetworkResourceModelUuid()));
2303         }
2304
2305         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworksByNetworkModelCustomizationUuid", null);
2306         return resultList;
2307     }
2308     
2309     public List<NetworkResourceCustomization> getAllNetworksByNetworkType(String networkType) {
2310         long startTime = System.currentTimeMillis();
2311         LOGGER.debug("Catalog database: getServiceNetworksByNetworkType - " + networkType);
2312
2313         NetworkResource nr = this.getNetworkResource(networkType);
2314         if (nr == null) {
2315             return new ArrayList<>();
2316         }
2317         String networkResourceId = nr.getModelUUID();
2318
2319         LOGGER.debug("Now searching for NRC's with networkResourceId = " + networkResourceId);
2320
2321         Query query = getSession().createQuery(
2322             "FROM NetworkResourceCustomization WHERE networkResourceModelUuid = :networkResourceId");
2323         query.setParameter("networkResourceId", networkResourceId);
2324
2325         @SuppressWarnings("unchecked")
2326         List<NetworkResourceCustomization> resultList = query.list();
2327
2328         if (resultList != null && ! resultList.isEmpty()) {
2329             LOGGER.debug("Found " + resultList.size() + " results");
2330             for (NetworkResourceCustomization nrc : resultList) {
2331                 nrc.setNetworkType(networkType);
2332                 nrc.setNetworkResource(nr);
2333             }
2334         }
2335         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworksByNetworkType", null);
2336
2337         return resultList;
2338     }
2339     public ArrayList<VfModuleCustomization> getAllVfmcForVrc(VnfResourceCustomization vrc) {
2340         LOGGER.debug("Catalog database: getAllVfmcForVrc - " + vrc.getModelCustomizationUuid());
2341
2342         List<VnfResCustomToVfModuleCustom> vfmcs = this.getVRCtoVFMC(vrc.getModelCustomizationUuid(), null);
2343         if (vfmcs == null || vfmcs.isEmpty()) {
2344                 return new ArrayList<>();
2345         }
2346         ArrayList<VfModuleCustomization> vfModuleCusts = new ArrayList<>();
2347         for (VnfResCustomToVfModuleCustom vfmc : vfmcs) {
2348                 VfModuleCustomization vfmcust = this.getVfModuleCustomizationByModelCustomizationId(vfmc.getVfModuleCustModelCustomizationUuid());
2349                 if (vfmcust != null) {
2350                         vfModuleCusts.add(vfmcust);
2351                 }
2352         }
2353         return vfModuleCusts;
2354     }
2355
2356     //1702 API Spec cont'd - Query for all VnfResources in a Service:
2357     //1707 modified for db refactoring
2358     public List<VnfResourceCustomization> getAllVnfsByServiceModelUuid(String serviceModelUuid) {
2359         LOGGER.debug("Catalog database: getAllVnfsByServiceModelUuid - " + serviceModelUuid);
2360
2361         Query query = getSession().createQuery("FROM Service WHERE modelUUID = :serviceModelUuid");
2362         query.setParameter("serviceModelUuid", serviceModelUuid);
2363         @SuppressWarnings("unchecked")
2364         List<Service> serviceList = query.list();
2365
2366         if (serviceList.isEmpty()) {
2367                 LOGGER.debug("Unable to find a service with modelUuid=" + serviceModelUuid);
2368                 return new ArrayList<>();
2369         }
2370
2371         serviceList.sort(new MavenLikeVersioningComparator());
2372         Collections.reverse (serviceList);
2373
2374         // Step 2 - Now query to get the related VnfResourceCustomizations
2375
2376         List<ServiceToResourceCustomization> strcs = this.getSTR(serviceModelUuid, null, "vnf");
2377
2378         if (strcs.isEmpty()) {
2379                 LOGGER.debug("Unable to find any related vnfs to a service with modelUuid=" + serviceModelUuid);
2380                 return new ArrayList<>();
2381     }
2382
2383         ArrayList<VnfResourceCustomization> allVrcs = new ArrayList<>();
2384         for (ServiceToResourceCustomization strc : strcs) {
2385                 LOGGER.debug("Try to find VRC for mcu=" + strc.getResourceModelCustomizationUUID());
2386                 VnfResourceCustomization vrc = this.getVnfResourceCustomizationByModelCustomizationUuid(strc.getResourceModelCustomizationUUID());
2387                 if (vrc != null)
2388                         allVrcs.add(vrc);
2389         }
2390         return allVrcs;
2391
2392     }
2393     public List<VnfResourceCustomization> getAllVnfsByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
2394         LOGGER.debug("Catalog database: getAllVnfsByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
2395
2396         Query query = getSession().createQuery("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
2397         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2398         @SuppressWarnings("unchecked")
2399         List<Service> resultList = query.list();
2400
2401         if (resultList.isEmpty()) {
2402                 return new ArrayList<>();
2403         }
2404         resultList.sort(new MavenLikeVersioningComparator());
2405         Collections.reverse (resultList);
2406         Service service = resultList.get(0);
2407         //now just call the method that takes the version - the service object will have the highest version
2408         return this.getAllVnfsByServiceModelUuid(service.getModelUUID());
2409     }
2410     public List<VnfResourceCustomization> getAllVnfsByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
2411         long startTime = System.currentTimeMillis();
2412         LOGGER.debug("Catalog database: getAllVnfsByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
2413
2414         Query query = getSession().createQuery(
2415             "FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
2416         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2417         query.setParameter("serviceModelVersion", serviceModelVersion);
2418
2419         @SuppressWarnings("unchecked")
2420         List<Service> resultList = query.list();
2421
2422         if (resultList.isEmpty()) {
2423                 return new ArrayList<>();
2424                 }
2425         resultList.sort(new MavenLikeVersioningComparator());
2426         Collections.reverse (resultList);
2427         Service service = resultList.get(0);
2428         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVnfsByServiceModelInvariantUuid", null);
2429         return this.getAllVnfsByServiceModelUuid(service.getModelUUID());
2430             }
2431
2432     public List<VnfResourceCustomization> getAllVnfsByServiceName(String serviceName, String serviceVersion)  {
2433         LOGGER.debug("Catalog database: getAllVnfsByServiceName - " + serviceName + ", version=" + serviceVersion);
2434         if (serviceVersion == null || serviceVersion.equals("")) {
2435             return this.getAllVnfsByServiceName(serviceName);
2436         }
2437
2438         Query query = getSession().createQuery(
2439             "FROM Service WHERE modelName = :serviceName and version = :serviceVersion");
2440         query.setParameter("serviceName", serviceName);
2441         query.setParameter("serviceVersion", serviceVersion);
2442
2443         @SuppressWarnings("unchecked")
2444         List<Service> resultList = query.list();
2445
2446         if (resultList.isEmpty()) {
2447             return Collections.EMPTY_LIST;
2448         }
2449         Service service = resultList.get(0);
2450         return this.getAllVnfsByServiceModelUuid(service.getModelUUID());
2451     }
2452     public List<VnfResourceCustomization> getAllVnfsByServiceName(String serviceName) {
2453         LOGGER.debug("Catalog database: getAllVnfsByServiceName - " + serviceName);
2454
2455         Query query = getSession().createQuery("FROM Service WHERE modelName = :serviceName");
2456         query.setParameter("serviceName", serviceName);
2457
2458         @SuppressWarnings("unchecked")
2459         List<Service> resultList = query.list();
2460
2461         if (resultList.isEmpty()) {
2462             return Collections.EMPTY_LIST;
2463         }
2464         resultList.sort(new MavenLikeVersioningComparator());
2465         Collections.reverse (resultList);
2466         Service service = resultList.get(0);
2467
2468         return this.getAllVnfsByServiceModelUuid(service.getModelUUID());
2469     }
2470
2471     public List<VnfResourceCustomization> getAllVnfsByVnfModelCustomizationUuid(String vnfModelCustomizationUuid) {
2472         long startTime = System.currentTimeMillis();
2473         LOGGER.debug("Catalog database: getAllVnfsByVnfModelCustomizationUuid - " + vnfModelCustomizationUuid);
2474
2475         Query query1 = getSession().createQuery("FROM VnfResourceCustomization WHERE modelCustomizationUuid = :vrcmcu");
2476         query1.setParameter("vrcmcu", vnfModelCustomizationUuid);
2477         @SuppressWarnings("unchecked")
2478         List<VnfResourceCustomization> resultList1 = query1.list();
2479
2480         if (resultList1.isEmpty()) {
2481             LOGGER.debug("Found no records matching " + vnfModelCustomizationUuid);
2482             return Collections.EMPTY_LIST;
2483         }
2484
2485         for (VnfResourceCustomization vrc : resultList1) {
2486             VnfResource vr = this.getVnfResourceByModelUuid(vrc.getVnfResourceModelUuid());
2487             vrc.setVnfResource(vr);
2488             vrc.setVfModuleCustomizations(this.getAllVfmcForVrc(vrc));
2489         }
2490
2491         LOGGER.debug("Returning " + resultList1.size() + " vnf modules");
2492         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVnfsByVnfModelCustomizationUuid", null);
2493         return resultList1;
2494     }
2495
2496     //1702 API Spec cont'd - Query for all allotted resources in a Service
2497
2498     public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelUuid(String serviceModelUuid) {
2499         long startTime = System.currentTimeMillis();
2500         LOGGER.debug("Catalog database: getAllAllottedResourcesByServiceModelUuid - " + serviceModelUuid);
2501
2502         List<ServiceToResourceCustomization> strcs = this.getSTR(serviceModelUuid, null, "allottedResource");
2503         if (strcs == null || strcs.isEmpty()) {
2504                 LOGGER.debug("No AR entries found for " + serviceModelUuid);
2505             return new ArrayList<>();
2506         }
2507         LOGGER.debug("Found " + strcs.size() + " entries in ServiceToResourceCustomizations with smu=" + serviceModelUuid + ", allottedResource"); 
2508
2509         ArrayList<AllottedResourceCustomization> masterList = new ArrayList<>();
2510         for (ServiceToResourceCustomization star : strcs) {
2511                 String arModelCustomizationUuid = star.getResourceModelCustomizationUUID();
2512             LOGGER.debug("Now searching for AllottedResourceCustomization for " + arModelCustomizationUuid);
2513             List<AllottedResourceCustomization> resultSet = this.getAllAllottedResourcesByArModelCustomizationUuid(arModelCustomizationUuid);
2514             masterList.addAll(resultSet);
2515         }
2516         LOGGER.debug("Returning " + masterList.size() + " ARC records");
2517         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllAllottedResourcesByServiceModelUuid", null);
2518         return masterList;
2519     }
2520
2521     public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
2522         LOGGER.debug("Catalog database: getAllAllottedResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
2523
2524         Query query = getSession().createQuery("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
2525         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2526         @SuppressWarnings("unchecked")
2527         List<Service> serviceList = query.list();
2528
2529         if (serviceList.isEmpty()) {
2530             LOGGER.debug("Could not find Service for " + serviceModelInvariantUuid);
2531             return new ArrayList<>();
2532         }
2533
2534         serviceList.sort(new MavenLikeVersioningComparator());
2535         Collections.reverse (serviceList);
2536         Service service = serviceList.get(0);
2537
2538         String serviceModelUuid = service.getModelUUID();
2539         LOGGER.debug("The highest version for the Service " + serviceModelInvariantUuid + " is " + serviceModelUuid);
2540
2541         return this.getAllAllottedResourcesByServiceModelUuid(serviceModelUuid);
2542     }
2543
2544     public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
2545         LOGGER.debug("Catalog database: getAllAllottedResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
2546
2547         Query query = getSession().createQuery(
2548             "FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
2549         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2550         query.setParameter("serviceModelVersion", serviceModelVersion);
2551
2552         @SuppressWarnings("unchecked")
2553         List<Service> serviceList = query.list();
2554
2555         if (serviceList.isEmpty()) {
2556             LOGGER.debug("No Service found with smu=" + serviceModelInvariantUuid + " and smv=" + serviceModelVersion);
2557             return new ArrayList<>();
2558         }
2559
2560         serviceList.sort(new MavenLikeVersioningComparator());
2561         Collections.reverse (serviceList);
2562         Service service = serviceList.get(0);
2563
2564         String serviceModelUuid = service.getModelUUID();
2565
2566         return this.getAllAllottedResourcesByServiceModelUuid(serviceModelUuid);
2567     }
2568
2569     public List<AllottedResourceCustomization> getAllAllottedResourcesByArModelCustomizationUuid(String arModelCustomizationUuid) {
2570         long startTime = System.currentTimeMillis();
2571         LOGGER.debug("Catalog database: getAllAllottedResourcesByArModelCustomizationUuid - " + arModelCustomizationUuid);
2572
2573         Query query = getSession().createQuery(
2574             "FROM AllottedResourceCustomization WHERE modelCustomizationUuid = :arModelCustomizationUuid");
2575         query.setParameter("arModelCustomizationUuid", arModelCustomizationUuid);
2576
2577         @SuppressWarnings("unchecked")
2578         List<AllottedResourceCustomization> resultList = query.list();
2579
2580         if (resultList.isEmpty()) {
2581                 LOGGER.debug("No ARC found with arc_mcu=" + arModelCustomizationUuid);
2582                 return new ArrayList<>();
2583         }
2584         // There should only be one - but we'll handle if multiple
2585         for (AllottedResourceCustomization arc : resultList) {
2586                 AllottedResource ar = this.getAllottedResourceByModelUuid(arc.getArModelUuid());
2587                 arc.setAllottedResource(ar);
2588         }
2589         
2590         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllAllottedResourcesByArModelCustomizationUuid", null);
2591         return resultList;
2592     }
2593
2594     public AllottedResource getAllottedResourceByModelUuid(String arModelUuid) {
2595         long startTime = System.currentTimeMillis ();
2596         LOGGER.debug ("Catalog database - get Allotted Resource with modelUuid= " + arModelUuid);
2597
2598         String hql = "FROM AllottedResource WHERE modelUuid = :arModelUuid";
2599         Query query = getSession ().createQuery (hql);
2600         query.setParameter ("arModelUuid", arModelUuid);
2601
2602         @SuppressWarnings("unchecked")
2603         List <AllottedResource> resultList = query.list ();
2604
2605         // See if something came back. Name is unique, so
2606         if (resultList.isEmpty ()) {
2607             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. AllottedResource not found", "CatalogDB", "getAllottedResourceByModelUuid", null);
2608             return null;
2609         }
2610         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllottedResourceByModelUuid", null);
2611         return resultList.get (0);
2612         
2613     }
2614     
2615     //1702 API Spec cont'd - Query for all resources in a Service:
2616     public ServiceMacroHolder getAllResourcesByServiceModelUuid(String serviceModelUuid) {
2617         long startTime = System.currentTimeMillis();
2618         LOGGER.debug("Catalog database: getAllResourcesByServiceModelUuid - " + serviceModelUuid);
2619
2620         StringBuilder hql = new StringBuilder("FROM Service WHERE modelUUID = :serviceModelUuid");
2621         Query query = getSession().createQuery(hql.toString());
2622         query.setParameter("serviceModelUuid", serviceModelUuid);
2623         LOGGER.debug("Query: " + hql.toString() + ", smu=" + serviceModelUuid);
2624         @SuppressWarnings("unchecked")
2625         List<Service> serviceList = query.list();
2626
2627         if (serviceList.isEmpty()) {
2628             LOGGER.debug("Unable to find a Service with serviceModelUuid=" + serviceModelUuid);
2629             return new ServiceMacroHolder();
2630         }
2631
2632         serviceList.sort(new MavenLikeVersioningComparator());
2633         Collections.reverse (serviceList);
2634         Service service = serviceList.get(0);
2635
2636         ServiceMacroHolder smh = new ServiceMacroHolder(service);
2637         ArrayList<NetworkResourceCustomization> nrcList = (ArrayList<NetworkResourceCustomization>) this.getAllNetworksByServiceModelUuid(serviceModelUuid);
2638         smh.setNetworkResourceCustomization(nrcList);
2639         ArrayList<AllottedResourceCustomization> arcList = (ArrayList<AllottedResourceCustomization>) this.getAllAllottedResourcesByServiceModelUuid(serviceModelUuid);
2640         smh.setAllottedResourceCustomization(arcList);
2641         ArrayList<VnfResourceCustomization> vnfList = (ArrayList<VnfResourceCustomization>) this.getAllVnfsByServiceModelUuid(serviceModelUuid);
2642         smh.setVnfResourceCustomizations(vnfList);
2643
2644         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllResourcesByServiceModelUuid", null);
2645         return smh;
2646     }
2647     public ServiceMacroHolder getAllResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
2648         long startTime = System.currentTimeMillis();
2649         LOGGER.debug("Catalog database: getAllResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
2650
2651         Query query = getSession().createQuery("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
2652         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2653         @SuppressWarnings("unchecked")
2654         List<Service> serviceList = query.list();
2655
2656         if (serviceList.isEmpty()) {
2657             LOGGER.debug("Unable to find a Service with serviceModelInvariantUuid=" + serviceModelInvariantUuid);
2658             return new ServiceMacroHolder();
2659         }
2660
2661         serviceList.sort(new MavenLikeVersioningComparator());
2662         Collections.reverse (serviceList);
2663         Service service = serviceList.get(0);
2664
2665         ServiceMacroHolder smh = new ServiceMacroHolder(service);
2666         ArrayList<NetworkResourceCustomization> nrcList = (ArrayList<NetworkResourceCustomization>) this.getAllNetworksByServiceModelUuid(service.getModelUUID());
2667         smh.setNetworkResourceCustomization(nrcList);
2668         ArrayList<AllottedResourceCustomization> arcList = (ArrayList<AllottedResourceCustomization>) this.getAllAllottedResourcesByServiceModelUuid(service.getModelUUID());
2669         smh.setAllottedResourceCustomization(arcList);
2670         ArrayList<VnfResourceCustomization> vnfList = (ArrayList<VnfResourceCustomization>) this.getAllVnfsByServiceModelUuid(service.getModelUUID());
2671         smh.setVnfResourceCustomizations(vnfList);
2672
2673         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllResourcesByServiceModelInvariantUuid", null);
2674         return smh;
2675
2676     }
2677     public ServiceMacroHolder getAllResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
2678         long startTime = System.currentTimeMillis();
2679         LOGGER.debug("Catalog database: getAllResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
2680
2681         Query query = getSession().createQuery(
2682             "FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
2683         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
2684         query.setParameter("serviceModelVersion", serviceModelVersion);
2685         //TODO make this a unique query
2686         @SuppressWarnings("unchecked")
2687         List<Service> serviceList = query.list();
2688
2689         if (serviceList.isEmpty()) {
2690             LOGGER.debug("Unable to find a Service with serviceModelInvariantUuid=" + serviceModelInvariantUuid + " and serviceModelVersion=" + serviceModelVersion);
2691             return new ServiceMacroHolder();
2692         }
2693
2694         serviceList.sort(new MavenLikeVersioningComparator());
2695         Collections.reverse (serviceList);
2696         Service service = serviceList.get(0);
2697
2698         ServiceMacroHolder smh = new ServiceMacroHolder(service);
2699         ArrayList<NetworkResourceCustomization> nrcList = (ArrayList<NetworkResourceCustomization>) this.getAllNetworksByServiceModelUuid(service.getModelUUID());
2700         smh.setNetworkResourceCustomization(nrcList);
2701         ArrayList<AllottedResourceCustomization> arcList = (ArrayList<AllottedResourceCustomization>) this.getAllAllottedResourcesByServiceModelUuid(service.getModelUUID());
2702         smh.setAllottedResourceCustomization(arcList);
2703         ArrayList<VnfResourceCustomization> vnfList = (ArrayList<VnfResourceCustomization>) this.getAllVnfsByServiceModelUuid(service.getModelUUID());
2704         smh.setVnfResourceCustomizations(vnfList);
2705
2706         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllResourcesByServiceModelUuid with version", null);
2707         return smh;
2708     }
2709
2710     // 1707 New API queries
2711     public NetworkResourceCustomization getSingleNetworkByModelCustomizationUuid(String modelCustomizationUuid) {
2712         LOGGER.debug("Catalog database; getSingleNetworkByModelCustomizationUuid - " + modelCustomizationUuid);
2713         List<NetworkResourceCustomization> resultList = this.getAllNetworksByNetworkModelCustomizationUuid(modelCustomizationUuid);
2714         if (resultList == null || resultList.isEmpty()) {
2715             return null;
2716         }
2717         return resultList.get(0);
2718     }
2719     public AllottedResourceCustomization getSingleAllottedResourceByModelCustomizationUuid(String modelCustomizationUuid) {
2720         LOGGER.debug("Catalog database; getSingleAllottedResourceByModelCustomizationUuid - " + modelCustomizationUuid);
2721         List<AllottedResourceCustomization> resultList = this.getAllAllottedResourcesByArModelCustomizationUuid(modelCustomizationUuid);
2722         if (resultList == null || resultList.isEmpty()) {
2723             return null;
2724         }
2725         return resultList.get(0);
2726     }
2727     @Deprecated
2728     public VnfResource getSingleVnfResourceByModelCustomizationUuid(String modelCustomizationUuid) {
2729         /*
2730         long startTime = System.currentTimeMillis();
2731         LOGGER.debug("Catalog database; getSingleVnfResourceByModelCustomizationUuid - " + modelCustomizationUuid);
2732         List<VnfResource> resultList = this.getAllVnfsByVnfModelCustomizationUuid(modelCustomizationUuid);
2733         if (resultList == null || resultList.size() < 1) {
2734             return null;
2735         }
2736         return resultList.get(0);
2737         */
2738         return null;
2739     }
2740
2741     private void populateNetworkResourceType(List<NetworkResourceCustomization> resultList) {
2742         HashMap<String, NetworkResource> networkResources = new HashMap<>();
2743
2744         for (NetworkResourceCustomization nrc : resultList) {
2745                 String network_id = nrc.getNetworkResourceModelUuid();
2746             if (network_id == null) {
2747                 nrc.setNetworkResource(null);
2748                 nrc.setNetworkType("UNKNOWN_NETWORK_ID_NULL");
2749                 continue;
2750             }
2751             if (networkResources.containsKey(network_id)) {
2752                 nrc.setNetworkResource(networkResources.get(network_id));
2753                         nrc.setNetworkType(networkResources.get(network_id).getModelName());
2754             } else {
2755                 NetworkResource nr = this.getNetworkResourceById(network_id);
2756                 if (nr == null) {
2757                     nrc.setNetworkType("INVALID_NETWORK_TYPE_ID_NOT_FOUND");
2758                 } else {
2759                                 nrc.setNetworkType(nr.getModelName());
2760                     nrc.setNetworkResource(nr);
2761                     networkResources.put(network_id, nr);
2762                 }
2763             }
2764         }
2765     }
2766
2767     /**
2768      * Return a VNF recipe that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME, and ACTION
2769      * first query VF_MODULE table by type, and then use the ID to query
2770      * VNF_RECIPE by VF_MODULE_ID and ACTION
2771      *
2772      * @param vnfType
2773      * @parm vfModuleModelName
2774      * @param action
2775      * @return VnfRecipe object or null if none found
2776      */
2777     public VnfRecipe getVfModuleRecipe (String vnfType, String vfModuleModelName, String action) {
2778         String vfModuleType = vnfType + "::" + vfModuleModelName;
2779
2780         long startTime = System.currentTimeMillis ();
2781         LOGGER.debug ("Catalog database - get VF MODULE  with type " + vfModuleType);
2782
2783         Query query = getSession ().createQuery ("FROM VfModule WHERE type = :type ");
2784         query.setParameter (TYPE, vfModuleType);
2785
2786         @SuppressWarnings("unchecked")
2787         List <VfModule> resultList = query.list ();
2788
2789         if (resultList.isEmpty ()) {
2790             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF Module Entry not found", "CatalogDB", "getVfModuleRecipe", null);
2791             return null;
2792         }
2793
2794         resultList.sort(new MavenLikeVersioningComparator());
2795         Collections.reverse (resultList);
2796
2797         VfModule vfMod = resultList.get(0);
2798
2799         String vfModuleId = vfMod.getModelUUID();
2800
2801         LOGGER.debug ("Catalog database - get VNF recipe with vf module id " + vfModuleId
2802                                       + " and action "
2803                                       + action);
2804
2805         Query query1 = getSession ().createQuery ("FROM VnfRecipe WHERE vfModuleId = :vfModuleId AND action = :action ");
2806         query1.setParameter (VF_MODULE_MODEL_UUID, vfModuleId);
2807         query1.setParameter (ACTION, action);
2808
2809         @SuppressWarnings("unchecked")
2810         List <VnfRecipe> resultList1 = query1.list ();
2811
2812         if (resultList1.isEmpty ()) {
2813             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVfModuleRecipe", null);
2814             return null;
2815         }
2816
2817         resultList1.sort(new MavenLikeVersioningComparator());
2818         Collections.reverse (resultList1);
2819
2820         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe found", "CatalogDB", "getVfModuleRecipe", null);
2821         return resultList1.get (0);
2822     }
2823
2824     /**
2825      * Return a VNF Module List that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME,
2826      * ASDC_SERVICE_MODEL_VERSION, MODEL_VERSION, and ACTION
2827      *
2828      * @param vfModuleType
2829      * @parm modelCustomizationUuid
2830      * @param asdcServiceModelVersion
2831      * @param modelVersion
2832      * @param action
2833      * @return VfModule list
2834      */
2835     public List<VfModule> getVfModule (String vfModuleType, String modelCustomizationUuid, String asdcServiceModelVersion, String modelVersion, String action) {
2836         StringBuilder hql;
2837         Query query;
2838         if(modelCustomizationUuid != null){
2839             hql = new StringBuilder ("FROM VfModule WHERE modelCustomizationUuid = :modelCustomizationUuid AND version = :version");
2840
2841             LOGGER.debug ("Catalog database - get VF MODULE  with type " + vfModuleType + ", asdcServiceModelVersion " + asdcServiceModelVersion + ", modelVersion " + modelVersion);
2842
2843             query = getSession ().createQuery (hql.toString ());
2844             query.setParameter ("modelCustomizationUuid", modelCustomizationUuid);
2845             query.setParameter ("version", asdcServiceModelVersion);
2846         }else{
2847             hql = new StringBuilder ("FROM VfModule WHERE type = :type AND version = :version AND modelVersion = :modelVersion");
2848
2849             LOGGER.debug ("Catalog database - get VF MODULE  with type " + vfModuleType + ", asdcServiceModelVersion " + asdcServiceModelVersion + ", modelVersion " + modelVersion);
2850
2851             query = getSession ().createQuery (hql.toString ());
2852             query.setParameter (TYPE, vfModuleType);
2853             query.setParameter ("version", asdcServiceModelVersion);
2854             query.setParameter ("modelVersion", modelVersion);
2855         }
2856
2857         @SuppressWarnings("unchecked")
2858         List <VfModule> resultList = query.list ();
2859         return resultList;
2860     }
2861
2862     
2863     /**
2864      * Return a VNF COMPONENTSrecipe that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME,
2865      * MODEL_CUSTOMIZATION_UUID, ASDC_SERVICE_MODEL_VERSION, MODEL_VERSION, and ACTION
2866      * first query VF_MODULE table by type, and then use the ID to query
2867      * VNF_COMPONENTS_RECIPE by VF_MODULE_ID and ACTION
2868      *
2869      * @param vnfType
2870      * @parm vfModuleModelName
2871      * @param action
2872      * @return VnfRecipe object or null if none found
2873      */
2874     public VnfComponentsRecipe getVnfComponentsRecipe (String vnfType, String vfModuleModelName, String modelCustomizationUuid, String asdcServiceModelVersion, String modelVersion, String action) {
2875         String vfModuleType = vnfType + "::" + vfModuleModelName;
2876         long startTime = System.currentTimeMillis ();
2877         List <VfModule> resultList = getVfModule(vfModuleType, modelCustomizationUuid,  asdcServiceModelVersion,  modelVersion,  action);
2878
2879         if (resultList.isEmpty ()) {
2880             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF Module Entry not found", "CatalogDB", "getVnfComponentsRecipe", null);
2881             return null;
2882         }
2883
2884         resultList.sort(new MavenLikeVersioningComparator());
2885         Collections.reverse (resultList);
2886
2887         VfModule vfMod = resultList.get(0);
2888
2889         String vfModuleId = vfMod.getModelUUID();
2890
2891         LOGGER.debug ("Catalog database - get Vnf Components recipe with vf module id " + vfModuleId
2892                 + " and action "
2893                 + action);
2894
2895         Query query1 = getSession ().createQuery (
2896             "FROM VnfComponentsRecipe WHERE vfModuleId = :vfModuleId AND action = :action ");
2897         query1.setParameter (VF_MODULE_MODEL_UUID, vfModuleId);
2898         query1.setParameter (ACTION, action);
2899
2900         @SuppressWarnings("unchecked")
2901         List <VnfComponentsRecipe> resultList1 = query1.list ();
2902
2903         if (resultList1.isEmpty ()) {
2904             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfComponentsRecipe", null);
2905             return null;
2906         }
2907
2908         resultList1.sort(new MavenLikeVersioningComparator());
2909         Collections.reverse (resultList1);
2910
2911         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe found", "CatalogDB", "getVnfComponentsRecipe", null);
2912         if (resultList1.size() > 1 && (!resultList1. get (0).getOrchestrationUri().equals(resultList1.get (1).getOrchestrationUri ()))) {
2913             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Different ORCHESTRATION URIs found for same VERSION and ID. No result returned.", "CatalogDB", "getVnfComponentsRecipe", null);
2914             return null;
2915         }
2916         return resultList1.get (0);
2917     }
2918
2919     /**
2920      * Return a VNF COMPONENTSrecipe that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME,
2921      * ASDC_SERVICE_MODEL_VERSION, MODEL_VERSION, and ACTION
2922      * first query VF_MODULE table by type, and then use the ID to query
2923      * VNF_COMPONENTS_RECIPE by VF_MODULE_ID and ACTION
2924      *
2925      * @param vnfType
2926      * @parm vfModuleModelName
2927      * @param action
2928      * @return VnfRecipe object or null if none found
2929      */
2930     public VnfComponentsRecipe getVnfComponentsRecipeByVfModule(List <VfModule> resultList,  String action) {
2931         long startTime = System.currentTimeMillis ();
2932
2933         if (resultList.isEmpty ()) {
2934             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF Module Entry not found", "CatalogDB", "getVnfComponentsRecipe", null);
2935             return null;
2936         }
2937
2938         resultList.sort(new MavenLikeVersioningComparator());
2939         Collections.reverse (resultList);
2940
2941         VfModule vfMod = resultList.get(0);
2942
2943         String vfModuleId = vfMod.getModelName();
2944
2945         LOGGER.debug ("Catalog database - get Vnf Components recipe with vf module id " + vfModuleId
2946                                       + " and action "
2947                                       + action);
2948
2949         Query query1 = getSession ().createQuery (
2950             "FROM VnfComponentsRecipe WHERE vfModuleId = :vfModuleId AND action = :action ");
2951         query1.setParameter (VF_MODULE_MODEL_UUID, vfModuleId);
2952         query1.setParameter (ACTION, action);
2953
2954         @SuppressWarnings("unchecked")
2955         List <VnfComponentsRecipe> resultList1 = query1.list ();
2956
2957         if (resultList1.isEmpty ()) {
2958             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfComponentsRecipe", null);
2959             return null;
2960         }
2961
2962         resultList1.sort(new MavenLikeVersioningComparator());
2963         Collections.reverse (resultList1);
2964
2965         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe found", "CatalogDB", "getVnfComponentsRecipe", null);
2966         if (resultList1.size() > 1 && (!resultList1. get (0).getOrchestrationUri().equals(resultList1.get (1).getOrchestrationUri ()))) {
2967             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Different ORCHESTRATION URIs found for same VERSION and ID. No result returned.", "CatalogDB", "getVnfComponentsRecipe", null);
2968             return null;
2969         }
2970         return resultList1.get (0);
2971     }
2972
2973
2974     /**
2975      * Return all VNF Resources in the Catalog DB
2976      *
2977      * @return A list of VnfResource objects
2978      */
2979     @SuppressWarnings("unchecked")
2980     public List <VnfResource> getAllVnfResources () {
2981
2982         long startTime = System.currentTimeMillis ();
2983         LOGGER.debug ("Catalog database - get all VNF resources");
2984
2985         String hql = "FROM VnfResource";
2986         Query query = getSession ().createQuery (hql);
2987
2988         List <VnfResource> result = query.list ();
2989         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVnfResources", null);
2990         return result;
2991     }
2992
2993     /**
2994      * Return VNF Resources in the Catalog DB that match a given VNF role
2995      *
2996      * @return A list of VnfResource objects
2997      */
2998     @SuppressWarnings("unchecked")
2999     @Deprecated // vnfRole is no longer in VnfResource
3000     public List <VnfResource> getVnfResourcesByRole (String vnfRole) {
3001
3002         long startTime = System.currentTimeMillis ();
3003         LOGGER.debug ("Catalog database - get all VNF resources for role " + vnfRole);
3004
3005         String hql = "FROM VnfResource WHERE vnfRole = :vnfRole";
3006         Query query = getSession ().createQuery (hql);
3007         query.setParameter ("vnfRole", vnfRole);
3008
3009         List <VnfResource> resources = query.list ();
3010         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourcesByRole", null);
3011         return resources;
3012     }
3013
3014     /**
3015      * Return VNF Resources in the Catalog DB that match a given VNF role
3016      *
3017      * @return A list of VnfResource objects
3018      */
3019     @SuppressWarnings("unchecked")
3020     public List<VnfResourceCustomization> getVnfResourceCustomizationsByRole(String vnfRole) {
3021         long startTime = System.currentTimeMillis ();
3022         LOGGER.debug ("Catalog database - get all VNF resource customizations for role " + vnfRole);
3023
3024         String hql = "FROM VnfResourceCustomization WHERE nfRole = :vnfRole";
3025         Query query = getSession ().createQuery (hql);
3026         query.setParameter ("vnfRole", vnfRole);
3027
3028         List <VnfResourceCustomization> resources = query.list ();
3029         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceCustomizationsByRole", null);
3030         return resources;
3031     }
3032
3033     /**
3034      * Return all Network Resources in the Catalog DB
3035      *
3036      * @return A list of NetworkResource objects
3037      */
3038     @SuppressWarnings("unchecked")
3039     public List <NetworkResource> getAllNetworkResources () {
3040
3041         long startTime = System.currentTimeMillis ();
3042         LOGGER.debug ("Catalog database - get all network resources");
3043
3044         String hql = "FROM NetworkResource";
3045         Query query = getSession ().createQuery (hql);
3046
3047         List <NetworkResource> result = query.list ();
3048         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworkResources", null);
3049         return result;
3050     }
3051
3052     @SuppressWarnings("unchecked")
3053     public List<NetworkResourceCustomization> getAllNetworkResourceCustomizations() {
3054         long startTime = System.currentTimeMillis ();
3055         LOGGER.debug ("Catalog database - get all network resource customizations");
3056
3057         String hql = "FROM NetworkResourceCustomization";
3058         Query query = getSession ().createQuery (hql);
3059
3060         List <NetworkResourceCustomization> result = query.list ();
3061         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworkResourceCustomizations", null);
3062         return result;  
3063     }
3064     
3065     /**
3066      * Return all VF Modules in the Catalog DB
3067      *
3068      * @return A list of VfModule objects
3069      */
3070     @SuppressWarnings("unchecked")
3071     public List <VfModule> getAllVfModules () {
3072
3073         long startTime = System.currentTimeMillis ();
3074         LOGGER.debug ("Catalog database - get all vf modules");
3075
3076         String hql = "FROM VfModule";
3077         Query query = getSession ().createQuery (hql);
3078
3079         List <VfModule> result = query.list ();
3080         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVfModules", null);
3081         return result;
3082     }
3083
3084    @SuppressWarnings("unchecked")
3085    public List <VfModuleCustomization> getAllVfModuleCustomizations () {
3086
3087        long startTime = System.currentTimeMillis ();
3088        LOGGER.debug ("Catalog database - get all vf module customizations");
3089
3090        String hql = "FROM VfModuleCustomization";
3091        Query query = getSession ().createQuery (hql);
3092
3093        List <VfModuleCustomization> result = query.list ();
3094        LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVfModuleCustomizations", null);
3095        return result;
3096    }
3097     
3098
3099     /**
3100      * Return all HeatEnvironment in the Catalog DB
3101      *
3102      * @return A list of HeatEnvironment objects
3103      */
3104     @SuppressWarnings("unchecked")
3105     public List <HeatEnvironment> getAllHeatEnvironment () {
3106
3107         long startTime = System.currentTimeMillis ();
3108         LOGGER.debug ("Catalog database - get all Heat environments");
3109
3110         String hql = "FROM HeatEnvironment";
3111         Query query = getSession ().createQuery (hql);
3112
3113         List <HeatEnvironment> result = query.list ();
3114         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllHeatEnvironment", null);
3115         return result;
3116     }
3117
3118     /**
3119      * Fetch the Environment by Environment ID - 1510
3120      */
3121     @Deprecated // no longer in heat envt table
3122     public HeatEnvironment getHeatEnvironment (int id) {
3123
3124         long startTime = System.currentTimeMillis ();
3125         LOGGER.debug ("Catalog database - get Heat environment with id " + id);
3126
3127         String hql = "FROM HeatEnvironment WHERE id = :idValue";
3128
3129         LOGGER.debug ("getHeatEnvironment called with id=" + id);
3130
3131         Query query = getSession ().createQuery (hql);
3132         query.setParameter ("idValue", id);
3133
3134         @SuppressWarnings("unchecked")
3135         List <HeatEnvironment> resultList = query.list ();
3136
3137         // See if something came back.
3138         if (resultList.isEmpty ()) {
3139             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Heat environment not found", "CatalogDB", "getHeatEnvironment", null);
3140             return null;
3141         }
3142         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatEnvironment", null);
3143         return resultList.get (0);
3144     }
3145
3146     /**
3147      * Fetch the nested templates - 1510
3148      */
3149
3150     @Deprecated
3151     public Map <String, Object> getNestedTemplates (int templateId) {
3152         Map <String, Object> nestedTemplates;
3153         long startTime = System.currentTimeMillis ();
3154         LOGGER.debug ("Catalog database - getNestedTemplates called with templateId " + templateId);
3155
3156         String hql = "FROM HeatNestedTemplate where parent_template_id = :parentIdValue";
3157
3158         Query query = getSession ().createQuery (hql);
3159         query.setParameter ("parentIdValue", templateId);
3160
3161         @SuppressWarnings("unchecked")
3162         List <HeatNestedTemplate> resultList = query.list ();
3163         // If nothing comes back, there are no nested templates
3164         if (resultList.isEmpty ()) {
3165             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No nestedTemplate found", "CatalogDB", "getNestedTemplates", null);
3166             LOGGER.debug ("No nestedTemplates found for templateId=" + templateId + ", " + hql);
3167             return null;
3168         }
3169         // Now, for each entry in NESTED_HEAT_TEMPLATES, we need to grab the template body from HEAT_TEMPLATE
3170         nestedTemplates = new HashMap <> ();
3171         for (HeatNestedTemplate hnt : resultList) {
3172             LOGGER.debug ("Querying for " + hnt);
3173             HeatTemplate ht = this.getHeatTemplate (hnt.getChildTemplateId ());
3174             if (ht == null) {
3175                 LOGGER.debug ("No template found matching childTemplateId=" + hnt.getChildTemplateId ());
3176                 continue;
3177             }
3178             String providerResourceFile = hnt.getProviderResourceFile ();
3179             String heatTemplateBody = ht.getTemplateBody ();
3180             if (providerResourceFile != null && heatTemplateBody != null) {
3181                 nestedTemplates.put (providerResourceFile, heatTemplateBody);
3182             } else {
3183                 LOGGER.debug ("providerResourceFile or heatTemplateBody were null - do not add to HashMap!");
3184             }
3185         }
3186         // Make sure we're not returning an empty map - if so, just return null
3187         if (nestedTemplates.isEmpty ()) {
3188             LOGGER.debug ("nestedTemplates is empty - just return null");
3189             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Nested template is empty", "CatalogDB", "getNestedTemplate", null);
3190             return null;
3191         }
3192         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNestedTemplate", null);
3193         return nestedTemplates;
3194     }
3195     /**
3196      * Return a Map<String, Object> for returning the child templates and their contents
3197      * 
3198      * @param parentHeatTemplateId
3199      * @return Map<String,Object> or null if none found
3200      */
3201     public Map <String, Object> getNestedTemplates (String parentHeatTemplateId) {
3202         Map <String, Object> nestedTemplates;
3203         long startTime = System.currentTimeMillis ();
3204         LOGGER.debug ("Catalog database - getNestedTemplates called with parentTemplateId " + parentHeatTemplateId);
3205
3206         String hql = "FROM HeatNestedTemplate where parentTemplateId = :parentHeatTemplateId";
3207
3208         Query query = getSession ().createQuery (hql);
3209         query.setParameter ("parentHeatTemplateId", parentHeatTemplateId);
3210
3211         @SuppressWarnings("unchecked")
3212         List <HeatNestedTemplate> resultList = query.list ();
3213         // If nothing comes back, there are no nested templates
3214         if (resultList.isEmpty ()) {
3215             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No nestedTemplate found", "CatalogDB", "getNestedTemplates", null);
3216             LOGGER.debug ("No nestedTemplates found for templateId=" + parentHeatTemplateId + ", " + hql);
3217             return null;
3218         }
3219         // Now, for each entry in NESTED_HEAT_TEMPLATES, we need to grab the template body from HEAT_TEMPLATE
3220         nestedTemplates = new HashMap <> ();
3221         for (HeatNestedTemplate hnt : resultList) {
3222             LOGGER.debug ("Querying for " + hnt);
3223             HeatTemplate ht = this.getHeatTemplateByArtifactUuid (hnt.getChildTemplateId ());
3224             if (ht == null) {
3225                 LOGGER.debug ("No template found matching childTemplateId=" + hnt.getChildTemplateId ());
3226                 continue;
3227             }
3228             String providerResourceFile = hnt.getProviderResourceFile ();
3229             String heatTemplateBody = ht.getTemplateBody ();
3230             if (providerResourceFile != null && heatTemplateBody != null) {
3231                 nestedTemplates.put (providerResourceFile, heatTemplateBody);
3232             } else {
3233                 LOGGER.debug ("providerResourceFile or heatTemplateBody were null - do not add to HashMap!");
3234             }
3235         }
3236         // Make sure we're not returning an empty map - if so, just return null
3237         if (nestedTemplates.isEmpty ()) {
3238             LOGGER.debug ("nestedTemplates is empty - just return null");
3239             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Nested template is empty", "CatalogDB", "getNestedTemplate", null);
3240             return null;
3241         }
3242         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNestedTemplate", null);
3243         return nestedTemplates;
3244     }
3245
3246     /*
3247      * Fetch any files in the HEAT_FILES table 1510
3248      */
3249     @Deprecated
3250     public Map <String, HeatFiles> getHeatFiles (int vnfResourceId) {
3251        Map <String, HeatFiles> heatFiles;
3252
3253         long startTime = System.currentTimeMillis ();
3254         LOGGER.debug ("Catalog database - getHeatFiles called with vnfResourceId " + vnfResourceId);
3255         String hql = "FROM HeatFiles where vnf_resource_id = :vnfResourceIdValue";
3256
3257         Query query = getSession ().createQuery (hql);
3258         query.setParameter ("vnfResourceIdValue", vnfResourceId);
3259
3260         @SuppressWarnings("unchecked")
3261         List <HeatFiles> resultList = query.list ();
3262         // If nothing comes back, there are no heat files
3263         if (resultList.isEmpty ()) {
3264             LOGGER.debug ("No heatFiles found for vnfResourceId=" + vnfResourceId);
3265             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No heat files", "CatalogDB", "getHeatFiles", null);
3266             return null;
3267         }
3268         // Now, we just need to return a HashMap (key=fileName, object=fileBody)
3269         heatFiles = new HashMap <> ();
3270         for (HeatFiles hf : resultList) {
3271             LOGGER.debug ("Adding " + hf.getFileName () + "->" + hf.getFileBody ());
3272             heatFiles.put (hf.getFileName (), hf);
3273         }
3274         // Make sure we're not returning an empty map - if so, just return null
3275         if (heatFiles.isEmpty ()) {
3276             LOGGER.debug ("heatFiles is empty - just return null");
3277             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Heat files is empty", "CatalogDB", "getHeatFiles", null);
3278             return null;
3279         }
3280         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFiles", null);
3281         return heatFiles;
3282     }
3283
3284     // New 1607 - with modularization, use new table to determine which HEAT_FILES entries to attach
3285     @Deprecated
3286     public Map <String, HeatFiles> getHeatFilesForVfModule(int vfModuleId) {
3287         /*
3288         Map <String, HeatFiles> heatFiles = null;
3289
3290         long startTime = System.currentTimeMillis ();
3291         LOGGER.debug ("Catalog database - getHeatFilesForVfModule called with vfModuleId " + vfModuleId);
3292         String hql = "FROM VfModuleToHeatFiles where vf_module_id = :vfModuleIdValue";
3293
3294         Query query = getSession ().createQuery (hql);
3295         query.setParameter ("vfModuleIdValue", vfModuleId);
3296
3297         List<VfModuleToHeatFiles> mapList = query.list();
3298         if (mapList.isEmpty()) {
3299             LOGGER.debug ("No heatFiles found for vfModuleId=" + vfModuleId);
3300             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No heatfiles found for vfModule", "CatalogDB", "getHeatFilesForVfModule", null);
3301             return null;
3302         }
3303         //Now the fun part - we have a list of the heat files we need to get - could clean this up with a join
3304         heatFiles = new HashMap<String, HeatFiles>();
3305         for (VfModuleToHeatFiles vmthf : mapList) {
3306                 int heatFilesId = vmthf.getHeatFilesId();
3307                 hql = "FROM HeatFiles where id = :id_value";
3308                 query = getSession().createQuery(hql);
3309                 query.setParameter("id_value", heatFilesId);
3310                 List<HeatFiles> fileList = query.list();
3311                 if (fileList.isEmpty()) {
3312                         // Should this throw an exception??
3313                         LOGGER.debug("Unable to find a HEAT_FILES entry at " + heatFilesId);
3314                 String errorString = "_ERROR|" + heatFilesId;
3315                         // The receiving code needs to know to throw an exception for this - or ignore it.
3316                         heatFiles.put(errorString, null);
3317                 } else {
3318                         // Should only ever have 1 result - add it to our Map
3319                         LOGGER.debug("Retrieved " + fileList.size() + " heat file entry at " + heatFilesId);
3320                         for (HeatFiles hf : fileList) {
3321                                 LOGGER.debug("Adding " + hf.getFileName() + "->" + hf.getFileBody());
3322                                 heatFiles.put(hf.getFileName(), hf);
3323                         }
3324                 }
3325         }
3326         if (heatFiles.isEmpty()) {
3327             LOGGER.debug ("heatFiles is empty - just return null");
3328             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. HeatFiles is empty", "CatalogDB", "getHeatFilesForVfModule", null);
3329             return null;
3330         }
3331         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFilesForVfModule", null);
3332         return heatFiles;
3333         */
3334         return null;
3335     }
3336     
3337     /**
3338      * Return a VfModuleToHeatFiles object 
3339      * 
3340      * @param vfModuleModelUuid, heatFilesArtifactUuid
3341      * @return VfModuleToHeatFiles or null if none found
3342      */ 
3343     public VfModuleToHeatFiles getVfModuleToHeatFilesEntry(String vfModuleModelUuid, String heatFilesArtifactUuid) {
3344
3345         LOGGER.debug ("Catalog database - getVfModuleToHeatFilesEntry with vfModuleModelUuid " + vfModuleModelUuid + ", heatFilesArtifactUuid=" + heatFilesArtifactUuid);
3346         String hql = "FROM VfModuleToHeatFiles where vfModuleModelUuid = :vfModuleModelUuidValue and heatFilesArtifactUuid = :heatFilesArtifactUuidValue";
3347         
3348         HashMap<String, String> parameters = new HashMap<>();
3349         parameters.put("vfModuleModelUuidValue", vfModuleModelUuid);
3350         parameters.put("heatFilesArtifactUuidValue", heatFilesArtifactUuid);
3351         
3352         VfModuleToHeatFiles vmthf = null;
3353         
3354         try {
3355                 vmthf = this.executeQuerySingleRow(hql, parameters, true);
3356         } catch (Exception e) {
3357                 throw e;
3358         }
3359         return vmthf;
3360     }
3361
3362     
3363     /**
3364      * Return a ServiceToResourceCustomization object 
3365      * 
3366      * @param serviceModelUuid
3367      * @param resourceModelCustomizationUuid
3368      * @param modelType
3369      * @return VfModuleToHeatFiles or null if none found
3370      */ 
3371     public ServiceToResourceCustomization getServiceToResourceCustomization(String serviceModelUuid, String resourceModelCustomizationUuid, String modelType) {
3372
3373         LOGGER.debug ("Catalog database - getServiceToResourceCustomization with serviceModelUuid=" + serviceModelUuid + ", resourceModelCustomizationUuid=" + resourceModelCustomizationUuid + ", modelType=" + modelType);
3374         String hql = "FROM ServiceToResourceCustomization where serviceModelUUID = :serviceModelUuidValue and resourceModelCustomizationUUID = :resourceModelCustomizationUuidValue and modelType = :modelTypeValue ";
3375         
3376         HashMap<String, String> parameters = new HashMap<>();
3377         parameters.put("serviceModelUuidValue", serviceModelUuid);
3378         parameters.put("resourceModelCustomizationUuidValue", resourceModelCustomizationUuid);
3379         parameters.put("modelTypeValue", modelType);
3380         
3381         ServiceToResourceCustomization strc = null;
3382         
3383         try {
3384                 strc = this.executeQuerySingleRow(hql, parameters, true);
3385         } catch (Exception e) {
3386                 throw e;
3387         }
3388         return strc;
3389     }
3390
3391     /**
3392      * Return a Map<String, HeatFiles> for returning the heat files associated with a vfModule 1707
3393      * 
3394      * @param vfModuleModelUuid
3395      * @return Map<String,Object> or null if none found
3396      */ 
3397     public Map <String, HeatFiles> getHeatFilesForVfModule(String vfModuleModelUuid) {
3398         Map <String, HeatFiles> heatFiles;
3399
3400         long startTime = System.currentTimeMillis ();
3401         LOGGER.debug ("Catalog database - getHeatFilesForVfModule called with vfModuleModelUuid " + vfModuleModelUuid);
3402         String hql = "FROM VfModuleToHeatFiles where vfModuleModelUuid = :vfModuleModelUuidValue";
3403
3404         Query query = getSession ().createQuery (hql);
3405         query.setParameter ("vfModuleModelUuidValue", vfModuleModelUuid);
3406        
3407         @SuppressWarnings("unchecked")
3408         List<VfModuleToHeatFiles> mapList = query.list();
3409         if (mapList.isEmpty()) {
3410             LOGGER.debug ("No heatFiles found for vfModuleModelUuid=" + vfModuleModelUuid);
3411             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No heatfiles found for vfModule", "CatalogDB", "getHeatFilesForVfModule", null);
3412             return null;
3413         }
3414         //Now the fun part - we have a list of the heat files we need to get - could clean this up with a join
3415         heatFiles = new HashMap<>();
3416         for (VfModuleToHeatFiles vmthf : mapList) {
3417                 String heatFilesUuid = vmthf.getHeatFilesArtifactUuid();
3418                 hql = "FROM HeatFiles where artifactUuid = :heatFilesUuidValue";
3419                 query = getSession().createQuery(hql);
3420                 query.setParameter("heatFilesUuidValue", heatFilesUuid);
3421                 @SuppressWarnings("unchecked")
3422                 List<HeatFiles> fileList = query.list();
3423                 if (fileList.isEmpty()) {
3424                         // Should this throw an exception??
3425                         LOGGER.debug("Unable to find a HEAT_FILES entry at " + heatFilesUuid);
3426                 String errorString = "_ERROR|" + heatFilesUuid;
3427                         // The receiving code needs to know to throw an exception for this - or ignore it.
3428                         heatFiles.put(errorString, null);
3429                 } else {
3430                         // Should only ever have 1 result - add it to our Map
3431                         LOGGER.debug("Retrieved " + fileList.size() + " heat file entry at " + heatFilesUuid);
3432                         for (HeatFiles hf : fileList) {
3433                                 LOGGER.debug("Adding " + hf.getFileName() + "->" + hf.getFileBody());
3434                                 heatFiles.put(hf.getFileName(), hf);
3435                         }
3436                 }
3437         }
3438         if (heatFiles.isEmpty()) {
3439             LOGGER.debug ("heatFiles is empty - just return null");
3440             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. HeatFiles is empty", "CatalogDB", "getHeatFilesForVfModule", null);
3441             return null;
3442         }
3443         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFilesForVfModule", null);
3444         return heatFiles;       
3445     }
3446
3447     /**
3448      * Get the heat template object based on asdc attributes
3449      *
3450      * @param templateName The template name, generally the yaml filename. "example.yaml"
3451      * @param version The version as specified by ASDC. "1.1"
3452      * @param asdcResourceName The ASDC resource name provided in the ASDC artifact
3453      *
3454      * @return The HeatTemplate
3455      */
3456     @Deprecated // asdcResourceName is no longer in heatTeamplate
3457     public HeatTemplate getHeatTemplate (String templateName, String version, String asdcResourceName) {
3458
3459         long startTime = System.currentTimeMillis ();
3460         LOGGER.debug ("Catalog database - getHeatTemplate with name " + templateName
3461                                       + " and version "
3462                                       + version
3463                                       + " and ASDC resource name "
3464                                       + asdcResourceName);
3465
3466         String hql = "FROM HeatTemplate WHERE templateName = :template_name AND version = :version AND asdcResourceName = :asdcResourceName";
3467         Query query = getSession ().createQuery (hql);
3468         query.setParameter ("template_name", templateName);
3469         query.setParameter ("version", version);
3470         query.setParameter ("asdcResourceName", asdcResourceName);
3471
3472         @SuppressWarnings("unchecked")
3473         List <HeatTemplate> resultList = query.list ();
3474
3475         // See if something came back.
3476         if (resultList.isEmpty ()) {
3477             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Heat template not found", "CatalogDB", "getHeatTemplate", null);
3478             return null;
3479         }
3480         // Name + Version is unique, so should only be one element
3481         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
3482         return resultList.get (0);
3483     }
3484
3485
3486     /**
3487      * Save the Heat Template
3488      *
3489      * @param heat The heat template
3490      * @param paramSet The list of heat template parameters
3491      */
3492     public void saveHeatTemplate (HeatTemplate heat, Set <HeatTemplateParam> paramSet) {
3493
3494         long startTime = System.currentTimeMillis ();
3495         LOGGER.debug ("Catalog database - save Heat Template with name " + heat.getTemplateName() + ", artifactUUID=" + heat.getArtifactUuid());
3496
3497         heat.setParameters(null);
3498         try {
3499             
3500             HeatTemplate heatTemp = this.getHeatTemplateByArtifactUuidRegularQuery(heat.getArtifactUuid());
3501             
3502             if (heatTemp == null) {
3503                 this.getSession ().save (heat);
3504
3505                 if (paramSet != null) {
3506                         StringBuilder sb = new StringBuilder("Parameters: ");
3507                     for (HeatTemplateParam param : paramSet) {
3508                         param.setHeatTemplateArtifactUuid(heat.getArtifactUuid());
3509                         sb.append(param.getParamName()).append(", ");
3510                     }
3511                     LOGGER.debug(sb.toString());
3512                     heat.setParameters (paramSet);
3513                     try {
3514                         Session session = this.getSession();
3515                         if (!(session.isConnected() && session.isOpen())) {
3516                                 LOGGER.debug("Initial session is not connected or open - get another");
3517                                 session = this.getSession();
3518                         }
3519                         session.save(heat);
3520                     } catch (HibernateException he1) {
3521                         LOGGER.debug("Hibernate Exception encountered on first attempt at save(heat) - try again..." + he1.getMessage(), he1);
3522                         try {
3523                                 Session session = this.getSession();
3524                                 session.save(heat);
3525                         } catch (HibernateException he2) {
3526                                 LOGGER.debug("Hibernate Exception encountered on second attempt at save(heat)" + he2.getMessage());
3527                                 LOGGER.debug(Arrays.toString(he2.getStackTrace()));
3528                                 throw he2;
3529                         } catch (Exception e2) {
3530                                 LOGGER.debug("General Exception encountered on second attempt at save(heat)..." + e2.getMessage(),e2);
3531                                 LOGGER.debug(Arrays.toString(e2.getStackTrace()));
3532                                 throw e2;
3533                         }
3534                         
3535                     } catch (Exception e1) {
3536                         LOGGER.debug("General Exception encountered on first attempt at save(heat) - try again..." + e1.getMessage(), e1);
3537                         LOGGER.debug(Arrays.toString(e1.getStackTrace()));
3538                         try {
3539                                 Session session = this.getSession();
3540                                 session.save(heat);
3541                         } catch (HibernateException he2) {
3542                                 LOGGER.debug("General Exception encountered on second attempt at save(heat)" + he2.getMessage(), he2);
3543                                 LOGGER.debug(Arrays.toString(he2.getStackTrace()));
3544                                 throw he2;
3545                         } catch (Exception e2) {
3546                                 LOGGER.debug("General Exception encountered on second attempt at save(heat)..." + e2.getMessage(), e2);
3547                                 LOGGER.debug(Arrays.toString(e2.getStackTrace()));
3548                                 throw e2;
3549                         }
3550                     }
3551                 }
3552
3553             } else {
3554                 heat.setArtifactUuid(heatTemp.getArtifactUuid());
3555             }
3556         } finally {
3557             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatTemplate", null);
3558         }
3559     }
3560
3561     /**
3562      * Retrieves a Heat environment from DB based on its unique key.
3563      *
3564      * @param name the environment artifact name
3565      * @param version the environment resource version
3566      * @param asdcResourceName the environment resource name
3567      * @return the heat environment from DB or null if not found
3568      */
3569     @Deprecated
3570     public HeatEnvironment getHeatEnvironment (String name, String version, String asdcResourceName) {
3571         long startTime = System.currentTimeMillis ();
3572         LOGGER.debug ("Catalog database - get Heat environment with name " + name
3573                                       + " and version "
3574                                       + version
3575                                       + " and ASDC resource name "
3576                                       + asdcResourceName);
3577
3578         String hql = "FROM HeatEnvironment WHERE name=:name AND version=:version AND asdcResourceName=:asdcResourceName";
3579         Query query = getSession ().createQuery (hql);
3580         query.setParameter ("name", name);
3581         query.setParameter ("version", version);
3582         query.setParameter ("asdcResourceName", asdcResourceName);
3583         HeatEnvironment env = null;
3584         try {
3585                 env = (HeatEnvironment) query.uniqueResult ();
3586         } catch (org.hibernate.NonUniqueResultException nure) {
3587                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: envName='" + name + "', version='" + version + "' and asdcResourceName=" + asdcResourceName, nure);
3588                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for envName=" + name + " and version=" + version + " and asdcResourceName=" + asdcResourceName, "", "", MsoLogger.ErrorCode.DataError, "non unique result for envName=" + name);
3589                 env = null;
3590         } catch (org.hibernate.HibernateException he) {
3591                 LOGGER.debug("Hibernate Exception - while searching for: envName='" + name + "', asdc_service_model_version='" + version + "' and asdcResourceName=" + asdcResourceName, he);
3592                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for envName=" + name + " and version=" + version + " and asdcResourceName=" + asdcResourceName, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for envName=" + name);
3593                 env = null;
3594         } catch (Exception e) {
3595                 LOGGER.debug("Generic Exception - while searching for: envName='" + name + "', asdc_service_model_version='" + version + "' and asdcResourceName=" + asdcResourceName, e);
3596                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for envName=" + name + " and version=" + version + " and asdcResourceName=" + asdcResourceName, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for envName=" + name);
3597                 env = null;
3598         }
3599         if (env == null) {
3600                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getHeatTemplate", null);
3601         } else {
3602                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
3603         }
3604         return env;
3605     }
3606
3607     /**
3608      * Retrieves a Heat environment from DB based on its unique key. 1707
3609      *
3610      * @param artifactUuid the environment artifact name
3611      * @param version the environment resource version
3612      * @return the heat environment from DB or null if not found
3613      */
3614     public HeatEnvironment getHeatEnvironment (String artifactUuid, String version) {
3615         long startTime = System.currentTimeMillis ();
3616         LOGGER.debug ("Catalog database - get Heat environment with uuid " + artifactUuid
3617                                       + " and version "
3618                                       + version);
3619
3620         String hql = "FROM HeatEnvironment WHERE artifactUuid=:artifactUuid AND version=:version";
3621         Query query = getSession ().createQuery (hql);
3622         query.setParameter ("artifactUuid", artifactUuid);
3623         query.setParameter ("version", version);
3624         HeatEnvironment env = null;
3625         try {
3626                 env = (HeatEnvironment) query.uniqueResult ();
3627         } catch (org.hibernate.NonUniqueResultException nure) {
3628                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: envName='" + artifactUuid + "', version='" + version);
3629                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for artifactUUID=" + artifactUuid + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "non unique result for ArtifactUUID=" + artifactUuid);
3630                 throw nure;
3631         } catch (org.hibernate.HibernateException he) {
3632                 LOGGER.debug("Hibernate Exception - while searching for: artifactUUID='" + artifactUuid + "', asdc_service_model_version='" + version + " " + he.getMessage() );
3633                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for artifactUUID=" + artifactUuid + " and version=" + version , "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for artifactUUID=" + artifactUuid);
3634                 throw he;
3635         } catch (Exception e) {
3636                 LOGGER.debug("Generic Exception - while searching for: artifactUUID='" + artifactUuid + "', asdc_service_model_version='" + version  + " " + e.getMessage());
3637                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for artifactUUID=" + artifactUuid + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for artifactUUID=" + artifactUuid);
3638                 throw e;
3639         }
3640         if (env == null) {
3641                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getHeatTemplate", null);
3642         } else {
3643                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
3644         }
3645         return env;
3646     }
3647
3648     /**
3649      * Save the HeatEnvironment
3650      *
3651      * @param env The Environment
3652      */
3653     public void saveHeatEnvironment (HeatEnvironment env) {
3654         long startTime = System.currentTimeMillis ();
3655         LOGGER.debug ("Catalog database - save Heat environment with name "
3656                                       + env.getEnvironment() + " and ArtifactUUID " + env.getArtifactUuid());
3657         try {
3658             HeatEnvironment dbEnv = getHeatEnvironment (env.getArtifactUuid(), env.getVersion ());
3659             if (dbEnv == null) {
3660
3661                 this.getSession ().save (env);
3662
3663             } else {
3664                 env.setArtifactUuid(dbEnv.getArtifactUuid());
3665             }
3666
3667         } finally {
3668             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatTemplate", null);
3669         }
3670     }
3671
3672     /**
3673      * Save the heatTemplate
3674      *
3675      * @param heat The heat template
3676      */
3677     public void saveHeatTemplate (HeatTemplate heat) {
3678         long startTime = System.currentTimeMillis ();
3679         LOGGER.debug ("Catalog database - save Heat template with name " + heat.getTemplateName ());
3680         try {
3681             this.getSession ().update (heat);
3682         } finally {
3683             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatTemplate", null);
3684         }
3685     }
3686
3687     public void saveHeatFile (HeatFiles heatFile) {
3688         long startTime = System.currentTimeMillis ();
3689         LOGGER.debug ("Catalog database - save Heat file with name " + heatFile.getFileName ());
3690         try {
3691             this.getSession ().save (heatFile);
3692         } finally {
3693             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatFile", null);
3694         }
3695     }
3696
3697     public void saveVnfRecipe (VnfRecipe vnfRecipe) {
3698         long startTime = System.currentTimeMillis ();
3699         LOGGER.debug ("Catalog database - save VNF recipe with VNF type " + vnfRecipe.getVnfType ());
3700         try {
3701             this.getSession ().save (vnfRecipe);
3702         } finally {
3703             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVnfRecipe", null);
3704         }
3705     }
3706
3707     public void saveVnfComponentsRecipe (VnfComponentsRecipe vnfComponentsRecipe) {
3708         long startTime = System.currentTimeMillis ();
3709         LOGGER.debug ("Catalog database - save VNF Component recipe with VNF type " + vnfComponentsRecipe.getVnfType ());
3710         try {
3711             this.getSession ().save (vnfComponentsRecipe);
3712         } finally {
3713             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVnfComponentsRecipe", null);
3714         }
3715     }
3716
3717
3718     public void saveOrUpdateVnfResource (VnfResource vnfResource) {
3719         long startTime = System.currentTimeMillis ();
3720         LOGGER.debug ("Catalog database - save VNF Resource with VNF type " + vnfResource.getModelName());
3721         try {
3722
3723                 VnfResource existing = this.getVnfResourceByModelUuid(vnfResource.getModelUuid());
3724                 if (existing == null) {
3725                         LOGGER.debug("No existing entry found - attempting to save...");
3726                 this.getSession ().save (vnfResource);
3727                 } else {
3728                         LOGGER.debug("Existing vnf resource found!");
3729             }
3730
3731         } finally {
3732             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateVnfResource", null);
3733         }
3734     }
3735
3736     public boolean saveVnfResourceCustomization (VnfResourceCustomization vnfResourceCustomization) {
3737         long startTime = System.currentTimeMillis ();
3738         LOGGER.debug ("Catalog database - save VNF Resource Customization with Name " + vnfResourceCustomization.getModelInstanceName());
3739         try {
3740                 LOGGER.debug(vnfResourceCustomization.toString());
3741         } catch (Exception e) {
3742                 LOGGER.debug("Unable to print VRC " + e.getMessage(), e);
3743         }
3744         try {
3745                          // Check if NetworkResourceCustomzation record already exists.  If so, skip saving it.
3746                         // List<NetworkResource> networkResourceList = getAllNetworksByNetworkModelCustomizationUuid(networkResourceCustomization.getModelCustomizationUuid());
3747                          // Do any matching customization records exist?
3748                         // if(networkResourceList.size() == 0){
3749                                                  
3750                                 // networkResourceCustomization.setNetworkResourceModelUuid(networkResource.getModelUuid());
3751         //      this.getSession().flush();
3752         //      this.getSession().clear();
3753                 
3754                 VnfResourceCustomization existing = this.getVnfResourceCustomizationByModelCustomizationUuid(vnfResourceCustomization.getModelCustomizationUuid());
3755                 
3756                 if (existing == null) {
3757                         LOGGER.debug("No existing entry found...attempting to save...");
3758                         this.getSession ().save (vnfResourceCustomization);
3759                         return true;
3760                 }else {
3761                         try {
3762                                 LOGGER.debug("Existing VRC entry found\n" + existing.toString());
3763                         } catch (Exception e) {
3764                                 LOGGER.debug("Unable to print VRC2 " + e.getMessage(), e);
3765                         }
3766                         return false;
3767                 }
3768                                                  
3769         } finally {
3770             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVnfResourceCustomization", null);
3771         }
3772     }
3773     
3774     public void saveAllottedResourceCustomization (AllottedResourceCustomization resourceCustomization) {
3775         long startTime = System.currentTimeMillis ();
3776         LOGGER.debug ("Catalog database - save Allotted Resource with Name " + resourceCustomization.getModelInstanceName());
3777         try {
3778             List<AllottedResourceCustomization> allottedResourcesList = getAllAllottedResourcesByArModelCustomizationUuid(resourceCustomization.getModelCustomizationUuid());
3779
3780             if(allottedResourcesList.isEmpty()){
3781                 this.getSession ().save(resourceCustomization);
3782             }
3783
3784         } finally {
3785             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateAllottedResourceCustomization", null);
3786         }
3787     }
3788
3789     public void saveAllottedResource (AllottedResource allottedResource) {
3790         long startTime = System.currentTimeMillis ();
3791         LOGGER.debug ("Catalog database - save Allotted Resource with Name " + allottedResource.getModelName());
3792         try { 
3793                 AllottedResource existing = this.getAllottedResourceByModelUuid(allottedResource.getModelUuid());
3794                 if (existing == null) {
3795                         this.getSession ().save (allottedResource);
3796                 } else {
3797                         LOGGER.debug("Found existing allottedResource with this modelUuid - no need to save");
3798                 }
3799          
3800         } finally {
3801             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateAllottedResourceCustomization", null);
3802         }
3803     }
3804     
3805     public void saveNetworkResource (NetworkResource networkResource) throws RecordNotFoundException {
3806         long startTime = System.currentTimeMillis ();
3807         LOGGER.debug ("Catalog database - save Network Resource with Network Name " + networkResource.getModelName());
3808         try {
3809                          // Check if NetworkResourceCustomzation record already exists.  If so, skip saving it.
3810                         // List<NetworkResource> networkResourceList = getAllNetworksByNetworkModelCustomizationUuid(networkResourceCustomization.getModelCustomizationUuid());
3811                          // Do any matching customization records exist?
3812                         if(getNetworkResourceByModelUuid(networkResource.getModelUUID()) == null){
3813                                  this.getSession ().save(networkResource);
3814                         }
3815   
3816         
3817         } finally {
3818             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNetworkResourceCustomization", null);
3819         }
3820     }
3821     
3822     public void saveToscaCsar (ToscaCsar toscaCsar) throws RecordNotFoundException {
3823         
3824
3825         long startTime = System.currentTimeMillis ();
3826         LOGGER.debug ("Catalog database - save Tosca Csar with Name " + toscaCsar.getName());
3827         try {
3828                 
3829                 if(getToscaCsar(toscaCsar.getArtifactChecksum()) == null){
3830                         this.getSession ().save (toscaCsar);
3831                 }
3832                 LOGGER.debug("Temporarily disabling saveToscaCsar pending further investigation 2017-06-02");
3833         
3834         } finally {
3835             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveToscaCsar", null);
3836         }
3837     }
3838     
3839
3840     /**
3841      * Return the newest version of a specific Tosca CSAR Record resource (queried by Name).
3842      *
3843      * @param artifactChecksum
3844      * @return ToscaCsar object or null if none found
3845      */
3846     public ToscaCsar getToscaCsar (String artifactChecksum) {
3847
3848         long startTime = System.currentTimeMillis ();
3849         LOGGER.debug ("Catalog database - get Tosca CSAR record with artifactChecksum " + artifactChecksum);
3850
3851         String hql = "FROM ToscaCsar WHERE artifactChecksum = :artifactChecksum";
3852         Query query = getSession ().createQuery (hql);
3853         query.setParameter ("artifactChecksum", artifactChecksum);
3854
3855         @SuppressWarnings("unchecked")
3856         List <ToscaCsar> resultList = query.list ();
3857
3858         // See if something came back. Name is unique, so
3859         if (resultList.isEmpty ()) {
3860             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Tosca Csar not found", "CatalogDB", "getToscaCsar", null);
3861             return null;
3862         }
3863
3864         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getToscaCsar", null);
3865         return resultList.get (0);
3866     }
3867     
3868     /**
3869      * Return a specific Tosca CSAR Record resource (queried by atrifact uuid).
3870      *
3871      * @param toscaCsarArtifactUUID the artifact uuid of the tosca csar
3872      * @return ToscaCsar object or null if none found
3873      */
3874     public ToscaCsar getToscaCsarByUUID(String toscaCsarArtifactUUID){
3875         long startTime = System.currentTimeMillis ();
3876         LOGGER.debug ("Catalog database - get Tosca CSAR record with artifactUUID " + toscaCsarArtifactUUID);
3877
3878         String hql = "FROM ToscaCsar WHERE artifactUUID = :toscaCsarArtifactUUID";
3879         Query query = getSession ().createQuery (hql);
3880         query.setParameter ("toscaCsarArtifactUUID", toscaCsarArtifactUUID);
3881
3882         @SuppressWarnings("unchecked")
3883         List <ToscaCsar> resultList = query.list ();
3884
3885         // See if something came back. Name is unique, so
3886         if (resultList.isEmpty ()) {
3887             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Tosca Csar not found", "CatalogDB", "getToscaCsarByUUID", null);
3888             return null;
3889         }
3890
3891         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getToscaCsarByUUID", null);
3892         return resultList.get (0);
3893     }
3894
3895     /**
3896      * Return a specific Tosca CSAR Record resource (queried by service model uuid).
3897      * <br>
3898      * 
3899      * @param serviceModelUUID the service model uuid
3900      * @return ToscaCsar object or null if none found
3901      * @since ONAP Beijing Release
3902      */
3903     public ToscaCsar getToscaCsarByServiceModelUUID(String serviceModelUUID){
3904         long startTime = System.currentTimeMillis ();
3905         LOGGER.debug ("Catalog database - get Tosca CSAR record with serviceModelUUID " + serviceModelUUID);
3906         Service service = getServiceByModelUUID(serviceModelUUID);
3907         if(null == service){
3908             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Service not found", "CatalogDB", "getToscaCsarByServiceModelUUID", null);
3909             return null;
3910         }
3911         ToscaCsar csar = getToscaCsarByUUID(service.getToscaCsarArtifactUUID());
3912         if(null == csar){
3913             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Tosca csar of the service not found", "CatalogDB", "getToscaCsarByServiceModelUUID", null);
3914             return null;
3915         }
3916         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getToscaCsarByServiceModelUUID", null);
3917         return csar;
3918     }
3919     
3920     public void saveTempNetworkHeatTemplateLookup (TempNetworkHeatTemplateLookup tempNetworkHeatTemplateLookup) {
3921         long startTime = System.currentTimeMillis ();
3922         LOGGER.debug ("Catalog database - save TempNetworkHeatTemplateLookup with Network Model Name " + tempNetworkHeatTemplateLookup.getNetworkResourceModelName() +
3923                               " and Heat Template Artifact UUID " + tempNetworkHeatTemplateLookup.getHeatTemplateArtifactUuid());
3924         try {
3925                  this.getSession ().save (tempNetworkHeatTemplateLookup);
3926       
3927         } finally {
3928             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveTempNetworkHeatTemplateLookup", null); 
3929         }
3930     }
3931     
3932     public void saveVfModuleToHeatFiles (VfModuleToHeatFiles vfModuleToHeatFiles) {
3933         long startTime = System.currentTimeMillis ();
3934         LOGGER.debug ("Catalog database - save VfModuleToHeatFiles with VF Module UUID " + vfModuleToHeatFiles.getVfModuleModelUuid() +
3935                               " and Heat Files Artifact UUID " + vfModuleToHeatFiles.getHeatFilesArtifactUuid());
3936         try {
3937                 
3938                 this.getSession ().save (vfModuleToHeatFiles);
3939       
3940         } finally {
3941             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVFModuleToHeatFiles", null); 
3942         }
3943     }
3944     
3945     public void saveVnfResourceToVfModuleCustomization(VnfResourceCustomization vnfResourceCustomizationUUID, VfModuleCustomization vfModuleCustomizationUUID) throws RecordNotFoundException {
3946         long startTime = System.currentTimeMillis ();
3947         VnfResCustomToVfModuleCustom vnfResCustomToVfModuleCustom = new VnfResCustomToVfModuleCustom();
3948         
3949         if(vnfResourceCustomizationUUID != null && vfModuleCustomizationUUID != null){
3950                 vnfResCustomToVfModuleCustom.setVnfResourceCustModelCustomizationUuid(vnfResourceCustomizationUUID.getModelCustomizationUuid());
3951                 vnfResCustomToVfModuleCustom.setVfModuleCustModelCustomizationUuid(vfModuleCustomizationUUID.getModelCustomizationUuid());
3952                 String vnfId = vnfResourceCustomizationUUID.getModelCustomizationUuid();
3953                 String vfId = vfModuleCustomizationUUID.getModelCustomizationUuid();
3954                 LOGGER.debug ("Catalog database - save VnfResCustomToVfModuleCustom with vnf=" + vnfId + ", vf=" + vfId);
3955                 try {
3956                         VnfResCustomToVfModuleCustom existing = this.getVnfResCustomToVfModule(vnfId, vfId);
3957                         if (existing == null) {
3958                                 LOGGER.debug("No existing entry found - will now try to save");
3959                                 this.getSession ().save (vnfResCustomToVfModuleCustom);
3960                         } else {
3961                                 LOGGER.debug("Existing entry already found - no save needed");
3962                         }
3963                 } finally {
3964                         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVnfResourceToVfModuleCustomization", null);
3965                 }
3966         }
3967     }
3968     
3969     public void saveNetworkResourceCustomization (NetworkResourceCustomization networkResourceCustomization) throws RecordNotFoundException {
3970         long startTime = System.currentTimeMillis ();
3971         LOGGER.debug ("Catalog database - save Network Resource Customization with Network Name " + networkResourceCustomization.getModelInstanceName());
3972         try {
3973             // Check if NetworkResourceCustomzation record already exists.  If so, skip saving it.
3974             List<NetworkResourceCustomization> networkResourceCustomizationList = getAllNetworksByNetworkModelCustomizationUuid(networkResourceCustomization.getModelCustomizationUuid());
3975             // Do any matching customization records exist?
3976             if(networkResourceCustomizationList.isEmpty()){
3977
3978                 // Retreive the record from the Network_Resource table associated to the Customization record based on ModelName
3979                         // ?? is it modelInstanceName with 1707?
3980                         NetworkResource networkResource = getNetworkResource(networkResourceCustomization.getModelInstanceName());
3981
3982                 if(networkResource == null){
3983                                 throw new RecordNotFoundException("No record found in NETWORK_RESOURCE table for model name " + networkResourceCustomization.getModelInstanceName());
3984                 }
3985
3986                         networkResourceCustomization.setNetworkResourceModelUuid(networkResource.getModelUUID());
3987
3988                 this.getSession ().save(networkResourceCustomization);
3989             }
3990
3991
3992         } finally {
3993             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNetworkResourceCustomization", null);
3994         }
3995     }
3996
3997     @Deprecated  // table is gone - mapped to ServiceToResource
3998     public void saveServiceToNetworks (ServiceToNetworks serviceToNetworks) {
3999         long startTime = System.currentTimeMillis ();
4000         LOGGER.debug ("Catalog database - save to ServiceToNetworks table with NetworkModelCustomizationUUID of " + serviceToNetworks.getNetworkModelCustomizationUuid() + " and ServiceModelUUID of " + serviceToNetworks.getServiceModelUuid());
4001         try {
4002             this.getSession ().save(serviceToNetworks);
4003
4004         } finally {
4005             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNetworkResourceCustomization", null);
4006         }
4007     }
4008
4009     public void saveServiceToResourceCustomization(ServiceToResourceCustomization serviceToResource) {
4010         long startTime = System.currentTimeMillis ();
4011         LOGGER.debug ("Catalog database - save to ServiceToResourceCustomization table with ServiceModelUuid of " + serviceToResource.getServiceModelUUID() + ", ResourceModelUUID of " + serviceToResource.getResourceModelCustomizationUUID() + " and model_type=" + serviceToResource.getModelType());
4012         ServiceToResourceCustomization strc = this.getServiceToResourceCustomization(serviceToResource.getServiceModelUUID(), serviceToResource.getResourceModelCustomizationUUID(), serviceToResource.getModelType());
4013         try {
4014                 if (strc != null) {
4015                         LOGGER.debug("**This ServiceToResourceCustomization record already exists - no need to save");
4016                 } else {
4017                  this.getSession ().save(serviceToResource);
4018                 }
4019         } finally {
4020             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveServiceToResourceCustomization", null);
4021         }
4022     }
4023     
4024     @Deprecated // table is gone - mapped to ServiceToResourceCustomization
4025     public void saveServiceToAllottedResources (ServiceToAllottedResources serviceToAllottedResources) {
4026         long startTime = System.currentTimeMillis ();
4027         LOGGER.debug ("Catalog database - save to serviceToAllottedResources table with ARModelCustomizationUUID of " + serviceToAllottedResources.getArModelCustomizationUuid() + " and ServiceModelUUID of " + serviceToAllottedResources.getServiceModelUuid());
4028         try {
4029             this.getSession ().save(serviceToAllottedResources);
4030
4031         } finally {
4032             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveServiceToAllottedResources", null);
4033         }
4034     }
4035
4036     public void saveService (Service service) {
4037         long startTime = System.currentTimeMillis ();
4038         LOGGER.debug ("Catalog database - save Service with ServiceName/Version/serviceUUID(SERVICE_NAME_VERSION_ID)" + service.getModelName()+"/"+service.getVersion()+"/"+service.getModelUUID());
4039         try {
4040             Service serviceInvariantDB = null;
4041             // Retrieve existing service record by nameVersionId
4042                 Service serviceDB = this.getServiceByModelUUID(service.getModelUUID());
4043             if (serviceDB == null) {
4044                 // Check to see if a record with the same modelInvariantId already exists.  This tells us that a previous version exists and we can copy its recipe Record for the new service record.
4045                 serviceInvariantDB = this.getServiceByInvariantUUID(service.getModelInvariantUUID());
4046                 // Save the new Service record
4047                 this.getSession ().save (service);
4048             }
4049
4050             if(serviceInvariantDB != null){  // existing modelInvariantId was found.
4051                 // copy the recipe record with the matching invariant id.  We will duplicate this for the new service record
4052                 List<ServiceRecipe> serviceRecipes = getServiceRecipes(serviceInvariantDB.getModelUUID());
4053
4054                 if(serviceRecipes != null && ! serviceRecipes.isEmpty()){
4055                     for(ServiceRecipe serviceRecipe : serviceRecipes){
4056                         if(serviceRecipe != null){
4057                             // Fetch the service record that we just added.  We do this so we can extract its Id column value, this will be the foreign key we use in the service recipe table.
4058                                         Service newService = this.getServiceByModelUUID(service.getModelUUID());
4059                             // Create a new ServiceRecipe record based on the existing one we just copied from the DB.
4060                             ServiceRecipe newServiceRecipe = new ServiceRecipe();
4061                             newServiceRecipe.setAction(serviceRecipe.getAction());
4062                             newServiceRecipe.setDescription(serviceRecipe.getDescription());
4063                             newServiceRecipe.setOrchestrationUri(serviceRecipe.getOrchestrationUri());
4064                             newServiceRecipe.setRecipeTimeout(serviceRecipe.getRecipeTimeout());
4065                             newServiceRecipe.setServiceParamXSD(serviceRecipe.getServiceParamXSD());
4066                                         newServiceRecipe.setServiceModelUUID(newService.getModelUUID());
4067                             newServiceRecipe.setVersion(serviceRecipe.getVersion());
4068                                         // Check recipe does not exist before inserting
4069                                         ServiceRecipe recipe = getServiceRecipeByModelUUID(newServiceRecipe.getServiceModelUUID(), newServiceRecipe.getAction());
4070                             // Save the new recipe record in the service_recipe table and associate it to the new service record that we just added.
4071                                         if(recipe == null){
4072                             this.getSession ().save (newServiceRecipe);
4073                         }
4074                     }
4075                 }
4076               }
4077             }
4078
4079                
4080         } finally {
4081             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateService", null);
4082         }
4083     }
4084
4085     public void saveOrUpdateVfModule (VfModule vfModule) {
4086         long startTime = System.currentTimeMillis ();
4087         LOGGER.debug ("Catalog database - save or update VF Module with VF Model Name " + vfModule.getModelName());
4088         VfModule vfModuleInvariantDB = null;
4089         try {
4090                 LOGGER.debug("heat template id = " + vfModule.getHeatTemplateArtifactUUId() + ", vol template id = "+ vfModule.getVolHeatTemplateArtifactUUId());
4091                 LOGGER.debug(vfModule.toString());
4092         } catch (Exception e) {
4093                 LOGGER.debug("unable to print vfmodule " + e.getMessage(), e);
4094         }
4095         try {
4096                 VfModule existing = this.getVfModuleByModelUUID(vfModule.getModelUUID());
4097                 if (existing == null) {
4098                         // Check to see if a record with the same modelInvariantId already exists.  This tells us that a previous version exists and we can copy its recipe Record for the new service record.
4099                         vfModuleInvariantDB = this.getVfModuleByModelInvariantUuid(vfModule.getModelInvariantUUID());
4100                         LOGGER.debug("No existing entry found, attempting to save...");
4101                 this.getSession ().save (vfModule);
4102                 } else {
4103                         try {
4104                                 LOGGER.debug("Found an existing vf module!\n" + existing.toString());
4105                         } catch (Exception e) {
4106                                 LOGGER.debug("unable to print vfmodule2 " + e.getMessage(), e);
4107             }
4108                 }
4109                 
4110             if(vfModuleInvariantDB != null){  // existing modelInvariantId was found.
4111                 // copy the recipe record with the matching invariant id.  We will duplicate this for the new service record                    
4112                 List<VnfComponentsRecipe> vfRecipes = getVnfComponentRecipes(vfModuleInvariantDB.getModelUUID());
4113
4114                 
4115                 if(vfRecipes != null && ! vfRecipes.isEmpty()){
4116                         for(VnfComponentsRecipe vfRecipe : vfRecipes){
4117                                 if(vfRecipe != null){
4118                                         // Fetch the service record that we just added.  We do this so we can extract its Id column value, this will be the foreign key we use in the service recipe table.
4119                                         VfModule newRecipe = this.getVfModuleByModelUUID(vfModule.getModelUUID());
4120                                         // Create a new ServiceRecipe record based on the existing one we just copied from the DB.
4121                                         VnfComponentsRecipe newVnfRecipe = new VnfComponentsRecipe();
4122                                         newVnfRecipe.setAction(vfRecipe.getAction());
4123                                         newVnfRecipe.setDescription(vfRecipe.getDescription());
4124                                         newVnfRecipe.setOrchestrationUri(vfRecipe.getOrchestrationUri());
4125                                         newVnfRecipe.setRecipeTimeout(vfRecipe.getRecipeTimeout());
4126                                         newVnfRecipe.setParamXSD(vfRecipe.getParamXSD());
4127                                         newVnfRecipe.setVfModuleModelUUId(newRecipe.getModelUUID());
4128                                         newVnfRecipe.setVersion(vfRecipe.getVersion());
4129                                         newVnfRecipe.setVnfComponentType(vfRecipe.getVnfComponentType());
4130                                         newVnfRecipe.setVnfType(vfRecipe.getVnfType());
4131                                         // Check recipe does not exist before inserting
4132          //                             VnfComponentsRecipe recipe = getVnfComponentRecipes(newVnfRecipe.getVfModuleModelUUId());
4133                                         List<VnfComponentsRecipe> recipe = getVnfComponentRecipes(newVnfRecipe.getVfModuleModelUUId());
4134                                         // Save the new recipe record in the service_recipe table and associate it to the new service record that we just added.
4135         //                              if(recipe == null){
4136                                                 this.getSession ().save (newVnfRecipe);
4137         //                              }
4138                                 }
4139                         }
4140                 }
4141   
4142              }
4143
4144         } finally {
4145             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateVfModule", null);
4146         }
4147     }
4148
4149     public void saveOrUpdateVfModuleCustomization (VfModuleCustomization vfModuleCustomization) {
4150         long startTime = System.currentTimeMillis ();
4151         LOGGER.debug ("Catalog database - save VF Module Customization with VF Customization Model Name UUID " + vfModuleCustomization.getVfModuleModelUuid());
4152         try {
4153                 LOGGER.debug("env id = " + vfModuleCustomization.getHeatEnvironmentArtifactUuid() + ", vol Env=" + vfModuleCustomization.getVolEnvironmentArtifactUuid());
4154                 LOGGER.debug(vfModuleCustomization.toString());
4155         } catch (Exception e) {
4156                 LOGGER.debug("unable to print vfmodulecust " + e.getMessage(), e);
4157         }
4158         try {
4159                 VfModuleCustomization existing = this.getVfModuleCustomizationByModelCustomizationId(vfModuleCustomization.getModelCustomizationUuid());
4160                 if (existing == null) {
4161                         LOGGER.debug("No existing entry found, attempting to save...");
4162                 this.getSession ().save (vfModuleCustomization);
4163                 } else {
4164                         try {
4165                                 LOGGER.debug("Found an existing vf module customization entry\n" + existing.toString());
4166                         } catch (Exception e) {
4167                                 LOGGER.debug("unable to print vfmodulecust2 " + e.getMessage(), e);
4168                 }
4169                 }
4170       
4171         } finally {
4172             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateVfModuleCustomization", null); 
4173         }
4174     }
4175
4176     @Deprecated
4177     public HeatNestedTemplate getNestedHeatTemplate(int parentTemplateId, int childTemplateId) {
4178           long startTime = System.currentTimeMillis ();
4179           LOGGER.debug ("Catalog database - get nested Heat template with PerentId-Child Id "
4180                                         + parentTemplateId +"-"+childTemplateId);
4181           try {
4182               HeatNestedTemplate nestedTemplate = new HeatNestedTemplate ();
4183 //              nestedTemplate.setParentTemplateId (parentTemplateId);
4184 //              nestedTemplate.setChildTemplateId (childTemplateId);
4185               
4186               return (HeatNestedTemplate)session.get (HeatNestedTemplate.class,nestedTemplate);
4187           } finally {
4188               LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNestedHeatTemplate", null);
4189           }
4190     }
4191     
4192     // 1707 version
4193     public HeatNestedTemplate getNestedHeatTemplate(String parentTemplateId, String childTemplateId) {
4194           long startTime = System.currentTimeMillis ();
4195         LOGGER.debug ("Catalog database - get nested Heat template with PerentId="
4196                                       + parentTemplateId +", ChildId="+childTemplateId);
4197         try {
4198             HeatNestedTemplate nestedTemplate = new HeatNestedTemplate ();
4199               nestedTemplate.setParentTemplateId (parentTemplateId);
4200               nestedTemplate.setChildTemplateId (childTemplateId);
4201
4202               return (HeatNestedTemplate)session.get (HeatNestedTemplate.class,nestedTemplate);
4203           } finally {
4204               LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNestedHeatTemplate", null);
4205           }
4206     }
4207
4208     // 1707
4209     public void saveNestedHeatTemplate (String parentTemplateId, HeatTemplate childTemplate, String yamlFile) {
4210         long startTime = System.currentTimeMillis ();
4211         LOGGER.debug ("Catalog database - save nested Heat template with name "
4212                                       + childTemplate.getTemplateName () + ",parentId=" + parentTemplateId + ",childId=" + childTemplate.getArtifactUuid() + ", providerResourceFile=" + yamlFile);
4213         try {
4214       
4215                 saveHeatTemplate(childTemplate, childTemplate.getParameters());
4216                 if (getNestedHeatTemplate(parentTemplateId,childTemplate.getArtifactUuid()) == null) { 
4217                     HeatNestedTemplate nestedTemplate = new HeatNestedTemplate ();
4218                     nestedTemplate.setParentTemplateId (parentTemplateId);
4219                     nestedTemplate.setChildTemplateId (childTemplate.getArtifactUuid ());
4220                     nestedTemplate.setProviderResourceFile (yamlFile);
4221                     session.flush();
4222                     session.save (nestedTemplate);
4223                 }
4224         } finally {
4225             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNestedHeatTemplate", null);
4226         }
4227     }
4228
4229     @Deprecated
4230     public HeatFiles getHeatFiles(int vnfResourceId,String fileName,String asdcResourceName, String version) {
4231           long startTime = System.currentTimeMillis ();
4232           LOGGER.debug ("Catalog database - getHeatFiles with name " + fileName
4233                                         + " and vnfResourceID "
4234                                         + vnfResourceId
4235 //                                        + " and ASDC resource name "
4236                                         + asdcResourceName
4237                                         + " and version "
4238                                         + version);
4239
4240           String hql = "FROM HeatFiles WHERE fileName = :fileName AND vnfResourceId = :vnfResourceId AND asdcResourceName = :asdcResourceName AND version = :version";
4241           Query query = getSession ().createQuery (hql);
4242           query.setParameter ("fileName", fileName);
4243           query.setParameter ("vnfResourceId", vnfResourceId);
4244           query.setParameter ("asdcResourceName", asdcResourceName);
4245           query.setParameter ("version", version);
4246
4247           @SuppressWarnings("unchecked")
4248
4249           HeatFiles heatFilesResult = null;
4250           try {
4251                   heatFilesResult = (HeatFiles) query.uniqueResult ();
4252           } catch (org.hibernate.NonUniqueResultException nure) {
4253                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: fileName='" + fileName + "', vnfResourceId='" + vnfResourceId + "' and asdcResourceName=" + asdcResourceName + " and version=" + version);
4254                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for fileName=" + fileName + " and vnfResourceId=" + vnfResourceId + " and asdcResourceName=" + asdcResourceName + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for fileName=" + fileName);
4255                 throw nure;
4256           } catch (org.hibernate.HibernateException he) {
4257                 LOGGER.debug("Hibernate Exception - while searching for: fileName='" + fileName + "', vnfResourceId='" + vnfResourceId + "' and asdcResourceName=" + asdcResourceName + " and version=" + version + " " + he.getMessage());
4258                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for fileName=" + fileName + " and vnfResourceId=" + vnfResourceId + " and asdcResourceName=" + asdcResourceName + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for fileName=" + fileName);
4259                 throw he;
4260           } catch (Exception e) {
4261                 LOGGER.debug("Generic Exception - while searching for: fileName='" + fileName + "', vnfResourceId='" + vnfResourceId + "' and asdcResourceName=" + asdcResourceName + " and version=" + version + " " + e.getMessage());
4262                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for fileName=" + fileName + " and vnfResourceId=" + vnfResourceId + " and asdcResourceName=" + asdcResourceName + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for fileName=" + fileName);
4263                 throw e;
4264           }
4265
4266           // See if something came back.
4267           if (heatFilesResult == null) {
4268               LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. HeatFiles not found", "CatalogDB", "getHeatFiles", null);
4269               return null;
4270           }
4271           // Name + Version is unique, so should only be one element
4272           LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFiles", null);
4273           return heatFilesResult;
4274     }
4275
4276     public HeatFiles getHeatFiles(String artifactUuid) {
4277           long startTime = System.currentTimeMillis ();
4278         LOGGER.debug ("Catalog database - getHeatFiles with artifactUuid " + artifactUuid);
4279
4280         String hql = "FROM HeatFiles WHERE artifactUuid = :artifactUuid";
4281         Query query = getSession ().createQuery (hql);
4282         query.setParameter ("artifactUuid", artifactUuid);
4283
4284         @SuppressWarnings("unchecked")
4285       
4286         HeatFiles heatFilesResult = null;
4287         try {
4288           heatFilesResult = (HeatFiles) query.uniqueResult ();
4289         } catch (org.hibernate.NonUniqueResultException nure) {
4290                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: artifactUuid='" + artifactUuid );
4291                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for artifactUuid=" + artifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for artifactUuid=" + artifactUuid);
4292                 throw nure;
4293         } catch (org.hibernate.HibernateException he) {
4294                 LOGGER.debug("Hibernate Exception - while searching for: artifactUuid='" + artifactUuid + " " + he.getMessage());
4295                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for artifactUuid=" + artifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for artifactUuid=" + artifactUuid);
4296                 throw he;
4297         } catch (Exception e) {
4298                 LOGGER.debug("Generic Exception - while searching for: artifactUuid='" + artifactUuid  + " " + e.getMessage());
4299                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for artifactUuid=" + artifactUuid , "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for artifactUuid=" + artifactUuid);
4300                 throw e;
4301         } 
4302         
4303         // See if something came back.
4304         if (heatFilesResult == null) {
4305             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. HeatFiles not found", "CatalogDB", "getHeatFiles", null);
4306             return null;
4307         }
4308         // Name + Version is unique, so should only be one element
4309         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFiles", null);
4310         return heatFilesResult;
4311   }
4312     
4313     public void saveHeatFiles (HeatFiles childFile) {
4314          long startTime = System.currentTimeMillis ();
4315          LOGGER.debug ("Catalog database - save Heat File with name "
4316                                        + childFile.getFileName());
4317          try {
4318 //             HeatFiles heatFiles = getHeatFiles (childFile.getVnfResourceId(), childFile.getFileName(), childFile.getAsdcResourceName (),childFile.getVersion());
4319              HeatFiles heatFiles = getHeatFiles (childFile.getArtifactUuid());
4320              if (heatFiles == null) {
4321
4322                  // asdc_heat_files_save
4323                  this.getSession ().save (childFile);
4324
4325              } else {
4326                  /* replaced 'heatFiles' by 'childFile'
4327                     Based on following comment:
4328                                         It must be childFile.setId instead of heatFiles.setId, we must return the ID if it exists in DB.
4329                                  */
4330                  childFile.setArtifactUuid(heatFiles.getArtifactUuid());
4331              }
4332
4333          } finally {
4334              LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatFiles", null);
4335          }
4336     }
4337
4338     public void saveVfModuleToHeatFiles (String parentVfModuleId, HeatFiles childFile) {
4339         long startTime = System.currentTimeMillis ();
4340         LOGGER.debug ("Catalog database - save Heat File to VFmodule link "
4341                                       + childFile.getFileName());
4342         try {
4343             saveHeatFiles (childFile);
4344             VfModuleToHeatFiles checkExistingEntry = this.getVfModuleToHeatFilesEntry(parentVfModuleId, childFile.getArtifactUuid());
4345             if (checkExistingEntry == null) {
4346                 VfModuleToHeatFiles vfModuleToHeatFile = new VfModuleToHeatFiles ();
4347                         vfModuleToHeatFile.setVfModuleModelUuid(parentVfModuleId);
4348                         vfModuleToHeatFile.setHeatFilesArtifactUuid(childFile.getArtifactUuid());
4349                         session.flush();
4350                         session.save (vfModuleToHeatFile);
4351             } else {
4352                 LOGGER.debug("**Found existing VfModuleToHeatFiles entry for " + checkExistingEntry.toString());
4353                 LOGGER.debug("No need to save...");
4354             }
4355           
4356         } finally {
4357             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVfModuleToHeatFiles", null);
4358         }
4359     }
4360
4361     /**
4362      * Return a Network Resource that matches the Network Customization defined by given MODEL_CUSTOMIZATION_UUID
4363      *
4364      * @param modelUUID
4365      * @return NetworkRecipe object or null if none found
4366      */
4367     public NetworkResource getNetworkResourceByModelUuid(String modelUUID) {
4368
4369         long startTime = System.currentTimeMillis ();
4370         LOGGER.debug ("Catalog database - get network resource with modelUUID " + modelUUID);
4371
4372         try {
4373             String hql =  "FROM NetworkResource WHERE modelUUID=:modelUUID";
4374             Query query = getSession ().createQuery (hql);
4375             query.setParameter (MODEL_UUID, modelUUID);
4376
4377             @SuppressWarnings("unchecked")
4378             List <NetworkResource> resultList = query.list ();
4379
4380             if (resultList.isEmpty ()) {
4381                 return null;
4382             }
4383             
4384             resultList.sort(new MavenLikeVersioningComparator());
4385             Collections.reverse (resultList);
4386             
4387             return resultList.get (0);
4388         } catch (Exception e) {
4389                 LOGGER.debug("Error trying to find Network Resource with " + modelUUID +", " + e.getMessage(),e);
4390         } finally {
4391             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResourceByModelUuid", null);
4392         }
4393         return null;
4394     }
4395
4396
4397     /**
4398      * Return a Network recipe that matches a given NETWORK_TYPE, ACTION, and, if specified, SERVICE_TYPE
4399      *
4400      * @param networkType
4401      * @param action
4402      * @param serviceType
4403      * @return NetworkRecipe object or null if none found
4404      */
4405     public NetworkRecipe getNetworkRecipe (String networkType, String action, String serviceType) {
4406
4407         long startTime = System.currentTimeMillis ();
4408         LOGGER.debug ("Catalog database - get network recipe with network type " + networkType
4409                                       + " and action "
4410                                       + action
4411                                       + " and service type "
4412                                       + serviceType);
4413
4414         try {
4415             String hql;
4416             if (serviceType == null) {
4417                 hql = "FROM NetworkRecipe WHERE networkType = :networkType AND action = :action AND serviceType IS NULL ";
4418             } else {
4419                 hql = "FROM NetworkRecipe WHERE networkType = :networkType AND action = :action AND serviceType = :serviceType ";
4420             }
4421             Query query = getSession ().createQuery (hql);
4422             query.setParameter (NETWORK_TYPE, networkType);
4423             query.setParameter (ACTION, action);
4424             if (serviceType != null) {
4425                 query.setParameter ("serviceType", serviceType);
4426             }
4427
4428             @SuppressWarnings("unchecked")
4429             List <NetworkRecipe> resultList = query.list ();
4430
4431             if (resultList.isEmpty ()) {
4432                 return null;
4433             }
4434
4435             resultList.sort(new MavenLikeVersioningComparator());
4436             Collections.reverse (resultList);
4437
4438             return resultList.get (0);
4439         } finally {
4440             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkRecipe", null);
4441         }
4442     }
4443
4444     /**
4445      * Return a Network recipe that matches a given MODEL_UUID and ACTION
4446      *
4447      * @param modelName
4448      * @param action
4449      * @return NetworkRecipe object or null if none found
4450      */
4451     public NetworkRecipe getNetworkRecipeByModuleUuid (String networkModelUuid, String action) {
4452         LOGGER.debug ("Catalog database - get network recipe with network model uuid " + networkModelUuid
4453                 + " and action "
4454                 + action
4455                 );
4456         NetworkResource networkResource = getNetworkResourceByModelUuid(networkModelUuid);
4457         if(null == networkResource){
4458             return null;
4459         }
4460         
4461         NetworkRecipe recipe = getNetworkRecipeByNameVersion(networkResource.getModelName(), networkResource.getModelVersion(), action);
4462         return recipe;        
4463     }
4464     
4465     /**
4466      * Return a Network recipe that matches a given MODEL_NAME and ACTION
4467      *
4468      * @param modelName
4469      * @param action
4470      * @return NetworkRecipe object or null if none found
4471      */
4472     public NetworkRecipe getNetworkRecipe (String modelName, String action) {
4473
4474         long startTime = System.currentTimeMillis ();
4475         LOGGER.debug ("Catalog database - get network recipe with network model name " + modelName
4476                                       + " and action "
4477                                       + action
4478                                       );
4479
4480         try {
4481             String hql = "FROM NetworkRecipe WHERE modelName = :modelName AND action = :action";
4482
4483             Query query = getSession ().createQuery (hql);
4484             query.setParameter (MODEL_NAME, modelName);
4485             query.setParameter (ACTION, action);
4486
4487             @SuppressWarnings("unchecked")
4488             List <NetworkRecipe> resultList = query.list ();
4489
4490             if (resultList.isEmpty ()) {
4491                 return null;
4492             }
4493
4494             resultList.sort(new MavenLikeVersioningComparator());
4495             Collections.reverse (resultList);
4496
4497             return resultList.get (0);
4498         } finally {
4499             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkRecipe", null);
4500         }
4501     }
4502
4503     /**
4504      * get network recipe by module name and version and action.
4505      * <br>
4506      * 
4507      * @param modelName
4508      * @param modelVersion
4509      * @param action
4510      * @return
4511      * @since ONAP Beijing Release
4512      */
4513     public NetworkRecipe getNetworkRecipeByNameVersion(String modelName, String modelVersion, String action) {
4514
4515         long startTime = System.currentTimeMillis ();
4516         LOGGER.debug ("Catalog database - get network recipe with network model name " + modelName
4517                                       +"model version " + modelVersion + " and action " + action);
4518
4519         try {
4520             String hql = "FROM NetworkRecipe WHERE modelName = :modelName AND version=:version AND action = :action";
4521
4522             Query query = getSession ().createQuery (hql);
4523             query.setParameter (MODEL_NAME, modelName);
4524             query.setParameter (MODEL_VERSION, modelVersion);
4525             query.setParameter (ACTION, action);
4526
4527             @SuppressWarnings("unchecked")
4528             List <NetworkRecipe> resultList = query.list ();
4529
4530             if (resultList.isEmpty ()) {
4531                 return null;
4532             }
4533
4534             resultList.sort(new MavenLikeVersioningComparator());
4535             Collections.reverse (resultList);
4536
4537             return resultList.get (0);
4538         } finally {
4539             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkRecipe", null);
4540         }
4541     }
4542     
4543     /**
4544      * Return a Network Resource that matches the Network Customization defined by given MODEL_CUSTOMIZATION_UUID
4545      *
4546      * @param modelCustomizationUuid
4547      * @return NetworkRecipe object or null if none found
4548      */
4549     public NetworkResource getNetworkResourceByModelCustUuid(String modelCustomizationUuid) {
4550
4551         long startTime = System.currentTimeMillis ();
4552         LOGGER.debug ("Catalog database - get network resource with modelCustomizationUuid " + modelCustomizationUuid);
4553
4554         try {
4555             String hql =  "select n FROM NetworkResource n, NetworkResourceCustomization c WHERE n.modelUUID=c.networkResourceModelUuid and c.modelCustomizationUuid = :modelCustomizationUuid";
4556             Query query = getSession ().createQuery (hql);
4557             query.setParameter (MODEL_CUSTOMIZATION_UUID, modelCustomizationUuid);
4558
4559             @SuppressWarnings("unchecked")
4560             List <NetworkResource> resultList = query.list ();
4561
4562             if (resultList.isEmpty ()) {
4563                 return null;
4564             }
4565
4566             resultList.sort(new MavenLikeVersioningComparator());
4567             Collections.reverse (resultList);
4568
4569             return resultList.get (0);
4570         } catch (Exception e) {
4571                 LOGGER.debug("Error trying to find Network Resource with " + modelCustomizationUuid +", " + e.getMessage(),e);
4572         } finally {
4573             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResourceByModelCustUuid", null);
4574         }
4575         return null;
4576     }
4577
4578     /**
4579      * Return a VnfComponents recipe that matches a given VNF_TYPE, VNF_COMPONENT_TYPE, ACTION, and, if specified,
4580      * SERVICE_TYPE
4581      *
4582      * @param vnfType
4583      * @param vnfComponentType
4584      * @param action
4585      * @param serviceType
4586      * @return VnfComponentsRecipe object or null if none found
4587      */
4588     public VnfComponentsRecipe getVnfComponentsRecipe (String vnfType,
4589                                                        String vnfComponentType,
4590                                                        String action,
4591                                                        String serviceType) {
4592
4593         long startTime = System.currentTimeMillis ();
4594         LOGGER.debug ("Catalog database - get Vnf Component recipe with network type " + vnfType
4595                                       + " and component type "
4596                                       + vnfComponentType
4597                                       + " and action "
4598                                       + action
4599                                       + " and service type "
4600                                       + serviceType);
4601
4602         try {
4603             String hql;
4604             if (serviceType == null) {
4605                 hql = "FROM VnfComponentsRecipe WHERE vnfType = :vnfType AND vnfComponentType = :vnfComponentType AND action = :action AND serviceType IS NULL ";
4606             } else {
4607                 hql = "FROM VnfComponentsRecipe WHERE vnfType = :vnfType AND vnfComponentType = :vnfComponentType AND action = :action AND serviceType = :serviceType ";
4608             }
4609             Query query = getSession ().createQuery (hql);
4610             query.setParameter (VNF_TYPE, vnfType);
4611             query.setParameter (VNF_COMPONENT_TYPE, vnfComponentType);
4612             query.setParameter (ACTION, action);
4613             if (serviceType != null) {
4614                 query.setParameter ("serviceType", serviceType);
4615             }
4616
4617             @SuppressWarnings("unchecked")
4618             List <VnfComponentsRecipe> resultList = query.list ();
4619
4620             if (resultList.isEmpty ()) {
4621                 return null;
4622             }
4623             resultList.sort(new MavenLikeVersioningComparator());
4624             Collections.reverse (resultList);
4625
4626             return resultList.get (0);
4627         } finally {
4628             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfComponentsRecipe", null);
4629         }
4630     }
4631
4632     /**
4633      * Return a VnfComponents recipe that matches a given VF_MODULE_ID, VNF_COMPONENT_TYPE, ACTION
4634      *
4635      * @param vfModuleModelUUId
4636      * @param vnfComponentType
4637      * @param action
4638      * @return VnfComponentsRecipe object or null if none found
4639      */
4640     public VnfComponentsRecipe getVnfComponentsRecipeByVfModuleModelUUId (String vfModuleModelUUId,
4641                                                        String vnfComponentType,
4642                                                        String action) {
4643
4644         long startTime = System.currentTimeMillis ();
4645         LOGGER.debug ("Catalog database - get Vnf Component recipe with vfModuleModelUUId " + vfModuleModelUUId
4646                                       + " and component type "
4647                                       + vnfComponentType
4648                                       + " and action "
4649                                       + action);
4650
4651         try {
4652             String hql;
4653             hql = "FROM VnfComponentsRecipe WHERE vfModuleModelUUId = :vfModuleModelUUId AND vnfComponentType = :vnfComponentType AND action = :action ";
4654
4655             Query query = getSession ().createQuery (hql);
4656             query.setParameter (VF_MODULE_MODEL_UUID, vfModuleModelUUId);
4657             query.setParameter (VNF_COMPONENT_TYPE, vnfComponentType);
4658             query.setParameter (ACTION, action);
4659
4660             @SuppressWarnings("unchecked")
4661             List <VnfComponentsRecipe> resultList = query.list ();
4662
4663             if (resultList.isEmpty ()) {
4664                 return null;
4665             }
4666             resultList.sort(new MavenLikeVersioningComparator());
4667             Collections.reverse (resultList);
4668
4669             return resultList.get (0);
4670         } finally {
4671             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfComponentsRecipeByVfModuleModelUUId", null);
4672         }
4673     }
4674     
4675     public List<VnfComponentsRecipe> getVnfComponentRecipes (String vfModuleModelUUId) {
4676         
4677         StringBuilder hql = null;
4678         
4679         hql = new StringBuilder ("FROM VnfComponentsRecipe WHERE vfModuleModelUUId = :vfModuleModelUUId");
4680         
4681         long startTime = System.currentTimeMillis ();
4682         LOGGER.debug ("Catalog database - get Service recipe with vfModuleModelUUId " + vfModuleModelUUId);
4683
4684         Query query = getSession ().createQuery (hql.toString ());
4685         query.setParameter ("vfModuleModelUUId", vfModuleModelUUId);
4686         
4687         @SuppressWarnings("unchecked")
4688         List <VnfComponentsRecipe> resultList = query.list ();
4689
4690         if (resultList.isEmpty ()) {
4691             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getVfModuleRecipes", null);
4692             return Collections.EMPTY_LIST;
4693         }
4694         
4695         resultList.sort(new MavenLikeVersioningComparator());
4696         Collections.reverse (resultList);
4697
4698         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleRecipes", null);
4699         return resultList;
4700     }
4701
4702
4703
4704     public void saveOrUpdateVnfComponent (VnfComponent vnfComponent) {
4705         long startTime = System.currentTimeMillis ();
4706
4707         LOGGER.debug ("Catalog database - save VnfComponent where vnfId="+ vnfComponent.getVnfId()+ " AND componentType="+ vnfComponent.getComponentType());
4708
4709         VnfComponent vnfComponentDb = this.getVnfComponent(vnfComponent.getVnfId(), vnfComponent.getComponentType());
4710
4711         try {
4712
4713                 this.getSession ().save (vnfComponent);
4714
4715         } finally {
4716             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateVnfComponent", null);
4717         }
4718     }
4719
4720     /**
4721      * Return a VfModule record that matches a given MODEL_NAME
4722      *
4723      * @param modelName
4724      * @return VfModule object or null if none found
4725      */
4726     public VfModule getVfModule (String modelName) {
4727
4728         long startTime = System.currentTimeMillis ();
4729         LOGGER.debug ("Catalog database - get vf module with model name " + modelName);
4730
4731         try {
4732             String hql;
4733
4734             hql = "FROM VfModule WHERE modelName = :modelName";
4735
4736             Query query = getSession ().createQuery (hql);
4737             query.setParameter (MODEL_NAME, modelName);
4738
4739             @SuppressWarnings("unchecked")
4740             List <VfModule> resultList = query.list ();
4741
4742             if (resultList.isEmpty ()) {
4743                 return null;
4744             }
4745             resultList.sort(new MavenLikeVersioningComparator());
4746             Collections.reverse (resultList);
4747
4748             return resultList.get (0);
4749         } finally {
4750             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModule", null);
4751         }
4752     }
4753
4754     /**
4755      * Return a VfModule record that matches a given MODEL_NAME
4756      *
4757      * @param modelUUID
4758      * @return VfModule object or null if none found
4759      */
4760     public VfModule getVfModuleByModelUUID (String modelUUID) {
4761
4762         long startTime = System.currentTimeMillis ();
4763         LOGGER.debug ("Catalog database - get vf module with modelUUID " + modelUUID);
4764
4765         try {
4766             String hql;
4767
4768             hql = "FROM VfModule WHERE modelUUID = :modelUUID";
4769
4770             Query query = getSession ().createQuery (hql);
4771             query.setParameter (MODEL_UUID, modelUUID);
4772
4773             @SuppressWarnings("unchecked")
4774             List <VfModule> resultList = query.list ();
4775
4776             if (resultList.isEmpty ()) {
4777                 return null;
4778             }
4779             resultList.sort(new MavenLikeVersioningComparator());
4780             Collections.reverse (resultList);
4781
4782             return resultList.get (0);
4783         } finally {
4784             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelUUID", null);
4785         }
4786     }
4787     
4788     /**
4789      * Return a Service recipe that matches a given service ModelUUID and action
4790      * (modelUUID) and ACTION
4791      *
4792      * @param modelUUID
4793      * @param action    
4794      * @return ServiceRecipe object or null if none found
4795      */
4796     public ServiceRecipe getServiceRecipeByModelUUID(String modelUUID, String action) {                     
4797
4798         long startTime = System.currentTimeMillis();
4799         LOGGER.debug("Catalog database - get Service recipe with modelUUID=" + modelUUID + " and action=" + action);
4800
4801         try {
4802                         String hql;
4803                         // based on the new SERVICE_RECIPE schema where SERVICE_MODEL_UUID == MODEL_UUID, a JOIN with the SERVICE table is no longer needed
4804 //                      hql = "SELECT new ServiceRecipe(SR.id, SR.serviceModelUUID, SR.action, SR.description, " +
4805 //                                      "SR.orchestrationUri, SR.serviceParamXSD, case when SR.recipeTimeout is null then 0 else SR.recipeTimeout end, " +
4806 //                                      "case when SR.serviceTimeoutInterim is null then 0 else SR.serviceTimeoutInterim end, SR.created) " +
4807 //                                      "FROM Service as S RIGHT OUTER JOIN S.recipes SR " +
4808 //                                      "WHERE SR.serviceModelUUID = :modelUUID AND SR.action = :action";
4809                         hql = "FROM ServiceRecipe WHERE serviceModelUUID = :modelUUID AND action = :action";
4810                         Query query = getSession().createQuery(hql);
4811                         query.setParameter(MODEL_UUID, modelUUID);
4812                         query.setParameter(ACTION, action);
4813
4814                         @SuppressWarnings("unchecked")
4815                         List<ServiceRecipe> recipeResultList = query.list();
4816                         if (recipeResultList.isEmpty()) {
4817                                 LOGGER.debug("Catalog database - recipeResultList is null");
4818                                 return null;
4819                         }
4820                         recipeResultList.sort(new MavenLikeVersioningComparator());
4821                         Collections.reverse(recipeResultList);
4822                         LOGGER.debug("Catalog database - recipeResultList contains " + recipeResultList.get(0).toString());
4823
4824                         return recipeResultList.get(0);
4825         } finally {
4826             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipeByModelUUID", null);
4827         }
4828     }
4829     
4830     /**
4831      * Return a Service recipe that matches a given SERVICE_NAME_VERSION_ID
4832      * (MODEL_VERSION_ID) and ACTION
4833      *
4834      * @param modelVersionId
4835      * @param action    
4836      * @return ServiceRecipe object or null if none found
4837      */
4838     @Deprecated
4839     public ServiceRecipe getServiceRecipe(String modelVersionId,
4840                                        String action) {                     
4841
4842         long startTime = System.currentTimeMillis();
4843         LOGGER.debug("Catalog database - get Service recipe with modeVersionId=" + modelVersionId
4844                                       + " and action=" + action);
4845
4846         try {
4847                         String hql;
4848                         // Note: Even with the implementation of the HQL JOIN below, the code for the two separate
4849                         //       SELECTs will be retained/commented for now in the event some subsequent JOIN issue arises
4850                         // 1st query to get the Service record for the given SERVICE_NAME_VERSION_ID (MODEL_VERSION_ID)
4851 /*                      hql = "FROM Service WHERE serviceNameVersionId = :serviceNameVersionId";
4852                         Query query = getSession().createQuery(hql);
4853                         query.setParameter(SERVICE_NAME_VERSION_ID, modelVersionId);
4854
4855                         @SuppressWarnings("unchecked")
4856                         List<Service> serviceResultList = query.list();
4857                         if (serviceResultList.isEmpty()) {
4858                                 LOGGER.debug("Catalog database - serviceResultList is null");
4859                                 return null;
4860                         }
4861                         Collections.sort(serviceResultList, new MavenLikeVersioningComparator());
4862                         Collections.reverse(serviceResultList);
4863                         LOGGER.debug("Catalog database - serviceResultList contains " + serviceResultList.get(0).toString());
4864
4865                         // 2nd query to get the ServiceRecipe record corresponding to the Service from the 1st query
4866                         hql = "FROM ServiceRecipe WHERE serviceModelUUID = :serviceModelUUID AND action = :action";
4867                         query = getSession().createQuery(hql);
4868                         // The SERVICE table 'id' field maps to the SERVICE_RECIPE table 'SERVICE_ID' field
4869                         query.setParameter(SERVICE_ID, serviceResultList.get(0).getId());
4870                         query.setParameter(ACTION, action);
4871 */
4872                         // The following SELECT performs a JOIN across the SERVICE and SERVICE_RECIPE tables. It required a new
4873                         // CTR in the ServiceRecipe Class to populate that object (the other option was to parse the Object[]
4874                         // returned by createQuery() and manually populate the ServiceRecipe object). Two of the 'int' fields in the
4875                         // SERVICE_RECIPE DB schema (the timeouts) permit NULL values which required some additional code in the
4876                         // SELECT to generate a default of 0 (needed by the CTR) in the cases where the value is NULL.
4877                         hql = "SELECT new ServiceRecipe(SR.id, SR.serviceModelUUID, SR.action, SR.description, " +
4878                                         "SR.orchestrationUri, SR.serviceParamXSD, case when SR.recipeTimeout is null then 0 else SR.recipeTimeout end, " +
4879                                         "case when SR.serviceTimeoutInterim is null then 0 else SR.serviceTimeoutInterim end, SR.created) " +
4880                                         "FROM Service as S RIGHT OUTER JOIN S.recipes SR " +
4881                                         "WHERE SR.serviceModelUUID = S.id AND S.serviceNameVersionId = :serviceNameVersionId AND SR.action = :action";
4882                         Query query = getSession().createQuery(hql);
4883                         query.setParameter(MODEL_UUID, modelVersionId);
4884                         query.setParameter(ACTION, action);
4885
4886                         @SuppressWarnings("unchecked")
4887                         List<ServiceRecipe> recipeResultList = query.list();
4888                         if (recipeResultList.isEmpty()) {
4889                                 LOGGER.debug("Catalog database - recipeResultList is null");
4890                                 return null;
4891                         }
4892                         recipeResultList.sort(new MavenLikeVersioningComparator());
4893                         Collections.reverse(recipeResultList);
4894                         LOGGER.debug("Catalog database - recipeResultList contains " + recipeResultList.get(0).toString());
4895
4896                         return recipeResultList.get(0);
4897         } finally {
4898             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipe", null);
4899         }
4900     }
4901
4902     /**
4903      * Return a Model recipe that matches a given MODEL_TYPE, MODEL_VERSION_ID, ACTION
4904      * Note: This method is not currently used but was retained in the event the
4905      *       architecture moves back to a MODEL/MODEL_RECIPE structure.
4906      *
4907      * @param modelType
4908      * @param modelVersionId
4909      * @param action
4910      * @return ModelRecipe object or null if none found
4911      */
4912     public ModelRecipe getModelRecipe(String modelType,
4913                                       String modelVersionId,
4914                                       String action) {
4915
4916         long startTime = System.currentTimeMillis();
4917         LOGGER.debug("Catalog database - get Model recipe with modelType=" + modelType
4918                 + " and modeVersionId=" + modelVersionId
4919                 + " and action=" + action);
4920
4921         try {
4922             String hql;
4923             // TBD - at some point it would be desirable to figure out how to do a  HQL JOIN across
4924             //       the MODEL and MODEL_RECIPE tables in HQL instead of 2 separate queries.
4925             //       There seems to be 2 issues: formatting a hql query that executes successfully
4926             //       and then being able to generate a result that will fit into the ModelRecipe class.
4927
4928             // 1st query to get the Model record for the given MODEL_TYPE and MODEL_VERSION_ID
4929             hql = "FROM Model WHERE modelType = :modelType AND modelVersionId = :modelVersionId";
4930             Query query = getSession().createQuery(hql);
4931             query.setParameter(MODEL_TYPE, modelType);
4932             query.setParameter(MODEL_VERSION_ID, modelVersionId);
4933
4934             @SuppressWarnings("unchecked")
4935             List<Model> modelResultList = query.list();
4936             if (modelResultList.isEmpty()) {
4937                 LOGGER.debug("Catalog database - modelResultList is null");
4938                 return null;
4939             }
4940             modelResultList.sort(new MavenLikeVersioningComparator());
4941             Collections.reverse(modelResultList);
4942             LOGGER.debug("Catalog database - modelResultList contains " + modelResultList.get(0).toString());
4943
4944             // 2nd query to get the ModelRecipe record corresponding to the Model from the 1st query
4945             hql = "FROM ModelRecipe WHERE modelId = :modelId AND action = :action";
4946             query = getSession().createQuery(hql);
4947             // The MODEL table 'id' field maps to the MODEL_RECIPE table 'MODEL_ID' field
4948             query.setParameter(MODEL_ID, modelResultList.get(0).getId());
4949             query.setParameter(ACTION, action);
4950
4951             @SuppressWarnings("unchecked")
4952             List<ModelRecipe> recipeResultList = query.list();
4953             if (recipeResultList.isEmpty()) {
4954                 LOGGER.debug("Catalog database - recipeResultList is null");
4955                 return null;
4956             }
4957             recipeResultList.sort(new MavenLikeVersioningComparator());
4958             Collections.reverse(recipeResultList);
4959             LOGGER.debug("Catalog database - recipeResultList contains " + recipeResultList.get(0).toString());
4960
4961             return recipeResultList.get(0);
4962         } finally {
4963             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getModelRecipe", null);
4964         }
4965     }
4966
4967
4968     /**
4969      * Verify the health of the DB.
4970      *
4971      * @return boolean value indicate whether DB is healthy
4972      */
4973     public boolean healthCheck () {
4974         long startTime = System.currentTimeMillis ();
4975         Session session = this.getSession ();
4976
4977         // Query query = session.createQuery (" from ActiveRequests ");
4978         Query query = session.createSQLQuery (" show tables ");
4979
4980         List<?> list = query.list();
4981         LOGGER.debug("healthCheck CatalogDB - Successful");
4982         return true;
4983     }
4984     
4985     public < E > E executeQuerySingleRow(String hql, HashMap<String, String> variables, boolean retry) {
4986         long startTime = System.currentTimeMillis();
4987         LOGGER.debug("Catalog database - executeQuery: " + hql + (retry ? ", retry=true" : ", retry=false"));
4988         Query query = getSession().createQuery(hql);
4989
4990         StringBuilder sb = new StringBuilder();
4991         if (variables != null) {
4992                 for(Map.Entry<String, String> entry : variables.entrySet()){
4993                         sb.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
4994                         query.setParameter(entry.getKey(), entry.getValue());
4995                 }
4996         }
4997         LOGGER.debug("Variables:\n" + sb.toString());
4998
4999         E theObject = null;
5000         try {
5001             theObject = (E) query.uniqueResult();
5002         } catch (org.hibernate.NonUniqueResultException nure) {
5003                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row");
5004                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for " + hql, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for " + hql );
5005                 throw nure;
5006         } catch (org.hibernate.HibernateException he) {
5007                 LOGGER.debug("Hibernate Exception - while performing " + hql + "; he message:" + he.getMessage());
5008                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception while performing hql=" + hql, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for hql=" + hql);
5009                 if (retry) {
5010                         LOGGER.debug("***WILL RETRY***");
5011                         return this.executeQuerySingleRow(hql, variables, false);
5012                 } else {
5013                         throw he;
5014                 }
5015         } catch (Exception e) {
5016                 LOGGER.debug("Generic Exception - while performing '" + hql + "'");
5017                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception performing " + hql, "", "", MsoLogger.ErrorCode.DataError, "Generic exception performing " + hql);
5018                 if (retry) {
5019                         LOGGER.debug("***WILL RETRY***");
5020                         return this.executeQuerySingleRow(hql, variables, false);
5021                 } else {
5022                         throw e;
5023                 }
5024         }
5025
5026         if (theObject == null) {
5027                 LOGGER.debug("Returning null");
5028                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "executeQuerySingleRow", null);
5029         } else {
5030                 LOGGER.debug("Returning an Object");
5031                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "executeQuerySingleRow", null);
5032         }
5033         return theObject;
5034     }
5035     
5036     public < E > List<E> executeQueryMultipleRows(String hql, HashMap<String, String> variables, boolean retry) {
5037         long startTime = System.currentTimeMillis ();
5038         LOGGER.debug("Catalog database - executeQuery: " + hql + (retry ? ", retry=true" : ", retry=false"));
5039         Query query = getSession().createQuery(hql);
5040
5041         StringBuilder sb = new StringBuilder();
5042         if (variables != null) {
5043                 for(Map.Entry<String, String> entry : variables.entrySet()){
5044                         sb.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
5045                         query.setParameter(entry.getKey(), entry.getValue());
5046                 }
5047         }
5048         LOGGER.debug("Variables:\n" + sb.toString());
5049
5050         List<E> theObjects = null;
5051         try {
5052                 theObjects = (List<E>) query.list ();
5053         } catch (org.hibernate.HibernateException he) {
5054                 LOGGER.debug("Hibernate Exception - while performing " + hql + "; he message:" + he.getMessage());
5055                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception while performing hql=" + hql, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for hql=" + hql);
5056                 if (retry) {
5057                         LOGGER.debug("***WILL RETRY***");
5058                         return this.executeQuerySingleRow(hql, variables, false);
5059                 } else {
5060                         throw he;
5061                 }
5062         } catch (Exception e) {
5063                 LOGGER.debug("Generic Exception - while performing '" + hql + "'");
5064                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception performing " + hql, "", "", MsoLogger.ErrorCode.DataError, "Generic exception performing " + hql);
5065                 if (retry) {
5066                         LOGGER.debug("***WILL RETRY***");
5067                         return this.executeQuerySingleRow(hql, variables, false);
5068                 } else {
5069                         throw e;
5070                 }
5071         }
5072
5073         if (theObjects == null) {
5074                 LOGGER.debug("Returning null");
5075                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "executeQuerySingleRow", null);
5076         } else {
5077                 try {
5078                         LOGGER.debug("Returning theObjects:" + theObjects.size());
5079                 } catch (Exception e) {
5080                         LOGGER.debug("Returning theObjects",e);
5081                 }
5082                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "executeQuerySingleRow", null);
5083         }
5084         return theObjects;
5085     }
5086     
5087     
5088     /**
5089      * get allotted resource recipe by module name and version and action.
5090      * <br>
5091      * 
5092      * @param modelName
5093      * @param modelVersion
5094      * @param action
5095      * @return
5096      * @since ONAP Beijing Release
5097      */
5098     public ArRecipe getArRecipeByNameVersion(String modelName, String modelVersion, String action) {
5099
5100         long startTime = System.currentTimeMillis ();
5101         LOGGER.debug ("Catalog database - get ar recipe with ar model name " + modelName
5102                                       +"model version " + modelVersion + " and action " + action);
5103
5104         try {
5105             String hql = "FROM ArRecipe WHERE modelName = :modelName AND version=:version AND action = :action";
5106
5107             Query query = getSession ().createQuery (hql);
5108             query.setParameter (MODEL_NAME, modelName);
5109             query.setParameter (MODEL_VERSION, modelVersion);
5110             query.setParameter (ACTION, action);
5111
5112             @SuppressWarnings("unchecked")
5113             List <ArRecipe> resultList = query.list ();
5114
5115             if (resultList.isEmpty ()) {
5116                 return null;
5117             }
5118
5119             resultList.sort(new MavenLikeVersioningComparator());
5120             Collections.reverse (resultList);
5121
5122             return resultList.get (0);
5123         } finally {
5124             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkRecipe", null);
5125         }
5126     }
5127     
5128     /**
5129      * Return a allotted resource recipe that matches a given MODEL_UUID and ACTION
5130      *
5131      * @param modelName
5132      * @param action
5133      * @return ArRecipe object or null if none found
5134      */
5135     public ArRecipe getArRecipeByModuleUuid (String ArModelUuid, String action) {
5136         LOGGER.debug ("Catalog database - get ar recipe with ar model uuid " + ArModelUuid
5137                 + " and action "
5138                 + action
5139                 );
5140         AllottedResource arResource = this.getAllottedResourceByModelUuid(ArModelUuid);
5141         if(null == arResource){
5142             return null;
5143         }
5144         
5145         ArRecipe recipe = getArRecipeByNameVersion(arResource.getModelName(), arResource.getModelVersion(), action);
5146         return recipe;        
5147     }
5148     
5149 }