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.
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);
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());
+ }
+ }
}
@Getter
@Setter
private static XacmlPdpActivator current = null;
+ private final RestServer restServer;
// The parameters of this policy xacml pdp activator
private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
final XacmlPdpHearbeatPublisher heartbeat;
final TopicSinkClient sinkClient;
final XacmlState state;
- final RestServer restServer;
try {
XacmlPdpApplicationManager appmgr =
heartbeat::start,
heartbeat::terminate);
- addAction("REST server",
- restServer::start,
- restServer::stop);
-
// @formatter:on
}
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();
+ }
}
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);
}
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;
// 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
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;
// Start the service
//
main = startXacmlPdpService(fileParams);
+ XacmlPdpActivator.getCurrent().startXacmlRestController();
//
// Make sure it is running
//
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;
// Start the service
//
main = startXacmlPdpService(fileParams);
+ XacmlPdpActivator.getCurrent().startXacmlRestController();
//
// Make sure it is running
//
/**
* Creates the activator.
*/
+ @Override
@Before
public void setUp() {
activator = new XacmlPdpActivator(parGroup);
@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