diff options
7 files changed, 81 insertions, 5 deletions
diff --git a/main/src/main/java/org/onap/policy/pdpx/main/XacmlState.java b/main/src/main/java/org/onap/policy/pdpx/main/XacmlState.java index b7aa231f..3d96b4bd 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/XacmlState.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/XacmlState.java @@ -32,11 +32,15 @@ import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager; import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator; +import org.slf4j.Logger; +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); /** * The application manager. @@ -103,6 +107,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); @@ -149,4 +156,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()); + } + } } diff --git a/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java b/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java index 8d213954..396919dc 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java @@ -62,6 +62,7 @@ public class XacmlPdpActivator extends ServiceManagerContainer { @Getter @Setter private static XacmlPdpActivator current = null; + private final RestServer restServer; // The parameters of this policy xacml pdp activator private final XacmlPdpParameterGroup xacmlPdpParameterGroup; @@ -85,7 +86,6 @@ public class XacmlPdpActivator extends ServiceManagerContainer { final XacmlPdpHearbeatPublisher heartbeat; final TopicSinkClient sinkClient; final XacmlState state; - final RestServer restServer; try { XacmlPdpApplicationManager appmgr = @@ -145,10 +145,6 @@ public class XacmlPdpActivator extends ServiceManagerContainer { heartbeat::start, heartbeat::terminate); - addAction("REST server", - restServer::start, - restServer::stop); - // @formatter:on } @@ -204,4 +200,30 @@ public class XacmlPdpActivator extends ServiceManagerContainer { source.unregister(msgDispatcher); } } + + /** + * Start the xacmlpdp rest controller. + */ + public void startXacmlRestController() { + if (isXacmlRestControllerAlive()) { + LOGGER.info("Xacml rest controller already running"); + } else { + restServer.start(); + } + } + + /** + * Stop the xacmlpdp rest controller. + */ + public void stopXacmlRestController() { + if (isXacmlRestControllerAlive()) { + restServer.stop(); + } else { + LOGGER.info("Xacml rest controller already stopped"); + } + } + + public boolean isXacmlRestControllerAlive() { + return restServer.isAlive(); + } } diff --git a/main/src/test/java/org/onap/policy/pdpx/main/CommonRest.java b/main/src/test/java/org/onap/policy/pdpx/main/CommonRest.java index a32bc6fd..d2379e09 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/CommonRest.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/CommonRest.java @@ -120,6 +120,9 @@ public class CommonRest { final String[] xacmlPdpConfigParameters = {"-c", CommonRest.CONFIG_FILE}; main = new Main(xacmlPdpConfigParameters); + // start xacml rest controller + XacmlPdpActivator.getCurrent().startXacmlRestController(); + if (!NetworkUtil.isTcpPortOpen("localhost", port, 20, 1000L)) { throw new IllegalStateException("server is not listening on port " + port); } diff --git a/main/src/test/java/org/onap/policy/pdpx/main/XacmlStateTest.java b/main/src/test/java/org/onap/policy/pdpx/main/XacmlStateTest.java index 20136bf0..eef1f1be 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/XacmlStateTest.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/XacmlStateTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Arrays; @@ -135,6 +136,16 @@ public class XacmlStateTest { // ensure info was saved status = state.genHeartbeat(); assertEquals(PdpState.SAFE, status.getState()); + + 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 diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestAbbreviateDecisionResults.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestAbbreviateDecisionResults.java index 37a669d2..d416cd3b 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestAbbreviateDecisionResults.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestAbbreviateDecisionResults.java @@ -67,6 +67,7 @@ import org.onap.policy.pdpx.main.PolicyXacmlPdpException; import org.onap.policy.pdpx.main.parameters.CommonTestData; import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup; import org.onap.policy.pdpx.main.startstop.Main; +import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator; import org.onap.policy.xacml.pdp.application.monitoring.MonitoringPdpApplication; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -130,6 +131,7 @@ public class TestAbbreviateDecisionResults { // Start the service // main = startXacmlPdpService(fileParams); + XacmlPdpActivator.getCurrent().startXacmlRestController(); // // Make sure it is running // diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java index 042180b4..6c6e35b9 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java @@ -61,6 +61,7 @@ import org.onap.policy.pdpx.main.PolicyXacmlPdpException; import org.onap.policy.pdpx.main.parameters.CommonTestData; import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup; import org.onap.policy.pdpx.main.startstop.Main; +import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -116,6 +117,7 @@ public class TestDecision { // Start the service // main = startXacmlPdpService(fileParams); + XacmlPdpActivator.getCurrent().startXacmlRestController(); // // Make sure it is running // diff --git a/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java index cd4b78d9..5223bcd8 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java @@ -64,6 +64,7 @@ public class TestXacmlPdpActivator extends CommonRest { /** * Creates the activator. */ + @Override @Before public void setUp() { activator = new XacmlPdpActivator(parGroup); @@ -72,11 +73,20 @@ public class TestXacmlPdpActivator extends CommonRest { @Test public void testXacmlPdpActivator() throws Exception { assertFalse(activator.isAlive()); + assertFalse(activator.isXacmlRestControllerAlive()); 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_GROUP_NAME, activator.getParameterGroup().getName()); + activator.startXacmlRestController(); + assertTrue(activator.isXacmlRestControllerAlive()); + + activator.stopXacmlRestController(); + assertFalse(activator.isXacmlRestControllerAlive()); } @Test |