support for PAP delta updates
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / LifecycleStateActiveTest.java
index 54f4b6a..bd6b392 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 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.
@@ -32,8 +33,6 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.TimeUnit;
@@ -46,8 +45,8 @@ import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpStatus;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
 import org.onap.policy.models.pdp.enums.PdpState;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 
 /**
  * Lifecycle State Active Test.
@@ -56,12 +55,6 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
     private static final String POLICY_COMPLIANT_VCPE_BAD_INTEGER_JSON =
             "src/test/resources/tosca-policy-compliant-vcpe-bad-integer.json";
-    private static final String POLICY_OPERATIONAL_FIREWALL_JSON =
-            "src/test/resources/tosca-policy-operational-firewall.json";
-    private static final String POLICY_OPERATIONAL_RESTART_V_2_JSON =
-            "src/test/resources/tosca-policy-operational-restart.v2.json";
-    private static final String POLICY_OPERATIONAL_RESTART_JSON =
-            "src/test/resources/tosca-policy-operational-restart.json";
 
     /**
      * Start tests in the Active state.
@@ -84,7 +77,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         change.setState(PdpState.ACTIVE);
         change.setName(fsm.getName());
 
-        fsm.setSubGroupAction("a");
+        fsm.setSubGroup("a");
         fsm.source.offer(new StandardCoder().encode(change));
         controllerSupport.getController().start();
     }
@@ -107,7 +100,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
     private void assertActive() {
         assertEquals(PdpState.ACTIVE, fsm.state());
         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
-        assertEquals("a", fsm.getSubgroup());
+        assertEquals("a", fsm.getSubGroup());
         assertTrue(fsm.isAlive());
         waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
     }
@@ -166,13 +159,13 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         fsm.source.offer(new StandardCoder().encode(change));
         assertEquals(PdpState.ACTIVE, fsm.state());
         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
-        assertNotEquals("b", fsm.getSubgroup());
+        assertNotEquals("b", fsm.getSubGroup());
 
         change.setName(fsm.getName());
         fsm.source.offer(new StandardCoder().encode(change));
         assertEquals(PdpState.ACTIVE, fsm.state());
         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
-        assertEquals("a", fsm.getSubgroup());
+        assertEquals("a", fsm.getSubGroup());
 
         change.setState(PdpState.SAFE);
         fsm.source.offer(new StandardCoder().encode(change));
@@ -199,19 +192,18 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         update.setName(NetworkUtil.getHostname());
         update.setPdpGroup("W");
         update.setPdpSubgroup("w");
-        update.setPolicies(Collections.emptyList());
 
         fsm.start(controllerSupport.getController());
         assertTrue(fsm.update(update));
 
         assertEquals(PdpState.ACTIVE, fsm.state());
         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
-        assertEquals("w", fsm.getSubgroup());
+        assertEquals("w", fsm.getSubGroup());
 
-        String restartV1 =
-            Files.readString(Paths.get(POLICY_OPERATIONAL_RESTART_JSON), StandardCharsets.UTF_8);
-        ToscaPolicy toscaPolicyRestartV1 = new StandardCoder().decode(restartV1, ToscaPolicy.class);
-        update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
+        ToscaPolicy toscaPolicyRestartV1 =
+            getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
+        toscaPolicyRestartV1.getProperties().put("controllerName", "lifecycle");
+        update.setPoliciesToBeDeployed(List.of(toscaPolicyRestartV1));
 
         int qlength = fsm.client.getSink().getRecentEvents().length;
 
@@ -219,18 +211,17 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         assertTrue(fsm.update(update));
         assertEquals(qlength + 1, fsm.client.getSink().getRecentEvents().length);
-        assertEquals(4, fsm.policyTypesMap.size());
+        assertEquals(3, fsm.policyTypesMap.size());
         assertNotNull(fsm.getPolicyTypesMap().get(
-                new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
+                new ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
         assertNotNull(fsm.getPolicyTypesMap().get(
-                new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
+                new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
         assertNotNull(fsm.getPolicyTypesMap().get(
-                new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0")));
-        assertNotNull(fsm.getPolicyTypesMap().get(
-                new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
+                new ToscaConceptIdentifier("onap.policies.controlloop.operational.common.Drools",
                 "1.0.0")));
         PdpStatus cachedStatus = new StandardCoder()
                                     .decode(fsm.client.getSink().getRecentEvents()[qlength], PdpStatus.class);
+        assertEquals("foo", cachedStatus.getPdpType());
         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
 
         List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
@@ -242,7 +233,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         assertTrue(fsm.update(update));
         assertEquals(qlength + 2, fsm.client.getSink().getRecentEvents().length);
-        assertEquals(4, fsm.policyTypesMap.size());
+        assertEquals(3, fsm.policyTypesMap.size());
         cachedStatus = new StandardCoder()
             .decode(fsm.client.getSink().getRecentEvents()[qlength + 1], PdpStatus.class);
         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
@@ -254,10 +245,12 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         // undeploy operational.restart policy
 
-        update.setPolicies(Collections.emptyList());
+        update.setPolicies(List.of());
+        update.setPoliciesToBeDeployed(List.of());
+        update.setPoliciesToBeUndeployed(List.of(toscaPolicyRestartV1.getIdentifier()));
         assertTrue(fsm.update(update));
         assertEquals(qlength + 3, fsm.client.getSink().getRecentEvents().length);
-        assertEquals(4, fsm.policyTypesMap.size());
+        assertEquals(3, fsm.policyTypesMap.size());
         cachedStatus = new StandardCoder()
             .decode(fsm.client.getSink().getRecentEvents()[qlength + 2], PdpStatus.class);
         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
@@ -268,10 +261,12 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         // redeploy operational.restart policy
 
-        update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
+        update.setPolicies(List.of());
+        update.setPoliciesToBeUndeployed(List.of());
+        update.setPoliciesToBeDeployed(List.of(toscaPolicyRestartV1));
         assertTrue(fsm.update(update));
         assertEquals(qlength + 4, fsm.client.getSink().getRecentEvents().length);
-        assertEquals(4, fsm.policyTypesMap.size());
+        assertEquals(3, fsm.policyTypesMap.size());
         cachedStatus = new StandardCoder()
             .decode(fsm.client.getSink().getRecentEvents()[qlength + 3], PdpStatus.class);
         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
@@ -283,13 +278,16 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         // deploy a new version of the operational.restart policy
 
-        String restartV2 =
-            Files.readString(Paths.get(POLICY_OPERATIONAL_RESTART_V_2_JSON), StandardCharsets.UTF_8);
-        ToscaPolicy toscaPolicyRestartV2 = new StandardCoder().decode(restartV2, ToscaPolicy.class);
-        update.setPolicies(Arrays.asList(toscaPolicyRestartV2));
+        ToscaPolicy toscaPolicyRestartV2 =
+            getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
+        toscaPolicyRestartV2.setVersion("2.0.0");
+        toscaPolicyRestartV2.getProperties().put("controllerName", "lifecycle");
+        update.setPolicies(List.of());
+        update.setPoliciesToBeUndeployed(List.of(toscaPolicyRestartV1.getIdentifier()));
+        update.setPoliciesToBeDeployed(List.of(toscaPolicyRestartV2));
         assertTrue(fsm.update(update));
         assertEquals(qlength + 5, fsm.client.getSink().getRecentEvents().length);
-        assertEquals(4, fsm.policyTypesMap.size());
+        assertEquals(3, fsm.policyTypesMap.size());
         cachedStatus = new StandardCoder()
             .decode(fsm.client.getSink().getRecentEvents()[qlength + 4], PdpStatus.class);
         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
@@ -302,13 +300,15 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         // deploy another policy : firewall
 
-        String firewall =
-            Files.readString(Paths.get(POLICY_OPERATIONAL_FIREWALL_JSON), StandardCharsets.UTF_8);
-        ToscaPolicy toscaPolicyFirewall = new StandardCoder().decode(firewall, ToscaPolicy.class);
-        update.setPolicies(Arrays.asList(toscaPolicyRestartV2, toscaPolicyFirewall));
+        ToscaPolicy toscaPolicyFirewall =
+            getExamplesPolicy("policies/vFirewall.policy.operational.input.tosca.json", "operational.modifyconfig");
+        toscaPolicyFirewall.getProperties().put("controllerName", "lifecycle");
+        update.setPolicies(List.of());
+        update.setPoliciesToBeUndeployed(List.of());
+        update.setPoliciesToBeDeployed(List.of(toscaPolicyRestartV2, toscaPolicyFirewall));
         assertTrue(fsm.update(update));
         assertEquals(qlength + 6, fsm.client.getSink().getRecentEvents().length);
-        assertEquals(4, fsm.policyTypesMap.size());
+        assertEquals(3, fsm.policyTypesMap.size());
         cachedStatus = new StandardCoder()
             .decode(fsm.client.getSink().getRecentEvents()[qlength + 5], PdpStatus.class);
         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
@@ -324,6 +324,9 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         long interval = 10 * originalInterval;
         update.setPdpHeartbeatIntervalMs(interval * 1000L);
 
+        update.setPolicies(List.of());
+        update.setPoliciesToBeUndeployed(List.of());
+        update.setPoliciesToBeDeployed(List.of());
         assertTrue(fsm.update(update));
 
         assertEquals(PdpState.ACTIVE, fsm.state());
@@ -334,7 +337,10 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         String badIntegerPolicy =
             Files.readString(Paths.get(POLICY_COMPLIANT_VCPE_BAD_INTEGER_JSON), StandardCharsets.UTF_8);
         ToscaPolicy toscaPolicyRestartBad = new StandardCoder().decode(badIntegerPolicy, ToscaPolicy.class);
-        update.setPolicies(Arrays.asList(toscaPolicyRestartBad));
+        update.setPolicies(List.of());
+        update.setPoliciesToBeUndeployed(List.of(toscaPolicyRestartV2.getIdentifier(),
+                toscaPolicyFirewall.getIdentifier()));
+        update.setPoliciesToBeDeployed(List.of(toscaPolicyRestartBad));
         assertFalse(fsm.update(update));
 
         assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));