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