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