Merge "Added canceling subsription to pnf-pnp workflow"
[so.git] / mso-catalog-db / src / main / java / org / openecomp / mso / db / catalog / CatalogDatabase.java
index bd3b988..4fb5ebc 100644 (file)
@@ -93,7 +93,8 @@ public class CatalogDatabase implements Closeable {
     private static final String MODEL_TYPE = "modelType";
     private static final String MODEL_VERSION_ID = "modelVersionId";
     private static final String MODEL_CUSTOMIZATION_UUID = "modelCustomizationUuid";
-       private static final String VF_MODULE_MODEL_UUID = "vfModuleModelUUId";
+    private static final String VF_MODULE_MODEL_UUID = "vfModuleModelUUId";
+    private static final String NETWORK_SERVICE = "network service";
 
     protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.GENERAL);
 
@@ -1175,14 +1176,11 @@ public class CatalogDatabase implements Closeable {
      * @return VnfRecipe object or null if none found
      */
     public VnfRecipe getVnfRecipeByNameVersion(String modelName, String modelVersion, String action) {
-        StringBuilder hql = new StringBuilder("FROM VnfRecipe WHERE vnfType = :vnfType AND version= :version AND action = :action ");
 
         long startTime = System.currentTimeMillis();
-        LOGGER.debug("Catalog database - get VNF recipe with name " + modelName
-                                      + " and action "
-                                      + action);
+        LOGGER.debug("Catalog database - get VNF recipe with name " + modelName + " and action " + action);
 
-        Query query = getSession().createQuery(hql.toString());
+        Query query = getSession().createQuery("FROM VnfRecipe WHERE vnfType = :vnfType AND version= :version AND action = :action ");
         query.setParameter(VNF_TYPE, modelName);
         query.setParameter(MODEL_VERSION, modelVersion);
         query.setParameter(ACTION, action);
@@ -1195,7 +1193,7 @@ public class CatalogDatabase implements Closeable {
             return null;
         }
 
-        Collections.sort(resultList, new MavenLikeVersioningComparator());
+        resultList.sort(new MavenLikeVersioningComparator());
         Collections.reverse(resultList);
 
         LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null);
@@ -1205,7 +1203,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a Network recipe that matches a given MODEL_UUID and ACTION
      *
-     * @param modelName
+     * @param vnfModelUuid
      * @param action
      * @return NetworkRecipe object or null if none found
      */
@@ -1219,10 +1217,34 @@ public class CatalogDatabase implements Closeable {
             return null;
         }
         
-        VnfRecipe recipe = this.getVnfRecipeByNameVersion(vnfResource.getModelName(), vnfResource.getModelVersion(), action);
+        VnfRecipe recipe = this.getVnfRecipeByNameVersion(vnfResource.getModelName(), vnfResource.getVersion(), action);
+
+        if (recipe == null && vnfResource.getSubCategory().equalsIgnoreCase(NETWORK_SERVICE)) {
+            recipe = getDefaultVnfRecipe(action);
+        }
         return recipe;        
     }
 
+    private VnfRecipe getDefaultVnfRecipe(String action) {
+        long startTime = System.currentTimeMillis();
+        LOGGER.debug("Catalog database - get default VNF recipe with action: " + action);
+
+        Query query = getSession().createQuery("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action ");
+        query.setParameter(VNF_TYPE, "NS_DEFAULT");
+        query.setParameter(ACTION, action);
+
+        @SuppressWarnings("unchecked")
+        List <VnfRecipe> resultList = query.list();
+
+        if (resultList.isEmpty()) {
+            LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null);
+            return null;
+        }
+
+        LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null);
+        return resultList.get(0);
+    }
+
     /**
      * Return a VNF recipe that matches a given VF_MODULE_ID and ACTION
      *
@@ -1638,7 +1660,7 @@ public class CatalogDatabase implements Closeable {
      * Return the VnfResourceCustomization object identified by the given modelCustomizationUuid 1707
      * Note that the corresponding VnfResource Object will be put in the VnfResourceCustomization bean
      *
-     * @param getVnfResourceCustomizationByModelVersionId
+     * @param modelVersionId
      * @return VnfResourceCustomization or null if not found
      */
     public VnfResourceCustomization getVnfResourceCustomizationByModelVersionId(String modelVersionId) {
@@ -1920,7 +1942,7 @@ public class CatalogDatabase implements Closeable {
      * Return the VnfResourceCustomization object identified by the given modelCustomizationUuid 1707
      * Note that the corresponding VnfResource Object will be put in the VnfResourceCustomization bean
      *
-     * @param modelCustomizationUuid
+     * @param modelUuid
      * @return VnfResourceCustomization or null if not found
      */
     public VnfResource getVnfResourceByModelUuid(String modelUuid) {
@@ -2828,7 +2850,7 @@ public class CatalogDatabase implements Closeable {
      * Return a VNF Module List that matches a given VNF_TYPE, VF_MODULE_MODEL_NAME,
      * ASDC_SERVICE_MODEL_VERSION, MODEL_VERSION, and ACTION
      *
-     * @param vnfModuleType
+     * @param vfModuleType
      * @parm modelCustomizationUuid
      * @param asdcServiceModelVersion
      * @param modelVersion
@@ -3366,7 +3388,9 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a ServiceToResourceCustomization object 
      * 
-     * @param vfModuleModelUuid, heatFilesArtifactUuid
+     * @param serviceModelUuid
+     * @param resourceModelCustomizationUuid
+     * @param modelType
      * @return VfModuleToHeatFiles or null if none found
      */ 
     public ServiceToResourceCustomization getServiceToResourceCustomization(String serviceModelUuid, String resourceModelCustomizationUuid, String modelType) {
@@ -3392,7 +3416,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a Map<String, HeatFiles> for returning the heat files associated with a vfModule 1707
      * 
-     * @param parentHeatTemplateId
+     * @param vfModuleModelUuid
      * @return Map<String,Object> or null if none found
      */ 
     public Map <String, HeatFiles> getHeatFilesForVfModule(String vfModuleModelUuid) {
@@ -3608,7 +3632,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Retrieves a Heat environment from DB based on its unique key. 1707
      *
-     * @param name the environment artifact name
+     * @param artifactUuid the environment artifact name
      * @param version the environment resource version
      * @return the heat environment from DB or null if not found
      */
@@ -3841,7 +3865,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return the newest version of a specific Tosca CSAR Record resource (queried by Name).
      *
-     * @param ToscaCsar
+     * @param artifactChecksum
      * @return ToscaCsar object or null if none found
      */
     public ToscaCsar getToscaCsar (String artifactChecksum) {
@@ -4206,28 +4230,6 @@ public class CatalogDatabase implements Closeable {
           }
     }
 
-    @Deprecated
-    public void saveNestedHeatTemplate (int parentTemplateId, HeatTemplate childTemplate, String yamlFile) {
-       /*
-        long startTime = System.currentTimeMillis ();
-        LOGGER.debug ("Catalog database - save nested Heat template with name "
-                                      + childTemplate.getTemplateName ());
-        try {
-
-               saveHeatTemplate(childTemplate, childTemplate.getParameters());
-               if (getNestedHeatTemplate(parentTemplateId,childTemplate.getId()) == null) {
-                   HeatNestedTemplate nestedTemplate = new HeatNestedTemplate ();
-                   nestedTemplate.setParentTemplateId (parentTemplateId);
-                   nestedTemplate.setChildTemplateId (childTemplate.getId ());
-                   nestedTemplate.setProviderResourceFile (yamlFile);
-                   session.save (nestedTemplate);
-               }
-        } finally {
-            LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveNestedHeatTemplate", null);
-        }
-        */
-    }
-    
     // 1707
     public void saveNestedHeatTemplate (String parentTemplateId, HeatTemplate childTemplate, String yamlFile) {
         long startTime = System.currentTimeMillis ();
@@ -4358,26 +4360,6 @@ public class CatalogDatabase implements Closeable {
          }
     }
 
-    @Deprecated
-    public void saveVfModuleToHeatFiles (int parentVfModuleId, HeatFiles childFile) {
-       /*
-        long startTime = System.currentTimeMillis ();
-        LOGGER.debug ("Catalog database - save Heat File to VFmodule link "
-                                      + childFile.getFileName());
-        try {
-            saveHeatFiles (childFile);
-            VfModuleToHeatFiles vfModuleToHeatFile = new VfModuleToHeatFiles ();
-               vfModuleToHeatFile.setVfModuleId(parentVfModuleId);
-               vfModuleToHeatFile.setHeatFilesId(childFile.getId());
-
-               session.save (vfModuleToHeatFile);
-
-        } finally {
-            LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "saveVfModuleToHeatFiles", null);
-        }
-        */
-    }
-    
     public void saveVfModuleToHeatFiles (String parentVfModuleId, HeatFiles childFile) {
         long startTime = System.currentTimeMillis ();
         LOGGER.debug ("Catalog database - save Heat File to VFmodule link "
@@ -4404,9 +4386,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a Network Resource that matches the Network Customization defined by given MODEL_CUSTOMIZATION_UUID
      *
-     * @param networkType
-     * @param action
-     * @param serviceType
+     * @param modelUUID
      * @return NetworkRecipe object or null if none found
      */
     public NetworkResource getNetworkResourceByModelUuid(String modelUUID) {
@@ -4489,7 +4469,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a Network recipe that matches a given MODEL_UUID and ACTION
      *
-     * @param modelName
+     * @param networkModelUuid
      * @param action
      * @return NetworkRecipe object or null if none found
      */
@@ -4504,7 +4484,17 @@ public class CatalogDatabase implements Closeable {
         }
         
         NetworkRecipe recipe = getNetworkRecipeByNameVersion(networkResource.getModelName(), networkResource.getModelVersion(), action);
-        return recipe;        
+
+        if (recipe == null) {
+            recipe = getDefaultNetworkReceipe(action);
+        }
+
+        return recipe;
+    }
+
+    private NetworkRecipe getDefaultNetworkReceipe(String action) {
+        String modelName = "SDNC_DEFAULT";
+        return getNetworkRecipe(modelName, action);
     }
     
     /**
@@ -4576,7 +4566,7 @@ public class CatalogDatabase implements Closeable {
                 return null;
             }
 
-            Collections.sort (resultList, new MavenLikeVersioningComparator ());
+            resultList.sort(new MavenLikeVersioningComparator());
             Collections.reverse (resultList);
 
             return resultList.get (0);
@@ -4588,9 +4578,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a Network Resource that matches the Network Customization defined by given MODEL_CUSTOMIZATION_UUID
      *
-     * @param networkType
-     * @param action
-     * @param serviceType
+     * @param modelCustomizationUuid
      * @return NetworkRecipe object or null if none found
      */
     public NetworkResource getNetworkResourceByModelCustUuid(String modelCustomizationUuid) {
@@ -4679,7 +4667,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a VnfComponents recipe that matches a given VF_MODULE_ID, VNF_COMPONENT_TYPE, ACTION
      *
-     * @param vfModuleId
+     * @param vfModuleModelUUId
      * @param vnfComponentType
      * @param action
      * @return VnfComponentsRecipe object or null if none found
@@ -4801,7 +4789,7 @@ public class CatalogDatabase implements Closeable {
     /**
      * Return a VfModule record that matches a given MODEL_NAME
      *
-     * @param modelName
+     * @param modelUUID
      * @return VfModule object or null if none found
      */
     public VfModule getVfModuleByModelUUID (String modelUUID) {
@@ -5163,7 +5151,7 @@ public class CatalogDatabase implements Closeable {
                 return null;
             }
 
-            Collections.sort (resultList, new MavenLikeVersioningComparator ());
+            resultList.sort(new MavenLikeVersioningComparator());
             Collections.reverse (resultList);
 
             return resultList.get (0);