Revert "Leave xacml-pdp REST server always running" 21/123421/1
authorJim Hahn <jrh3@att.com>
Fri, 20 Aug 2021 13:50:44 +0000 (09:50 -0400)
committerJim Hahn <jrh3@att.com>
Fri, 20 Aug 2021 13:51:12 +0000 (09:51 -0400)
This reverts commit 031a0fe51dff21445034befc8a6d8732622acd07.

Issue-ID: POLICY-3531
Change-Id: I20eddfc11589b6ab4822a93b8c8c3d3f209537c0
Signed-off-by: Jim Hahn <jrh3@att.com>
main/src/main/java/org/onap/policy/pdpx/main/XacmlState.java
main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java
main/src/test/java/org/onap/policy/pdpx/main/XacmlStateTest.java
main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java

index 17995fd..d1e326f 100644 (file)
@@ -40,6 +40,7 @@ import org.slf4j.LoggerFactory;
  * Current state of this XACML PDP.
  */
 public class XacmlState {
+    // The logger for this class
     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlState.class);
 
     /**
@@ -112,6 +113,9 @@ public class XacmlState {
 
         PdpStatus status2 = makeResponse(message, "");
 
+        // start/stop rest controller based on state change
+        handleXacmlRestController();
+
         // these fields aren't needed in the response, so clear them out to avoid sending
         status2.setPolicies(null);
 
@@ -165,4 +169,23 @@ public class XacmlState {
         status2.setResponse(resp);
         return status2;
     }
+
+    /**
+     * Manages the Xacml-Pdp rest controller based on the Xacml-Pdp State.
+     * Current supported states:
+     * ACTIVE  - rest service is running and handling requests
+     * PASSIVE - rest service is not running
+     */
+    private void handleXacmlRestController() {
+        if (status.getState() == PdpState.ACTIVE) {
+            LOGGER.info("State change: {} - Starting rest controller", status.getState());
+            XacmlPdpActivator.getCurrent().startXacmlRestController();
+        } else if (status.getState() == PdpState.PASSIVE) {
+            LOGGER.info("State change: {} - Stopping rest controller", status.getState());
+            XacmlPdpActivator.getCurrent().stopXacmlRestController();
+        } else {
+            // unsupported state
+            LOGGER.warn("Unsupported state: {}", status.getState());
+        }
+    }
 }
index 050d8b2..892b383 100644 (file)
@@ -152,16 +152,11 @@ public class XacmlPdpActivator extends ServiceManagerContainer {
         addAction("Terminate PDP",
             () -> { },
             () -> sendTerminateMessage(sinkClient, state));
-
         // initial heart beats act as registration messages
         addAction("Heartbeat Publisher",
             heartbeat::start,
             heartbeat::terminate);
 
-        addAction("REST Server",
-            restServer::start,
-            restServer::stop);
-
         // @formatter:on
     }
 
index feaaf4f..5ff3d5c 100644 (file)
@@ -26,6 +26,7 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import org.junit.AfterClass;
@@ -129,10 +130,12 @@ public class XacmlStateTest {
         req.setState(PdpState.ACTIVE);
         status = state.updateInternalState(req);
         assertEquals(PdpState.ACTIVE, status.getState());
+        verify(act).startXacmlRestController();
 
         req.setState(PdpState.PASSIVE);
         status = state.updateInternalState(req);
         assertEquals(PdpState.PASSIVE, status.getState());
+        verify(act).stopXacmlRestController();
     }
 
     @Test
index 9025722..c874761 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -74,17 +74,6 @@ public class TestXacmlPdpActivator extends CommonRest {
         activator = new XacmlPdpActivator(parGroup);
     }
 
-    /**
-     * Teardown tests.
-     * @throws PolicyXacmlPdpException on termination errors
-     */
-    @After
-    public void teardown() throws PolicyXacmlPdpException {
-        if (activator != null && activator.isAlive()) {
-            activator.stop();
-        }
-    }
-
     @Test
     public void testXacmlPdpActivator() throws Exception {
         assertFalse(activator.isAlive());
@@ -92,15 +81,17 @@ public class TestXacmlPdpActivator extends CommonRest {
         activator.start();
         assertTrue(activator.isAlive());
 
+        // XacmlPdp starts in PASSIVE state so the rest controller should not be alive
+        assertFalse(activator.isXacmlRestControllerAlive());
         assertTrue(activator.getParameterGroup().isValid());
         assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, activator.getParameterGroup().getName());
         assertEquals(CommonTestData.PDPX_GROUP, activator.getParameterGroup().getPdpGroup());
 
-        activator.stopXacmlRestController();
-        assertFalse(activator.isXacmlRestControllerAlive());
-
         activator.startXacmlRestController();
         assertTrue(activator.isXacmlRestControllerAlive());
+
+        activator.stopXacmlRestController();
+        assertFalse(activator.isXacmlRestControllerAlive());
     }
 
     @Test
@@ -123,4 +114,15 @@ public class TestXacmlPdpActivator extends CommonRest {
         activator.stop();
         assertFalse(activator.isAlive());
     }
+
+    /**
+     * Teardown tests.
+     * @throws PolicyXacmlPdpException on termination errors
+     */
+    @After
+    public void teardown() throws PolicyXacmlPdpException {
+        if (activator != null && activator.isAlive()) {
+            activator.stop();
+        }
+    }
 }