Cleanup project's name in Sonar
[so.git] / mso-catalog-db / src / main / java / org / openecomp / mso / db / catalog / CatalogDatabase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.db.catalog;
22
23 import java.io.Closeable;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import org.openecomp.mso.db.catalog.beans.*;
31 import org.hibernate.HibernateException;
32 import org.hibernate.NonUniqueResultException;
33 import org.hibernate.Query;
34 import org.hibernate.Session;
35 import org.hibernate.SessionFactory;
36 import org.hibernate.cfg.Configuration;
37 import org.hibernate.service.ServiceRegistry;
38 import org.hibernate.service.ServiceRegistryBuilder;
39
40 import org.openecomp.mso.db.HibernateUtils;
41 import org.openecomp.mso.db.catalog.utils.MavenLikeVersioningComparator;
42 import org.openecomp.mso.db.catalog.utils.RecordNotFoundException;
43 import org.openecomp.mso.logger.MessageEnum;
44 import org.openecomp.mso.logger.MsoLogger;
45
46 /**
47  * This class encapsulates all of the objects that can be queried from a Catalog database.
48  * Clients must use these methods to retrieve catalog objects. The session is not
49  * available for clients to do their own direct queries to the database.
50  *
51  *
52  */
53 public class CatalogDatabase implements Closeable {
54
55     protected static HibernateUtils hibernateUtils = new HibernateUtilsCatalogDb ();
56
57     private static final String NETWORK_TYPE = "networkType";
58     private static final String ACTION = "action";
59     private static final String VNF_TYPE = "vnfType";
60     private static final String SERVICE_TYPE = "serviceType";
61     private static final String VNF_COMPONENT_TYPE = "vnfComponentType";
62     private static final String MODEL_ID = "modelId";
63     private static final String MODEL_NAME = "modelName";
64     private static final String TYPE = "type";
65     private static final String MODEL_TYPE = "modelType";
66     private static final String MODEL_VERSION_ID = "modelVersionId";
67     private static final String MODEL_CUSTOMIZATION_UUID = "modelCustomizationUuid";
68     private static final String VF_MODULE_ID = "vfModuleId";
69     private static final String SERVICE_NAME_VERSION_ID= "serviceNameVersionId";
70
71     protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.GENERAL);
72
73     protected Session session = null;
74
75     public CatalogDatabase () {
76     }
77
78
79     private Session getSession () {
80
81              if (session == null) {
82             try {
83                 session = hibernateUtils.getSessionFactory ().openSession ();
84                 session.beginTransaction ();
85             } catch (HibernateException he) {
86                 LOGGER.error (MessageEnum.GENERAL_EXCEPTION_ARG, "Error creating Hibernate Session: " + he, "", "", MsoLogger.ErrorCode.DataError, "Error creating Hibernate Session: " + he);
87                 throw he;
88             }
89         }
90
91         return session;
92     }
93
94     /**
95      * Close an open Catalog Database session.
96      * This method should always be called when a client is finished using a
97      * CatalogDatabase instance.
98      */
99     @Override
100     public void close () {
101         if (session != null) {
102             session.close ();
103             session = null;
104         }
105     }
106
107     /**
108      * Commits the current transaction on this session and starts a fresh one.
109      */
110     public void commit () {
111         getSession ().getTransaction ().commit ();
112         getSession ().beginTransaction ();
113     }
114
115     /**
116      * Rolls back current transaction and starts a fresh one.
117      */
118     public void rollback () {
119         getSession ().getTransaction ().rollback ();
120         getSession ().beginTransaction ();
121     }
122
123     /**
124      * Return all Heat Templates in the Catalog DB
125      *
126      * @return A list of HeatTemplate objects
127      */
128     @SuppressWarnings("unchecked")
129     public List <HeatTemplate> getAllHeatTemplates () {
130         long startTime = System.currentTimeMillis ();
131         LOGGER.debug ("Catalog database - get all Heat templates");
132         String hql = "FROM HeatTemplate";
133         Query query = getSession ().createQuery (hql);
134
135         List <HeatTemplate> result = query.list ();
136         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllHeatTemplates", null);
137         return result;
138     }
139
140     /**
141      * Fetch a specific Heat Template by ID.
142      *
143      * @param templateId
144      * @return HeatTemplate object or null if none found
145      */
146     public HeatTemplate getHeatTemplate (int templateId) {
147         long startTime = System.currentTimeMillis ();
148         LOGGER.debug ("Catalog database - get Heat template with id " + templateId);
149
150         HeatTemplate template = (HeatTemplate) getSession ().get (HeatTemplate.class, templateId);
151         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
152         return template;
153     }
154
155     /**
156      * Return the newest version of a specific Heat Template (queried by Name).
157      *
158      * @param templateName
159      * @return HeatTemplate object or null if none found
160      */
161     public HeatTemplate getHeatTemplate (String templateName) {
162
163         long startTime = System.currentTimeMillis ();
164         LOGGER.debug ("Catalog database - get Heat template with name " + templateName);
165
166         String hql = "FROM HeatTemplate WHERE templateName = :template_name";
167         Query query = getSession ().createQuery (hql);
168         query.setParameter ("template_name", templateName);
169
170         @SuppressWarnings("unchecked")
171         List <HeatTemplate> resultList = query.list ();
172
173         // See if something came back. Name is unique, so
174         if (resultList.isEmpty ()) {
175             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No template found", "CatalogDB", "getHeatTemplate", null);
176             return null;
177         }
178         Collections.sort (resultList, new MavenLikeVersioningComparator ());
179         Collections.reverse (resultList);
180
181         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
182         return resultList.get (0);
183     }
184
185     /**
186      * Return a specific version of a specific Heat Template (queried by Name).
187      *
188      * @param templateName
189      * @param version
190      * @return HeatTemplate object or null if none found
191      */
192     public HeatTemplate getHeatTemplate (String templateName, String version) {
193
194         long startTime = System.currentTimeMillis ();
195         LOGGER.debug ("Catalog database - get Heat template with name " + templateName
196                                       + " and version "
197                                       + version);
198
199         String hql = "FROM HeatTemplate WHERE templateName = :template_name AND version = :version";
200         Query query = getSession ().createQuery (hql);
201         query.setParameter ("template_name", templateName);
202         query.setParameter ("version", version);
203
204         @SuppressWarnings("unchecked")
205         List <HeatTemplate> resultList = query.list ();
206
207         // See if something came back.
208         if (resultList.isEmpty ()) {
209             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No template found.", "CatalogDB", "getHeatTemplate", null);
210             return null;
211         }
212         // Name + Version is unique, so should only be one element
213         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
214         return resultList.get (0);
215     }
216
217     /**
218      * Fetch a Service definition by InvariantUUID
219      */
220     public Service getServiceByInvariantUUID (String modelInvariantUUID) {
221
222         long startTime = System.currentTimeMillis ();
223         LOGGER.debug ("Catalog database - get service with Invariant UUID " + modelInvariantUUID);
224
225         String hql = "FROM Service WHERE modelInvariantUUID = :model_invariant_uuid";
226         Query query = getSession ().createQuery (hql);
227         query.setParameter ("model_invariant_uuid", modelInvariantUUID);
228
229         @SuppressWarnings("unchecked")
230         List <Service> resultList = query.list ();
231
232         // See if something came back.
233         if (resultList.isEmpty ()) {
234             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByName", null);
235             return null;
236         }
237         Collections.sort (resultList, new MavenLikeVersioningComparator ());
238         Collections.reverse (resultList);
239
240         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByName", null);
241         return resultList.get (0);
242     }
243
244     /**
245      * Fetch a Service definition
246      */
247     public Service getService (String serviceName) {
248
249         long startTime = System.currentTimeMillis ();
250         LOGGER.debug ("Catalog database - get service with name " + serviceName);
251
252         String hql = "FROM Service WHERE serviceName = :service_name";
253         Query query = getSession ().createQuery (hql);
254         query.setParameter ("service_name", serviceName);
255
256         Service service = null;
257         try {
258                 service = (Service) query.uniqueResult ();
259         } catch (org.hibernate.NonUniqueResultException nure) {
260                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: serviceName='" + serviceName + "'");
261                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for serviceName=" + serviceName, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for serviceName=" + serviceName);
262                 return null;
263         } catch (org.hibernate.HibernateException he) {
264                 LOGGER.debug("Hibernate Exception - while searching for: serviceName='" + serviceName + "'");
265                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for serviceName=" + serviceName, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for serviceName=" + serviceName);
266                 return null;
267         } catch (Exception e) {
268                 LOGGER.debug("Generic Exception - while searching for: serviceName='" + serviceName);
269                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for serviceName=" + serviceName, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for serviceName=" + serviceName);
270                 return null;
271         }
272         if (service == null) {
273                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getService", null);
274         } else {
275                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getService", null);
276         }
277
278         return service;
279     }
280
281     /**
282      * Fetch a Service definition
283      */
284     public Service getServiceByUUID (String serviceNameVersionId) {
285
286         long startTime = System.currentTimeMillis ();
287         LOGGER.debug ("Catalog database - get service with UUID " + serviceNameVersionId);
288
289         String hql = "FROM Service WHERE serviceNameVersionId = :service_id";
290         Query query = getSession ().createQuery (hql);
291         query.setParameter ("service_id", serviceNameVersionId);
292
293         Service service = null;
294         try {
295                 service = (Service) query.uniqueResult ();
296         } catch (org.hibernate.NonUniqueResultException nure) {
297                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: serviceNameVersionId='" + serviceNameVersionId + "'");
298                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for serviceNameVersionId=" + serviceNameVersionId, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for serviceNameVersionId=" + serviceNameVersionId);
299                 return null;
300         } catch (org.hibernate.HibernateException he) {
301                 LOGGER.debug("Hibernate Exception - while searching for: serviceName='" + serviceNameVersionId + "'");
302                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for serviceNameVersionId=" + serviceNameVersionId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for serviceNameVersionId=" + serviceNameVersionId);
303                 return null;
304         } catch (Exception e) {
305                 LOGGER.debug("Generic Exception - while searching for: serviceName='" + serviceNameVersionId);
306                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for serviceNameVersionId=" + serviceNameVersionId, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for serviceNameVersionId=" + serviceNameVersionId);
307                 return null;
308         }
309         if (service == null) {
310                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getService", null);
311         } else {
312                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getService", null);
313         }
314
315         return service;
316     }
317
318     /**
319      * Fetch the Common Service API definition using Http Method + serviceNameVersionId
320      */
321     public Service getService(HashMap<String, String> map, String httpMethod) {
322
323         String serviceNameVersionId = map.get("serviceNameVersionId");
324         Query query;
325         String serviceId = "not_set";
326         String serviceVersion = "not_set";
327
328         if(serviceNameVersionId != null && serviceNameVersionId.length() > 0){
329                 LOGGER.debug ("Catalog database - get serviceNameVersionId with id " + serviceNameVersionId);
330
331                 String hql = "FROM Service WHERE service_name_version_id = :service_name_version_id and http_method = :http_method";
332                 query = getSession ().createQuery (hql);
333             query.setParameter ("service_name_version_id", serviceNameVersionId);
334          } else {
335                 serviceId = map.get("serviceId");
336                 serviceVersion = map.get("serviceVersion");
337             LOGGER.debug ("Catalog database - get serviceId with id " + serviceId + " and serviceVersion with " + serviceVersion);
338
339             String hql = "FROM Service WHERE service_id = :service_id and service_version = :service_version and http_method = :http_method";
340             query = getSession ().createQuery (hql);
341             query.setParameter ("service_id", serviceId);
342             query.setParameter ("service_version", serviceVersion);
343          }
344
345         query.setParameter ("http_method", httpMethod);
346
347         long startTime = System.currentTimeMillis ();
348         Service service = null;
349         try {
350                 service = (Service) query.uniqueResult ();
351         } catch (org.hibernate.NonUniqueResultException nure) {
352                 LOGGER.debug("Non Unique Result Exception - data integrity error: service_id='" + serviceId + "', serviceVersion='" + serviceVersion + "'");
353                 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);
354                 service = null;
355         } catch (org.hibernate.HibernateException he) {
356                 LOGGER.debug("Hibernate Exception - while searching for: service_id='" + serviceId + "', serviceVersion='" + serviceVersion + "'");
357                 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);
358                 service = null;
359         } catch (Exception e) {
360                 LOGGER.debug("Generic Exception - while searching for: service_id='" + serviceId + "', serviceVersion='" + serviceVersion + "'");
361                 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);
362                 service = null;
363         }
364         if (service == null) {
365                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getService", null);
366         } else {
367                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getService", null);
368         }
369         return service;
370     }
371
372     /**
373      * Return the newest version of a Service (queried by Name).
374      *
375      * @param serviceName
376      * @return Service object or null if none found
377      */
378     public Service getServiceByName (String serviceName) {
379
380         long startTime = System.currentTimeMillis ();
381         LOGGER.debug ("Catalog database - get service with name " + serviceName);
382
383         String hql = "FROM Service WHERE serviceName = :service_name";
384         Query query = getSession ().createQuery (hql);
385         query.setParameter ("service_name", serviceName);
386
387         @SuppressWarnings("unchecked")
388         List <Service> resultList = query.list ();
389
390         // See if something came back.
391         if (resultList.isEmpty ()) {
392             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByName", null);
393             return null;
394         }
395         Collections.sort (resultList, new MavenLikeVersioningComparator ());
396         Collections.reverse (resultList);
397
398         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByName", null);
399         return resultList.get (0);
400     }
401
402     public Service getServiceByVersionAndInvariantId(String modelInvariantId, String modelVersion) throws Exception {
403         long startTime = System.currentTimeMillis ();
404         LOGGER.debug ("Catalog database - get service with modelInvariantId: " + modelInvariantId + " and modelVersion: " + modelVersion);
405
406         String hql = "FROM Service WHERE modelInvariantUUID = :MODEL_INVARIANT_UUID AND version = :VERSION_STR";
407         Query query = getSession ().createQuery (hql);
408         query.setParameter ("MODEL_INVARIANT_UUID", modelInvariantId);
409         query.setParameter("VERSION_STR", modelVersion);
410
411         Service result = null;
412         try {
413             result = (Service) query.uniqueResult();
414         } catch (org.hibernate.NonUniqueResultException nure) {
415             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelInvariantId='" + modelInvariantId + "', modelVersion='" + modelVersion + "'");
416             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelInvariantId=" + modelInvariantId + " and modelVersion=" + modelVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelInvariantId=" + modelInvariantId);
417             throw new Exception("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelInvariantId='" + modelInvariantId + "', modelVersion='" + modelVersion + "'");
418         }
419         // See if something came back.
420         if (result==null) {
421             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByVersionAndInvariantId", null);
422             return null;
423         }
424
425         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByVersionAndInvariantId", null);
426         return result;
427     }
428
429     /**
430      * Return a Service recipe that matches a given SERVICE_NAME_VERSION_ID
431      * (MODEL_VERSION_ID) and ACTION
432      *
433      * @param modelVersionId
434      * @param action
435      * @return ServiceRecipe object or null if none found
436      */
437     public ServiceRecipe getServiceRecipe(String modelVersionId,
438                                        String action) {
439
440         long startTime = System.currentTimeMillis();
441         LOGGER.debug("Catalog database - get Service recipe with modeVersionId=" + modelVersionId
442                                       + " and action=" + action);
443
444         try {
445                         String hql;
446
447                         hql = "SELECT new ServiceRecipe(SR.id, SR.serviceId, SR.action, SR.description, " +
448                                         "SR.orchestrationUri, SR.serviceParamXSD, case when SR.recipeTimeout is null then 0 else SR.recipeTimeout end, " +
449                                         "case when SR.serviceTimeoutInterim is null then 0 else SR.serviceTimeoutInterim end, SR.created) " +
450                                         "FROM Service as S RIGHT OUTER JOIN S.recipes SR " +
451                                         "WHERE SR.serviceId = S.id AND S.serviceNameVersionId = :serviceNameVersionId AND SR.action = :action";
452                         Query query = getSession().createQuery(hql);
453                         query.setParameter(SERVICE_NAME_VERSION_ID, modelVersionId);
454                         query.setParameter(ACTION, action);
455
456                         @SuppressWarnings("unchecked")
457                         List<ServiceRecipe> recipeResultList = query.list();
458                         if (recipeResultList.isEmpty()) {
459                                 LOGGER.debug("Catalog database - recipeResultList is null");
460                                 return null;
461                         }
462                         Collections.sort(recipeResultList, new MavenLikeVersioningComparator());
463                         Collections.reverse(recipeResultList);
464                         LOGGER.debug("Catalog database - recipeResultList contains " + recipeResultList.get(0).toString());
465
466                         return recipeResultList.get(0);
467         } finally {
468             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipe", null);
469         }
470     }
471
472     /**
473      * Return a newest version of Service recipe that matches a given SERVICE_ID and ACTION
474      *
475      * @param serviceId
476      * @param action     *
477      * @return ServiceRecipe object or null if none found
478      */
479     public ServiceRecipe getServiceRecipe (int serviceId, String action) {
480
481         StringBuilder hql =  null;
482
483         if(action == null){
484             hql = new StringBuilder ("FROM ServiceRecipe WHERE serviceId = :serviceId");
485         }else {
486             hql = new StringBuilder ("FROM ServiceRecipe WHERE serviceId = :serviceId AND action = :action ");
487         }
488
489         long startTime = System.currentTimeMillis ();
490         LOGGER.debug ("Catalog database - get Service recipe with serviceId " + Integer.toString(serviceId)
491                                       + " and action "
492                                       + action
493                                       );
494
495         Query query = getSession ().createQuery (hql.toString ());
496         query.setParameter ("serviceId", serviceId);
497         query.setParameter (ACTION, action);
498         if(action != null){
499             query.setParameter (ACTION, action);
500         }
501
502         @SuppressWarnings("unchecked")
503         List <ServiceRecipe> resultList = query.list ();
504
505         if (resultList.isEmpty ()) {
506             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getServiceRecipe", null);
507             return null;
508         }
509
510         Collections.sort (resultList, new MavenLikeVersioningComparator ());
511         Collections.reverse (resultList);
512
513         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipe", null);
514         return resultList.get (0);
515     }
516
517     /**
518      * @param serviceName
519      * @param action
520      * @return ServiceRecipe object or null if none found. returns a newest version of Service recipe that matches a given serviceName, action and for the newest service version
521      */
522     public ServiceRecipe getServiceRecipeByServiceNameAndAction(String serviceName, String action) {
523         Service service = getServiceByName(serviceName);
524         if (service != null ){
525             return getServiceRecipe(service.getId(),action);
526         }
527         return null;
528     }
529
530     public List<ServiceRecipe> getServiceRecipes (int serviceId) {
531
532         StringBuilder hql = null;
533
534         hql = new StringBuilder ("FROM ServiceRecipe WHERE serviceId = :serviceId");
535
536         long startTime = System.currentTimeMillis ();
537         LOGGER.debug ("Catalog database - get Service recipe with serviceId " + Integer.toString(serviceId));
538
539         Query query = getSession ().createQuery (hql.toString ());
540         query.setParameter ("serviceId", serviceId);
541
542         @SuppressWarnings("unchecked")
543         List <ServiceRecipe> resultList = query.list ();
544
545         if (resultList.isEmpty ()) {
546             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getServiceRecipes", null);
547             return null;
548         }
549
550         Collections.sort (resultList, new MavenLikeVersioningComparator ());
551         Collections.reverse (resultList);
552
553         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipes", null);
554         return resultList;
555     }
556
557     /**
558      * Return the VNF component data - queried by the VNFs ID and the component type.
559      *
560      * @param vnfId
561      * @param type
562      * @return VnfComponent object or null if none found
563      */
564     public VnfComponent getVnfComponent (int vnfId, String type) {
565
566         long startTime = System.currentTimeMillis();
567         LOGGER.debug ("Catalog database - get VnfComponent where vnfId="+ vnfId+ " AND componentType="+ type);
568
569         String hql = "FROM VnfComponent WHERE vnfId = :vnf_id AND componentType = :type";
570         Query query = getSession ().createQuery (hql);
571         query.setParameter ("vnf_id", vnfId);
572         query.setParameter ("type", type);
573
574         VnfComponent result = null;
575         try {
576                 result = (VnfComponent) query.uniqueResult();
577         } catch (org.hibernate.NonUniqueResultException nure) {
578                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: vnf_id='" + vnfId + "', componentType='" + type + "'");
579                 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);
580                 result = null;
581         } catch (org.hibernate.HibernateException he) {
582                 LOGGER.debug("Hibernate Exception - while searching for: vnf_id='" + vnfId + "', componentType='" + type + "'");
583                 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);
584                 result = null;
585         } catch (Exception e) {
586                 LOGGER.debug("Generic Exception - while searching for: vnf_id='" + vnfId + "', componentType='" + type + "'");
587                 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);
588                 result = null;
589         }
590
591         //LOGGER.debug("Found VNF Component: " + result.toString());
592         if (result != null) {
593             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfComponent", null);
594         } else {
595             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No VNFComponent found", "CatalogDB", "getVnfComponent", null);
596         }
597         return result;
598     }
599
600     /**
601      * Return the newest version of a specific VNF resource (queried by Name).
602      *
603      * @param vnfType
604      * @return VnfResource object or null if none found
605      */
606     public VnfResource getVnfResource (String vnfType) {
607
608         long startTime = System.currentTimeMillis ();
609         LOGGER.debug ("Catalog database - get vnf resource with name " + vnfType);
610
611         String hql = "FROM VnfResource WHERE vnfType = :vnf_name";
612         Query query = getSession ().createQuery (hql);
613         query.setParameter ("vnf_name", vnfType);
614
615         @SuppressWarnings("unchecked")
616         List <VnfResource> resultList = query.list ();
617
618         // See if something came back. Name is unique, so
619         if (resultList.isEmpty ()) {
620             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF not found", "CatalogDB", "getVnfResource", null);
621             return null;
622         }
623         Collections.sort (resultList, new MavenLikeVersioningComparator ());
624         Collections.reverse (resultList);
625
626         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResource", null);
627         return resultList.get (0);
628     }
629
630     /**
631      * Return the newest version of a specific VNF resource (queried by Name).
632      *
633      * @param vnfType
634      * @param version
635      * @return VnfResource object or null if none found
636      */
637     public VnfResource getVnfResource (String vnfType, String serviceVersion) {
638
639         long startTime = System.currentTimeMillis ();
640         LOGGER.debug ("Catalog database - get VNF resource with name " + vnfType);
641
642         String hql = "FROM VnfResource WHERE vnfType = :vnfName and version = :serviceVersion";
643         Query query = getSession ().createQuery (hql);
644         query.setParameter ("vnfName", vnfType);
645         query.setParameter ("serviceVersion", serviceVersion);
646
647         VnfResource resource = null;
648         try {
649                 resource = (VnfResource) query.uniqueResult ();
650         } catch (org.hibernate.NonUniqueResultException nure) {
651                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: vnfType='" + vnfType + "', serviceVersion='" + serviceVersion + "'");
652                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfType=" + vnfType + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnfType=" + vnfType);
653                 resource = null;
654         } catch (org.hibernate.HibernateException he) {
655                 LOGGER.debug("Hibernate Exception - while searching for: vnfType='" + vnfType + "', asdc_service_model_version='" + serviceVersion + "'");
656                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for vnfType=" + vnfType + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfType=" + vnfType);
657                 resource = null;
658         } catch (Exception e) {
659                 LOGGER.debug("Generic Exception - while searching for: vnfType='" + vnfType + "', serviceVersion='" + serviceVersion + "'");
660                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for vnfType=" + vnfType + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for vnfType=" + vnfType);
661                 resource = null;
662         }
663         if (resource == null) {
664                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResource", null);
665         } else {
666                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResource", null);
667         }
668         return resource;
669     }
670
671     /**
672      * Return the newest version of a specific VNF resource (queried by modelCustomizationId).
673      *
674      * @param vnfType
675      * @param version
676      * @return VnfResource object or null if none found
677      */
678     public VnfResource getVnfResourceByModelCustomizationId (String modelCustomizationId, String serviceVersion) {
679
680         long startTime = System.currentTimeMillis ();
681         LOGGER.debug ("Catalog database - get VNF resource with modelCustomizationId " + modelCustomizationId);
682
683         String hql = "FROM VnfResource WHERE modelCustomizationUuid = :modelCustomizationId and version = :serviceVersion";
684         Query query = getSession ().createQuery (hql);
685         query.setParameter ("modelCustomizationId", modelCustomizationId);
686         query.setParameter ("serviceVersion", serviceVersion);
687
688         VnfResource resource = null;
689         try {
690             resource = (VnfResource) query.uniqueResult ();
691         } catch (org.hibernate.NonUniqueResultException nure) {
692             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelCustomizationUuid='" + modelCustomizationId + "', serviceVersion='" + serviceVersion + "'");
693             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelCustomizationUuid=" + modelCustomizationId + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationId=" + modelCustomizationId);
694             resource = null;
695         } catch (org.hibernate.HibernateException he) {
696             LOGGER.debug("Hibernate Exception - while searching for: modelCustomizationId='" + modelCustomizationId + "', asdc_service_model_version='" + serviceVersion + "'");
697             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelCustomizationId=" + modelCustomizationId + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationId=" + modelCustomizationId);
698             resource = null;
699         } catch (Exception e) {
700             LOGGER.debug("Generic Exception - while searching for: modelCustomizationId='" + modelCustomizationId + "', serviceVersion='" + serviceVersion + "'");
701             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelCustomizationId=" + modelCustomizationId + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationId=" + modelCustomizationId);
702             resource = null;
703         }
704         if (resource == null) {
705             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVnfResource", null);
706         } else {
707             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResource", null);
708         }
709         return resource;
710     }
711
712     /**
713      * Return the newest version of a specific VNF resource (queried by ID).
714      *
715      * @param id The vnf id
716      * @return VnfResource object or null if none found
717      */
718     public VnfResource getVnfResourceById (int id) {
719
720         long startTime = System.currentTimeMillis ();
721         LOGGER.debug ("Catalog database - get VNF resource with id " + id);
722
723         String hql = "FROM VnfResource WHERE id = :id";
724         Query query = getSession ().createQuery (hql);
725         query.setParameter ("id", id);
726
727         @SuppressWarnings("unchecked")
728         List <VnfResource> resultList = query.list ();
729
730         // See if something came back. Name is unique, so
731         if (resultList.isEmpty ()) {
732             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VnfResource not found", "CatalogDB", "getVnfResourceById", null);
733             return null;
734         }
735         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourceById", null);
736         return resultList.get (0);
737     }
738
739     /**
740      * Return the newest version of a vfModule - 1607
741      *
742      */
743     public VfModule getVfModuleModelName (String modelName) {
744
745         long startTime = System.currentTimeMillis ();
746         LOGGER.debug ("Catalog database - get vfModuleModelName with name " + modelName);
747
748         String hql = "FROM VfModule WHERE model_name = :model_name";
749         Query query = getSession ().createQuery (hql);
750         query.setParameter ("model_name", modelName);
751
752         @SuppressWarnings("unchecked")
753         List <VfModule> resultList = query.list ();
754
755         // See if something came back. Name is unique, so
756         if (resultList.isEmpty ()) {
757             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF not found", "CatalogDB", "getVfModuleModelName", null);
758             return null;
759         }
760         Collections.sort (resultList, new MavenLikeVersioningComparator ());
761         Collections.reverse (resultList);
762
763         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleModelName", null);
764         return resultList.get (0);
765     }
766
767     public VfModule getVfModuleModelName (String modelName, String model_version) {
768
769         long startTime = System.currentTimeMillis ();
770         LOGGER.debug ("Catalog database - get vfModuleModelName with type='" + modelName + "' and asdc_service_model_version='" + model_version + "'");
771
772         String hql = "FROM VfModule WHERE model_name = :model_name and version = :model_version";
773         Query query = getSession ().createQuery (hql);
774         query.setParameter ("model_name", modelName);
775         query.setParameter ("model_version", model_version);
776
777         VfModule module = null;
778         try {
779                 module = (VfModule) query.uniqueResult ();
780         } catch (org.hibernate.NonUniqueResultException nure) {
781                 LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: type='" + modelName + "', asdc_service_model_version='" + model_version + "'");
782                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for type=" + modelName + " and version=" + model_version, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for type=" + modelName);
783                 module = null;
784         } catch (org.hibernate.HibernateException he) {
785                 LOGGER.debug("Hibernate Exception - while searching for: type='" + modelName + "', asdc_service_model_version='" + model_version + "'");
786                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for type=" + modelName + " and version=" + model_version, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for type=" + modelName);
787                 module = null;
788         } catch (Exception e) {
789                 LOGGER.debug("Generic Exception - while searching for: type='" + modelName + "', asdc_service_model_version='" + model_version + "'");
790                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for type=" + modelName + " and version=" + model_version, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for type=" + modelName);
791                 module = null;
792         }
793         if (module == null) {
794                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleModelName", null);
795         } else {
796                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleModelName", null);
797         }
798         return module;
799     }
800
801
802     /**
803      * Return the newest version of a specific Network resource (queried by Type).
804      *
805      * @param networkType
806      * @return NetworkResource object or null if none found
807      */
808     public NetworkResource getNetworkResource (String networkType) {
809
810         long startTime = System.currentTimeMillis ();
811         LOGGER.debug ("Catalog database - get network resource with type " + networkType);
812
813         String hql = "FROM NetworkResource WHERE networkType = :network_type";
814         Query query = getSession ().createQuery (hql);
815         query.setParameter ("network_type", networkType);
816
817         @SuppressWarnings("unchecked")
818         List <NetworkResource> resultList = query.list ();
819
820         // See if something came back. Name is unique, so
821         if (resultList.isEmpty ()) {
822             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Network Resource not found", "CatalogDB", "getNetworkResource", null);
823             return null;
824         }
825
826         Collections.sort (resultList, new MavenLikeVersioningComparator ());
827         Collections.reverse (resultList);
828         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResource", null);
829         return resultList.get (0);
830     }
831
832     /**
833      * Return a VNF recipe that matches a given VNF_TYPE, ACTION, and, if specified, SERVICE_TYPE
834      *
835      * @param vnfType
836      * @param action
837      * @param serviceType The service Name, if null or empty is provided, it won't be taken into account
838      * @return VnfRecipe object or null if none found
839      */
840     public VnfRecipe getVnfRecipe (String vnfType, String action, String serviceType) {
841         boolean withServiceType = false;
842
843         StringBuilder hql = new StringBuilder ("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action ");
844
845         // If query c
846         if (serviceType == null || serviceType.isEmpty ()) {
847             hql.append ("AND serviceType is NULL ");
848         } else {
849             hql.append ("AND serviceType = :serviceType ");
850             withServiceType = true;
851         }
852
853         long startTime = System.currentTimeMillis ();
854         LOGGER.debug ("Catalog database - get VNF recipe with name " + vnfType
855                                       + " and action "
856                                       + action
857                                       + " and service type "
858                                       + serviceType);
859
860         Query query = getSession ().createQuery (hql.toString ());
861         query.setParameter (VNF_TYPE, vnfType);
862         query.setParameter (ACTION, action);
863         if (withServiceType) {
864             query.setParameter (SERVICE_TYPE, serviceType);
865         }
866
867         @SuppressWarnings("unchecked")
868         List <VnfRecipe> resultList = query.list ();
869
870         if (resultList.isEmpty ()) {
871             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null);
872             return null;
873         }
874
875         Collections.sort (resultList, new MavenLikeVersioningComparator ());
876         Collections.reverse (resultList);
877
878         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null);
879         return resultList.get (0);
880     }
881
882     /**
883      * Return a VNF recipe that matches a given VNF_TYPE and ACTION
884      *
885      * @param vnfType
886      * @param action
887      * @return VnfRecipe object or null if none found
888      */
889     public VnfRecipe getVnfRecipe (String vnfType, String action) {
890         StringBuilder hql = new StringBuilder ("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action ");
891
892         long startTime = System.currentTimeMillis ();
893         LOGGER.debug ("Catalog database - get VNF recipe with name " + vnfType
894                                       + " and action "
895                                       + action);
896
897         Query query = getSession ().createQuery (hql.toString ());
898         query.setParameter (VNF_TYPE, vnfType);
899         query.setParameter (ACTION, action);
900
901         @SuppressWarnings("unchecked")
902         List <VnfRecipe> resultList = query.list ();
903
904         if (resultList.isEmpty ()) {
905             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null);
906             return null;
907         }
908
909         Collections.sort (resultList, new MavenLikeVersioningComparator ());
910         Collections.reverse (resultList);
911
912         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null);
913         return resultList.get (0);
914     }
915
916     /**
917      * Return a VNF recipe that matches a given VF_MODULE_ID and ACTION
918      *
919      * @param vfModuleId
920      * @param action
921      * @return VnfRecipe object or null if none found
922      */
923     public VnfRecipe getVnfRecipeByVfModuleId (String vnfType, String vfModuleId, String action) {
924
925         StringBuilder hql = new StringBuilder ("FROM VnfRecipe WHERE vfModuleId = :vfModuleId and action = :action  ");
926
927         long startTime = System.currentTimeMillis ();
928         LOGGER.debug ("Catalog database - get VNF Recipe with vfModuleId " + vfModuleId);
929
930         Query query = getSession ().createQuery (hql.toString ());
931         query.setParameter (VF_MODULE_ID, vfModuleId);
932         query.setParameter (ACTION, action);
933
934         @SuppressWarnings("unchecked")
935         List <VnfRecipe> resultList = query.list ();
936
937         if (resultList.isEmpty ()) {
938             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe Entry not found", "CatalogDB", "getVnfRecipeByVfModuleId", null);
939             return null;
940         }
941
942         Collections.sort (resultList, new MavenLikeVersioningComparator ());
943         Collections.reverse (resultList);
944
945         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF Recipe Entry found", "CatalogDB", "getVnfRecipeByVfModuleId", null);
946         return resultList.get (0);
947     }
948
949     public VfModule getVfModuleTypeByUuid(String modelCustomizationUuid) {
950         long startTime = System.currentTimeMillis();
951         LOGGER.debug("Catalog database - get vfModuleTypeByUuid with uuid=" + modelCustomizationUuid);
952
953         String hql = "FROM VfModule WHERE modelCustomizationUuid = :modelCustomizationUuid";
954         Query query = getSession().createQuery(hql);
955         query.setParameter("modelCustomizationUuid", modelCustomizationUuid);
956
957         VfModule module = null;
958         try {
959             module = (VfModule) query.uniqueResult();
960         } catch (org.hibernate.NonUniqueResultException nure) {
961             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelCustomizationUuid='" + modelCustomizationUuid + "'");
962             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationUuid==" + modelCustomizationUuid);
963             module = null;
964         } catch (org.hibernate.HibernateException he) {
965             LOGGER.debug("Hibernate Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "'");
966             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
967             module = null;
968         } catch (Exception e) {
969             LOGGER.debug("Generic Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "'");
970             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
971             module = null;
972         }
973         if (module == null) {
974             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleTypeByUuid", null);
975         } else {
976             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleTypeByUuid", null);
977         }
978         return module;
979     }
980
981     public VfModule getVfModuleType(String type) {
982         long startTime = System.currentTimeMillis();
983         LOGGER.debug("Catalog database - get vfModuleType with type " + type);
984
985         String hql = "FROM VfModule WHERE type = :type";
986         Query query = getSession().createQuery(hql);
987         query.setParameter("type",  type);
988
989         @SuppressWarnings("unchecked")
990         List<VfModule> resultList = query.list();
991         if (resultList.isEmpty()) {
992             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF not found", "CatalogDB", "getVfModuleType", null);
993             return null;
994         }
995         Collections.sort (resultList, new MavenLikeVersioningComparator ());
996         Collections.reverse (resultList);
997
998         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
999         return resultList.get (0);
1000     }
1001
1002     public VfModule getVfModuleType(String type, String version) {
1003
1004         long startTime = System.currentTimeMillis();
1005         LOGGER.debug ("Catalog database - get vfModuleType with type " + type + " and model_version " + version);
1006
1007         String hql = "FROM VfModule WHERE type = :type and version = :version";
1008         Query query = getSession().createQuery(hql);
1009         query.setParameter ("type", type);
1010         query.setParameter ("version", version);
1011         VfModule module = null;
1012         try {
1013                 module = (VfModule) query.uniqueResult ();
1014         } catch (org.hibernate.NonUniqueResultException nure) {
1015                 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 + "'");
1016                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for type=" + type + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for type==" + type);
1017                 module = null;
1018         } catch (org.hibernate.HibernateException he) {
1019                 LOGGER.debug("Hibernate Exception - while searching for: type='" + type + "', asdc_service_model_version='" + version + "'");
1020                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for type=" + type + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for type=" + type);
1021                 module = null;
1022         } catch (Exception e) {
1023                 LOGGER.debug("Generic Exception - while searching for: type='" + type + "', asdc_service_model_version='" + version + "'");
1024                 LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for type=" + type + " and version=" + version, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for type=" + type);
1025                 module = null;
1026         }
1027         if (module == null) {
1028                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null);
1029         } else {
1030                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
1031         }
1032         return module;
1033     }
1034
1035     public VnfResource getVnfResourceByServiceUuid(String serviceModelInvariantUuid) {
1036         long startTime = System.currentTimeMillis();
1037         LOGGER.debug ("Catalog database - get vfModuleType with serviceModelInvariantUuid " + serviceModelInvariantUuid);
1038
1039         String hql = "FROM VnfResource WHERE serviceModelInvariantUuid = :serviceModelInvariantUuid";
1040         Query query = getSession().createQuery(hql);
1041         query.setParameter ("serviceModelInvariantUuid", serviceModelInvariantUuid);
1042         VnfResource vnfResource = null;
1043         try {
1044             vnfResource = (VnfResource) query.uniqueResult ();
1045         } catch (org.hibernate.NonUniqueResultException nure) {
1046             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: serviceModelInvariantUuid='" + serviceModelInvariantUuid);
1047             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for serviceModelInvariantUuid==" + serviceModelInvariantUuid);
1048             vnfResource = null;
1049         } catch (org.hibernate.HibernateException he) {
1050             LOGGER.debug("Hibernate Exception - while searching for: serviceModelInvariantUuid='" + serviceModelInvariantUuid + "'");
1051             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1052             vnfResource = null;
1053         } catch (Exception e) {
1054             LOGGER.debug("Generic Exception - while searching for: serviceModelInvariantUuid='" + serviceModelInvariantUuid + "'");
1055             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1056             vnfResource = null;
1057         }
1058         if (vnfResource == null) {
1059             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null);
1060         } else {
1061             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
1062         }
1063         return vnfResource;
1064     }
1065
1066     public VnfResource getVnfResourceByVnfUuid(String vnfResourceModelInvariantUuid) {
1067         long startTime = System.currentTimeMillis();
1068         LOGGER.debug ("Catalog database - get vfModuleType with vnfResourceModelInvariantUuid " + vnfResourceModelInvariantUuid);
1069
1070         String hql = "FROM VnfResource WHERE vnfResourceModelInvariantUuid = :vnfResourceModelInvariantUuid";
1071         Query query = getSession().createQuery(hql);
1072         query.setParameter ("vnfResourceModelInvariantUuid", vnfResourceModelInvariantUuid);
1073         VnfResource vnfResource = null;
1074         try {
1075             vnfResource = (VnfResource) query.uniqueResult ();
1076         } catch (org.hibernate.NonUniqueResultException nure) {
1077             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: vnfResourceModelInvariantUuid='" + vnfResourceModelInvariantUuid);
1078             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnfResourceModelInvariantUuid==" + vnfResourceModelInvariantUuid);
1079             vnfResource = null;
1080         } catch (org.hibernate.HibernateException he) {
1081             LOGGER.debug("Hibernate Exception - while searching for: vnfResourceModelInvariantUuid='" + vnfResourceModelInvariantUuid + "'");
1082             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid);
1083             vnfResource = null;
1084         } catch (Exception e) {
1085             LOGGER.debug("Generic Exception - while searching for: vnfResourceModelInvariantUuid='" + vnfResourceModelInvariantUuid + "'");
1086             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid);
1087             vnfResource = null;
1088         }
1089         if (vnfResource == null) {
1090             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null);
1091         } else {
1092             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null);
1093         }
1094         return vnfResource;
1095     }
1096
1097     public VnfResource getVnfResourceByType(String vnfType) {
1098         return this.getVnfResource(vnfType);
1099     }
1100
1101     public VfModule getVfModuleByModelInvariantUuid(String vfModuleModelInvariantUuid) {
1102         long startTime = System.currentTimeMillis();
1103         LOGGER.debug ("Catalog database - get vfModuleTypeByUuid with uuid " + vfModuleModelInvariantUuid);
1104
1105         String hql = "FROM VfModule WHERE vfModuleModelInvariantUuid = :vfModuleModelInvariantUuid ";
1106         Query query = getSession().createQuery(hql);
1107         query.setParameter ("vfModuleModelInvariantUuid", vfModuleModelInvariantUuid);
1108         VfModule module = null;
1109         try {
1110             module = (VfModule) query.uniqueResult ();
1111         } catch (org.hibernate.NonUniqueResultException nure) {
1112             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: vfModuleModelInvariantUuid='" + vfModuleModelInvariantUuid + "'");
1113             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vfModuleModelInvariantUuid=" + vfModuleModelInvariantUuid , "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vfModuleModelInvariantUuid==" + vfModuleModelInvariantUuid);
1114             module = null;
1115         } catch (org.hibernate.HibernateException he) {
1116             LOGGER.debug("Hibernate Exception - while searching for: vfModuleModelInvariantUuid='" + vfModuleModelInvariantUuid + "'");
1117             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for vfModuleModelInvariantUuid=" + vfModuleModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vfModuleModelInvariantUuid=" + vfModuleModelInvariantUuid);
1118             module = null;
1119         } catch (Exception e) {
1120             LOGGER.debug("Generic Exception - while searching for: vfModuleModelInvariantUuid='" + vfModuleModelInvariantUuid + "'");
1121             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for vfModuleModelInvariantUuid=" + vfModuleModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for vfModuleModelInvariantUuid=" + vfModuleModelInvariantUuid);
1122             module = null;
1123         }
1124         if (module == null) {
1125             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelInvariantUuid", null);
1126         } else {
1127             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelInvariantUuid", null);
1128         }
1129         return module;
1130     }
1131
1132     public VfModule getVfModuleByModelCustomizationUuid(String modelCustomizationUuid) {
1133         long startTime = System.currentTimeMillis();
1134         LOGGER.debug ("Catalog database - get vfModuleTypeByModelCustomizationUuid with uuid " + modelCustomizationUuid);
1135
1136         String hql = "FROM VfModule WHERE modelCustomizationUuid = :modelCustomizationUuid ";
1137         Query query = getSession().createQuery(hql);
1138         query.setParameter ("modelCustomizationUuid", modelCustomizationUuid);
1139         VfModule module = null;
1140         try {
1141             module = (VfModule) query.uniqueResult ();
1142         } catch (org.hibernate.NonUniqueResultException nure) {
1143             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: modelCustomizationUuid='" + modelCustomizationUuid + "'");
1144             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vfModuleModelInvariantUuid=" + modelCustomizationUuid , "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelCustomizationUuid==" + modelCustomizationUuid);
1145             module = null;
1146         } catch (org.hibernate.HibernateException he) {
1147             LOGGER.debug("Hibernate Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "'");
1148             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1149             module = null;
1150         } catch (Exception e) {
1151             LOGGER.debug("Generic Exception - while searching for: modelCustomizationUuid='" + modelCustomizationUuid + "'");
1152             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for modelCustomizationUuid=" + modelCustomizationUuid);
1153             module = null;
1154         }
1155         if (module == null) {
1156             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleByModelCustomizationUuid", null);
1157         } else {
1158             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleByModelCustomizationUuid", null);
1159         }
1160         return module;
1161     }
1162
1163     public VfModule getVfModuleByType(String vfModuleType) {
1164         return this.getVfModuleType(vfModuleType);
1165     }
1166
1167     public List<VfModule> getVfModulesForVnfResource(VnfResource vnfResource) {
1168         if (vnfResource == null)
1169             return null;
1170         int vnfResourceId = vnfResource.getId();
1171
1172         long startTime = System.currentTimeMillis();
1173         LOGGER.debug("Catalog database - getVfModulesForVnfResource - vnfResource: " + vnfResource.toString());
1174
1175         return this.getVfModulesForVnfResource(vnfResourceId);
1176
1177     }
1178
1179     public List<VfModule> getVfModulesForVnfResource(int vnfResourceId) {
1180         long startTime = System.currentTimeMillis();
1181         LOGGER.debug("Catalog database - getVfModulesForVnfResource - vnfResourceId: " + vnfResourceId);
1182         StringBuilder hql = new StringBuilder("FROM VfModule where vnfResourceId = :vnfResourceId");
1183         Query query = getSession().createQuery(hql.toString());
1184         query.setParameter("vnfResourceId", vnfResourceId);
1185         List<VfModule> resultList = null;
1186         try {
1187             resultList = query.list();
1188             if (resultList != null)
1189                 LOGGER.debug("\tQuery found " + resultList.size() + " records.");
1190             else
1191                 LOGGER.debug("\tQuery found no records.");
1192         } catch (org.hibernate.HibernateException he) {
1193             LOGGER.debug("Hibernate Exception - getVfModulesForVnfResource - while searching for: vnfResourceId='" + vnfResourceId + " " + he.getMessage());
1194             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception - getVfModulesForVnfResource - searching for vnfResourceId=" + vnfResourceId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfResourceId=" + vnfResourceId);
1195         } catch (Exception e) {
1196             LOGGER.debug("Exception - getVfModulesForVnfResource - while searching for: vnfResourceId='" + vnfResourceId + " " + e.getMessage());
1197             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception - getVfModulesForVnfResource - searching for vnfResourceId=" + vnfResourceId, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for vnfResourceId=" + vnfResourceId);
1198         }
1199         if (resultList == null) {
1200             resultList = new ArrayList<VfModule>();
1201         }
1202         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModulesForVnfResource", null);
1203         return resultList;
1204     }
1205
1206     public Service getServiceByUuid (String serviceModelInvariantUuid) {
1207
1208         long startTime = System.currentTimeMillis ();
1209         LOGGER.debug ("Catalog database - get service with ModelInvariantUuid " + serviceModelInvariantUuid);
1210
1211         String hql = "FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid";
1212         Query query = getSession ().createQuery (hql);
1213         query.setParameter ("serviceModelInvariantUuid", serviceModelInvariantUuid);
1214
1215         Service service = null;
1216         try {
1217             service = (Service) query.uniqueResult ();
1218         } catch (org.hibernate.NonUniqueResultException nure) {
1219             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: serviceModelInvariantUuid='" + serviceModelInvariantUuid + "'");
1220             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1221             return null;
1222         } catch (org.hibernate.HibernateException he) {
1223             LOGGER.debug("Hibernate Exception - while searching for: serviceName='" + serviceModelInvariantUuid + "'");
1224             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1225             return null;
1226         } catch (Exception e) {
1227             LOGGER.debug("Generic Exception - while searching for: serviceModelInvariantUuid='" + serviceModelInvariantUuid);
1228             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1229             return null;
1230         }
1231         if (service == null) {
1232             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getService", null);
1233         } else {
1234             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getService", null);
1235         }
1236
1237         return service;
1238     }
1239
1240     public NetworkResource getNetworkResourceById(Integer id) {
1241         long startTime = System.currentTimeMillis ();
1242         LOGGER.debug ("Catalog database - getNetworkResource with id " + id);
1243
1244         String hql = "FROM NetworkResource WHERE id = :id";
1245         Query query = getSession ().createQuery (hql);
1246         query.setParameter ("id", id);
1247
1248         NetworkResource networkResource = null;
1249         try {
1250             networkResource = (NetworkResource) query.uniqueResult ();
1251         } catch (org.hibernate.NonUniqueResultException nure) {
1252             LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: NETWORK_RESOURCE.id='" + id + "'");
1253             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for NETWORK_RESOURCE.id=" + id, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for NETWORK_RESOURCE.id=" + id);
1254             return null;
1255         } catch (org.hibernate.HibernateException he) {
1256             LOGGER.debug("Hibernate Exception - while searching for: NETWORK_RESOURCE.id='" + id + "'");
1257             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching for NETWORK_RESOURCE.id=" + id, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching for NETWORK_RESOURCE.id=" + id);
1258             return null;
1259         } catch (Exception e) {
1260             LOGGER.debug("Generic Exception - while searching for: NETWORK_RESOURCE.id='" + id);
1261             LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Generic exception searching for NETWORK_RESOURCE.id=" + id, "", "", MsoLogger.ErrorCode.DataError, "Generic exception searching for NETWORK_RESOURCE.id=" + id);
1262             return null;
1263         }
1264
1265         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResourceById", null);
1266         return networkResource;
1267
1268     }
1269
1270     // 1702 API Spec - Query for all networks in a Service:
1271     public List<NetworkResourceCustomization> getAllNetworksByServiceModelUuid(String serviceModelUuid) {
1272         long startTime = System.currentTimeMillis();
1273         LOGGER.debug("Catalog database: getServiceNetworksByServiceModelUuid - " + serviceModelUuid);
1274
1275         // This is a 2-step process (3 really) - 1) query ServiceToNetworks, 2) query NetworkResourceCustomization, 3) populate the networkType
1276
1277         StringBuilder hql1 = new StringBuilder("FROM ServiceToNetworks WHERE serviceModelUuid = :serviceModelUuid");
1278         Query query = getSession().createQuery(hql1.toString());
1279         query.setParameter("serviceModelUuid", serviceModelUuid);
1280         @SuppressWarnings("unchecked")
1281         List<ServiceToNetworks> resultList1 = query.list();
1282         if (resultList1 == null || resultList1.size() < 1) {
1283             LOGGER.debug("Found no matches to the query - FROM ServiceToNetworks WHERE serviceModelUuid = " + serviceModelUuid);
1284             return new ArrayList<NetworkResourceCustomization>();
1285         }
1286         LOGGER.debug("Found " + resultList1.size() + " entries in ServiceToNetworks with smu=" + serviceModelUuid);
1287
1288         ArrayList<NetworkResourceCustomization> masterList = new ArrayList<NetworkResourceCustomization>();
1289         for (ServiceToNetworks stn : resultList1) {
1290             String networkModelCustomizationUuid = stn.getNetworkModelCustomizationUuid();
1291             LOGGER.debug("Now searching for NetworkResourceCustomization for " + networkModelCustomizationUuid);
1292             List<NetworkResourceCustomization> resultSet = this.getAllNetworksByNetworkModelCustomizationUuid(networkModelCustomizationUuid);
1293             for (NetworkResourceCustomization nrc : resultSet) {
1294                 masterList.add(nrc);
1295             }
1296         }
1297         LOGGER.debug("Returning " + masterList.size() + " NRC records");
1298         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworksByServiceModelUuid", null);
1299         return masterList;
1300
1301     }
1302     public List<NetworkResourceCustomization> getAllNetworksByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
1303         long startTime = System.currentTimeMillis();
1304         LOGGER.debug("Catalog database: getServiceNetworksByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
1305
1306         StringBuilder hql = new StringBuilder("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
1307         Query query = getSession().createQuery(hql.toString());
1308         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1309         @SuppressWarnings("unchecked")
1310         List<Service> serviceList = query.list();
1311
1312         if (serviceList.isEmpty()) {
1313             LOGGER.debug("Could not find Service for " + serviceModelInvariantUuid);
1314             return new ArrayList<NetworkResourceCustomization>();
1315         }
1316
1317         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1318         Collections.reverse (serviceList);
1319         Service service = serviceList.get(0);
1320
1321         String serviceNameVersionId = service.getServiceNameVersionId();
1322         LOGGER.debug("The highest version for the Service " + serviceModelInvariantUuid + " is " + serviceNameVersionId);
1323
1324         // Service.serviceNameVersionId == ServiceToNetworks.serviceModelUuid
1325         return this.getAllNetworksByServiceModelUuid(serviceNameVersionId);
1326     }
1327     public List<NetworkResourceCustomization> getAllNetworksByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
1328         long startTime = System.currentTimeMillis();
1329         LOGGER.debug("Catalog database: getServiceNetworksByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
1330
1331         StringBuilder hql = new StringBuilder("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
1332         Query query = getSession().createQuery(hql.toString());
1333         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1334         query.setParameter("serviceModelVersion", serviceModelVersion);
1335
1336         //TODO
1337         //can fix this later - no time - could do a unique query here - but this should work
1338         @SuppressWarnings("unchecked")
1339         List<Service> serviceList = query.list();
1340
1341         if (serviceList.isEmpty()) {
1342             LOGGER.debug("No Service found with smu=" + serviceModelInvariantUuid + " and smv=" + serviceModelVersion);
1343             return new ArrayList<NetworkResourceCustomization>();
1344         }
1345
1346         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1347         Collections.reverse (serviceList);
1348         Service service = serviceList.get(0);
1349
1350         String serviceNameVersionId = service.getServiceNameVersionId();
1351
1352         // Service.serviceNameVersionId == ServiceToNetworks.serviceModelUuid
1353         return this.getAllNetworksByServiceModelUuid(serviceNameVersionId);
1354
1355     }
1356     public List<NetworkResourceCustomization> getAllNetworksByNetworkModelCustomizationUuid(String networkModelCustomizationUuid) {
1357         long startTime = System.currentTimeMillis();
1358         LOGGER.debug("Catalog database: getAllNetworksByNetworkModelCustomizationUuid - " + networkModelCustomizationUuid);
1359
1360         StringBuilder hql = new StringBuilder("FROM NetworkResourceCustomization WHERE modelCustomizationUuid = :networkModelCustomizationUuid");
1361         Query query = getSession().createQuery(hql.toString());
1362         query.setParameter("networkModelCustomizationUuid", networkModelCustomizationUuid);
1363
1364         @SuppressWarnings("unchecked")
1365         List<NetworkResourceCustomization> resultList = query.list();
1366
1367         this.populateNetworkResourceType(resultList);
1368
1369         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworksByNetworkModelCustomizationUuid", null);
1370         return resultList;
1371     }
1372     public List<NetworkResourceCustomization> getAllNetworksByNetworkType(String networkType) {
1373         long startTime = System.currentTimeMillis();
1374         LOGGER.debug("Catalog database: getServiceNetworksByNetworkType - " + networkType);
1375
1376         NetworkResource nr = this.getNetworkResource(networkType);
1377         if (nr == null) {
1378             return new ArrayList<NetworkResourceCustomization>();
1379         }
1380         Integer networkResourceId = nr.getId();
1381
1382         LOGGER.debug("Now searching for NRC's with networkResourceId = " + networkResourceId);
1383         StringBuilder hql = new StringBuilder("FROM NetworkResourceCustomization WHERE networkResourceId = :networkResourceId");
1384
1385         Query query = getSession().createQuery(hql.toString());
1386         query.setParameter("networkResourceId", networkResourceId);
1387
1388         @SuppressWarnings("unchecked")
1389         List<NetworkResourceCustomization> resultList = query.list();
1390
1391         if (resultList != null && resultList.size() > 0) {
1392             LOGGER.debug("Found " + resultList.size() + " results");
1393             for (NetworkResourceCustomization nrc : resultList) {
1394                 nrc.setNetworkType(networkType);
1395                 nrc.setNetworkResource(nr);
1396             }
1397         }
1398         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworksByNetworkType", null);
1399
1400         return resultList;
1401     }
1402
1403     //1702 API Spec cont'd - Query for all VnfResources in a Service:
1404     public List<VnfResource> getAllVnfsByServiceModelUuid(String serviceModelUuid) {
1405         long startTime = System.currentTimeMillis();
1406         LOGGER.debug("Catalog database: getAllVnfsByServiceModelUuid - " + serviceModelUuid);
1407
1408         StringBuilder hql = new StringBuilder("FROM Service WHERE serviceNameVersionId = :serviceModelUuid");
1409         Query query = getSession().createQuery(hql.toString());
1410         query.setParameter("serviceModelUuid", serviceModelUuid);
1411         @SuppressWarnings("unchecked")
1412         List<Service> serviceList = query.list();
1413
1414         if (serviceList.isEmpty()) {
1415             return new ArrayList<VnfResource>();
1416         }
1417
1418         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1419         Collections.reverse (serviceList);
1420         Service service = serviceList.get(0);
1421
1422         String serviceModelInvariantUuid = service.getModelInvariantUUID();
1423         String serviceModelVersion = service.getVersion();
1424
1425         return this.getAllVnfsByServiceModelInvariantUuid(serviceModelInvariantUuid, serviceModelVersion);
1426
1427     }
1428     public List<VnfResource> getAllVnfsByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
1429         long startTime = System.currentTimeMillis();
1430         LOGGER.debug("Catalog database: getAllVnfsByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
1431
1432         StringBuilder hqlService = new StringBuilder("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
1433         Query query = getSession().createQuery(hqlService.toString());
1434         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1435         @SuppressWarnings("unchecked")
1436         List<Service> resultList = query.list();
1437
1438         if (resultList.isEmpty()) {
1439             return new ArrayList<VnfResource>();
1440         }
1441         Collections.sort (resultList, new MavenLikeVersioningComparator ());
1442         Collections.reverse (resultList);
1443         Service service = resultList.get(0);
1444         //now just call the method that takes the version - the service object will have the highest version
1445         return this.getAllVnfsByServiceModelInvariantUuid(serviceModelInvariantUuid, service.getVersion());
1446     }
1447     public List<VnfResource> getAllVnfsByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
1448         long startTime = System.currentTimeMillis();
1449         LOGGER.debug("Catalog database: getAllVnfsByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
1450
1451         StringBuilder hql = new StringBuilder("FROM VnfResource WHERE serviceModelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
1452         Query query = getSession().createQuery(hql.toString());
1453         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1454         query.setParameter("serviceModelVersion", serviceModelVersion);
1455
1456         @SuppressWarnings("unchecked")
1457         List<VnfResource> resultList = query.list();
1458
1459         if (resultList.isEmpty()) {
1460             return new ArrayList<VnfResource>();
1461         }
1462         // so we have a list of VnfResource objects - but we need to add each one's VfModule objects
1463         for (VnfResource vnfResource : resultList) {
1464             List<VfModule> vfModules = this.getVfModulesForVnfResource(vnfResource);
1465             if (vfModules != null && !vfModules.isEmpty()) {
1466                 for (VfModule vfm : vfModules) {
1467                     vnfResource.addVfModule(vfm);
1468                 }
1469             }
1470         }
1471         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVnfsByServiceModelInvariantUuid", null);
1472         return resultList;
1473
1474     }
1475     public List<VnfResource> getAllVnfsByServiceName(String serviceName, String serviceVersion)  {
1476         long startTime = System.currentTimeMillis();
1477         LOGGER.debug("Catalog database: getAllVnfsByServiceName - " + serviceName + ", version=" + serviceVersion);
1478         if (serviceVersion == null || serviceVersion.equals("")) {
1479             return this.getAllVnfsByServiceName(serviceName);
1480         }
1481
1482         StringBuilder hql = new StringBuilder("FROM Service WHERE serviceName = :serviceName and version = :serviceVersion");
1483         Query query = getSession().createQuery(hql.toString());
1484         query.setParameter("serviceName", serviceName);
1485         query.setParameter("serviceVersion", serviceVersion);
1486
1487         @SuppressWarnings("unchecked")
1488         List<Service> resultList = query.list();
1489
1490         if (resultList.isEmpty()) {
1491             return null;
1492         }
1493
1494         Service service = resultList.get(0);
1495
1496         return this.getAllVnfsByServiceModelInvariantUuid(service.getModelInvariantUUID(), service.getVersion());
1497     }
1498     public List<VnfResource> getAllVnfsByServiceName(String serviceName) {
1499         long startTime = System.currentTimeMillis();
1500         LOGGER.debug("Catalog database: getAllVnfsByServiceName - " + serviceName);
1501
1502         StringBuilder hql = new StringBuilder("FROM Service WHERE serviceName = :serviceName");
1503         Query query = getSession().createQuery(hql.toString());
1504         query.setParameter("serviceName", serviceName);
1505
1506         @SuppressWarnings("unchecked")
1507         List<Service> resultList = query.list();
1508
1509         if (resultList.isEmpty()) {
1510             return null;
1511         }
1512         Collections.sort (resultList, new MavenLikeVersioningComparator ());
1513         Collections.reverse (resultList);
1514         Service service = resultList.get(0);
1515
1516         return this.getAllVnfsByServiceModelInvariantUuid(service.getModelInvariantUUID(), service.getVersion());
1517     }
1518     public List<VnfResource> getAllVnfsByVnfModelCustomizationUuid(String vnfModelCustomizationUuid) {
1519         long startTime = System.currentTimeMillis();
1520         LOGGER.debug("Catalog database: getAllVnfsByVnfModelCustomizationUuid - " + vnfModelCustomizationUuid);
1521
1522         StringBuilder hql = new StringBuilder("FROM VnfResource WHERE modelCustomizationUuid = :vnfModelCustomizationUuid");
1523         Query query = getSession().createQuery(hql.toString());
1524         query.setParameter("vnfModelCustomizationUuid", vnfModelCustomizationUuid);
1525
1526         @SuppressWarnings("unchecked")
1527         List<VnfResource> resultList = query.list();
1528
1529         if (resultList.isEmpty()) {
1530             LOGGER.debug("Found no records matching " + vnfModelCustomizationUuid);
1531             return null;
1532         }
1533         // so we have a list of VnfResource objects - but we need to add each one's VfModule objects
1534         for (VnfResource vnfResource : resultList) {
1535             LOGGER.debug("Finding vfModules for vnfResource.id=" + vnfResource.getId());
1536             List<VfModule> vfModules = this.getVfModulesForVnfResource(vnfResource);
1537             if (vfModules != null && !vfModules.isEmpty()) {
1538                 LOGGER.debug("\tFound " + vfModules.size() + " vf modules");
1539                 for (VfModule vfm : vfModules) {
1540                     vnfResource.addVfModule(vfm);
1541                 }
1542             }
1543         }
1544         LOGGER.debug("Returning " + resultList + " vnf modules");
1545         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVnfsByVnfModelCustomizationUuid", null);
1546         return resultList;
1547     }
1548
1549     //1702 API Spec cont'd - Query for all allotted resources in a Service
1550
1551     public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelUuid(String serviceModelUuid) {
1552         long startTime = System.currentTimeMillis();
1553         LOGGER.debug("Catalog database: getAllAllottedResourcesByServiceModelUuid - " + serviceModelUuid);
1554
1555         // This is a 2-step process (3 really) - 1) query ServiceToAllottedResources, 2) query AllottedResourceCustomization
1556
1557         StringBuilder hql1 = new StringBuilder("FROM ServiceToAllottedResources WHERE serviceModelUuid = :serviceModelUuid");
1558         Query query = getSession().createQuery(hql1.toString());
1559         query.setParameter("serviceModelUuid", serviceModelUuid);
1560         @SuppressWarnings("unchecked")
1561         List<ServiceToAllottedResources> resultList1 = query.list();
1562         if (resultList1 == null || resultList1.size() < 1) {
1563             LOGGER.debug("Found no matches to the query " + hql1.toString());
1564             return new ArrayList<AllottedResourceCustomization>();
1565         }
1566         LOGGER.debug("Found " + resultList1.size() + " entries in ServiceToAllottedResources with smu=" + serviceModelUuid);
1567
1568         ArrayList<AllottedResourceCustomization> masterList = new ArrayList<AllottedResourceCustomization>();
1569         for (ServiceToAllottedResources star : resultList1) {
1570             String arModelCustomizationUuid = star.getArModelCustomizationUuid();
1571             LOGGER.debug("Now searching for AllottedResourceCustomization for " + arModelCustomizationUuid);
1572             List<AllottedResourceCustomization> resultSet = this.getAllAllottedResourcesByArModelCustomizationUuid(arModelCustomizationUuid);
1573             for (AllottedResourceCustomization arc : resultSet) {
1574                 masterList.add(arc);
1575             }
1576         }
1577         LOGGER.debug("Returning " + masterList.size() + " ARC records");
1578         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllAllottedResourcesByServiceModelUuid", null);
1579         return masterList;
1580     }
1581
1582     public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
1583         long startTime = System.currentTimeMillis();
1584         LOGGER.debug("Catalog database: getAllAllottedResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
1585
1586         StringBuilder hql = new StringBuilder("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
1587         Query query = getSession().createQuery(hql.toString());
1588         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1589         @SuppressWarnings("unchecked")
1590         List<Service> serviceList = query.list();
1591
1592         if (serviceList.isEmpty()) {
1593             LOGGER.debug("Could not find Service for " + serviceModelInvariantUuid);
1594             return new ArrayList<AllottedResourceCustomization>();
1595         }
1596
1597         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1598         Collections.reverse (serviceList);
1599         Service service = serviceList.get(0);
1600
1601         String serviceNameVersionId = service.getServiceNameVersionId();
1602         LOGGER.debug("The highest version for the Service " + serviceModelInvariantUuid + " is " + serviceNameVersionId);
1603
1604         // Service.serviceNameVersionId == ServiceToAllottedResources.serviceModelUuid
1605         return this.getAllAllottedResourcesByServiceModelUuid(serviceNameVersionId);
1606     }
1607
1608     public List<AllottedResourceCustomization> getAllAllottedResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
1609         long startTime = System.currentTimeMillis();
1610         LOGGER.debug("Catalog database: getAllAllottedResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
1611
1612         StringBuilder hql = new StringBuilder("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
1613         Query query = getSession().createQuery(hql.toString());
1614         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1615         query.setParameter("serviceModelVersion", serviceModelVersion);
1616
1617         //TODO
1618         //can fix this later - no time - could do a unique query here - but this should work
1619         @SuppressWarnings("unchecked")
1620         List<Service> serviceList = query.list();
1621
1622         if (serviceList.isEmpty()) {
1623             LOGGER.debug("No Service found with smu=" + serviceModelInvariantUuid + " and smv=" + serviceModelVersion);
1624             return new ArrayList<AllottedResourceCustomization>();
1625         }
1626
1627         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1628         Collections.reverse (serviceList);
1629         Service service = serviceList.get(0);
1630
1631         String serviceNameVersionId = service.getServiceNameVersionId();
1632
1633         // Service.serviceNameVersionId == ServiceToNetworks.serviceModelUuid
1634         return this.getAllAllottedResourcesByServiceModelUuid(serviceNameVersionId);
1635     }
1636
1637     public List<AllottedResourceCustomization> getAllAllottedResourcesByArModelCustomizationUuid(String arModelCustomizationUuid) {
1638         long startTime = System.currentTimeMillis();
1639         LOGGER.debug("Catalog database: getAllAllottedResourcesByArModelCustomizationUuid - " + arModelCustomizationUuid);
1640
1641         StringBuilder hql = new StringBuilder("FROM AllottedResourceCustomization WHERE modelCustomizationUuid = :arModelCustomizationUuid");
1642         Query query = getSession().createQuery(hql.toString());
1643         query.setParameter("arModelCustomizationUuid", arModelCustomizationUuid);
1644
1645         @SuppressWarnings("unchecked")
1646         List<AllottedResourceCustomization> resultList = query.list();
1647
1648         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllAllottedResourcesByArModelCustomizationUuid", null);
1649         return resultList;
1650     }
1651
1652     //1702 API Spec cont'd - Query for all resources in a Service:
1653     public ServiceMacroHolder getAllResourcesByServiceModelUuid(String serviceModelUuid) {
1654         long startTime = System.currentTimeMillis();
1655         LOGGER.debug("Catalog database: getAllResourcesByServiceModelUuid - " + serviceModelUuid);
1656
1657         StringBuilder hql = new StringBuilder("FROM Service WHERE serviceNameVersionId = :serviceModelUuid");
1658         Query query = getSession().createQuery(hql.toString());
1659         query.setParameter("serviceModelUuid", serviceModelUuid);
1660         @SuppressWarnings("unchecked")
1661         List<Service> serviceList = query.list();
1662
1663         if (serviceList.isEmpty()) {
1664             LOGGER.debug("Unable to find a Service with serviceModelUuid=" + serviceModelUuid);
1665             return new ServiceMacroHolder();
1666         }
1667
1668         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1669         Collections.reverse (serviceList);
1670         Service service = serviceList.get(0);
1671
1672         ServiceMacroHolder smh = new ServiceMacroHolder(service);
1673         ArrayList<NetworkResourceCustomization> nrcList = (ArrayList<NetworkResourceCustomization>) this.getAllNetworksByServiceModelUuid(serviceModelUuid);
1674         smh.setNetworkResourceCustomization(nrcList);
1675         ArrayList<AllottedResourceCustomization> arcList = (ArrayList<AllottedResourceCustomization>) this.getAllAllottedResourcesByServiceModelUuid(serviceModelUuid);
1676         smh.setAllottedResourceCustomization(arcList);
1677         ArrayList<VnfResource> vnfList = (ArrayList<VnfResource>) this.getAllVnfsByServiceModelInvariantUuid(service.getModelInvariantUUID(), service.getVersion());
1678         smh.setVnfResources(vnfList);
1679
1680         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllResourcesByServiceModelUuid", null);
1681         return smh;
1682     }
1683     public ServiceMacroHolder getAllResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid) {
1684         long startTime = System.currentTimeMillis();
1685         LOGGER.debug("Catalog database: getAllResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid);
1686
1687         StringBuilder hql = new StringBuilder("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid");
1688         Query query = getSession().createQuery(hql.toString());
1689         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1690         @SuppressWarnings("unchecked")
1691         List<Service> serviceList = query.list();
1692
1693         if (serviceList.isEmpty()) {
1694             LOGGER.debug("Unable to find a Service with serviceModelInvariantUuid=" + serviceModelInvariantUuid);
1695             return new ServiceMacroHolder();
1696         }
1697
1698         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1699         Collections.reverse (serviceList);
1700         Service service = serviceList.get(0);
1701
1702         ServiceMacroHolder smh = new ServiceMacroHolder(service);
1703         ArrayList<NetworkResourceCustomization> nrcList = (ArrayList<NetworkResourceCustomization>) this.getAllNetworksByServiceModelUuid(service.getServiceNameVersionId());
1704         smh.setNetworkResourceCustomization(nrcList);
1705         ArrayList<AllottedResourceCustomization> arcList = (ArrayList<AllottedResourceCustomization>) this.getAllAllottedResourcesByServiceModelUuid(service.getServiceNameVersionId());
1706         smh.setAllottedResourceCustomization(arcList);
1707         ArrayList<VnfResource> vnfList = (ArrayList<VnfResource>) this.getAllVnfsByServiceModelInvariantUuid(service.getModelInvariantUUID(), service.getVersion());
1708         smh.setVnfResources(vnfList);
1709
1710         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllResourcesByServiceModelInvariantUuid", null);
1711         return smh;
1712
1713     }
1714     public ServiceMacroHolder getAllResourcesByServiceModelInvariantUuid(String serviceModelInvariantUuid, String serviceModelVersion) {
1715         long startTime = System.currentTimeMillis();
1716         LOGGER.debug("Catalog database: getAllResourcesByServiceModelInvariantUuid - " + serviceModelInvariantUuid + ", version=" + serviceModelVersion);
1717
1718         StringBuilder hql = new StringBuilder("FROM Service WHERE modelInvariantUUID = :serviceModelInvariantUuid and version = :serviceModelVersion");
1719         Query query = getSession().createQuery(hql.toString());
1720         query.setParameter("serviceModelInvariantUuid", serviceModelInvariantUuid);
1721         query.setParameter("serviceModelVersion", serviceModelVersion);
1722         //TODO make this a unique query
1723         @SuppressWarnings("unchecked")
1724         List<Service> serviceList = query.list();
1725
1726         if (serviceList.isEmpty()) {
1727             LOGGER.debug("Unable to find a Service with serviceModelInvariantUuid=" + serviceModelInvariantUuid + " and serviceModelVersion=" + serviceModelVersion);
1728             return new ServiceMacroHolder();
1729         }
1730
1731         Collections.sort (serviceList, new MavenLikeVersioningComparator ());
1732         Collections.reverse (serviceList);
1733         Service service = serviceList.get(0);
1734
1735         ServiceMacroHolder smh = new ServiceMacroHolder(service);
1736         ArrayList<NetworkResourceCustomization> nrcList = (ArrayList<NetworkResourceCustomization>) this.getAllNetworksByServiceModelUuid(service.getServiceNameVersionId());
1737         smh.setNetworkResourceCustomization(nrcList);
1738         ArrayList<AllottedResourceCustomization> arcList = (ArrayList<AllottedResourceCustomization>) this.getAllAllottedResourcesByServiceModelUuid(service.getServiceNameVersionId());
1739         smh.setAllottedResourceCustomization(arcList);
1740         ArrayList<VnfResource> vnfList = (ArrayList<VnfResource>) this.getAllVnfsByServiceModelInvariantUuid(service.getModelInvariantUUID(), service.getVersion());
1741         smh.setVnfResources(vnfList);
1742
1743         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllResourcesByServiceModelUuid with version", null);
1744         return smh;
1745     }
1746
1747     // 1707 New API queries
1748     public NetworkResourceCustomization getSingleNetworkByModelCustomizationUuid(String modelCustomizationUuid) {
1749         long startTime = System.currentTimeMillis();
1750         LOGGER.debug("Catalog database; getSingleNetworkByModelCustomizationUuid - " + modelCustomizationUuid);
1751         List<NetworkResourceCustomization> resultList = this.getAllNetworksByNetworkModelCustomizationUuid(modelCustomizationUuid);
1752         if (resultList == null || resultList.size() < 1) {
1753             return null;
1754         }
1755         return resultList.get(0);
1756     }
1757     public AllottedResourceCustomization getSingleAllottedResourceByModelCustomizationUuid(String modelCustomizationUuid) {
1758         long startTime = System.currentTimeMillis();
1759         LOGGER.debug("Catalog database; getSingleAllottedResourceByModelCustomizationUuid - " + modelCustomizationUuid);
1760         List<AllottedResourceCustomization> resultList = this.getAllAllottedResourcesByArModelCustomizationUuid(modelCustomizationUuid);
1761         if (resultList == null || resultList.size() < 1) {
1762             return null;
1763         }
1764         return resultList.get(0);
1765     }
1766     public VnfResource getSingleVnfResourceByModelCustomizationUuid(String modelCustomizationUuid) {
1767         long startTime = System.currentTimeMillis();
1768         LOGGER.debug("Catalog database; getSingleVnfResourceByModelCustomizationUuid - " + modelCustomizationUuid);
1769         List<VnfResource> resultList = this.getAllVnfsByVnfModelCustomizationUuid(modelCustomizationUuid);
1770         if (resultList == null || resultList.size() < 1) {
1771             return null;
1772         }
1773         return resultList.get(0);
1774     }
1775
1776     private void populateNetworkResourceType(List<NetworkResourceCustomization> resultList) {
1777         HashMap<Integer, NetworkResource> networkResources = new HashMap<Integer, NetworkResource>();
1778
1779         for (NetworkResourceCustomization nrc : resultList) {
1780             Integer network_id = nrc.getNetworkResourceId();
1781             if (network_id == null) {
1782                 nrc.setNetworkResource(null);
1783                 nrc.setNetworkType("UNKNOWN_NETWORK_ID_NULL");
1784                 continue;
1785             }
1786             if (networkResources.containsKey(network_id)) {
1787                 nrc.setNetworkResource(networkResources.get(network_id));
1788                 nrc.setNetworkType(networkResources.get(network_id).getNetworkType());
1789             } else {
1790                 NetworkResource nr = this.getNetworkResourceById(network_id);
1791                 if (nr == null) {
1792                     nrc.setNetworkType("INVALID_NETWORK_TYPE_ID_NOT_FOUND");
1793                 } else {
1794                     nrc.setNetworkType(nr.getNetworkType());
1795                     nrc.setNetworkResource(nr);
1796                     networkResources.put(network_id, nr);
1797                 }
1798             }
1799         }
1800     }
1801
1802     /**
1803      * Return a VNF recipe that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME, and ACTION
1804      * first query VF_MODULE table by type, and then use the ID to query
1805      * VNF_RECIPE by VF_MODULE_ID and ACTION
1806      *
1807      * @param vnfType
1808      * @parm vfModuleModelName
1809      * @param action
1810      * @return VnfRecipe object or null if none found
1811      */
1812     public VnfRecipe getVfModuleRecipe (String vnfType, String vfModuleModelName, String action) {
1813         String vfModuleType = vnfType + "::" + vfModuleModelName;
1814
1815         StringBuilder hql = new StringBuilder ("FROM VfModule WHERE type = :type ");
1816
1817         long startTime = System.currentTimeMillis ();
1818         LOGGER.debug ("Catalog database - get VF MODULE  with type " + vfModuleType);
1819
1820         Query query = getSession ().createQuery (hql.toString ());
1821         query.setParameter (TYPE, vfModuleType);
1822
1823         @SuppressWarnings("unchecked")
1824         List <VfModule> resultList = query.list ();
1825
1826         if (resultList.isEmpty ()) {
1827             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF Module Entry not found", "CatalogDB", "getVfModuleRecipe", null);
1828             return null;
1829         }
1830
1831         Collections.sort (resultList, new MavenLikeVersioningComparator ());
1832         Collections.reverse (resultList);
1833
1834         VfModule vfMod = resultList.get(0);
1835
1836         int id = vfMod.getId();
1837         String vfModuleId = Integer.toString(id);
1838
1839         StringBuilder hql1 = new StringBuilder ("FROM VnfRecipe WHERE vfModuleId = :vfModuleId AND action = :action ");
1840
1841         LOGGER.debug ("Catalog database - get VNF recipe with vf module id " + vfModuleId
1842                                       + " and action "
1843                                       + action);
1844
1845         Query query1 = getSession ().createQuery (hql1.toString ());
1846         query1.setParameter (VF_MODULE_ID, vfModuleId);
1847         query1.setParameter (ACTION, action);
1848
1849         @SuppressWarnings("unchecked")
1850         List <VnfRecipe> resultList1 = query1.list ();
1851
1852         if (resultList1.isEmpty ()) {
1853             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVfModuleRecipe", null);
1854             return null;
1855         }
1856
1857         Collections.sort (resultList1, new MavenLikeVersioningComparator ());
1858         Collections.reverse (resultList1);
1859
1860         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe found", "CatalogDB", "getVfModuleRecipe", null);
1861         return resultList1.get (0);
1862     }
1863
1864     /**
1865      * Return a VNF Module List that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME,
1866      * ASDC_SERVICE_MODEL_VERSION, MODEL_VERSION, and ACTION
1867      *
1868      * @param vnfModuleType
1869      * @parm modelCustomizationUuid
1870      * @param asdcServiceModelVersion
1871      * @param modelVersion
1872      * @param action
1873      * @return VfModule list
1874      */
1875     public List<VfModule> getVfModule (String vfModuleType, String modelCustomizationUuid, String asdcServiceModelVersion, String modelVersion, String action) {
1876         StringBuilder hql;
1877         Query query;
1878         if(modelCustomizationUuid != null){
1879             hql = new StringBuilder ("FROM VfModule WHERE modelCustomizationUuid = :modelCustomizationUuid AND version = :version");
1880
1881             LOGGER.debug ("Catalog database - get VF MODULE  with type " + vfModuleType + ", asdcServiceModelVersion " + asdcServiceModelVersion + ", modelVersion " + modelVersion);
1882
1883             query = getSession ().createQuery (hql.toString ());
1884             query.setParameter ("modelCustomizationUuid", modelCustomizationUuid);
1885             query.setParameter ("version", asdcServiceModelVersion);
1886         }else{
1887             hql = new StringBuilder ("FROM VfModule WHERE type = :type AND version = :version AND modelVersion = :modelVersion");
1888
1889             LOGGER.debug ("Catalog database - get VF MODULE  with type " + vfModuleType + ", asdcServiceModelVersion " + asdcServiceModelVersion + ", modelVersion " + modelVersion);
1890
1891             query = getSession ().createQuery (hql.toString ());
1892             query.setParameter (TYPE, vfModuleType);
1893             query.setParameter ("version", asdcServiceModelVersion);
1894             query.setParameter ("modelVersion", modelVersion);
1895         }
1896
1897         @SuppressWarnings("unchecked")
1898         List <VfModule> resultList = query.list ();
1899         return resultList;
1900     }
1901
1902     /**
1903      * Return a VNF COMPONENTSrecipe that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME,
1904      * ASDC_SERVICE_MODEL_VERSION, MODEL_VERSION, and ACTION
1905      * first query VF_MODULE table by type, and then use the ID to query
1906      * VNF_COMPONENTS_RECIPE by VF_MODULE_ID and ACTION
1907      *
1908      * @param vnfType
1909      * @parm vfModuleModelName
1910      * @param action
1911      * @return VnfRecipe object or null if none found
1912      */
1913     public VnfComponentsRecipe getVnfComponentsRecipe (String vnfType, String vfModuleModelName, String modelCustomizationUuid, String asdcServiceModelVersion, String modelVersion, String action) {
1914         String vfModuleType = vnfType + "::" + vfModuleModelName;
1915         long startTime = System.currentTimeMillis ();
1916         List <VfModule> resultList = getVfModule(vfModuleType, modelCustomizationUuid,  asdcServiceModelVersion,  modelVersion,  action);
1917
1918         if (resultList.isEmpty ()) {
1919             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF Module Entry not found", "CatalogDB", "getVnfComponentsRecipe", null);
1920             return null;
1921         }
1922
1923         Collections.sort (resultList, new MavenLikeVersioningComparator ());
1924         Collections.reverse (resultList);
1925
1926         VfModule vfMod = resultList.get(0);
1927
1928         int id = vfMod.getId();
1929         String vfModuleId = Integer.toString(id);
1930
1931         StringBuilder hql1 = new StringBuilder ("FROM VnfComponentsRecipe WHERE vfModuleId = :vfModuleId AND action = :action ");
1932
1933         LOGGER.debug ("Catalog database - get Vnf Components recipe with vf module id " + vfModuleId
1934                 + " and action "
1935                 + action);
1936
1937         Query query1 = getSession ().createQuery (hql1.toString ());
1938         query1.setParameter (VF_MODULE_ID, vfModuleId);
1939         query1.setParameter (ACTION, action);
1940
1941         @SuppressWarnings("unchecked")
1942         List <VnfComponentsRecipe> resultList1 = query1.list ();
1943
1944         if (resultList1.isEmpty ()) {
1945             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfComponentsRecipe", null);
1946             return null;
1947         }
1948
1949         Collections.sort (resultList1, new MavenLikeVersioningComparator ());
1950         Collections.reverse (resultList1);
1951
1952         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe found", "CatalogDB", "getVnfComponentsRecipe", null);
1953         if (resultList1.size() > 1 && (!resultList1. get (0).getOrchestrationUri().equals(resultList1.get (1).getOrchestrationUri ()))) {
1954             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);
1955             return null;
1956         }
1957         return resultList1.get (0);
1958     }
1959
1960     /**
1961      * Return a VNF COMPONENTSrecipe that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME,
1962      * ASDC_SERVICE_MODEL_VERSION, MODEL_VERSION, and ACTION
1963      * first query VF_MODULE table by type, and then use the ID to query
1964      * VNF_COMPONENTS_RECIPE by VF_MODULE_ID and ACTION
1965      *
1966      * @param vnfType
1967      * @parm vfModuleModelName
1968      * @param action
1969      * @return VnfRecipe object or null if none found
1970      */
1971     public VnfComponentsRecipe getVnfComponentsRecipeByVfModule(List <VfModule> resultList,  String action) {
1972         long startTime = System.currentTimeMillis ();
1973
1974         if (resultList.isEmpty ()) {
1975             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF Module Entry not found", "CatalogDB", "getVnfComponentsRecipe", null);
1976             return null;
1977         }
1978
1979         Collections.sort (resultList, new MavenLikeVersioningComparator ());
1980         Collections.reverse (resultList);
1981
1982         VfModule vfMod = resultList.get(0);
1983
1984         int id = vfMod.getId();
1985         String vfModuleId = Integer.toString(id);
1986
1987         StringBuilder hql1 = new StringBuilder ("FROM VnfComponentsRecipe WHERE vfModuleId = :vfModuleId AND action = :action ");
1988
1989         LOGGER.debug ("Catalog database - get Vnf Components recipe with vf module id " + vfModuleId
1990                                       + " and action "
1991                                       + action);
1992
1993         Query query1 = getSession ().createQuery (hql1.toString ());
1994         query1.setParameter (VF_MODULE_ID, vfModuleId);
1995         query1.setParameter (ACTION, action);
1996
1997         @SuppressWarnings("unchecked")
1998         List <VnfComponentsRecipe> resultList1 = query1.list ();
1999
2000         if (resultList1.isEmpty ()) {
2001             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfComponentsRecipe", null);
2002             return null;
2003         }
2004
2005         Collections.sort (resultList1, new MavenLikeVersioningComparator ());
2006         Collections.reverse (resultList1);
2007
2008         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe found", "CatalogDB", "getVnfComponentsRecipe", null);
2009         if (resultList1.size() > 1 && (!resultList1. get (0).getOrchestrationUri().equals(resultList1.get (1).getOrchestrationUri ()))) {
2010             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);
2011             return null;
2012         }
2013         return resultList1.get (0);
2014     }
2015
2016
2017     /**
2018      * Return all VNF Resources in the Catalog DB
2019      *
2020      * @return A list of VnfResource objects
2021      */
2022     @SuppressWarnings("unchecked")
2023     public List <VnfResource> getAllVnfResources () {
2024
2025         long startTime = System.currentTimeMillis ();
2026         LOGGER.debug ("Catalog database - get all VNF resources");
2027
2028         String hql = "FROM VnfResource";
2029         Query query = getSession ().createQuery (hql);
2030
2031         List <VnfResource> result = query.list ();
2032         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVnfResources", null);
2033         return result;
2034     }
2035
2036     /**
2037      * Return VNF Resources in the Catalog DB that match a given VNF role
2038      *
2039      * @return A list of VnfResource objects
2040      */
2041     @SuppressWarnings("unchecked")
2042     public List <VnfResource> getVnfResourcesByRole (String vnfRole) {
2043
2044         long startTime = System.currentTimeMillis ();
2045         LOGGER.debug ("Catalog database - get all VNF resources for role " + vnfRole);
2046
2047         String hql = "FROM VnfResource WHERE vnfRole = :vnfRole";
2048         Query query = getSession ().createQuery (hql);
2049         query.setParameter ("vnfRole", vnfRole);
2050
2051         List <VnfResource> resources = query.list ();
2052         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfResourcesByRole", null);
2053         return resources;
2054     }
2055
2056     /**
2057      * Return all Network Resources in the Catalog DB
2058      *
2059      * @return A list of NetworkResource objects
2060      */
2061     @SuppressWarnings("unchecked")
2062     public List <NetworkResource> getAllNetworkResources () {
2063
2064         long startTime = System.currentTimeMillis ();
2065         LOGGER.debug ("Catalog database - get all network resources");
2066
2067         String hql = "FROM NetworkResource";
2068         Query query = getSession ().createQuery (hql);
2069
2070         List <NetworkResource> result = query.list ();
2071         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllNetworkResources", null);
2072         return result;
2073     }
2074
2075     /**
2076      * Return all VF Modules in the Catalog DB
2077      *
2078      * @return A list of VfModule objects
2079      */
2080     @SuppressWarnings("unchecked")
2081     public List <VfModule> getAllVfModules () {
2082
2083         long startTime = System.currentTimeMillis ();
2084         LOGGER.debug ("Catalog database - get all vf modules");
2085
2086         String hql = "FROM VfModule";
2087         Query query = getSession ().createQuery (hql);
2088
2089         List <VfModule> result = query.list ();
2090         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllVfModules", null);
2091         return result;
2092     }
2093
2094     /**
2095      * Return all HeatEnvironment in the Catalog DB
2096      *
2097      * @return A list of HeatEnvironment objects
2098      */
2099     @SuppressWarnings("unchecked")
2100     public List <HeatEnvironment> getAllHeatEnvironment () {
2101
2102         long startTime = System.currentTimeMillis ();
2103         LOGGER.debug ("Catalog database - get all Heat environments");
2104
2105         String hql = "FROM HeatEnvironment";
2106         Query query = getSession ().createQuery (hql);
2107
2108         List <HeatEnvironment> result = query.list ();
2109         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getAllHeatEnvironment", null);
2110         return result;
2111     }
2112
2113     /**
2114      * Fetch the Environment by Environment ID - 1510
2115      */
2116     public HeatEnvironment getHeatEnvironment (int id) {
2117
2118         long startTime = System.currentTimeMillis ();
2119         LOGGER.debug ("Catalog database - get Heat environment with id " + id);
2120
2121         String hql = "FROM HeatEnvironment WHERE id = :idValue";
2122
2123         LOGGER.debug ("getHeatEnvironment called with id=" + id);
2124
2125         Query query = getSession ().createQuery (hql);
2126         query.setParameter ("idValue", id);
2127
2128         @SuppressWarnings("unchecked")
2129         List <HeatEnvironment> resultList = query.list ();
2130
2131         // See if something came back.
2132         if (resultList.isEmpty ()) {
2133             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Heat environment not found", "CatalogDB", "getHeatEnvironment", null);
2134             return null;
2135         }
2136         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatEnvironment", null);
2137         return resultList.get (0);
2138     }
2139
2140     /**
2141      * Fetch the nested templates - 1510
2142      */
2143
2144     public Map <String, Object> getNestedTemplates (int templateId) {
2145         Map <String, Object> nestedTemplates = null;
2146         long startTime = System.currentTimeMillis ();
2147         LOGGER.debug ("Catalog database - getNestedTemplates called with templateId " + templateId);
2148
2149         String hql = "FROM HeatNestedTemplate where parent_template_id = :parentIdValue";
2150
2151         Query query = getSession ().createQuery (hql);
2152         query.setParameter ("parentIdValue", templateId);
2153
2154         @SuppressWarnings("unchecked")
2155         List <HeatNestedTemplate> resultList = query.list ();
2156         // If nothing comes back, there are no nested templates
2157         if (resultList.isEmpty ()) {
2158             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No nestedTemplate found", "CatalogDB", "getNestedTemplates", null);
2159             LOGGER.debug ("No nestedTemplates found for templateId=" + templateId + ", " + hql);
2160             return null;
2161         }
2162         // Now, for each entry in NESTED_HEAT_TEMPLATES, we need to grab the template body from HEAT_TEMPLATE
2163         nestedTemplates = new HashMap <String, Object> ();
2164         for (HeatNestedTemplate hnt : resultList) {
2165             LOGGER.debug ("Querying for " + hnt);
2166             HeatTemplate ht = this.getHeatTemplate (hnt.getChildTemplateId ());
2167             if (ht == null) {
2168                 LOGGER.debug ("No template found matching childTemplateId=" + hnt.getChildTemplateId ());
2169                 continue;
2170             }
2171             String providerResourceFile = hnt.getProviderResourceFile ();
2172             String heatTemplateBody = ht.getTemplateBody ();
2173             if (providerResourceFile != null && heatTemplateBody != null) {
2174                 nestedTemplates.put (providerResourceFile, heatTemplateBody);
2175             } else {
2176                 LOGGER.debug ("providerResourceFile or heatTemplateBody were null - do not add to HashMap!");
2177             }
2178         }
2179         // Make sure we're not returning an empty map - if so, just return null
2180         if (nestedTemplates.isEmpty ()) {
2181             LOGGER.debug ("nestedTemplates is empty - just return null");
2182             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Nested template is empty", "CatalogDB", "getNestedTemplate", null);
2183             return null;
2184         }
2185         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNestedTemplate", null);
2186         return nestedTemplates;
2187     }
2188
2189     /*
2190      * Fetch any files in the HEAT_FILES table 1510
2191      */
2192     public Map <String, HeatFiles> getHeatFiles (int vnfResourceId) {
2193        Map <String, HeatFiles> heatFiles = null;
2194
2195         long startTime = System.currentTimeMillis ();
2196         LOGGER.debug ("Catalog database - getHeatFiles called with vnfResourceId " + vnfResourceId);
2197         String hql = "FROM HeatFiles where vnf_resource_id = :vnfResourceIdValue";
2198
2199         Query query = getSession ().createQuery (hql);
2200         query.setParameter ("vnfResourceIdValue", vnfResourceId);
2201
2202         @SuppressWarnings("unchecked")
2203         List <HeatFiles> resultList = query.list ();
2204         // If nothing comes back, there are no heat files
2205         if (resultList.isEmpty ()) {
2206             LOGGER.debug ("No heatFiles found for vnfResourceId=" + vnfResourceId);
2207             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No heat files", "CatalogDB", "getHeatFiles", null);
2208             return null;
2209         }
2210         // Now, we just need to return a HashMap (key=fileName, object=fileBody)
2211         heatFiles = new HashMap <String, HeatFiles> ();
2212         for (HeatFiles hf : resultList) {
2213             LOGGER.debug ("Adding " + hf.getFileName () + "->" + hf.getFileBody ());
2214             heatFiles.put (hf.getFileName (), hf);
2215         }
2216         // Make sure we're not returning an empty map - if so, just return null
2217         if (heatFiles.isEmpty ()) {
2218             LOGGER.debug ("heatFiles is empty - just return null");
2219             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Heat files is empty", "CatalogDB", "getHeatFiles", null);
2220             return null;
2221         }
2222         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFiles", null);
2223         return heatFiles;
2224     }
2225
2226     // New 1607 - with modularization, use new table to determine which HEAT_FILES entries to attach
2227
2228     public Map <String, HeatFiles> getHeatFilesForVfModule(int vfModuleId) {
2229         Map <String, HeatFiles> heatFiles = null;
2230
2231         long startTime = System.currentTimeMillis ();
2232         LOGGER.debug ("Catalog database - getHeatFilesForVfModule called with vfModuleId " + vfModuleId);
2233         String hql = "FROM VfModuleToHeatFiles where vf_module_id = :vfModuleIdValue";
2234
2235         Query query = getSession ().createQuery (hql);
2236         query.setParameter ("vfModuleIdValue", vfModuleId);
2237
2238         List<VfModuleToHeatFiles> mapList = query.list();
2239         if (mapList.isEmpty()) {
2240             LOGGER.debug ("No heatFiles found for vfModuleId=" + vfModuleId);
2241             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. No heatfiles found for vfModule", "CatalogDB", "getHeatFilesForVfModule", null);
2242             return null;
2243         }
2244         //Now the fun part - we have a list of the heat files we need to get - could clean this up with a join
2245         //TODO - convert this all with one join - brute force for now due to time
2246         heatFiles = new HashMap<String, HeatFiles>();
2247         for (VfModuleToHeatFiles vmthf : mapList) {
2248                 int heatFilesId = vmthf.getHeatFilesId();
2249                 hql = "FROM HeatFiles where id = :id_value";
2250                 query = getSession().createQuery(hql);
2251                 query.setParameter("id_value", heatFilesId);
2252                 List<HeatFiles> fileList = query.list();
2253                 if (fileList.isEmpty()) {
2254                         // Should this throw an exception??
2255                         LOGGER.debug("Unable to find a HEAT_FILES entry at " + heatFilesId);
2256                 String errorString = "_ERROR|" + heatFilesId;
2257                         // The receiving code needs to know to throw an exception for this - or ignore it.
2258                         heatFiles.put(errorString, null);
2259                 } else {
2260                         // Should only ever have 1 result - add it to our Map
2261                         LOGGER.debug("Retrieved " + fileList.size() + " heat file entry at " + heatFilesId);
2262                         for (HeatFiles hf : fileList) {
2263                                 LOGGER.debug("Adding " + hf.getFileName() + "->" + hf.getFileBody());
2264                                 heatFiles.put(hf.getFileName(), hf);
2265                         }
2266                 }
2267         }
2268         if (heatFiles.isEmpty()) {
2269             LOGGER.debug ("heatFiles is empty - just return null");
2270             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. HeatFiles is empty", "CatalogDB", "getHeatFilesForVfModule", null);
2271             return null;
2272         }
2273         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFilesForVfModule", null);
2274         return heatFiles;
2275     }
2276
2277
2278
2279     /**
2280      * Get the heat template object based on asdc attributes
2281      *
2282      * @param templateName The template name, generally the yaml filename. "example.yaml"
2283      * @param version The version as specified by ASDC. "1.1"
2284      * @param asdcResourceName The ASDC resource name provided in the ASDC artifact
2285      *
2286      * @return The HeatTemplate
2287      */
2288     public HeatTemplate getHeatTemplate (String templateName, String version, String asdcResourceName) {
2289
2290         long startTime = System.currentTimeMillis ();
2291         LOGGER.debug ("Catalog database - getHeatTemplate with name " + templateName
2292                                       + " and version "
2293                                       + version
2294                                       + " and ASDC resource name "
2295                                       + asdcResourceName);
2296
2297         String hql = "FROM HeatTemplate WHERE templateName = :template_name AND version = :version AND asdcResourceName = :asdcResourceName";
2298         Query query = getSession ().createQuery (hql);
2299         query.setParameter ("template_name", templateName);
2300         query.setParameter ("version", version);
2301         query.setParameter ("asdcResourceName", asdcResourceName);
2302
2303         @SuppressWarnings("unchecked")
2304         List <HeatTemplate> resultList = query.list ();
2305
2306         // See if something came back.
2307         if (resultList.isEmpty ()) {
2308             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Heat template not found", "CatalogDB", "getHeatTemplate", null);
2309             return null;
2310         }
2311         // Name + Version is unique, so should only be one element
2312         LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
2313         return resultList.get (0);
2314     }
2315
2316     /**
2317      * Save the Heat Template
2318      *
2319      * @param heat The heat template
2320      * @param paramSet The list of heat template parameters
2321      */
2322     public void saveHeatTemplate (HeatTemplate heat, Set <HeatTemplateParam> paramSet) {
2323
2324         long startTime = System.currentTimeMillis ();
2325         LOGGER.debug ("Catalog database - save Heat Template with name " + heat.getTemplateName());
2326
2327         heat.setParameters(null);
2328         try {
2329             HeatTemplate heatTemp = this.getHeatTemplate (heat.getTemplateName (),
2330                                                           heat.getVersion (),
2331                                                           heat.getAsdcResourceName ());
2332             if (heatTemp == null) {
2333                 this.getSession ().save (heat);
2334
2335                 if (paramSet != null) {
2336                     for (HeatTemplateParam param : paramSet) {
2337                         param.setHeatTemplateId (heat.getId ());
2338                     }
2339                     heat.setParameters (paramSet);
2340                     this.getSession ().merge (heat);
2341                 }
2342
2343             } else {
2344                 heat.setId(heatTemp.getId());
2345             }
2346         } finally {
2347                 heat.setParameters(paramSet);
2348             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatTemplate", null);
2349         }
2350     }
2351
2352     /**
2353      * Retrieves a Heat environment from DB based on its unique key.
2354      *
2355      * @param name the environment artifact name
2356      * @param version the environment resource version
2357      * @param asdcResourceName the environment resource name
2358      * @return the heat environment from DB or null if not found
2359      */
2360     public HeatEnvironment getHeatEnvironment (String name, String version, String asdcResourceName) {
2361         long startTime = System.currentTimeMillis ();
2362         LOGGER.debug ("Catalog database - get Heat environment with name " + name
2363                                       + " and version "
2364                                       + version
2365                                       + " and ASDC resource name "
2366                                       + asdcResourceName);
2367
2368         String hql = "FROM HeatEnvironment WHERE name=:name AND version=:version AND asdcResourceName=:asdcResourceName";
2369         Query query = getSession ().createQuery (hql);
2370         query.setParameter ("name", name);
2371         query.setParameter ("version", version);
2372         query.setParameter ("asdcResourceName", asdcResourceName);
2373         HeatEnvironment env = null;
2374         try {
2375                 env = (HeatEnvironment) query.uniqueResult ();
2376         } catch (org.hibernate.NonUniqueResultException nure) {
2377                 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);
2378                 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);
2379                 env = null;
2380         } catch (org.hibernate.HibernateException he) {
2381                 LOGGER.debug("Hibernate Exception - while searching for: envName='" + name + "', asdc_service_model_version='" + version + "' and asdcResourceName=" + asdcResourceName);
2382                 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);
2383                 env = null;
2384         } catch (Exception e) {
2385                 LOGGER.debug("Generic Exception - while searching for: envName='" + name + "', asdc_service_model_version='" + version + "' and asdcResourceName=" + asdcResourceName);
2386                 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);
2387                 env = null;
2388         }
2389         if (env == null) {
2390                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getHeatTemplate", null);
2391         } else {
2392                 LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatTemplate", null);
2393         }
2394         return env;
2395     }
2396
2397     /**
2398      * Save the HeatEnvironment
2399      *
2400      * @param env The Environment
2401      */
2402     public void saveHeatEnvironment (HeatEnvironment env) {
2403         long startTime = System.currentTimeMillis ();
2404         LOGGER.debug ("Catalog database - save Heat environment with name "
2405                                       + env.getEnvironment());
2406         try {
2407             HeatEnvironment dbEnv = getHeatEnvironment (env.getName (), env.getVersion (), env.getAsdcResourceName ());
2408             if (dbEnv == null) {
2409
2410                 this.getSession ().save (env);
2411
2412             } else {
2413                 env.setId(dbEnv.getId());
2414             }
2415
2416         } finally {
2417             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatTemplate", null);
2418         }
2419     }
2420
2421     /**
2422      * Save the heatTemplate
2423      *
2424      * @param heat The heat template
2425      */
2426     public void saveHeatTemplate (HeatTemplate heat) {
2427         long startTime = System.currentTimeMillis ();
2428         LOGGER.debug ("Catalog database - save Heat template with name " + heat.getTemplateName ());
2429         try {
2430             this.getSession ().update (heat);
2431         } finally {
2432             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatTemplate", null);
2433         }
2434     }
2435
2436     public void saveHeatFile (HeatFiles heatFile) {
2437         long startTime = System.currentTimeMillis ();
2438         LOGGER.debug ("Catalog database - save Heat file with name " + heatFile.getFileName ());
2439         try {
2440             this.getSession ().save (heatFile);
2441         } finally {
2442             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatFile", null);
2443         }
2444     }
2445
2446     public void saveVnfRecipe (VnfRecipe vnfRecipe) {
2447         long startTime = System.currentTimeMillis ();
2448         LOGGER.debug ("Catalog database - save VNF recipe with VNF type " + vnfRecipe.getVnfType ());
2449         try {
2450             this.getSession ().save (vnfRecipe);
2451         } finally {
2452             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVnfRecipe", null);
2453         }
2454     }
2455
2456     public void saveVnfComponentsRecipe (VnfComponentsRecipe vnfComponentsRecipe) {
2457         long startTime = System.currentTimeMillis ();
2458         LOGGER.debug ("Catalog database - save VNF Component recipe with VNF type " + vnfComponentsRecipe.getVnfType ());
2459         try {
2460             this.getSession ().save (vnfComponentsRecipe);
2461         } finally {
2462             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVnfComponentsRecipe", null);
2463         }
2464     }
2465
2466
2467     public void saveOrUpdateVnfResource (VnfResource vnfResource) {
2468         long startTime = System.currentTimeMillis ();
2469         LOGGER.debug ("Catalog database - save VNF Resource with VNF type " + vnfResource.getVnfType ());
2470         try {
2471
2472             if (vnfResource.getId() != 0) {
2473                 this.getSession ().merge (vnfResource);
2474             } else {
2475                 this.getSession ().save (vnfResource);
2476             }
2477
2478         } finally {
2479             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateVnfResource", null);
2480         }
2481     }
2482
2483     public void saveAllottedResourceCustomization (AllottedResourceCustomization resourceCustomization) {
2484         long startTime = System.currentTimeMillis ();
2485         LOGGER.debug ("Catalog database - save Allotted Resource with Name " + resourceCustomization.getModelName());
2486         try {
2487             List<AllottedResourceCustomization> allottedResourcesList = getAllAllottedResourcesByArModelCustomizationUuid(resourceCustomization.getModelCustomizationUuid());
2488
2489             if(allottedResourcesList.size() == 0){
2490                 this.getSession ().save(resourceCustomization);
2491             }
2492
2493         } finally {
2494             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateAllottedResourceCustomization", null);
2495         }
2496     }
2497
2498     public void saveNetworkResourceCustomization (NetworkResourceCustomization networkResourceCustomization) throws RecordNotFoundException {
2499         long startTime = System.currentTimeMillis ();
2500         LOGGER.debug ("Catalog database - save Network Resource Customization with Network Name " + networkResourceCustomization.getModelName());
2501         try {
2502             // Check if NetworkResourceCustomzation record already exists.  If so, skip saving it.
2503             List<NetworkResourceCustomization> networkResourceCustomizationList = getAllNetworksByNetworkModelCustomizationUuid(networkResourceCustomization.getModelCustomizationUuid());
2504             // Do any matching customization records exist?
2505             if(networkResourceCustomizationList.size() == 0){
2506
2507                 // Retreive the record from the Network_Resource table associated to the Customization record based on ModelName
2508                 NetworkResource networkResource = getNetworkResource(networkResourceCustomization.getModelName());
2509
2510                 if(networkResource == null){
2511                     throw new RecordNotFoundException("No record found in NETWORK_RESOURCE table for model name " + networkResourceCustomization.getModelName());
2512                 }
2513
2514                 networkResourceCustomization.setNetworkResourceId(networkResource.getId());
2515
2516                 this.getSession ().save(networkResourceCustomization);
2517             }
2518
2519
2520         } finally {
2521             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNetworkResourceCustomization", null);
2522         }
2523     }
2524
2525     public void saveServiceToNetworks (ServiceToNetworks serviceToNetworks) {
2526         long startTime = System.currentTimeMillis ();
2527         LOGGER.debug ("Catalog database - save to ServiceToNetworks table with NetworkModelCustomizationUUID of " + serviceToNetworks.getNetworkModelCustomizationUuid() + " and ServiceModelUUID of " + serviceToNetworks.getServiceModelUuid());
2528         try {
2529             this.getSession ().save(serviceToNetworks);
2530
2531         } finally {
2532             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNetworkResourceCustomization", null);
2533         }
2534     }
2535
2536     public void saveServiceToAllottedResources (ServiceToAllottedResources serviceToAllottedResources) {
2537         long startTime = System.currentTimeMillis ();
2538         LOGGER.debug ("Catalog database - save to serviceToAllottedResources table with ARModelCustomizationUUID of " + serviceToAllottedResources.getArModelCustomizationUuid() + " and ServiceModelUUID of " + serviceToAllottedResources.getServiceModelUuid());
2539         try {
2540             this.getSession ().save(serviceToAllottedResources);
2541
2542         } finally {
2543             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveServiceToAllottedResources", null);
2544         }
2545     }
2546
2547     public void saveService (Service service) {
2548         long startTime = System.currentTimeMillis ();
2549         LOGGER.debug ("Catalog database - save Service with ServiceName/Version/serviceUUID(SERVICE_NAME_VERSION_ID)" + service.getServiceName()+"/"+service.getVersion()+"/"+service.getServiceNameVersionId());
2550         try {
2551             Service serviceInvariantDB = null;
2552             // Retrieve existing service record by nameVersionId
2553             Service serviceDB = this.getServiceByUUID(service.getServiceNameVersionId());
2554             if (serviceDB == null) {
2555                 // 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.
2556                 serviceInvariantDB = this.getServiceByInvariantUUID(service.getModelInvariantUUID());
2557                 // Save the new Service record
2558                 this.getSession ().save (service);
2559             }
2560
2561             if(serviceInvariantDB != null){  // existing modelInvariantId was found.
2562                 // copy the recipe record with the matching invariant id.  We will duplicate this for the new service record
2563                 List<ServiceRecipe> serviceRecipes = getServiceRecipes(serviceInvariantDB.getId());
2564
2565                 if(serviceRecipes != null && serviceRecipes.size() > 0){
2566                     for(ServiceRecipe serviceRecipe : serviceRecipes){
2567                         if(serviceRecipe != null){
2568                             // 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.
2569                             Service newService = this.getServiceByUUID(service.getServiceNameVersionId());
2570                             // Create a new ServiceRecipe record based on the existing one we just copied from the DB.
2571                             ServiceRecipe newServiceRecipe = new ServiceRecipe();
2572                             newServiceRecipe.setAction(serviceRecipe.getAction());
2573                             newServiceRecipe.setDescription(serviceRecipe.getDescription());
2574                             newServiceRecipe.setOrchestrationUri(serviceRecipe.getOrchestrationUri());
2575                             newServiceRecipe.setRecipeTimeout(serviceRecipe.getRecipeTimeout());
2576                             newServiceRecipe.setServiceParamXSD(serviceRecipe.getServiceParamXSD());
2577                             newServiceRecipe.setServiceId(newService.getId());
2578                             newServiceRecipe.setVersion(serviceRecipe.getVersion());
2579                             // Save the new recipe record in the service_recipe table and associate it to the new service record that we just added.
2580                             this.getSession ().save (newServiceRecipe);
2581                         }
2582                     }
2583                 }
2584
2585             }
2586
2587         } finally {
2588             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateService", null);
2589         }
2590     }
2591
2592     public void saveOrUpdateVfModule (VfModule vfModule) {
2593         long startTime = System.currentTimeMillis ();
2594         LOGGER.debug ("Catalog database - save VNF Module with VF Model Name " + vfModule.getModelName());
2595         try {
2596
2597             if (vfModule.getId() != 0) {
2598                 this.getSession ().merge (vfModule);
2599             } else {
2600                 this.getSession ().save (vfModule);
2601             }
2602
2603         } finally {
2604             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateVfModule", null);
2605         }
2606     }
2607
2608     public HeatNestedTemplate getNestedHeatTemplate(int parentTemplateId, int childTemplateId) {
2609           long startTime = System.currentTimeMillis ();
2610           LOGGER.debug ("Catalog database - get nested Heat template with PerentId-Child Id "
2611                                         + parentTemplateId +"-"+childTemplateId);
2612           try {
2613               HeatNestedTemplate nestedTemplate = new HeatNestedTemplate ();
2614               nestedTemplate.setParentTemplateId (parentTemplateId);
2615               nestedTemplate.setChildTemplateId (childTemplateId);
2616
2617               return (HeatNestedTemplate)session.get (HeatNestedTemplate.class,nestedTemplate);
2618           } finally {
2619               LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNestedHeatTemplate", null);
2620           }
2621     }
2622
2623     public void saveNestedHeatTemplate (int parentTemplateId, HeatTemplate childTemplate, String yamlFile) {
2624         long startTime = System.currentTimeMillis ();
2625         LOGGER.debug ("Catalog database - save nested Heat template with name "
2626                                       + childTemplate.getTemplateName ());
2627         try {
2628
2629                 saveHeatTemplate(childTemplate, childTemplate.getParameters());
2630                 if (getNestedHeatTemplate(parentTemplateId,childTemplate.getId()) == null) {
2631                     HeatNestedTemplate nestedTemplate = new HeatNestedTemplate ();
2632                     nestedTemplate.setParentTemplateId (parentTemplateId);
2633                     nestedTemplate.setChildTemplateId (childTemplate.getId ());
2634                     nestedTemplate.setProviderResourceFile (yamlFile);
2635                     session.save (nestedTemplate);
2636                 }
2637         } finally {
2638             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNestedHeatTemplate", null);
2639         }
2640     }
2641
2642     public HeatFiles getHeatFiles(int vnfResourceId,String fileName,String asdcResourceName, String version) {
2643           long startTime = System.currentTimeMillis ();
2644           LOGGER.debug ("Catalog database - getHeatFiles with name " + fileName
2645                                         + " and vnfResourceID "
2646                                         + vnfResourceId
2647 //                                        + " and ASDC resource name "
2648                                         + asdcResourceName
2649                                         + " and version "
2650                                         + version);
2651
2652           String hql = "FROM HeatFiles WHERE fileName = :fileName AND vnfResourceId = :vnfResourceId AND asdcResourceName = :asdcResourceName AND version = :version";
2653           Query query = getSession ().createQuery (hql);
2654           query.setParameter ("fileName", fileName);
2655           query.setParameter ("vnfResourceId", vnfResourceId);
2656           query.setParameter ("asdcResourceName", asdcResourceName);
2657           query.setParameter ("version", version);
2658
2659           @SuppressWarnings("unchecked")
2660
2661           HeatFiles heatFilesResult = null;
2662           try {
2663                   heatFilesResult = (HeatFiles) query.uniqueResult ();
2664           } catch (org.hibernate.NonUniqueResultException nure) {
2665                 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);
2666                 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);
2667                 heatFilesResult = null;
2668           } catch (org.hibernate.HibernateException he) {
2669                 LOGGER.debug("Hibernate Exception - while searching for: fileName='" + fileName + "', vnfResourceId='" + vnfResourceId + "' and asdcResourceName=" + asdcResourceName + " and version=" + version);
2670                 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);
2671                 heatFilesResult = null;
2672           } catch (Exception e) {
2673                 LOGGER.debug("Generic Exception - while searching for: fileName='" + fileName + "', vnfResourceId='" + vnfResourceId + "' and asdcResourceName=" + asdcResourceName + " and version=" + version);
2674                 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);
2675                 heatFilesResult = null;
2676           }
2677
2678           // See if something came back.
2679           if (heatFilesResult == null) {
2680               LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. HeatFiles not found", "CatalogDB", "getHeatFiles", null);
2681               return null;
2682           }
2683           // Name + Version is unique, so should only be one element
2684           LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getHeatFiles", null);
2685           return heatFilesResult;
2686     }
2687
2688     public void saveHeatFiles (HeatFiles childFile) {
2689          long startTime = System.currentTimeMillis ();
2690          LOGGER.debug ("Catalog database - save Heat File with name "
2691                                        + childFile.getFileName());
2692          try {
2693              HeatFiles heatFiles = getHeatFiles (childFile.getVnfResourceId(), childFile.getFileName(), childFile.getAsdcResourceName (),childFile.getVersion());
2694              if (heatFiles == null) {
2695
2696                  // asdc_heat_files_save
2697                  this.getSession ().save (childFile);
2698
2699              } else {
2700                  /* replaced 'heatFiles' by 'childFile'
2701                     Based on following comment:
2702                                         It must be childFile.setId instead of heatFiles.setId, we must return the ID if it exists in DB.
2703                                  */
2704                  childFile.setId(heatFiles.getId());
2705              }
2706
2707          } finally {
2708              LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveHeatFiles", null);
2709          }
2710     }
2711
2712     public void saveVfModuleToHeatFiles (int parentVfModuleId, HeatFiles childFile) {
2713         long startTime = System.currentTimeMillis ();
2714         LOGGER.debug ("Catalog database - save Heat File to VFmodule link "
2715                                       + childFile.getFileName());
2716         try {
2717             saveHeatFiles (childFile);
2718             VfModuleToHeatFiles vfModuleToHeatFile = new VfModuleToHeatFiles ();
2719                 vfModuleToHeatFile.setVfModuleId(parentVfModuleId);
2720                 vfModuleToHeatFile.setHeatFilesId(childFile.getId());
2721
2722                 session.save (vfModuleToHeatFile);
2723
2724         } finally {
2725             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVfModuleToHeatFiles", null);
2726         }
2727     }
2728
2729
2730     /**
2731      * Return a Network recipe that matches a given NETWORK_TYPE, ACTION, and, if specified, SERVICE_TYPE
2732      *
2733      * @param networkType
2734      * @param action
2735      * @param serviceType
2736      * @return NetworkRecipe object or null if none found
2737      */
2738     public NetworkRecipe getNetworkRecipe (String networkType, String action, String serviceType) {
2739
2740         long startTime = System.currentTimeMillis ();
2741         LOGGER.debug ("Catalog database - get network recipe with network type " + networkType
2742                                       + " and action "
2743                                       + action
2744                                       + " and service type "
2745                                       + serviceType);
2746
2747         try {
2748             String hql;
2749             if (serviceType == null) {
2750                 hql = "FROM NetworkRecipe WHERE networkType = :networkType AND action = :action AND serviceType IS NULL ";
2751             } else {
2752                 hql = "FROM NetworkRecipe WHERE networkType = :networkType AND action = :action AND serviceType = :serviceType ";
2753             }
2754             Query query = getSession ().createQuery (hql);
2755             query.setParameter (NETWORK_TYPE, networkType);
2756             query.setParameter (ACTION, action);
2757             if (serviceType != null) {
2758                 query.setParameter ("serviceType", serviceType);
2759             }
2760
2761             @SuppressWarnings("unchecked")
2762             List <NetworkRecipe> resultList = query.list ();
2763
2764             if (resultList.isEmpty ()) {
2765                 return null;
2766             }
2767
2768             Collections.sort (resultList, new MavenLikeVersioningComparator ());
2769             Collections.reverse (resultList);
2770
2771             return resultList.get (0);
2772         } finally {
2773             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkRecipe", null);
2774         }
2775     }
2776
2777     /**
2778      * Return a Network recipe that matches a given NETWORK_TYPE and ACTION
2779      *
2780      * @param networkType
2781      * @param action
2782      * @return NetworkRecipe object or null if none found
2783      */
2784     public NetworkRecipe getNetworkRecipe (String networkType, String action) {
2785
2786         long startTime = System.currentTimeMillis ();
2787         LOGGER.debug ("Catalog database - get network recipe with network type " + networkType
2788                                       + " and action "
2789                                       + action
2790                                       );
2791
2792         try {
2793             String hql = "FROM NetworkRecipe WHERE networkType = :networkType AND action = :action";
2794
2795             Query query = getSession ().createQuery (hql);
2796             query.setParameter (NETWORK_TYPE, networkType);
2797             query.setParameter (ACTION, action);
2798
2799             @SuppressWarnings("unchecked")
2800             List <NetworkRecipe> resultList = query.list ();
2801
2802             if (resultList.isEmpty ()) {
2803                 return null;
2804             }
2805
2806             Collections.sort (resultList, new MavenLikeVersioningComparator ());
2807             Collections.reverse (resultList);
2808
2809             return resultList.get (0);
2810         } finally {
2811             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkRecipe", null);
2812         }
2813     }
2814
2815     /**
2816      * Return a Network Resource that matches the Network Customization defined by given MODEL_CUSTOMIZATION_UUID
2817      *
2818      * @param networkType
2819      * @param action
2820      * @param serviceType
2821      * @return NetworkRecipe object or null if none found
2822      */
2823     public NetworkResource getNetworkResourceByModelCustUuid(String modelCustomizationUuid) {
2824
2825         long startTime = System.currentTimeMillis ();
2826         LOGGER.debug ("Catalog database - get network resource with modelCustomizationUuid " + modelCustomizationUuid);
2827
2828         try {
2829             String hql =  "select n FROM NetworkResource n, NetworkResourceCustomization c WHERE n.id=c.networkResourceId and c.modelCustomizationUuid = :modelCustomizationUuid";
2830             Query query = getSession ().createQuery (hql);
2831             query.setParameter (MODEL_CUSTOMIZATION_UUID, modelCustomizationUuid);
2832
2833             @SuppressWarnings("unchecked")
2834             List <NetworkResource> resultList = query.list ();
2835
2836             if (resultList.isEmpty ()) {
2837                 return null;
2838             }
2839
2840             Collections.sort (resultList, new MavenLikeVersioningComparator ());
2841             Collections.reverse (resultList);
2842
2843             return resultList.get (0);
2844         } finally {
2845             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResourceBySvcNtwkRsrc", null);
2846         }
2847     }
2848
2849     /**
2850      * Return a VnfComponents recipe that matches a given VNF_TYPE, VNF_COMPONENT_TYPE, ACTION, and, if specified,
2851      * SERVICE_TYPE
2852      *
2853      * @param vnfType
2854      * @param vnfComponentType
2855      * @param action
2856      * @param serviceType
2857      * @return VnfComponentsRecipe object or null if none found
2858      */
2859     public VnfComponentsRecipe getVnfComponentsRecipe (String vnfType,
2860                                                        String vnfComponentType,
2861                                                        String action,
2862                                                        String serviceType) {
2863
2864         long startTime = System.currentTimeMillis ();
2865         LOGGER.debug ("Catalog database - get Vnf Component recipe with network type " + vnfType
2866                                       + " and component type "
2867                                       + vnfComponentType
2868                                       + " and action "
2869                                       + action
2870                                       + " and service type "
2871                                       + serviceType);
2872
2873         try {
2874             String hql;
2875             if (serviceType == null) {
2876                 hql = "FROM VnfComponentsRecipe WHERE vnfType = :vnfType AND vnfComponentType = :vnfComponentType AND action = :action AND serviceType IS NULL ";
2877             } else {
2878                 hql = "FROM VnfComponentsRecipe WHERE vnfType = :vnfType AND vnfComponentType = :vnfComponentType AND action = :action AND serviceType = :serviceType ";
2879             }
2880             Query query = getSession ().createQuery (hql);
2881             query.setParameter (VNF_TYPE, vnfType);
2882             query.setParameter (VNF_COMPONENT_TYPE, vnfComponentType);
2883             query.setParameter (ACTION, action);
2884             if (serviceType != null) {
2885                 query.setParameter ("serviceType", serviceType);
2886             }
2887
2888             @SuppressWarnings("unchecked")
2889             List <VnfComponentsRecipe> resultList = query.list ();
2890
2891             if (resultList.isEmpty ()) {
2892                 return null;
2893             }
2894             Collections.sort (resultList, new MavenLikeVersioningComparator ());
2895             Collections.reverse (resultList);
2896
2897             return resultList.get (0);
2898         } finally {
2899             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfComponentsRecipe", null);
2900         }
2901     }
2902
2903     /**
2904      * Return a VnfComponents recipe that matches a given VF_MODULE_ID, VNF_COMPONENT_TYPE, ACTION
2905      *
2906      * @param vfModuleId
2907      * @param vnfComponentType
2908      * @param action
2909      * @return VnfComponentsRecipe object or null if none found
2910      */
2911     public VnfComponentsRecipe getVnfComponentsRecipeByVfModuleId (String vfModuleId,
2912                                                        String vnfComponentType,
2913                                                        String action) {
2914
2915         long startTime = System.currentTimeMillis ();
2916         LOGGER.debug ("Catalog database - get Vnf Component recipe with vfModuleId " + vfModuleId
2917                                       + " and component type "
2918                                       + vnfComponentType
2919                                       + " and action "
2920                                       + action);
2921
2922         try {
2923             String hql;
2924             hql = "FROM VnfComponentsRecipe WHERE vfModuleId = :vfModuleId AND vnfComponentType = :vnfComponentType AND action = :action ";
2925
2926             Query query = getSession ().createQuery (hql);
2927             query.setParameter (VF_MODULE_ID, vfModuleId);
2928             query.setParameter (VNF_COMPONENT_TYPE, vnfComponentType);
2929             query.setParameter (ACTION, action);
2930
2931             @SuppressWarnings("unchecked")
2932             List <VnfComponentsRecipe> resultList = query.list ();
2933
2934             if (resultList.isEmpty ()) {
2935                 return null;
2936             }
2937             Collections.sort (resultList, new MavenLikeVersioningComparator ());
2938             Collections.reverse (resultList);
2939
2940             return resultList.get (0);
2941         } finally {
2942             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfComponentsRecipeByVfModuleId", null);
2943         }
2944     }
2945
2946
2947
2948     public void saveOrUpdateVnfComponent (VnfComponent vnfComponent) {
2949         long startTime = System.currentTimeMillis ();
2950
2951         LOGGER.debug ("Catalog database - save VnfComponent where vnfId="+ vnfComponent.getVnfId()+ " AND componentType="+ vnfComponent.getComponentType());
2952
2953         VnfComponent vnfComponentDb = this.getVnfComponent(vnfComponent.getVnfId(), vnfComponent.getComponentType());
2954
2955         try {
2956
2957             if (vnfComponentDb != null) {
2958                 this.getSession ().merge (vnfComponent);
2959             } else {
2960                 this.getSession ().save (vnfComponent);
2961             }
2962
2963         } finally {
2964             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveOrUpdateVnfComponent", null);
2965         }
2966     }
2967
2968     /**
2969      * Return a VfModule record that matches a given MODEL_NAME
2970      *
2971      * @param modelName
2972      * @return VfModule object or null if none found
2973      */
2974     public VfModule getVfModule (String modelName) {
2975
2976         long startTime = System.currentTimeMillis ();
2977         LOGGER.debug ("Catalog database - get vf module with model name " + modelName);
2978
2979         try {
2980             String hql;
2981
2982             hql = "FROM VfModule WHERE modelName = :modelName";
2983
2984             Query query = getSession ().createQuery (hql);
2985             query.setParameter (MODEL_NAME, modelName);
2986
2987             @SuppressWarnings("unchecked")
2988             List <VfModule> resultList = query.list ();
2989
2990             if (resultList.isEmpty ()) {
2991                 return null;
2992             }
2993             Collections.sort (resultList, new MavenLikeVersioningComparator ());
2994             Collections.reverse (resultList);
2995
2996             return resultList.get (0);
2997         } finally {
2998             LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModule", null);
2999         }
3000     }
3001
3002     /**
3003      * Return a Model recipe that matches a given MODEL_TYPE, MODEL_VERSION_ID, ACTION
3004      * Note: This method is not currently used but was retained in the event the
3005      *       architecture moves back to a MODEL/MODEL_RECIPE structure.
3006      *
3007      * @param modelType
3008      * @param modelVersionId
3009      * @param action
3010      * @return ModelRecipe object or null if none found
3011      */
3012     public ModelRecipe getModelRecipe(String modelType,
3013                                       String modelVersionId,
3014                                       String action) {
3015
3016         long startTime = System.currentTimeMillis();
3017         LOGGER.debug("Catalog database - get Model recipe with modelType=" + modelType
3018                 + " and modeVersionId=" + modelVersionId
3019                 + " and action=" + action);
3020
3021         try {
3022             String hql;
3023             // TBD - at some point it would be desirable to figure out how to do a  HQL JOIN across
3024             //       the MODEL and MODEL_RECIPE tables in HQL instead of 2 separate queries.
3025             //       There seems to be 2 issues: formatting a hql query that executes successfully
3026             //       and then being able to generate a result that will fit into the ModelRecipe class.
3027
3028             // 1st query to get the Model record for the given MODEL_TYPE and MODEL_VERSION_ID
3029             hql = "FROM Model WHERE modelType = :modelType AND modelVersionId = :modelVersionId";
3030             Query query = getSession().createQuery(hql);
3031             query.setParameter(MODEL_TYPE, modelType);
3032             query.setParameter(MODEL_VERSION_ID, modelVersionId);
3033
3034             @SuppressWarnings("unchecked")
3035             List<Model> modelResultList = query.list();
3036             if (modelResultList.isEmpty()) {
3037                 LOGGER.debug("Catalog database - modelResultList is null");
3038                 return null;
3039             }
3040             Collections.sort(modelResultList, new MavenLikeVersioningComparator());
3041             Collections.reverse(modelResultList);
3042             LOGGER.debug("Catalog database - modelResultList contains " + modelResultList.get(0).toString());
3043
3044             // 2nd query to get the ModelRecipe record corresponding to the Model from the 1st query
3045             hql = "FROM ModelRecipe WHERE modelId = :modelId AND action = :action";
3046             query = getSession().createQuery(hql);
3047             // The MODEL table 'id' field maps to the MODEL_RECIPE table 'MODEL_ID' field
3048             query.setParameter(MODEL_ID, modelResultList.get(0).getId());
3049             query.setParameter(ACTION, action);
3050
3051             @SuppressWarnings("unchecked")
3052             List<ModelRecipe> recipeResultList = query.list();
3053             if (recipeResultList.isEmpty()) {
3054                 LOGGER.debug("Catalog database - recipeResultList is null");
3055                 return null;
3056             }
3057             Collections.sort(recipeResultList, new MavenLikeVersioningComparator());
3058             Collections.reverse(recipeResultList);
3059             LOGGER.debug("Catalog database - recipeResultList contains " + recipeResultList.get(0).toString());
3060
3061             return recipeResultList.get(0);
3062         } finally {
3063             LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getModelRecipe", null);
3064         }
3065     }
3066
3067
3068     /**
3069      * Verify the health of the DB.
3070      *
3071      * @return boolean value indicate whether DB is healthy
3072      */
3073     public boolean healthCheck () {
3074         long startTime = System.currentTimeMillis ();
3075         Session session = this.getSession ();
3076
3077         Query query = session.createSQLQuery (" show tables ");
3078
3079         List<?> list = query.list();
3080         LOGGER.debug("healthCheck CatalogDB - Successful");
3081         return true;
3082     }
3083 }