Set default and check existance of Policy Type
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / provider / SimpleToscaProvider.java
index a207c42..81a41aa 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.policy.models.tosca.simple.provider;
 
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -29,7 +30,9 @@ import javax.ws.rs.core.Response;
 import lombok.NonNull;
 
 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.dao.PfDao;
@@ -69,14 +72,9 @@ public class SimpleToscaProvider {
 
         // Add the policy type to the TOSCA service template
         List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version);
-        if (jpaPolicyTypeList != null) {
-            serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
-            return serviceTemplate;
-        } else {
-            String errorMessage = "policy type not found: " + name + ":" + version;
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
+        serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
+
+        return serviceTemplate;
     }
 
     /**
@@ -178,14 +176,8 @@ public class SimpleToscaProvider {
 
         // Add the policy type to the TOSCA service template
         List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version);
-        if (jpaPolicyList != null) {
-            serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
-            return serviceTemplate;
-        } else {
-            String errorMessage = "policy not found: " + name + ":" + version;
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
+        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
+        return serviceTemplate;
     }
 
     /**
@@ -202,6 +194,8 @@ public class SimpleToscaProvider {
         ToscaUtils.assertPoliciesExist(serviceTemplate);
 
         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+            verifyPolicyTypeForPolicy(dao, policy);
+
             dao.create(policy);
         }
 
@@ -280,4 +274,63 @@ public class SimpleToscaProvider {
 
         return conceptMap;
     }
+
+    /**
+     * 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 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());
+        } else {
+            policyType = dao.get(JpaToscaPolicyType.class, policyTypeKey);
+        }
+
+        if (policyType == null) {
+            String errorMessage =
+                    "policy type " + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist";
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
+    }
+
+    /**
+     * 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 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 (jpaPolicyTypeList.isEmpty()) {
+            return null;
+        }
+
+        // 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 lates policy type " + policyTypeName + " returned more than one entry";
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
+
+        return (JpaToscaPolicyType) filterdPolicyTypeList.get(0);
+    }
 }