From 76a0b386bcf5f30533c85f6d26c559bea19ce0fb Mon Sep 17 00:00:00 2001 From: sebdet Date: Fri, 22 Jan 2021 17:26:04 +0100 Subject: Policy list and Pdpgroup merger code Draft code of the policy list/pdpgroup merger + tests, this code will most likely be called by a camel Rest route and the answer will be returned to the client/ui. The policies listing is enriched with new fields pdp group info, like: 1. In which Pdp group/Subgroup the policy is currently deployed 2. What are the potential pdpgroups/subgroups where the policy can be deployed These info will be shown in the policy clamp UI to the user. Also some refactoring has been done for the sake of clarity. Issue-ID: POLICY-2924 Signed-off-by: sebdet Change-Id: Iba1ab6fea90735551d7d2e2bbff9ab2efdee093e Signed-off-by: sebdet --- .../policy/pdpgroup/PdpGroupAnalyzerTest.java | 82 ++++++++++------- .../policy/pdpgroup/PoliciesPdpMergerTest.java | 102 +++++++++++++++++++++ 2 files changed, 149 insertions(+), 35 deletions(-) create mode 100644 src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java (limited to 'src/test/java/org/onap') diff --git a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java index 1af048da5..5a9cac07b 100644 --- a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java +++ b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * ONAP CLAMP + * ONAP POLICY-CLAMP * ================================================================================ * Copyright (C) 2021 AT&T Intellectual Property. All rights * reserved. @@ -25,19 +25,15 @@ package org.onap.policy.clamp.policy.pdpgroup; import static org.assertj.core.api.Assertions.assertThat; -import com.google.gson.Gson; -import com.google.gson.JsonObject; import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import javax.transaction.Transactional; +import org.junit.BeforeClass; import org.junit.Test; -import org.onap.policy.clamp.clds.util.JsonUtils; import org.onap.policy.clamp.loop.template.PolicyModel; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** @@ -45,33 +41,13 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi */ public class PdpGroupAnalyzerTest { - private PolicyModel createPolicyModel(String policyType, String policyModelTosca, String version, - String policyAcronym, String policyVariant, String createdBy) { - PolicyModel policyModel = new PolicyModel(); - policyModel.setCreatedBy(createdBy); - policyModel.setPolicyAcronym(policyAcronym); - policyModel.setPolicyModelTosca(policyModelTosca); - policyModel.setPolicyModelType(policyType); - policyModel.setUpdatedBy(createdBy); - policyModel.setVersion(version); - return policyModel; - } + private static PdpGroups pdpGroups; /** - * This tests the pdpgroup GSON encode/decode and saving. + * This method preloads the pdpGroups for the tests. */ - @Test - @Transactional - public void testUpdatePdpGroup() { - // Create policyModel - PolicyModel policyModel = new PolicyModel(); - policyModel.setCreatedBy("user"); - policyModel.setPolicyAcronym("TEST"); - policyModel.setPolicyModelTosca("yaml"); - policyModel.setPolicyModelType("org.onap.test"); - policyModel.setUpdatedBy("user"); - policyModel.setVersion("1.0.0"); - + @BeforeClass + public static void setupPdpGroup() { // Create Pdp Groups // Those that do not work first PdpSubGroup pdpSubgroupBad = new PdpSubGroup(); @@ -83,7 +59,11 @@ public class PdpGroupAnalyzerTest { new ToscaPolicyTypeIdentifier("org.onap.tes", "1.0.0"), new ToscaPolicyTypeIdentifier("org.onap", "1.0.0") )); - + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap", "1.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "1.0.1"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos.new", "1.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.*", "1.0.0"))); PdpSubGroup pdpSubgroup1 = new PdpSubGroup(); pdpSubgroup1.setPdpType("subGroup1"); @@ -93,38 +73,70 @@ public class PdpGroupAnalyzerTest { PdpSubGroup pdpSubgroup2 = new PdpSubGroup(); pdpSubgroup2.setPdpType("subGroup2"); pdpSubgroup2.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.test", "1.0.0"))); + pdpSubgroup2.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "1.0.0"))); PdpSubGroup pdpSubgroup3 = new PdpSubGroup(); pdpSubgroup3.setPdpType("subGroup3"); pdpSubgroup3.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.test*", "1.0.0"))); + pdpSubgroup3.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"))); - + // Should match pdpSubgroup1 PdpGroup pdpGroup1 = new PdpGroup(); pdpGroup1.setName("pdpGroup1"); pdpGroup1.setPdpGroupState(PdpState.ACTIVE); pdpGroup1.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroupBad)); + // Should match pdpSubgroup1, pdpSubgroup2, pdpSubgroup3 + // Should match also for the policy (pdpSubgroup2) PdpGroup pdpGroup2 = new PdpGroup(); pdpGroup2.setName("pdpGroup2"); pdpGroup2.setPdpGroupState(PdpState.ACTIVE); pdpGroup2.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroup2, pdpSubgroup3, pdpSubgroupBad)); + /// Should not match PdpGroup pdpGroup3 = new PdpGroup(); pdpGroup3.setName("pdpGroup3"); pdpGroup3.setPdpGroupState(PdpState.ACTIVE); pdpGroup3.setPdpSubgroups(Arrays.asList(pdpSubgroupBad)); + // Should not match PdpGroup pdpGroup4 = new PdpGroup(); pdpGroup4.setName("pdpGroup4"); pdpGroup4.setPdpGroupState(PdpState.PASSIVE); pdpGroup4.setPdpSubgroups(Arrays.asList(pdpSubgroup1)); - PdpGroups pdpGroups = new PdpGroups(); + pdpGroups = new PdpGroups(); pdpGroups.setGroups(Arrays.asList(pdpGroup1, pdpGroup2, pdpGroup3, pdpGroup4)); - PdpGroupsAnalyzer.updatePdpGroup(Arrays.asList(policyModel), pdpGroups); + } + + @Test + public void testUpdatePdpGroupOfPolicyModels() { + // Create policyModel + PolicyModel policyModel = new PolicyModel(); + policyModel.setCreatedBy("user"); + policyModel.setPolicyAcronym("TEST"); + policyModel.setPolicyModelTosca("yaml"); + policyModel.setPolicyModelType("org.onap.test"); + policyModel.setUpdatedBy("user"); + policyModel.setVersion("1.0.0"); + PdpGroupsAnalyzer.updatePdpGroupOfPolicyModels(Arrays.asList(policyModel), pdpGroups); assertThat(policyModel.getPolicyPdpGroup().toString()).isEqualTo( "{\"supportedPdpGroups\":[{\"pdpGroup1\":[\"subGroup1\"]}," + "{\"pdpGroup2\":[\"subGroup1\",\"subGroup2\",\"subGroup3\"]}]}"); } + + @Test + public void testGetPdpGroupDeploymentsOfOnePolicy() { + assertThat( + PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.testos", "1.0.0").toString()) + .isEqualTo("{\"pdpGroupInfo\":{\"pdpGroup\":\"pdpGroup2\",\"pdpSubGroup\":\"subGroup2\"}}"); + } + + @Test + public void testGetPdpGroupDeploymentsOfOnePolicyNull() { + assertThat( + PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.DoNotExist", "1.0.0")) + .isNull(); + } } diff --git a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java new file mode 100644 index 000000000..b3fd3f19a --- /dev/null +++ b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java @@ -0,0 +1,102 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY-CLAMP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.clamp.policy.pdpgroup; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.util.Arrays; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.clamp.clds.util.JsonUtils; +import org.onap.policy.clamp.clds.util.ResourceFileUtils; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.skyscreamer.jsonassert.JSONAssert; + +/** + * This test class validates the PdpGroupAnalyzer class. + */ +public class PoliciesPdpMergerTest { + + private static String pdpGroupsJson; + + /** + * This method setups the pdpGroups required for the tests. + */ + @BeforeClass + public static void setupPdpGroup() { + // Create Pdp Groups + PdpSubGroup pdpSubgroup1 = new PdpSubGroup(); + pdpSubgroup1.setPdpType("subGroup1"); + pdpSubgroup1.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.*", "1.0.0"))); + + PdpSubGroup pdpSubgroup2 = new PdpSubGroup(); + pdpSubgroup2.setPdpType("subGroup2"); + pdpSubgroup2.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"), + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", "1.0.0"))); + pdpSubgroup2.setPolicies(Arrays.asList( + new ToscaPolicyIdentifier("MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", "1.0.0"))); + + PdpSubGroup pdpSubgroup3 = new PdpSubGroup(); + pdpSubgroup3.setPdpType("subGroup3"); + pdpSubgroup3.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"), + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", "1.0.0"))); + pdpSubgroup3.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"), + new ToscaPolicyIdentifier("OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", "1.0.0"))); + + // Should match pdpSubgroup1 + PdpGroup pdpGroup1 = new PdpGroup(); + pdpGroup1.setName("pdpGroup1"); + pdpGroup1.setPdpGroupState(PdpState.ACTIVE); + pdpGroup1.setPdpSubgroups(Arrays.asList(pdpSubgroup1)); + + // Should match pdpSubgroup1, pdpSubgroup2, pdpSubgroup3 + // Should match also for the policy (pdpSubgroup2) + PdpGroup pdpGroup2 = new PdpGroup(); + pdpGroup2.setName("pdpGroup2"); + pdpGroup2.setPdpGroupState(PdpState.ACTIVE); + pdpGroup2.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroup2, pdpSubgroup3)); + + PdpGroups pdpGroups = new PdpGroups(); + pdpGroups.setGroups(Arrays.asList(pdpGroup1, pdpGroup2)); + + pdpGroupsJson = JsonUtils.GSON.toJson(pdpGroups); + } + + @Test + public void testUpdatePdpGroupOfPolicyModels() throws IOException { + JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("clds/policy-merger.json"), PoliciesPdpMerger + .mergePoliciesAndPdpGroupStates( + ResourceFileUtils.getResourceAsString("http-cache/example/policy/api/v1/policies/.file"), + pdpGroupsJson), true); + } +} -- cgit 1.2.3-korg