diff options
Diffstat (limited to 'main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpHearbeatPublisher.java')
-rw-r--r-- | main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpHearbeatPublisher.java | 76 |
1 files changed, 54 insertions, 22 deletions
diff --git a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpHearbeatPublisher.java b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpHearbeatPublisher.java index 8ffccbef..29044744 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpHearbeatPublisher.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpHearbeatPublisher.java @@ -23,6 +23,8 @@ package org.onap.policy.pdpx.main.comm; import java.util.Timer; import java.util.TimerTask; import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.pdpx.main.XacmlState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,45 +32,75 @@ public class XacmlPdpHearbeatPublisher extends TimerTask { private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpHearbeatPublisher.class); - private Timer timer; - private XacmlPdpMessage heartbeatMessage; - private static TopicSinkClient topicSinkClient; - private static volatile boolean alive = false; + private final TopicSinkClient topicSinkClient; + + /** + * Tracks the state of this PDP. + */ + private final XacmlState currentState; + + /** + * Current timer interval, in milliseconds. + */ + private long intervalMs = 60000; + + private Timer timer = null; + /** * Constructor for instantiating XacmlPdpPublisher. * - * @param message of the PDP - * @param topicSinkClient used to send heartbeat message + * @param topicSinkClient used to send heart beat message + * @param state tracks the state of this PDP */ - public XacmlPdpHearbeatPublisher(TopicSinkClient topicSinkClient, XacmlPdpMessage message ) { + public XacmlPdpHearbeatPublisher(TopicSinkClient topicSinkClient, XacmlState state) { this.topicSinkClient = topicSinkClient; - this.heartbeatMessage = message; - timer = new Timer(false); - timer.scheduleAtFixedRate(this, 0, 60000); // time interval temp hard coded now but will be parameterized - setAlive(true); + this.currentState = state; } @Override public void run() { - topicSinkClient.send(heartbeatMessage.formatPdpStatusMessage()); - LOGGER.info("Sending Xacml PDP heartbeat to the PAP"); + PdpStatus message = currentState.genHeartbeat(); + LOGGER.info("Sending Xacml PDP heartbeat to the PAP - {}", message); + + topicSinkClient.send(message); } /** - * Method to terminate the heartbeat. + * Method to terminate the heart beat. */ - public void terminate() { - timer.cancel(); - timer.purge(); - setAlive(false); + public synchronized void terminate() { + if (timer != null) { + timer.cancel(); + timer.purge(); + timer = null; + } } - public static boolean isAlive() { - return alive; + /** + * Restarts the timer if the interval has changed. If the timer is not currently + * running, then it updates the interval, but does not start the timer. + * + * @param intervalMs desired interval, or {@code null} to leave it unchanged + */ + public synchronized void restart(Long intervalMs) { + if (intervalMs != null && intervalMs > 0 && intervalMs != this.intervalMs) { + this.intervalMs = intervalMs; + + if (timer != null) { + terminate(); + start(); + } + } } - public void setAlive(boolean alive) { - this.alive = alive; + /** + * Starts the timer. + */ + public synchronized void start() { + if (timer == null) { + timer = new Timer(true); + timer.scheduleAtFixedRate(this, 0, this.intervalMs); + } } } |