Support policy types with wild-cards 75/98075/1
authorJim Hahn <jrh3@att.com>
Wed, 6 Nov 2019 17:19:33 +0000 (12:19 -0500)
committerJim Hahn <jrh3@att.com>
Wed, 6 Nov 2019 20:52:56 +0000 (15:52 -0500)
Allow supported policy types to end with ".*", which causes the prefix
to be matched when new policies are deployed.  This entailed updates
in three areas:
- when a subgroup is added via the group api
- when a subgroup is updated via the group api
- when a policy is deployed via the simple api

Issue-ID: POLICY-1636
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: I4ae15971481ce5b2042b5d6fdfd16e11ad099c50
Signed-off-by: Jim Hahn <jrh3@att.com>
main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java
main/src/test/resources/simpleDeploy/createGroupsWildCard.json [new file with mode: 0644]
main/src/test/resources/simpleDeploy/daoPolicyListWildCard.json [new file with mode: 0644]
main/src/test/resources/simpleDeploy/daoPolicyListWildCardUnmatched.json [new file with mode: 0644]
main/src/test/resources/simpleDeploy/deployPoliciesWildCard.json [new file with mode: 0644]

index 1e00528..da9e46f 100644 (file)
@@ -441,6 +441,7 @@ public class PdpGroupDeployProvider extends ProviderBase {
         }
 
         result.addResult(validatePolicies(data, dbsub, subgrp));
+        result.addResult(validateSupportedTypes(data, subgrp));
         container.addResult(result);
 
         return result.isValid();
@@ -480,7 +481,7 @@ public class PdpGroupDeployProvider extends ProviderBase {
         BeanValidationResult result = new BeanValidationResult(subgrp.getPdpType(), subgrp);
 
         for (ToscaPolicyTypeIdentifier type : subgrp.getSupportedPolicyTypes()) {
-            if (data.getPolicyType(type) == null) {
+            if (!type.getName().endsWith(".*") && data.getPolicyType(type) == null) {
                 result.addResult(new ObjectValidationResult("policy type", type, ValidationStatus.INVALID,
                                 "unknown policy type"));
             }
@@ -529,7 +530,7 @@ public class PdpGroupDeployProvider extends ProviderBase {
                 result.addResult(new ObjectValidationResult(POLICY_RESULT_NAME, ident, ValidationStatus.INVALID,
                                 "unknown policy"));
 
-            } else if (!subgrp.getSupportedPolicyTypes().contains(policy.getTypeIdentifier())) {
+            } else if (!isPolicySupported(subgrp.getSupportedPolicyTypes(), policy.getTypeIdentifier())) {
                 result.addResult(new ObjectValidationResult(POLICY_RESULT_NAME, ident, ValidationStatus.INVALID,
                                 "not a supported policy for the subgroup"));
 
@@ -613,7 +614,7 @@ public class PdpGroupDeployProvider extends ProviderBase {
 
         return (group, subgroup) -> {
 
-            if (!subgroup.getSupportedPolicyTypes().contains(desiredType)) {
+            if (!isPolicySupported(subgroup.getSupportedPolicyTypes(), desiredType)) {
                 // doesn't support the desired policy type
                 return false;
             }
@@ -642,6 +643,32 @@ public class PdpGroupDeployProvider extends ProviderBase {
         };
     }
 
+    /**
+     * Determines if a policy type is supported.
+     *
+     * @param supportedTypes supported policy types, any of which may end with ".*"
+     * @param desiredType policy type of interest
+     * @return {@code true} if the policy type is supported, {@code false} otherwise
+     */
+    private boolean isPolicySupported(List<ToscaPolicyTypeIdentifier> supportedTypes,
+                    ToscaPolicyTypeIdentifier desiredType) {
+
+        if (supportedTypes.contains(desiredType)) {
+            return true;
+        }
+
+        String desiredTypeName = desiredType.getName();
+        for (ToscaPolicyTypeIdentifier type : supportedTypes) {
+            String supType = type.getName();
+            if (supType.endsWith(".*") && desiredTypeName.startsWith(supType.substring(0, supType.length() - 1))) {
+                // matches everything up to, AND INCLUDING, the "."
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Determines if a subgroup already contains the desired policy.
      *
index c2d368a..899b569 100644 (file)
@@ -325,7 +325,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
         PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0);
         subgrp.setDesiredInstanceCount(30);
         subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION));
-        subgrp.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("typeX", "9.8.7"));
+        subgrp.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7"));
 
         when(dao.getFilteredPolicyList(any()))
                         .thenReturn(loadPolicies("createGroupNewPolicy.json"))
@@ -418,6 +418,49 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
         assertGroupUpdateOnly(group);
     }
 
+    /**
+     * Tests addSubgroup() when the new subgroup has a wild-card policy type.
+     *
+     * @throws Exception if an error occurs
+     */
+    @Test
+    public void testAddSubGroupWildCardPolicyType() throws Exception {
+        when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json"));
+        when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList());
+
+        PdpGroups groups = loadPdpGroups("createGroupsWildCard.json");
+        PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0);
+        when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
+
+        prov.createOrUpdateGroups(groups);
+
+        PdpGroup newgrp = groups.getGroups().get(0);
+
+        PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1);
+        newsub.setCurrentInstanceCount(0);
+        newsub.setPdpInstances(new ArrayList<>(0));
+
+        assertEquals(newgrp.toString(), group.toString());
+    }
+
+    /**
+     * Tests addSubgroup() when the new subgroup has a wild-card policy type, but the
+     * policy doesn't have a matching type.
+     *
+     * @throws PfModelException if an error occurs
+     */
+    @Test
+    public void testAddSubGroupWildCardPolicyTypeUnmatched() throws PfModelException {
+        when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCardUnmatched.json"));
+        when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList());
+
+        PdpGroups groups = loadPdpGroups("createGroupsWildCard.json");
+        PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0);
+        when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
+
+        assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class);
+    }
+
     @Test
     public void testAddSubGroup_ValidationPolicyTypeNotFound() throws Exception {
         PdpGroups groups = loadPdpGroups("createGroupsNewSub.json");
@@ -526,7 +569,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
         PdpGroup group = new PdpGroup(newgrp);
         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
 
-        newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("typeX", "9.8.7"));
+        newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes()
+                        .add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7"));
 
         prov.createOrUpdateGroups(groups);
 
@@ -652,6 +696,41 @@ public class TestPdpGroupDeployProvider extends ProviderSuper {
         prov.deployPolicies(loadEmptyRequest());
     }
 
+    /**
+     * Tests deployPolicies() when the supported policy type uses a wild-card.
+     *
+     * @throws Exception if an error occurs
+     */
+    @Test
+    public void testDeployPoliciesWildCard() throws Exception {
+        when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("deployPoliciesWildCard.json"));
+        when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json"));
+        when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());
+
+        policy1.setName("policy.some");
+        policy1.setVersion(POLICY1_VERSION);
+        policy1.setType("some.type");
+        policy1.setTypeVersion("100.2.3");
+
+        PdpDeployPolicies depreq = loadRequest();
+        depreq.getPolicies().get(0).setName("policy.some");
+
+        prov.deployPolicies(depreq);
+
+        assertGroup(getGroupUpdates(), GROUP1_NAME);
+
+        List<PdpUpdate> requests = getUpdateRequests(1);
+        assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
+
+        // should have notified of added policy/PDPs
+        ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
+        verify(notifier).addDeploymentData(captor.capture());
+        assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB]");
+
+        // no undeployment notifications
+        verify(notifier, never()).addUndeploymentData(any());
+    }
+
     @Test
     public void testDeploySimplePolicies() throws Exception {
         prov.deployPolicies(loadEmptyRequest());
diff --git a/main/src/test/resources/simpleDeploy/createGroupsWildCard.json b/main/src/test/resources/simpleDeploy/createGroupsWildCard.json
new file mode 100644 (file)
index 0000000..a1d26d1
--- /dev/null
@@ -0,0 +1,56 @@
+{
+    "groups": [
+        {
+            "name": "groupA",
+            "version": "200.2.3",
+            "description": "my description",
+            "pdpGroupState": "ACTIVE",
+            "properties": {
+                "hello": "world"
+            },
+            "pdpSubgroups": [
+                {
+                    "pdpType": "pdpTypeA",
+                    "desiredInstanceCount": 1,
+                    "properties": {
+                        "abc": "def"
+                    },
+                    "supportedPolicyTypes": [
+                        {
+                            "name": "some.*",
+                            "version": "2.3.4"
+                        }
+                    ],
+                    "pdpInstances": [
+                        {
+                            "instanceId": "pdpA"
+                        }
+                    ],
+                    "policies": [
+                        {
+                            "name": "policy.some",
+                            "version": "1.2.3"
+                        }
+                    ]
+                },
+                {
+                    "pdpType": "pdpTypeB",
+                    "desiredInstanceCount": 1,
+                    "currentInstanceCount": 22,
+                    "supportedPolicyTypes": [
+                        {
+                            "name": "typeA",
+                            "version": "100.2.3"
+                        }
+                    ],
+                    "pdpInstances": [
+                        {
+                            "instanceId": "pdpB"
+                        }
+                    ],
+                    "policies": []
+                }
+            ]
+        }
+    ]
+}
diff --git a/main/src/test/resources/simpleDeploy/daoPolicyListWildCard.json b/main/src/test/resources/simpleDeploy/daoPolicyListWildCard.json
new file mode 100644 (file)
index 0000000..ab2f2b8
--- /dev/null
@@ -0,0 +1,10 @@
+{
+    "policies":  [
+        {
+            "name": "policy.some",
+            "version": "1.2.3",
+            "type": "some.type",
+            "type_version": "100.2.3"
+        }
+    ]
+}
diff --git a/main/src/test/resources/simpleDeploy/daoPolicyListWildCardUnmatched.json b/main/src/test/resources/simpleDeploy/daoPolicyListWildCardUnmatched.json
new file mode 100644 (file)
index 0000000..25fd064
--- /dev/null
@@ -0,0 +1,10 @@
+{
+    "policies":  [
+        {
+            "name": "policy.some",
+            "version": "1.2.3",
+            "type": "another.type",
+            "type_version": "100.2.3"
+        }
+    ]
+}
diff --git a/main/src/test/resources/simpleDeploy/deployPoliciesWildCard.json b/main/src/test/resources/simpleDeploy/deployPoliciesWildCard.json
new file mode 100644 (file)
index 0000000..9e43af0
--- /dev/null
@@ -0,0 +1,25 @@
+{
+    "groups": [
+        {
+            "name": "groupA",
+            "version": "200.2.3",
+            "pdpSubgroups": [
+                {
+                    "pdpType": "pdpTypeB",
+                    "supportedPolicyTypes": [
+                        {
+                            "name": "some.*",
+                            "version": "2.3.4"
+                        }
+                    ],
+                    "pdpInstances": [
+                        {
+                            "instanceId": "pdpB"
+                        }
+                    ],
+                    "policies": []
+                }
+            ]
+        }
+    ]
+}