Allow wild cards in supported type filter 38/98738/2
authorJim Hahn <jrh3@att.com>
Thu, 21 Nov 2019 19:59:52 +0000 (14:59 -0500)
committerJim Hahn <jrh3@att.com>
Thu, 21 Nov 2019 20:09:15 +0000 (15:09 -0500)
PAP was rejecting deployment of policies whose supported type
contained a wild card.  This was due to the fact that it was
querying the Pdp Groups using a filter on the supported type,
which did not take wild cards into consideration.  Updated the
filtering.

Change-Id: I50b3202a00ac85ff09a9861d8bbe1efb6dd49ae3
Issue-ID: POLICY-1636
Signed-off-by: Jim Hahn <jrh3@att.com>
models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java
models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupFilterTest.java

index d67f2d4..7faf197 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 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.
@@ -121,7 +122,7 @@ public class PdpGroupFilter implements PfObjectFilter<PdpGroup> {
             if (matchPolicyTypesExactly && areListsIdentical(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
                 return true;
             } else if (!matchPolicyTypesExactly
-                    && findSingleElement(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
+                    && findSupportedPolicyType(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
                 return true;
             }
         }
@@ -130,6 +131,32 @@ public class PdpGroupFilter implements PfObjectFilter<PdpGroup> {
 
     }
 
+    /**
+     * Find a single supported type.
+     *
+     * @param supportedPolicyTypes supported types
+     * @param typeFilter the list of types, one of which we wish to find supported by
+     *        the list we are searching
+     * @return true if one element of the elements to find is supported by an element on
+     *         the list we searched
+     */
+    private boolean findSupportedPolicyType(List<ToscaPolicyTypeIdentifier> supportedPolicyTypes,
+                    List<ToscaPolicyTypeIdentifier> typeFilter) {
+        for (ToscaPolicyTypeIdentifier supportedPolicyType : supportedPolicyTypes) {
+            String supName = supportedPolicyType.getName();
+            if (supName.endsWith(".*")) {
+                String substr = supName.substring(0, supName.length() - 1);
+                if (typeFilter.stream().anyMatch(type -> type.getName().startsWith(substr))) {
+                    return true;
+                }
+            } else if (typeFilter.contains(supportedPolicyType)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Filter PDP groups on policy.
      *
index 808bfe7..9ffb0f4 100644 (file)
@@ -27,7 +27,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
 import java.util.List;
-
+import java.util.stream.Collectors;
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.common.utils.coder.CoderException;
@@ -183,6 +183,22 @@ public class PdpGroupFilterTest {
         assertEquals(0, filteredList.size());
         identifierList.clear();
 
+        // don't match wild cards
+        identifierList.add(new ToscaPolicyTypeIdentifier(NON_EXISTANT, VERSION1));
+        filter = PdpGroupFilter.builder().policyTypeList(identifierList).build();
+        final List<PdpGroup> wildCards =
+                        pdpGroupList.stream().map(this::makeWildCardPolicyTypes).collect(Collectors.toList());
+        filteredList = filter.filter(wildCards);
+        assertEquals(0, filteredList.size());
+        identifierList.clear();
+
+        // match wild cards
+        identifierList.add(new ToscaPolicyTypeIdentifier(POLICY_TYPE0, VERSION1));
+        filter = PdpGroupFilter.builder().policyTypeList(identifierList).build();
+        filteredList = filter.filter(wildCards);
+        assertEquals(4, filteredList.size());
+        identifierList.clear();
+
         identifierList.add(new ToscaPolicyTypeIdentifier(POLICY_TYPE0, VERSION1));
         filter = PdpGroupFilter.builder().policyTypeList(identifierList).build();
         filteredList = filter.filter(pdpGroupList);
@@ -251,6 +267,27 @@ public class PdpGroupFilterTest {
         assertEquals(1, filteredList.size());
     }
 
+    /**
+     * Makes a clone of a PdpGroup, changing all occurrences of supported policy type,
+     * "policy.type.0", to a wild card type, "policy.type.*".
+     *
+     * @param group group to be cloned
+     * @return a new PdpGroup containing wild card policy types
+     */
+    private PdpGroup makeWildCardPolicyTypes(PdpGroup group) {
+        PdpGroup newGroup = new PdpGroup(group);
+
+        for (PdpSubGroup subgroup : newGroup.getPdpSubgroups()) {
+            for (ToscaPolicyTypeIdentifier subType : subgroup.getSupportedPolicyTypes()) {
+                if (POLICY_TYPE0.equals(subType.getName())) {
+                    subType.setName("policy.type.*");
+                }
+            }
+        }
+
+        return newGroup;
+    }
+
     @Test
     public void testFilterPolicy() {
         List<ToscaPolicyIdentifier> identifierList = new ArrayList<>();