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