From b477554c29b2d666e7bb0bc860afacc6e935c337 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 18 Feb 2020 16:14:59 +0000 Subject: Add safe entity delete, fix multiple entity get This review implements checks on whether entities are being used or referenced propr to allowing deletes. The review also fixes bugs on multiple entity get returning 4040 not found Issue-ID: POLICY-1402 Change-Id: I9cebb9a873098740e9ff4be6284d6307e19838bd Signed-off-by: liamfallon --- .../impl/DatabasePolicyModelsProviderImpl.java | 56 ++++++++++++++++ .../impl/DummyPolicyModelsProviderImpl.java | 2 +- .../impl/DatabasePolicyModelsProviderTest.java | 76 ++++++++++++++++++++++ .../impl/DummyPolicyModelsProviderTest.java | 7 ++ 4 files changed, 140 insertions(+), 1 deletion(-) (limited to 'models-provider/src') diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index 08c01b68f..73c66d072 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -49,8 +49,10 @@ import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; @@ -182,6 +184,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version) throws PfModelException { assertInitialized(); + + ToscaPolicyTypeIdentifier policyTypeIdentifier = new ToscaPolicyTypeIdentifier(name, version); + assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier); + return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version); } @@ -227,6 +233,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version) throws PfModelException { assertInitialized(); + + ToscaPolicyIdentifier policyIdentifier = new ToscaPolicyIdentifier(name, version); + assertPolicyNotDeployedInPdpGroup(policyIdentifier); + return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version); } @@ -255,6 +265,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException { assertInitialized(); + + assertPolicyNotDeployedInPdpGroup( + new ToscaPolicyIdentifier(policyId, policyVersion + LegacyProvider.LEGACY_MINOR_PATCH_SUFFIX)); + return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion); } @@ -283,6 +297,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { public Map deleteGuardPolicy(@NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException { assertInitialized(); + + assertPolicyNotDeployedInPdpGroup( + new ToscaPolicyIdentifier(policyId, policyVersion + LegacyProvider.LEGACY_MINOR_PATCH_SUFFIX)); + return new LegacyProvider().deleteGuardPolicy(pfDao, policyId, policyVersion); } @@ -375,4 +393,42 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } } + + /** + * Assert that the policy type is not supported in any PDP group. + * + * @param policyTypeIdentifier the policy type identifier + * @throws PfModelException if the policy type is supported in a PDP group + */ + private void assertPolicyTypeNotSupportedInPdpGroup(ToscaPolicyTypeIdentifier policyTypeIdentifier) + throws PfModelException { + for (PdpGroup pdpGroup : getPdpGroups(null)) { + for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) { + if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, + "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup " + + pdpSubGroup.getPdpType()); + } + } + } + } + + /** + * Assert that the policy is not deployed in a PDP group. + * + * @param policyIdentifier the identifier of the policy + * @throws PfModelException thrown if the policy is deployed in a PDP group + */ + private void assertPolicyNotDeployedInPdpGroup(final ToscaPolicyIdentifier policyIdentifier) + throws PfModelException { + for (PdpGroup pdpGroup : getPdpGroups(null)) { + for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) { + if (pdpSubGroup.getPolicies().contains(policyIdentifier)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, + "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup " + + pdpSubGroup.getPdpType()); + } + } + } + } } diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java index 3d1c9f61c..c09c6c233 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java @@ -258,7 +258,7 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { @Override public List deletePdpStatistics(final String name, final Date timestamp) { // Not implemented - return null; + return new ArrayList<>(); } /** diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index aa6802a5a..f085605f8 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -45,6 +45,7 @@ import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -536,4 +537,79 @@ public class DatabasePolicyModelsProviderTest { databaseProvider.close(); } + + @Test + public void testDeletePolicyDeployedInSubgroup() throws PfModelException { + List policies = new ArrayList<>(); + + policies.add(new ToscaPolicyIdentifier("p0", "0.0.1")); + policies.add(new ToscaPolicyIdentifier("p1", "0.0.1")); + + List supportedPolicyTypes = new ArrayList<>(); + supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt2", "0.0.1")); + + PdpSubGroup subGroup = new PdpSubGroup(); + subGroup.setPdpType("pdpType"); + subGroup.setSupportedPolicyTypes(supportedPolicyTypes); + subGroup.setPolicies(policies); + + List pdpSubgroups = new ArrayList<>(); + pdpSubgroups.add(subGroup); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("pdpGroup"); + pdpGroup.setPdpGroupState(PdpState.PASSIVE); + pdpGroup.setPdpSubgroups(pdpSubgroups); + + List pdpGroups = new ArrayList<>(); + pdpGroups.add(pdpGroup); + + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + databaseProvider.createPdpGroups(pdpGroups); + + assertThatThrownBy(() -> databaseProvider.deletePolicy("p0", "0.0.1")) + .hasMessageContaining("policy is in use, it is deployed in PDP group pdpGroup subgroup pdpType"); + + assertThatThrownBy(() -> databaseProvider.deletePolicy("p3", "0.0.1")) + .hasMessageContaining("service template not found in database"); + + databaseProvider.close(); + } + + @Test + public void testDeletePolicyTypeSupportedInSubgroup() throws PfModelException { + List supportedPolicyTypes = new ArrayList<>(); + supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt1", "0.0.1")); + supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt2", "0.0.1")); + + PdpSubGroup subGroup = new PdpSubGroup(); + subGroup.setPdpType("pdpType"); + subGroup.setSupportedPolicyTypes(supportedPolicyTypes); + + List pdpSubgroups = new ArrayList<>(); + pdpSubgroups.add(subGroup); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("pdpGroup"); + pdpGroup.setPdpGroupState(PdpState.PASSIVE); + pdpGroup.setPdpSubgroups(pdpSubgroups); + + List pdpGroups = new ArrayList<>(); + pdpGroups.add(pdpGroup); + + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + databaseProvider.createPdpGroups(pdpGroups); + + assertThatThrownBy(() -> databaseProvider.deletePolicyType("pt2", "0.0.1")) + .hasMessageContaining("policy type is in use, it is referenced in PDP group pdpGroup subgroup pdpType"); + + assertThatThrownBy(() -> databaseProvider.deletePolicyType("pt0", "0.0.1")) + .hasMessageContaining("service template not found in database"); + + databaseProvider.close(); + } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java index 500d9d4c0..3212428ae 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java @@ -28,6 +28,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.Date; import org.junit.Test; import org.onap.policy.models.pdp.concepts.Pdp; @@ -117,6 +118,12 @@ public class DummyPolicyModelsProviderTest { dummyProvider.updatePdp("name", "type", new Pdp()); dummyProvider.updatePdpStatistics(new ArrayList<>()); assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty()); + + assertTrue( + dummyProvider.getFilteredPdpStatistics("name", null, null, new Date(), new Date(), null, 0).isEmpty()); + assertTrue(dummyProvider.createPdpStatistics(null).isEmpty()); + assertTrue(dummyProvider.updatePdpStatistics(null).isEmpty()); + assertTrue(dummyProvider.deletePdpStatistics(null, new Date()).isEmpty()); } @Test -- cgit 1.2.3-korg