Adding Leadership Listener to IntentBatchService

ObjectiveTracker uses Leadership Listener to track intents that
it has become the leader of.

Change-Id: I039accb30d27ad718d79a9fec3f546dbdc78e62e
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentBatchQueue.java b/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentBatchQueue.java
index 52d166c..9d9b833 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentBatchQueue.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentBatchQueue.java
@@ -35,7 +35,11 @@
 import org.onlab.onos.cluster.LeadershipService;
 import org.onlab.onos.core.ApplicationId;
 import org.onlab.onos.core.CoreService;
+import org.onlab.onos.event.AbstractListenerRegistry;
+import org.onlab.onos.event.EventDeliveryService;
 import org.onlab.onos.net.intent.IntentBatchDelegate;
+import org.onlab.onos.net.intent.IntentBatchLeaderEvent;
+import org.onlab.onos.net.intent.IntentBatchListener;
 import org.onlab.onos.net.intent.IntentBatchService;
 import org.onlab.onos.net.intent.IntentOperations;
 import org.onlab.onos.store.hz.SQueue;
@@ -46,7 +50,6 @@
 import org.onlab.util.KryoNamespace;
 import org.slf4j.Logger;
 
-import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
 
@@ -74,6 +77,10 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected StoreService storeService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EventDeliveryService eventDispatcher;
+
+
     private HazelcastInstance theInstance;
     private ControllerNode localControllerNode;
     protected StoreSerializer serializer;
@@ -85,6 +92,9 @@
     private final Map<ApplicationId, IntentOperations> outstandingOps
             = Maps.newHashMap();
 
+    private final AbstractListenerRegistry<IntentBatchLeaderEvent, IntentBatchListener>
+            listenerRegistry = new AbstractListenerRegistry<>();
+
     @Activate
     public void activate() {
         theInstance = storeService.getHazelcastInstance();
@@ -103,11 +113,13 @@
             }
 
         };
+        eventDispatcher.addSink(IntentBatchLeaderEvent.class, listenerRegistry);
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
+        eventDispatcher.removeSink(IntentBatchLeaderEvent.class);
         leadershipService.removeListener(leaderListener);
         for (ApplicationId appId: batchQueues.keySet()) {
             leadershipService.withdraw(getTopic(appId));
@@ -277,12 +289,6 @@
     }
 
     @Override
-    public Set<IntentOperations> getCurrentOperations() {
-        //FIXME this is not really implemented
-        return Collections.emptySet();
-    }
-
-    @Override
     public boolean isLocalLeader(ApplicationId applicationId) {
         return myTopics.contains(applicationId);
     }
@@ -298,4 +304,14 @@
             this.delegate = null;
         }
     }
+
+    @Override
+    public void addListener(IntentBatchListener listener) {
+        listenerRegistry.addListener(listener);
+    }
+
+    @Override
+    public void removeListener(IntentBatchListener listener) {
+        listenerRegistry.removeListener(listener);
+    }
 }