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