Fixed xacml-pdp registration
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / comm / XacmlPdpHearbeatPublisher.java
index 8ffccbe..3177c09 100644 (file)
 
 package org.onap.policy.pdpx.main.comm;
 
-import java.util.Timer;
-import java.util.TimerTask;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import lombok.Getter;
 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;
 
-public class XacmlPdpHearbeatPublisher extends TimerTask {
+public class XacmlPdpHearbeatPublisher implements Runnable {
+    public static final int DEFAULT_INTERVAL_MS = 60000;
 
     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.
+     */
+    @Getter
+    private long intervalMs = DEFAULT_INTERVAL_MS;
+
+    private ScheduledExecutorService timerThread;
+
+    private ScheduledFuture<?> timer;
+
 
     /**
      * 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 (timerThread != null) {
+            timerThread.shutdownNow();
+            timerThread = null;
+            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 (timerThread != null) {
+                timer.cancel(false);
+                timer = timerThread.scheduleWithFixedDelay(this, 0, this.intervalMs, TimeUnit.MILLISECONDS);
+            }
+        }
     }
 
-    public void setAlive(boolean alive) {
-        this.alive = alive;
+    /**
+     * Starts the timer.
+     */
+    public synchronized void start() {
+        if (timerThread == null) {
+            timerThread = makeTimerThread();
+            timer = timerThread.scheduleWithFixedDelay(this, 0, this.intervalMs, TimeUnit.MILLISECONDS);
+        }
+    }
+
+    // these may be overridden by junit tests
+
+    protected ScheduledExecutorService makeTimerThread() {
+        return Executors.newScheduledThreadPool(1);
     }
 }