Remove policies field from PdpUpdate message 65/121565/2
authorwaynedunican <wayne.dunican@est.tech>
Thu, 27 May 2021 08:12:05 +0000 (09:12 +0100)
committerWayneDunican <wayne.dunican@est.tech>
Thu, 27 May 2021 19:03:00 +0000 (20:03 +0100)
Issue-ID: POLICY-3323
Change-Id: I14dafe4a20f95e2ebe4279e5b6ae141e2ee4fc48
Signed-off-by: waynedunican <wayne.dunican@est.tech>
Signed-off-by: WayneDunican <wayne.dunican@est.tech>
models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpUpdate.java
models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpUpdateTest.java
models-sim/policy-models-sim-pdp/src/main/java/org/onap/policy/models/sim/pdp/handler/PdpUpdateMessageHandler.java
models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpStateChangeListener.java
models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpUpdateListener.java

index 0c088b8..19f79ee 100644 (file)
@@ -51,14 +51,6 @@ public class PdpUpdate extends PdpMessage {
 
     private Long pdpHeartbeatIntervalMs;
 
-    /**
-     * Policies that the PDP should deploy. This is a complete list, so PDPs should be
-     * prepared to deploy new policies listed and undeploy policies that are no longer
-     * listed. Note: this list may be empty, as a PDP may remain attached to a subgroup
-     * even if all of the policies are removed from the subgroup.
-     */
-    private List<ToscaPolicy> policies = new LinkedList<>();
-
     /**
      * Policies that the PDP should deploy.
      */
@@ -87,7 +79,9 @@ public class PdpUpdate extends PdpMessage {
 
         this.description = source.description;
         this.pdpHeartbeatIntervalMs = source.pdpHeartbeatIntervalMs;
-        this.policies = (source.policies == null ? null
-                        : source.policies.stream().map(ToscaPolicy::new).collect(Collectors.toList()));
+        this.policiesToBeDeployed = (source.policiesToBeDeployed == null ? null
+                : source.policiesToBeDeployed.stream().map(ToscaPolicy::new).collect(Collectors.toList()));
+        this.policiesToBeUndeployed = (source.policiesToBeUndeployed == null ? null
+                : source.policiesToBeUndeployed.stream().map(ToscaConceptIdentifier::new).collect(Collectors.toList()));
     }
 }
index c37979f..a24b410 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Policy Models
  * ================================================================================
  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import static org.onap.policy.models.pdp.concepts.PdpMessageUtils.removeVariable
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 import org.junit.Test;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
@@ -41,7 +42,8 @@ public class PdpUpdateTest {
         assertThatThrownBy(() -> new PdpUpdate(null)).isInstanceOf(NullPointerException.class);
 
         PdpUpdate orig = new PdpUpdate();
-        orig.setPolicies(null);
+        orig.setPoliciesToBeDeployed(null);
+        orig.setPoliciesToBeUndeployed(null);
 
         // verify with null values
         assertEquals(removeVariableFields(orig.toString()), removeVariableFields(new PdpUpdate(orig).toString()));
@@ -62,14 +64,17 @@ public class PdpUpdateTest {
         policy2.setVersion("4.5.6");
 
         List<ToscaPolicy> policies = Arrays.asList(policy1, policy2);
-        orig.setPolicies(policies);
+        orig.setPoliciesToBeDeployed(policies);
+        orig.setPoliciesToBeUndeployed(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
 
         PdpUpdate other = new PdpUpdate(orig);
 
         assertEquals(removeVariableFields(orig.toString()), removeVariableFields(other.toString()));
 
         // ensure list and items are not the same object
-        assertNotSame(other.getPolicies(), policies);
-        assertNotSame(other.getPolicies().get(0), policies.get(0));
+        assertNotSame(other.getPoliciesToBeDeployed(), policies);
+        assertNotSame(other.getPoliciesToBeDeployed().get(0), policies.get(0));
+        assertNotSame(other.getPoliciesToBeUndeployed(), policies);
+        assertNotSame(other.getPoliciesToBeUndeployed().get(0), policies.get(0));
     }
 }
index 2262ee9..a90f74f 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,7 +21,9 @@
 
 package org.onap.policy.models.sim.pdp.handler;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 import org.onap.policy.common.endpoints.event.comm.TopicSink;
 import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
@@ -31,6 +33,7 @@ import org.onap.policy.models.pdp.enums.PdpResponseStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
 import org.onap.policy.models.sim.pdp.comm.PdpStatusPublisher;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
 /**
  * This class supports the handling of pdp update messages.
@@ -62,13 +65,19 @@ public class PdpUpdateMessageHandler {
                 }
                 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
                 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
-                pdpStatusContext
-                        .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
-                if (pdpStatusContext.getState().equals(PdpState.ACTIVE) && !pdpUpdateMsg.getPolicies().isEmpty()) {
+                @SuppressWarnings("unchecked")
+                List<ToscaPolicy> policies = Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST,
+                        List.class, new ArrayList<>());
+                policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed());
+                policies.removeIf(policy -> pdpUpdateMsg.getPoliciesToBeUndeployed().contains(policy.getIdentifier()));
+                pdpStatusContext.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier)
+                        .collect(Collectors.toList()));
+                if (pdpStatusContext.getState().equals(PdpState.ACTIVE)
+                        && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
                             PdpResponseStatus.SUCCESS, "Pdp engine started and policies are running.");
                 }
-                Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
+                Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST, policies);
                 if (null == pdpResponseDetails) {
                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
                             PdpResponseStatus.SUCCESS, "Pdp update successful.");
@@ -102,8 +111,10 @@ public class PdpUpdateMessageHandler {
             return false;
         }
 
-        return null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
-                .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
+        return null != pdpStatusContext.getPolicies() && (pdpStatusContext.getPolicies()
+                .containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(
+                        pdpUpdateMsg.getPoliciesToBeDeployed()))) && pdpStatusContext.getPolicies()
+                .stream().noneMatch(pdpUpdateMsg.getPoliciesToBeUndeployed()::contains);
     }
 
     /**
index 93015e9..20c9cc1 100644 (file)
@@ -133,7 +133,7 @@ public class TestPdpStateChangeListener {
         toscaPolicy.setProperties(propertiesMap);
         final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
         toscaPolicies.add(toscaPolicy);
-        pdpUpdateMsg.setPolicies(toscaPolicies);
+        pdpUpdateMsg.setPoliciesToBeDeployed(toscaPolicies);
         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
         return pdpUpdateMsg;
     }
index 7bf70dd..0827f26 100644 (file)
@@ -125,11 +125,11 @@ public class TestPdpUpdateListener {
         toscaPolicy.setProperties(propertiesMap);
         final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
         toscaPolicies.add(toscaPolicy);
-        pdpUpdateMsg.setPolicies(toscaPolicies);
+        pdpUpdateMsg.setPoliciesToBeDeployed(toscaPolicies);
         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
         assertEquals(pdpStatus.getPolicies(),
-                new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
+                new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
     }
 }