Undeploy all versions of a policy 93/89593/1
authorJim Hahn <jrh3@att.com>
Fri, 7 Jun 2019 15:33:59 +0000 (11:33 -0400)
committerJim Hahn <jrh3@att.com>
Fri, 7 Jun 2019 15:52:37 +0000 (11:52 -0400)
Modified the code to undeploy all versions of a policy if the
version is not specified.

Change-Id: Ic3815733b918e1cff8f381d54bceb710a35319b7
Issue-ID: POLICY-1782
Signed-off-by: Jim Hahn <jrh3@att.com>
main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java
main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java
main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java
main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java

index 7a3190b..0ca3245 100644 (file)
@@ -20,7 +20,9 @@
 
 package org.onap.policy.pap.main.rest.depundep;
 
+import java.util.Iterator;
 import java.util.function.BiFunction;
+import java.util.function.Predicate;
 import javax.ws.rs.core.Response.Status;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
@@ -117,20 +119,42 @@ public class PdpGroupDeleteProvider extends ProviderBase {
     }
 
     /**
-     * Returns a function that will remove the specified policy from a subgroup.
+     * Returns a function that will remove the desired policy from a subgroup.
      */
     @Override
-    protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy) {
-        ToscaPolicyIdentifier desiredIdent = policy.getIdentifier();
+    protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy,
+                    ToscaPolicyIdentifierOptVersion desiredIdent) {
 
-        // remove the policy from the subgroup
+        // construct a matcher based on whether or not the version was specified
+        Predicate<ToscaPolicyIdentifier> matcher;
+
+        if (desiredIdent.getVersion() != null) {
+            // version was specified - match the whole identifier
+            matcher = policy.getIdentifier()::equals;
+
+        } else {
+            // version was not specified - match the name only
+            String desnm = desiredIdent.getName();
+            matcher = ident -> ident.getName().equals(desnm);
+        }
+
+
+        // return a function that will remove the policy from the subgroup
         return (group, subgroup) -> {
 
-            boolean result = subgroup.getPolicies().remove(desiredIdent);
+            boolean result = false;
+
+            Iterator<ToscaPolicyIdentifier> iter = subgroup.getPolicies().iterator();
+            while (iter.hasNext()) {
+                ToscaPolicyIdentifier ident = iter.next();
 
-            logger.info("remove policy {} {} from subgroup {} {} count={}", desiredIdent.getName(),
-                            desiredIdent.getVersion(), group.getName(), subgroup.getPdpType(),
-                            subgroup.getPolicies().size());
+                if (matcher.test(ident)) {
+                    result = true;
+                    iter.remove();
+                    logger.info("remove policy {} {} from subgroup {} {} count={}", ident.getName(), ident.getVersion(),
+                                    group.getName(), subgroup.getPdpType(), subgroup.getPolicies().size());
+                }
+            }
 
             return result;
         };
index e30bf63..e861375 100644 (file)
@@ -523,7 +523,9 @@ public class PdpGroupDeployProvider extends ProviderBase {
      * Adds a policy to a subgroup, if it isn't there already.
      */
     @Override
-    protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy) {
+    protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy,
+                    ToscaPolicyIdentifierOptVersion requestedIdent) {
+
         ToscaPolicyIdentifier desiredIdent = policy.getIdentifier();
         ToscaPolicyTypeIdentifier desiredType = policy.getTypeIdentifier();
 
index d3f0d13..70ccd7a 100644 (file)
@@ -140,7 +140,7 @@ public abstract class ProviderBase {
                             + desiredPolicy.getName() + " " + desiredPolicy.getVersion());
         }
 
-        BiFunction<PdpGroup, PdpSubGroup, Boolean> updater = makeUpdater(policy);
+        BiFunction<PdpGroup, PdpSubGroup, Boolean> updater = makeUpdater(policy, desiredPolicy);
 
         for (PdpGroup group : groups) {
             upgradeGroup(data, group, updater);
@@ -153,9 +153,11 @@ public abstract class ProviderBase {
      * necessary/appropriate.
      *
      * @param policy policy to be added to or removed from each subgroup
+     * @param desiredPolicy request policy
      * @return a function to update a subgroup
      */
-    protected abstract BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy);
+    protected abstract BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy,
+                    ToscaPolicyIdentifierOptVersion desiredPolicy);
 
     /**
      * Finds the active PDP group(s) that supports the given policy type.
index 8ef9b65..a577e07 100644 (file)
@@ -56,6 +56,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper {
     private MyProvider prov;
     private SessionData session;
     private ToscaPolicyIdentifierOptVersion optIdent;
+    private ToscaPolicyIdentifierOptVersion fullIdent;
     private ToscaPolicyIdentifier ident;
     private BiFunction<PdpGroup, PdpSubGroup, Boolean> updater;
 
@@ -78,10 +79,11 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper {
         session = mock(SessionData.class);
         ident = policy1.getIdentifier();
         optIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), null);
+        fullIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), ident.getVersion());
 
         prov = new MyProvider();
 
-        updater = prov.makeUpdater(policy1);
+        updater = prov.makeUpdater(policy1, fullIdent);
     }
 
     @Test
@@ -163,7 +165,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper {
         when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group));
         when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1));
 
-        new PdpGroupDeleteProvider().undeploy(optIdent);
+        new PdpGroupDeleteProvider().undeploy(fullIdent);
 
         // should have updated the old group
         List<PdpGroup> updates = getGroupUpdates();
@@ -171,7 +173,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper {
         assertSame(group, updates.get(0));
         assertEquals(PdpState.ACTIVE, group.getPdpGroupState());
 
-        // should be one less item in the new group
+        // should be one less item in the new subgroup
         assertEquals(2, group.getPdpSubgroups().get(0).getPolicies().size());
 
         // should have updated the PDPs
@@ -213,17 +215,16 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper {
     }
 
     @Test
-    public void testMakeUpdater() {
+    public void testMakeUpdater_WithVersion() {
         /*
-         * this group has one policy with a different name, one matching policy, and one
-         * with a different version.
+         * this group has two matching policies and one policy with a different name.
          */
         PdpGroup group = loadGroup("undeploy.json");
 
         PdpSubGroup subgroup = group.getPdpSubgroups().get(0);
         int origSize = subgroup.getPolicies().size();
 
-        // invoke updater
+        // invoke updater - matching both name and version
         assertTrue(updater.apply(group, subgroup));
 
         // identified policy should have been removed
@@ -231,6 +232,25 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper {
         assertFalse(subgroup.getPolicies().contains(ident));
     }
 
+    @Test
+    public void testMakeUpdater_NullVersion() {
+        /*
+         * this group has two matching policies and one policy with a different name.
+         */
+        PdpGroup group = loadGroup("undeploy.json");
+
+        PdpSubGroup subgroup = group.getPdpSubgroups().get(0);
+        int origSize = subgroup.getPolicies().size();
+
+        // invoke updater - matching the name, but with a null (i.e., wild-card) version
+        updater = prov.makeUpdater(policy1, optIdent);
+        assertTrue(updater.apply(group, subgroup));
+
+        // identified policy should have been removed
+        assertEquals(origSize - 2, subgroup.getPolicies().size());
+        assertFalse(subgroup.getPolicies().contains(ident));
+    }
+
     @Test
     public void testMakeUpdater_NotFound() {
         /*
index c171e94..3a91363 100644 (file)
@@ -345,7 +345,9 @@ public class TestProviderBase extends ProviderSuper {
         }
 
         @Override
-        protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy) {
+        protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy,
+                        ToscaPolicyIdentifierOptVersion desiredPolicy) {
+
             return (group, subgroup) -> {
                 if (shouldUpdate.remove()) {
                     // queue indicated that the update should succeed