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