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