aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca/src/main/java/org
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-05-07 12:42:26 +0000
committerliamfallon <liam.fallon@est.tech>2019-05-07 12:42:26 +0000
commitf53879588a464c727ece62f87c7625b47e6de7f1 (patch)
tree980cddab937c38610587b50cc45a9c36540a2470 /models-tosca/src/main/java/org
parente936413c9082afed0fef4646b8f12d351c87800c (diff)
Set default and check existance of Policy Type
The TOSCA specification has a "bug" in that it does not have a field to specify the version of a policy type to use. We already had introduced the "type_version" field for this. This review introduces setting of the default version of a policy type to be be used by a policy as the latest version of the policy type in the database. As a side effect of this, we now have to check for existence of the policy type of a policy in the database. This means that creation/update of a policy with a non-existant policy type specified will now fail. Issue-ID: POLICY-1738 Change-Id: I27080cf6cd358948810dab6897c72dfe4d41fe91 Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java/org')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java71
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java5
2 files changed, 72 insertions, 4 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
index ef8ac05ec..81a41aa05 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
@@ -20,15 +20,21 @@
package org.onap.policy.models.tosca.simple.provider;
+import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+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;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
@@ -37,6 +43,8 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
import org.onap.policy.models.tosca.utils.ToscaUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class provides the provision of information on TOSCA concepts in the database to callers.
@@ -44,6 +52,8 @@ import org.onap.policy.models.tosca.utils.ToscaUtils;
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class SimpleToscaProvider {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
+
/**
* Get policy types.
*
@@ -184,6 +194,8 @@ public class SimpleToscaProvider {
ToscaUtils.assertPoliciesExist(serviceTemplate);
for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+ verifyPolicyTypeForPolicy(dao, policy);
+
dao.create(policy);
}
@@ -262,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);
+ }
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
index 93333c4e3..a73fb85e9 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
@@ -38,8 +38,7 @@ public final class ToscaUtils {
/**
* Private constructor to prevent subclassing.
*/
- private ToscaUtils() {
- }
+ private ToscaUtils() {}
/**
* Check if policy types have been specified is initialized.
@@ -80,6 +79,4 @@ public final class ToscaUtils {
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
}
-
-
}