Add changes for safe delete in policy-models 26/101926/1
authorliamfallon <liam.fallon@est.tech>
Tue, 18 Feb 2020 17:44:18 +0000 (17:44 +0000)
committerliamfallon <liam.fallon@est.tech>
Tue, 18 Feb 2020 17:52:02 +0000 (17:52 +0000)
Issue-ID: POLICY-1402
Change-Id: I3f97fbb4c11105118857d354970d6a3de1385f2d
Signed-off-by: liamfallon <liam.fallon@est.tech>
main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java
main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java
main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java
main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java
main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java
main/src/test/resources/policies/vDNS.policy.guard.frequency.no.policyversion.json

index 2a0874d..80c57d3 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP Policy API
  * ================================================================================
  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 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.api.main.rest.provider;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+
 import javax.ws.rs.core.Response;
 
 import org.apache.commons.lang3.tuple.Pair;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.pdp.concepts.PdpGroup;
-import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Class to provide all kinds of legacy guard policy operations.
@@ -49,17 +43,12 @@ import org.slf4j.LoggerFactory;
  */
 public class LegacyGuardPolicyProvider extends CommonModelProvider {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyProvider.class);
-
-    private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
-    private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
     private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
 
     static {
         GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
                 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
-        GUARD_POLICY_TYPE_MAP.put("guard.minmax.",
-                new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
+        GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
         GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
                 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
     }
@@ -82,9 +71,6 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider {
     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
-        if (policyVersion != null) {
-            validNumber(policyVersion, INVALID_POLICY_VERSION);
-        }
         return modelsProvider.getGuardPolicy(policyId, policyVersion);
     }
 
@@ -100,8 +86,8 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider {
     public Map<Pair<String, String>, Map<String, LegacyGuardPolicyOutput>> fetchDeployedGuardPolicies(String policyId)
             throws PfModelException {
 
-        return collectDeployedPolicies(
-                policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy, Map::putAll, new HashMap<>());
+        return collectDeployedPolicies(policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy,
+                Map::putAll, new HashMap<>());
     }
 
     /**
@@ -111,10 +97,8 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider {
      *
      * @return the map of LegacyGuardPolicyOutput objectst
      */
-    public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
-            throws PfModelException {
+    public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
 
-        validateGuardPolicyVersion(body);
         return modelsProvider.createGuardPolicy(body);
     }
 
@@ -129,107 +113,9 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider {
     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
-        validNumber(policyVersion, INVALID_POLICY_VERSION);
-        validateDeleteEligibility(policyId, policyVersion);
-
         return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
     }
 
-    /**
-     * 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;
-    }
-
     /**
      * Retrieves guard policy type given guard policy ID.
      *
index 6bffb2b..a433e02 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP Policy API
  * ================================================================================
  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 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.
@@ -25,18 +26,11 @@ package org.onap.policy.api.main.rest.provider;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import javax.ws.rs.core.Response;
 
 import org.apache.commons.lang3.tuple.Pair;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.pdp.concepts.PdpGroup;
-import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Class to provide all kinds of legacy operational policy operations.
@@ -45,10 +39,6 @@ import org.slf4j.LoggerFactory;
  */
 public class LegacyOperationalPolicyProvider extends CommonModelProvider {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyProvider.class);
-
-    private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
-    private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
 
@@ -70,9 +60,6 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider {
     public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
-        if (policyVersion != null) {
-            validNumber(policyVersion, INVALID_POLICY_VERSION);
-        }
         return modelsProvider.getOperationalPolicy(policyId, policyVersion);
     }
 
@@ -88,8 +75,8 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider {
     public Map<Pair<String, String>, List<LegacyOperationalPolicy>> fetchDeployedOperationalPolicies(String policyId)
             throws PfModelException {
 
-        return collectDeployedPolicies(
-                policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy, List::add, new ArrayList<>(5));
+        return collectDeployedPolicies(policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy,
+                List::add, new ArrayList<>(5));
     }
 
     /**
@@ -101,7 +88,6 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider {
      */
     public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
 
-        validateOperationalPolicyVersion(body);
         return modelsProvider.createOperationalPolicy(body);
     }
 
@@ -116,104 +102,6 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider {
     public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
-        validNumber(policyVersion, INVALID_POLICY_VERSION);
-        validateDeleteEligibility(policyId, policyVersion);
-
         return modelsProvider.deleteOperationalPolicy(policyId, policyVersion);
     }
-
-    /**
-     * 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<>(5);
-        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 specified version of the operational policy provided in the payload.
-     *
-     * @param body the operational policy payload
-     *
-     * @throws PfModelException on errors parsing PfModel
-     */
-    private void validateOperationalPolicyVersion(LegacyOperationalPolicy body) throws PfModelException {
-
-        validateOperationalPolicyVersionExist(body);
-        validateNoDuplicateVersionInDb(body);
-    }
-
-    /**
-     * Validates whether the version of the operational policy is specified in the payload.
-     *
-     * @param body the operational policy payload
-     *
-     * @throws PfModelException on errors parsing PfModel
-     */
-    private void validateOperationalPolicyVersionExist(LegacyOperationalPolicy 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 of the operational policy which is already stored in the database.
-     *
-     * @param body the operational policy payload
-     *
-     * @throws PfModelException on errors parsing PfModel
-     */
-    private void validateNoDuplicateVersionInDb(LegacyOperationalPolicy body) throws PfModelException {
-
-        try {
-            modelsProvider.getOperationalPolicy(body.getPolicyId(), body.getPolicyVersion());
-        } catch (PfModelRuntimeException exc) {
-            if (!hasSameOperationalPolicyFound(body, exc)) {
-                return;
-            }
-            throw new PfModelException(exc.getErrorResponse().getResponseCode(), "unexpected runtime error", exc);
-        }
-
-        // There is one duplicate version stored in the DB.
-        // Try to get the latest version
-        LegacyOperationalPolicy latest = modelsProvider.getOperationalPolicy(body.getPolicyId(), null);
-        final String[] versionArray = latest.getPolicyVersion().split("\\.");
-        String errMsg = "operational 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 operational policy found in the database.
-     *
-     * @param body the legacy operational policy payload
-     * @param exc the runtime exception thrown by policy model provider
-     *
-     * @return a boolean flag indicating the check result
-     */
-    private boolean hasSameOperationalPolicyFound(LegacyOperationalPolicy 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;
-    }
 }
\ No newline at end of file
index 15e409c..6b12395 100644 (file)
@@ -32,12 +32,8 @@ import javax.ws.rs.core.Response;
 import org.apache.commons.lang3.tuple.Pair;\r
 import org.onap.policy.models.base.PfConceptKey;\r
 import org.onap.policy.models.base.PfModelException;\r
-import org.onap.policy.models.pdp.concepts.PdpGroup;\r
-import org.onap.policy.models.pdp.concepts.PdpGroupFilter;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;\r
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
 \r
 /**\r
@@ -168,8 +164,6 @@ public class PolicyProvider extends CommonModelProvider {
     public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
             String policyVersion) throws PfModelException {\r
 \r
-        validateDeleteEligibility(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
-\r
         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
 \r
         if (!hasPolicy(serviceTemplate)) {\r
@@ -180,35 +174,6 @@ public class PolicyProvider extends CommonModelProvider {
         return serviceTemplate;\r
     }\r
 \r
-    /**\r
-     * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.\r
-     *\r
-     * @param policyTypeId the ID of policy type\r
-     * @param policyTypeVersion the version of policy type\r
-     * @param policyId the ID of policy\r
-     * @param policyVersion the version of policy\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion, String policyId,\r
-            String policyVersion) throws PfModelException {\r
-\r
-        // TODO: Remove this method when delete validation is implemented in the tosca provider\r
-        List<ToscaPolicyTypeIdentifier> policyTypes = new ArrayList<>(1);\r
-        policyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));\r
-        List<ToscaPolicyIdentifier> policies = new ArrayList<>(1);\r
-        policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));\r
-        PdpGroupFilter pdpGroupFilter =\r
-                PdpGroupFilter.builder().policyTypeList(policyTypes).policyList(policies).build();\r
-\r
-        List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
-\r
-        if (!pdpGroups.isEmpty()) {\r
-            throw new PfModelException(Response.Status.CONFLICT,\r
-                    constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));\r
-        }\r
-    }\r
-\r
     /**\r
      * Retrieves the specified version of the policy.\r
      *\r
index d325f2c..6c8e73e 100644 (file)
 \r
 package org.onap.policy.api.main.rest.provider;\r
 \r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import java.util.Map.Entry;\r
-\r
 import javax.ws.rs.core.Response;\r
 \r
 import org.onap.policy.models.base.PfModelException;\r
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
 \r
@@ -107,7 +100,7 @@ public class PolicyTypeProvider extends CommonModelProvider {
             throw new PfModelException(Response.Status.BAD_REQUEST,\r
                     "no policy types specified in the service template");\r
         }\r
-        validatePolicyTypeVersionExist(body);\r
+\r
         return modelsProvider.createPolicyTypes(body);\r
     }\r
 \r
@@ -124,8 +117,6 @@ public class PolicyTypeProvider extends CommonModelProvider {
     public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)\r
             throws PfModelException {\r
 \r
-        validateDeleteEligibility(policyTypeId, policyTypeVersion);\r
-\r
         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);\r
 \r
         if (!hasPolicyType(serviceTemplate)) {\r
@@ -136,50 +127,6 @@ public class PolicyTypeProvider extends CommonModelProvider {
         return serviceTemplate;\r
     }\r
 \r
-    /**\r
-     * Validates whether specified policy type can be deleted based on the rule that policy type parameterized by at\r
-     * least one policies cannot be deleted.\r
-     *\r
-     * @param policyTypeId the ID of policy type\r
-     * @param policyTypeVersion the version of policy type\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
-\r
-        ToscaPolicyFilter policyFilter =\r
-                ToscaPolicyFilter.builder().type(policyTypeId).typeVersion(policyTypeVersion).build();\r
-        List<ToscaPolicy> policies = modelsProvider.getFilteredPolicyList(policyFilter);\r
-        if (!policies.isEmpty()) {\r
-            throw new PfModelException(Response.Status.CONFLICT,\r
-                    constructDeletePolicyTypeViolationMessage(policyTypeId, policyTypeVersion, policies));\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Validates that each policy type has a version specified in the payload.\r
-     *\r
-     * @param body the TOSCA service template payload to check against\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private void validatePolicyTypeVersionExist(ToscaServiceTemplate body) throws PfModelException {\r
-\r
-        List<String> invalidPolicyTypeNames = new ArrayList<>();\r
-        for (Entry<String, ToscaPolicyType> policyType : body.getPolicyTypes().entrySet()) {\r
-            if (!"tosca.policies.Root".equals(policyType.getValue().getDerivedFrom())\r
-                    && policyType.getValue().getVersion() == null) {\r
-                invalidPolicyTypeNames.add(policyType.getKey());\r
-            }\r
-        }\r
-\r
-        if (!invalidPolicyTypeNames.isEmpty()) {\r
-            String errorMsg = "mandatory 'version' field is missing in policy types: "\r
-                    + String.join(", ", invalidPolicyTypeNames);\r
-            throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMsg);\r
-        }\r
-    }\r
-\r
     /**\r
      * Retrieves the specified version of the policy type.\r
      *\r
index 291bddd..d531a0a 100644 (file)
@@ -616,7 +616,7 @@ public class TestApiRestServer {
         Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
         ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
-        assertEquals("policies for onap.restart.tca:1.0.0 do not exist", error.getErrorMessage());
+        assertEquals("no policies found", error.getErrorMessage());
     }
 
     @Test
index 237da68..342d17b 100644 (file)
@@ -58,6 +58,7 @@ import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
 
@@ -131,7 +132,7 @@ public class TestLegacyGuardPolicyProvider {
 
         assertThatThrownBy(() -> {
             guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression");
 
         assertThatCode(() -> {
             ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
@@ -165,11 +166,11 @@ public class TestLegacyGuardPolicyProvider {
 
         assertThatThrownBy(() -> {
             guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1.0.0");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression");
 
         assertThatThrownBy(() -> {
             guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "latest");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"latest.0.0\", does not match regular expression");
 
         assertThatCode(() -> {
             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
@@ -260,8 +261,7 @@ public class TestLegacyGuardPolicyProvider {
             // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
             assertThatThrownBy(() -> {
                 guardPolicyProvider.deleteGuardPolicy(POLICY_NAME, POLICY_VERSION);
-            }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
-                    + " cannot be deleted as it is deployed in pdp groups");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
         } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
@@ -290,19 +290,26 @@ public class TestLegacyGuardPolicyProvider {
                 createdPolicy.get("guard.frequency.scaleout").getType());
         assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
 
-        assertThatThrownBy(() -> {
-            String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
-            LegacyGuardPolicyInput badPolicyToCreate =
-                    standardCoder.decode(badPolicyString, LegacyGuardPolicyInput.class);
-            guardPolicyProvider.createGuardPolicy(badPolicyToCreate);
-        }).hasMessage("mandatory field 'policy-version' is missing in the policy: guard.frequency.scaleout");
+        String defaultPolicyVersionString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
+        LegacyGuardPolicyInput defaultVersionPolicy =
+                standardCoder.decode(defaultPolicyVersionString, LegacyGuardPolicyInput.class);
+        createdPolicy = guardPolicyProvider.createGuardPolicy(defaultVersionPolicy);
+        assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout.noversion").getVersion());
+
+        assertThatCode(() -> {
+            String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+            LegacyGuardPolicyInput duplicatePolicyToCreate =
+                    standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class);
+            guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate);
+        }).doesNotThrowAnyException();
 
         assertThatThrownBy(() -> {
             String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
             LegacyGuardPolicyInput duplicatePolicyToCreate =
                     standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class);
+            duplicatePolicyToCreate.setContent(new LegacyGuardPolicyContent());
             guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate);
-        }).hasMessage("guard policy guard.frequency.scaleout:1 already exists; its latest version is 1");
+        }).hasMessageContaining("INVALID:entity in incoming fragment does not equal existing entity");
     }
 
     @Test
@@ -374,7 +381,7 @@ public class TestLegacyGuardPolicyProvider {
                     databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
             assertThatThrownBy(() -> {
                 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
-            }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
         } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
@@ -384,11 +391,11 @@ public class TestLegacyGuardPolicyProvider {
     public void testDeleteGuardPolicy() {
         assertThatThrownBy(() -> {
             guardPolicyProvider.deleteGuardPolicy("dummy", null);
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
 
         assertThatThrownBy(() -> {
             guardPolicyProvider.deleteGuardPolicy("dummy", "1.0.0");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression");
 
         assertThatCode(() -> {
             ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
index f9fc666..c008495 100644 (file)
@@ -128,7 +128,7 @@ public class TestLegacyOperationalPolicyProvider {
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression");
 
         ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
                 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
@@ -151,12 +151,12 @@ public class TestLegacyOperationalPolicyProvider {
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression");
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest");
             ;
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"latest.0.0\", does not match regular expression");
 
         operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
         policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
@@ -246,8 +246,7 @@ public class TestLegacyOperationalPolicyProvider {
             // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
             assertThatThrownBy(() -> {
                 operationalPolicyProvider.deleteOperationalPolicy(POLICY_NAME, POLICY_VERSION);
-            }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
-                    + " cannot be deleted as it is deployed in pdp groups");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
         } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
@@ -274,19 +273,26 @@ public class TestLegacyOperationalPolicyProvider {
         assertEquals("operational.restart", createdPolicy.getPolicyId());
         assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
 
-        assertThatThrownBy(() -> {
-            String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
-            LegacyOperationalPolicy badPolicyToCreate =
-                    standardCoder.decode(badPolicyString, LegacyOperationalPolicy.class);
-            operationalPolicyProvider.createOperationalPolicy(badPolicyToCreate);
-        }).hasMessage("mandatory field 'policy-version' is missing in the policy: operational.scaleout");
+        String defaultPolicyVersionString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
+        LegacyOperationalPolicy defaultPolicyVersionPolicy =
+                standardCoder.decode(defaultPolicyVersionString, LegacyOperationalPolicy.class);
+        createdPolicy = operationalPolicyProvider.createOperationalPolicy(defaultPolicyVersionPolicy);
+        assertEquals("1", createdPolicy.getPolicyVersion());
+
+        assertThatCode(() -> {
+            String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+            LegacyOperationalPolicy duplicatePolicyToCreate =
+                    standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
+            operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate);
+        }).doesNotThrowAnyException();
 
         assertThatThrownBy(() -> {
             String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
             LegacyOperationalPolicy duplicatePolicyToCreate =
                     standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
+            duplicatePolicyToCreate.setContent("some other content");
             operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate);
-        }).hasMessage("operational policy operational.restart:1 already exists; its latest version is 1");
+        }).hasMessageContaining("INVALID:entity in incoming fragment does not equal existing entity");
     }
 
     @Test
@@ -357,7 +363,7 @@ public class TestLegacyOperationalPolicyProvider {
                     databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
             assertThatThrownBy(() -> {
                 operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion);
-            }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
         } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
@@ -368,11 +374,11 @@ public class TestLegacyOperationalPolicyProvider {
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression");
 
         assertThatCode(() -> {
             ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
index 31be84a..d9b01fd 100644 (file)
@@ -236,8 +236,7 @@ public class TestPolicyProvider {
             assertThatThrownBy(() -> {
                 policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca",
                         "1.0.0");
-            }).hasMessageContaining("policy with ID " + policyId + ":" + policyVersion
-                    + " cannot be deleted as it is deployed in pdp groups");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
         } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
@@ -430,6 +429,6 @@ public class TestPolicyProvider {
         assertThatThrownBy(() -> {
             policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca",
                     "1.0.0");
-        }).hasMessageContaining("policies for onap.restart.tca:1.0.0 do not exist");
+        }).hasMessageContaining("no policies found");
     }
 }
index 3c64e64..82d2524 100644 (file)
@@ -158,7 +158,7 @@ public class TestPolicyTypeProvider {
                     standardYamlCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION),
                             ToscaServiceTemplate.class);
             policyTypeProvider.createPolicyType(badPolicyType);
-        }).hasMessage("mandatory 'version' field is missing in policy types: onap.policies.optimization.Resource");
+        }).hasMessageContaining("INVALID:key version is a null version");
 
         policyTypeProvider.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION);
     }
@@ -204,8 +204,8 @@ public class TestPolicyTypeProvider {
                 .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class);
         policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
 
-        String exceptionMessage = "policy type with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 "
-                + "cannot be deleted as it is parameterized by policies onap.restart.tca:1.0.0";
+        String exceptionMessage = "policy type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 is in use, "
+                + "it is referenced in policy onap.restart.tca:1.0.0";
         assertThatThrownBy(() -> {
             policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0");
         }).hasMessage(exceptionMessage);
@@ -219,6 +219,6 @@ public class TestPolicyTypeProvider {
 
         assertThatThrownBy(() -> {
             policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0");
-        }).hasMessage("policy types for onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 do not exist");
+        }).hasMessage("policy type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 not found");
     }
 }
index 346765e..518f09e 100644 (file)
@@ -1,5 +1,5 @@
 {
-  "policy-id" : "guard.frequency.scaleout",
+  "policy-id" : "guard.frequency.scaleout.noversion",
   "content" : {
             "actor": "SO",
             "recipe": "VF Module Create",