- /**
- * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
- *
- * @param policyId the ID of policy
- * @param policyVersion the version of policy
- *
- * @throws PfModelException the PfModel parsing exception
- */
- private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
-
- List<ToscaPolicyIdentifier> policies = new ArrayList<>();
- policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
- PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
-
- List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
-
- if (!pdpGroups.isEmpty()) {
- throw new PfModelException(Response.Status.CONFLICT,
- constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
- }
- }
-
- /**
- * Validates the provided guard policy version in the payload.
- *
- * @param body the guard policy payload
- *
- * @throws PfModelException the PfModel parsing exception
- */
- private void validateGuardPolicyVersion(LegacyGuardPolicyInput body) throws PfModelException {
-
- validateGuardPolicyVersionExist(body);
- validateNoDuplicateVersionInDb(body);
- }
-
- /**
- * Validates that the guard policy has version specified.
- *
- * @param body the guard policy payload
- *
- * @throws PfModelException the PfModel parsing exception
- */
- private void validateGuardPolicyVersionExist(LegacyGuardPolicyInput body) throws PfModelException {
-
- if (body.getPolicyVersion() == null) {
- String errMsg = "mandatory field 'policy-version' is missing in the policy: " + body.getPolicyId();
- throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg);
- }
- }
-
- /**
- * Validates that there is no duplicate version already stored in the database.
- *
- * @param body the guard policy payload
- *
- * @throws PfModelException the PfModel parsing exception
- */
- private void validateNoDuplicateVersionInDb(LegacyGuardPolicyInput body) throws PfModelException {
-
- try {
- modelsProvider.getGuardPolicy(body.getPolicyId(), body.getPolicyVersion());
- } catch (PfModelRuntimeException exc) {
- if (!hasSameGuardPolicyFound(body, exc)) {
- return;
- }
- throw new PfModelException(exc.getErrorResponse().getResponseCode(), "unexpected runtime error", exc);
- }
-
- // If it gets here, there is one duplicate version stored in the DB.
- // Try to get the latest version and return it to the user.
- Map<String, LegacyGuardPolicyOutput> latest = modelsProvider.getGuardPolicy(body.getPolicyId(), null);
- final String[] versionArray = latest.values().iterator().next().getVersion().split("\\.");
- String errMsg = "guard policy " + body.getPolicyId() + ":" + body.getPolicyVersion()
- + " already exists; its latest version is " + versionArray[0];
- throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg);
- }
-
- /**
- * Checks if the same guard policy found in the database.
- *
- * @param body the legacy guard policy payload
- * @param exc the thrown runtime exception from policy model provider
- *
- * @return a boolean flag indicating the check result
- */
- private boolean hasSameGuardPolicyFound(LegacyGuardPolicyInput body, PfModelRuntimeException exc) {
-
- if (exc.getErrorResponse().getResponseCode() == Response.Status.BAD_REQUEST
- && exc.getErrorResponse().getErrorMessage().contains("no policy found")) {
- LOGGER.debug("no duplicate policy {}:{} found in the DB", body.getPolicyId(), body.getPolicyVersion());
- return false;
- }
- return true;
- }
-