Implement validation and hierarchical get
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / provider / SimpleToscaProvider.java
index 2240ef0..9c7d6d3 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.models.tosca.simple.provider;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
 import javax.ws.rs.core.Response;
 
 import lombok.NonNull;
 
+import org.apache.commons.collections4.CollectionUtils;
+import org.onap.policy.models.base.PfConcept;
+import org.onap.policy.models.base.PfConceptFilter;
 import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.base.PfValidationResult;
 import org.onap.policy.models.dao.PfDao;
-import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
-import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
-import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType;
-import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes;
-import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,91 +57,283 @@ import org.slf4j.LoggerFactory;
 public class SimpleToscaProvider {
     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
 
+    // Recurring string constants
+    private static final String SERVICE_TEMPLATE_NOT_FOUND_IN_DATABASE = "service template not found in database";
+    private static final String DO_NOT_EXIST = " do not exist";
+
     /**
-     * Get policy types.
+     * Get Service Template.
      *
      * @param dao the DAO to use to access the database
-     * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy
-     *        types. A null key version returns all versions of the policy type name specified in the key.
-     * @return the policy types found
-     * @throws PfModelException on errors getting policy types
+     * @return the service template
+     * @throws PfModelException on errors getting the service template
      */
-    public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
-            throws PfModelException {
+    public JpaToscaServiceTemplate getServiceTemplate(@NonNull final PfDao dao) throws PfModelException {
+        LOGGER.debug("->getServiceTemplate");
 
-        // Create the structure of the TOSCA service template to contain the policy type
-        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
-        serviceTemplate.setPolicyTypes(new ToscaPolicyTypes());
+        JpaToscaServiceTemplate serviceTemplate = new SimpleToscaServiceTemplateProvider().read(dao);
+        if (serviceTemplate == null) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND, SERVICE_TEMPLATE_NOT_FOUND_IN_DATABASE);
+        }
 
-        // Add the policy type to the TOSCA service template
-        ToscaPolicyType policyType = dao.get(ToscaPolicyType.class, policyTypeKey);
-        if (policyType != null) {
-            serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType);
-            return serviceTemplate;
+        LOGGER.debug("<-getServiceTemplate: serviceTemplate={}", serviceTemplate);
+        return serviceTemplate;
+    }
+
+    /**
+     * Append a service template fragment to the service template in the database.
+     *
+     * @param dao the DAO to use to access the database
+     * @param incomingServiceTemplateFragment the service template containing the definition of the entities to be
+     *        created
+     * @return the TOSCA service template in the database after the operation
+     * @throws PfModelException on errors appending a service template to the template in the database
+     */
+    public JpaToscaServiceTemplate appendToServiceTemplate(@NonNull final PfDao dao,
+            @NonNull final JpaToscaServiceTemplate incomingServiceTemplateFragment) throws PfModelException {
+        LOGGER.debug("->appendServiceTemplateFragment: incomingServiceTemplateFragment={}",
+                incomingServiceTemplateFragment);
+
+        JpaToscaServiceTemplate dbServiceTemplate = new SimpleToscaServiceTemplateProvider().read(dao);
+
+        JpaToscaServiceTemplate serviceTemplateToWrite;
+        if (dbServiceTemplate == null) {
+            serviceTemplateToWrite = incomingServiceTemplateFragment;
         } else {
-            String errorMessage = "policy type not found: " + policyTypeKey.getId();
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+            serviceTemplateToWrite =
+                    ToscaServiceTemplateUtils.addFragment(dbServiceTemplate, incomingServiceTemplateFragment);
         }
+
+        PfValidationResult result = serviceTemplateToWrite.validate(new PfValidationResult());
+        if (!result.isValid()) {
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.toString());
+        }
+
+        new SimpleToscaServiceTemplateProvider().write(dao, serviceTemplateToWrite);
+
+        LOGGER.debug("<-appendServiceTemplateFragment: returnServiceTempalate={}", serviceTemplateToWrite);
+        return serviceTemplateToWrite;
     }
 
     /**
-     * Create policy types.
+     * Get data types.
      *
      * @param dao the DAO to use to access the database
-     * @param serviceTemplate the service template containing the definition of the policy types to be created
-     * @return the TOSCA service template containing the created policy types
-     * @throws PfModelException on errors creating policy types
+     * @param name the name of the data type to get, set to null to get all policy types
+     * @param version the version of the data type to get, set to null to get all versions
+     * @return the data types found
+     * @throws PfModelException on errors getting data types
      */
-    public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
-            @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
+    public JpaToscaServiceTemplate getDataTypes(@NonNull final PfDao dao, final String name, final String version)
+            throws PfModelException {
+        LOGGER.debug("->getDataTypes: name={}, version={}", name, version);
 
-        assertPolicyTypesExist(serviceTemplate);
+        JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
 
-        for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
-            dao.create(policyType);
+        if (!ToscaUtils.doDataTypesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+                    "data types for " + name + ":" + version + DO_NOT_EXIST);
         }
 
-        // Return the created policy types
-        ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
+        serviceTemplate.setPolicyTypes(null);
+        serviceTemplate.setTopologyTemplate(null);
 
-        for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
-            returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
+        ToscaUtils.getEntityTree(serviceTemplate.getDataTypes(), name, version);
+
+        if (!ToscaUtils.doDataTypesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+                    "data types for " + name + ":" + version + DO_NOT_EXIST);
         }
 
-        ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
-        returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
+        for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getConceptMap().values()) {
+            Collection<PfConceptKey> referencedDataTypeKeys = dataType.getReferencedDataTypes();
+
+            for (PfConceptKey referencedDataTypeKey : referencedDataTypeKeys) {
+                JpaToscaServiceTemplate dataTypeEntityTreeServiceTemplate =
+                        getDataTypes(dao, referencedDataTypeKey.getName(), referencedDataTypeKey.getVersion());
+
+                serviceTemplate =
+                        ToscaServiceTemplateUtils.addFragment(serviceTemplate, dataTypeEntityTreeServiceTemplate);
+            }
+        }
+
+        LOGGER.debug("<-getDataTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
+        return serviceTemplate;
+    }
+
+    /**
+     * Create data types.
+     *
+     * @param dao the DAO to use to access the database
+     * @param incomingServiceTemplate the service template containing the definition of the data types to be created
+     * @return the TOSCA service template containing the created data types
+     * @throws PfModelException on errors creating data types
+     */
+    public JpaToscaServiceTemplate createDataTypes(@NonNull final PfDao dao,
+            @NonNull final JpaToscaServiceTemplate incomingServiceTemplate) throws PfModelException {
+        LOGGER.debug("->createDataTypes: incomingServiceTemplate={}", incomingServiceTemplate);
+
+        ToscaUtils.assertDataTypesExist(incomingServiceTemplate);
+
+        JpaToscaServiceTemplate writtenServiceTemplate = appendToServiceTemplate(dao, incomingServiceTemplate);
+
+        LOGGER.debug("<-createDataTypes: returnServiceTempalate={}", writtenServiceTemplate);
+        return writtenServiceTemplate;
+    }
+
+    /**
+     * Update Data types.
+     *
+     * @param dao the DAO to use to access the database
+     * @param serviceTemplate the service template containing the definition of the data types to be modified
+     * @return the TOSCA service template containing the modified data types
+     * @throws PfModelException on errors updating Data types
+     */
+    public JpaToscaServiceTemplate updateDataTypes(@NonNull final PfDao dao,
+            @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
+        LOGGER.debug("->updateDataTypes: serviceTempalate={}", serviceTemplate);
+
+        ToscaUtils.assertDataTypesExist(serviceTemplate);
+
+        for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) {
+            dao.update(dataType);
+        }
+
+        // Return the created data types
+        JpaToscaDataTypes returnDataTypes = new JpaToscaDataTypes();
+
+        for (PfConceptKey dataTypeKey : serviceTemplate.getDataTypes().getConceptMap().keySet()) {
+            returnDataTypes.getConceptMap().put(dataTypeKey, dao.get(JpaToscaDataType.class, dataTypeKey));
+        }
+
+        JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
+        returnServiceTemplate.setDataTypes(returnDataTypes);
 
+        LOGGER.debug("<-updateDataTypes: returnServiceTempalate={}", returnServiceTemplate);
         return returnServiceTemplate;
     }
 
+    /**
+     * Delete Data types.
+     *
+     * @param dao the DAO to use to access the database
+     * @param dataTypeKey the data type key for the Data types to be deleted, if the version of the key is null, all
+     *        versions of the data type are deleted.
+     * @return the TOSCA service template containing the data types that were deleted
+     * @throws PfModelException on errors deleting data types
+     */
+    public JpaToscaServiceTemplate deleteDataType(@NonNull final PfDao dao, @NonNull final PfConceptKey dataTypeKey)
+            throws PfModelException {
+        LOGGER.debug("->deleteDataType: key={}", dataTypeKey);
+
+        JpaToscaServiceTemplate serviceTemplate = getDataTypes(dao, dataTypeKey.getName(), dataTypeKey.getVersion());
+
+        dao.delete(JpaToscaDataType.class, dataTypeKey);
+
+        LOGGER.debug("<-deleteDataType: key={}, serviceTempalate={}", dataTypeKey, serviceTemplate);
+        return serviceTemplate;
+    }
+
+    /**
+     * Get policy types.
+     *
+     * @param dao the DAO to use to access the database
+     * @param name the name of the policy type to get, set to null to get all policy types
+     * @param version the version of the policy type to get, set to null to get all versions
+     * @return the policy types found
+     * @throws PfModelException on errors getting policy types
+     */
+    public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
+            throws PfModelException {
+        LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
+
+        JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+
+        serviceTemplate.setDataTypes(null);
+        serviceTemplate.setTopologyTemplate(null);
+
+        if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+                    "policy types for " + name + ":" + version + DO_NOT_EXIST);
+        }
+
+        ToscaUtils.getEntityTree(serviceTemplate.getPolicyTypes(), name, version);
+
+        if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+                    "policy types for " + name + ":" + version + DO_NOT_EXIST);
+        }
+
+        for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getConceptMap().values()) {
+            Collection<PfConceptKey> referencedDataTypeKeys = policyType.getReferencedDataTypes();
+
+            for (PfConceptKey referencedDataTypeKey : referencedDataTypeKeys) {
+                JpaToscaServiceTemplate dataTypeEntityTreeServiceTemplate =
+                        getDataTypes(dao, referencedDataTypeKey.getName(), referencedDataTypeKey.getVersion());
+
+                serviceTemplate =
+                        ToscaServiceTemplateUtils.addFragment(serviceTemplate, dataTypeEntityTreeServiceTemplate);
+            }
+        }
+
+        LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
+        return serviceTemplate;
+    }
+
     /**
      * Create policy types.
      *
      * @param dao the DAO to use to access the database
+     * @param incomingServiceTemplate the service template containing the definition of the policy types to be created
+     * @return the TOSCA service template containing the created policy types
+     * @throws PfModelException on errors creating policy types
+     */
+    public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
+            @NonNull final JpaToscaServiceTemplate incomingServiceTemplate) throws PfModelException {
+        LOGGER.debug("->createPolicyTypes: serviceTempalate={}", incomingServiceTemplate);
+
+        ToscaUtils.assertPolicyTypesExist(incomingServiceTemplate);
+
+        JpaToscaServiceTemplate writtenServiceTemplate = appendToServiceTemplate(dao, incomingServiceTemplate);
+
+        LOGGER.debug("<-createPolicyTypes: returnServiceTempalate={}", writtenServiceTemplate);
+        return writtenServiceTemplate;
+    }
+
+    /**
+     * Update policy types.
+     *
+     * @param dao the DAO to use to access the database
      * @param serviceTemplate the service template containing the definition of the policy types to be modified
      * @return the TOSCA service template containing the modified policy types
      * @throws PfModelException on errors updating policy types
      */
-    public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
-            @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
+    public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
+            @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
+        LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
+
+        ToscaUtils.assertPolicyTypesExist(serviceTemplate);
 
-        assertPolicyTypesExist(serviceTemplate);
+        // Update the data types on the policy type
+        if (ToscaUtils.doDataTypesExist(serviceTemplate)) {
+            updateDataTypes(dao, serviceTemplate);
+        }
 
-        for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
+        for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
             dao.update(policyType);
         }
 
         // Return the created policy types
-        ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
+        JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
 
         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
-            returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
+            returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
         }
 
-        ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
+        JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
 
+        LOGGER.debug("<-updatePolicyTypes: returnServiceTempalate={}", returnServiceTemplate);
         return returnServiceTemplate;
     }
 
@@ -142,13 +346,16 @@ public class SimpleToscaProvider {
      * @return the TOSCA service template containing the policy types that were deleted
      * @throws PfModelException on errors deleting policy types
      */
-    public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
+    public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
             throws PfModelException {
+        LOGGER.debug("->deletePolicyType: key={}", policyTypeKey);
 
-        ToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
+        JpaToscaServiceTemplate serviceTemplate =
+                getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
 
-        dao.delete(ToscaPolicyType.class, policyTypeKey);
+        dao.delete(JpaToscaPolicyType.class, policyTypeKey);
 
+        LOGGER.debug("<-deletePolicyType: key={}, serviceTempalate={}", policyTypeKey, serviceTemplate);
         return serviceTemplate;
     }
 
@@ -156,59 +363,61 @@ public class SimpleToscaProvider {
      * Get policies.
      *
      * @param dao the DAO to use to access the database
-     * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
-     *        A null local name returns all policies for a parent policy type.
+     * @param name the name of the policy to get, set to null to get all policy types
+     * @param version the version of the policy to get, set to null to get all versions
      * @return the policies found
      * @throws PfModelException on errors getting policies
      */
-    public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
+    public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
             throws PfModelException {
+        LOGGER.debug("->getPolicies: name={}, version={}", name, version);
 
-        // Create the structure of the TOSCA service template to contain the policy type
-        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
-        serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
-        serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
+        JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
 
-        // Add the policy to the TOSCA service template
-        ToscaPolicy policy = dao.get(ToscaPolicy.class, policyKey);
-        if (policy != null) {
-            serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
-            return serviceTemplate;
-        } else {
-            String errorMessage = "policy not found: " + policyKey.getId();
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+                    "policies for " + name + ":" + version + DO_NOT_EXIST);
+        }
+
+        ToscaUtils.getEntityTree(serviceTemplate.getTopologyTemplate().getPolicies(), name, version);
+
+        if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+                    "policies for " + name + ":" + version + DO_NOT_EXIST);
+        }
+
+        for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().values()) {
+            if (policy.getDerivedFrom() != null) {
+                JpaToscaServiceTemplate referencedEntitiesServiceTemplate =
+                        getPolicyTypes(dao, policy.getDerivedFrom().getName(), policy.getDerivedFrom().getVersion());
+
+                serviceTemplate =
+                        ToscaServiceTemplateUtils.addFragment(serviceTemplate, referencedEntitiesServiceTemplate);
+            }
         }
+
+        LOGGER.debug("<-getPolicies: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
+        return serviceTemplate;
     }
 
     /**
      * Create policies.
      *
      * @param dao the DAO to use to access the database
-     * @param serviceTemplate the service template containing the definitions of the new policies to be created.
+     * @param incomingServiceTemplate the service template containing the definitions of the new policies to be created.
      * @return the TOSCA service template containing the policy types that were created
      * @throws PfModelException on errors creating policies
      */
-    public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
-            @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
-
-        assertPoliciesExist(serviceTemplate);
-
-        for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
-            dao.create(policy);
-        }
-
-        // Return the created policy types
-        ToscaPolicies returnPolicies = new ToscaPolicies();
-        returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
+    public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
+            @NonNull final JpaToscaServiceTemplate incomingServiceTemplate) throws PfModelException {
+        LOGGER.debug("->createPolicies: incomingServiceTemplate={}", incomingServiceTemplate);
 
-        for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
-            returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
-        }
+        ToscaUtils.assertPoliciesExist(incomingServiceTemplate);
 
-        serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
+        JpaToscaServiceTemplate writtenServiceTemplate = appendToServiceTemplate(dao, incomingServiceTemplate);
 
-        return serviceTemplate;
+        LOGGER.debug("<-createPolicies: serviceTemplate={}", writtenServiceTemplate);
+        return writtenServiceTemplate;
     }
 
     /**
@@ -219,25 +428,28 @@ public class SimpleToscaProvider {
      * @return the TOSCA service template containing the policies that were updated
      * @throws PfModelException on errors updating policies
      */
-    public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
-            @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
+    public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
+            @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
+        LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
 
-        assertPoliciesExist(serviceTemplate);
+        ToscaUtils.assertPoliciesExist(serviceTemplate);
 
-        for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+        for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+            verifyPolicyTypeForPolicy(dao, policy);
             dao.update(policy);
         }
 
         // Return the created policy types
-        ToscaPolicies returnPolicies = new ToscaPolicies();
+        JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
 
         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
-            returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
+            returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
         }
 
         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
 
+        LOGGER.debug("<-updatePolicies: serviceTemplate={}", serviceTemplate);
         return serviceTemplate;
     }
 
@@ -249,53 +461,74 @@ public class SimpleToscaProvider {
      * @return the TOSCA service template containing the policies that were deleted
      * @throws PfModelException on errors deleting policies
      */
-    public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
+    public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
             throws PfModelException {
+        LOGGER.debug("->deletePolicy: key={}", policyKey);
 
-        ToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
+        JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
 
-        dao.delete(ToscaPolicy.class, policyKey);
+        dao.delete(JpaToscaPolicy.class, policyKey);
 
+        LOGGER.debug("<-deletePolicy: key={}, serviceTempalate={}", policyKey, serviceTemplate);
         return serviceTemplate;
     }
 
     /**
-     * Check if policy types have been specified is initialized.
+     * Verify the policy type for a policy exists.
+     *
+     * @param dao the DAO to use to access policy types in the database
+     * @param policy the policy to check the policy type for
      */
-    private void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) {
-        if (serviceTemplate.getPolicyTypes() == null) {
-            String errorMessage = "no policy types specified on service template";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+    private void verifyPolicyTypeForPolicy(final PfDao dao, final JpaToscaPolicy policy) {
+        PfConceptKey policyTypeKey = policy.getType();
+
+        JpaToscaPolicyType policyType = null;
+
+        if (PfKey.NULL_KEY_VERSION.equals(policyTypeKey.getVersion())) {
+            policyType = getLatestPolicyTypeVersion(dao, policyTypeKey.getName());
+
+            if (policyType != null) {
+                policy.getType().setVersion(policyType.getKey().getVersion());
+            }
+        } else {
+            policyType = dao.get(JpaToscaPolicyType.class, policyTypeKey);
         }
 
-        if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
-            String errorMessage = "list of policy types specified on service template is empty";
-            LOGGER.warn(errorMessage);
+        if (policyType == null) {
+            String errorMessage =
+                    "policy type " + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist";
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
         }
     }
 
     /**
-     * Check if policy types have been specified is initialized.
+     * Get the latest version of the policy type for the given policy type name.
+     *
+     * @param dao the DAO to use to access policy types in the database
+     * @param policyTypeName the name of the policy type
+     * @return the latest policy type
      */
-    private void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) {
-        if (serviceTemplate.getTopologyTemplate() == null) {
-            String errorMessage = "topology template not specified on service template";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
+    private JpaToscaPolicyType getLatestPolicyTypeVersion(final PfDao dao, final String policyTypeName) {
+        // Policy type version is not specified, get the latest version from the database
+        List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, policyTypeName, null);
 
-        if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
-            String errorMessage = "no policies specified on topology template of service template";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        if (CollectionUtils.isEmpty(jpaPolicyTypeList)) {
+            return null;
         }
 
-        if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
-            String errorMessage = "list of policies specified on topology template of service template is empty";
-            LOGGER.warn(errorMessage);
+        // Create a filter to get the latest version of the policy type
+        PfConceptFilter pfConceptFilter = PfConceptFilter.builder().version(PfConceptFilter.LATEST_VERSION).build();
+
+        // FIlter the returned policy type list
+        List<PfConcept> policyTypeKeyList = new ArrayList<>(jpaPolicyTypeList);
+        List<PfConcept> filterdPolicyTypeList = pfConceptFilter.filter(policyTypeKeyList);
+
+        // We should have one and only one returned entry
+        if (filterdPolicyTypeList.size() != 1) {
+            String errorMessage = "search for latest policy type " + policyTypeName + " returned more than one entry";
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
         }
+
+        return (JpaToscaPolicyType) filterdPolicyTypeList.get(0);
     }
 }