+
+ /**
+ * 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);
+ }