Hz: remove listeners on deactivate

Change-Id: I544917508cd4b9513e3fcd3a100c44928954f413
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java
index f372ced..4188cba 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java
@@ -64,6 +64,8 @@
     private final MembershipListener listener = new InternalMembershipListener();
     private final Map<NodeId, State> states = new ConcurrentHashMap<>();
 
+    private String nodesListenerId;
+
     @Override
     @Activate
     public void activate() {
@@ -74,7 +76,7 @@
         OptionalCacheLoader<NodeId, DefaultControllerNode> nodeLoader
                 = new OptionalCacheLoader<>(serializer, rawNodes);
         nodes = new AbsentInvalidatingLoadingCache<>(newBuilder().build(nodeLoader));
-        rawNodes.addEntryListener(new RemoteCacheEventHandler<>(nodes), true);
+        nodesListenerId = rawNodes.addEntryListener(new RemoteCacheEventHandler<>(nodes), true);
 
         loadClusterNodes();
 
@@ -90,6 +92,7 @@
 
     @Deactivate
     public void deactivate() {
+        rawNodes.removeEntryListener(nodesListenerId);
         theInstance.getCluster().removeMembershipListener(listenerId);
         log.info("Stopped");
     }
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/core/impl/DistributedApplicationIdStore.java b/core/store/dist/src/main/java/org/onlab/onos/store/core/impl/DistributedApplicationIdStore.java
index 884bc12..4c3aa7a 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/core/impl/DistributedApplicationIdStore.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/core/impl/DistributedApplicationIdStore.java
@@ -22,6 +22,7 @@
 import com.hazelcast.core.EntryListener;
 import com.hazelcast.core.IAtomicLong;
 import com.hazelcast.core.MapEvent;
+
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -54,6 +55,8 @@
 
     protected Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
 
+    private String listenerId;
+
 
     @Override
     @Activate
@@ -73,7 +76,7 @@
         lastAppId = theInstance.getAtomicLong("applicationId");
 
         appIdsByName = new SMap<>(theInstance.<byte[], byte[]>getMap("appIdsByName"), this.serializer);
-        appIdsByName.addEntryListener((new RemoteAppIdEventHandler()), true);
+        listenerId = appIdsByName.addEntryListener((new RemoteAppIdEventHandler()), true);
 
         primeAppIds();
 
@@ -82,6 +85,7 @@
 
     @Deactivate
     public void deactivate() {
+        appIdsByName.removeEntryListener(listenerId);
         log.info("Stopped");
     }
 
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentStore.java b/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentStore.java
index 486e32f..c537a72 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentStore.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/intent/impl/HazelcastIntentStore.java
@@ -107,6 +107,8 @@
     private Timer getIntentTimer;
     private Timer getIntentStateTimer;
 
+    private String listenerId;
+
     private Timer createResponseTimer(String methodName) {
         return createTimer("IntentStore", methodName, "responseTime");
     }
@@ -150,7 +152,7 @@
         IMap<byte[], byte[]> rawStates = super.theInstance.getMap("intent-states");
         states = new SMap<>(rawStates , super.serializer);
         EntryListener<IntentId, IntentState> listener = new RemoteIntentStateListener();
-        states.addEntryListener(listener , true);
+        listenerId = states.addEntryListener(listener , true);
 
         transientStates.clear();
 
@@ -163,6 +165,7 @@
 
     @Deactivate
     public void deactivate() {
+        states.removeEntryListener(listenerId);
         log.info("Stopped");
     }
 
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java b/core/store/dist/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java
index 78a92a8..bf15ddc 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java
@@ -74,6 +74,8 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ClusterService clusterService;
 
+    private String listenerId;
+
     @Override
     @Activate
     public void activate() {
@@ -91,7 +93,7 @@
         };
 
         roleMap = new SMap<>(theInstance.<byte[], byte[]>getMap("nodeRoles"), this.serializer);
-        roleMap.addEntryListener((new RemoteMasterShipEventHandler()), true);
+        listenerId = roleMap.addEntryListener((new RemoteMasterShipEventHandler()), true);
         terms = new SMap<>(theInstance.<byte[], byte[]>getMap("terms"), this.serializer);
 
         log.info("Started");
@@ -99,6 +101,7 @@
 
     @Deactivate
     public void deactivate() {
+        roleMap.removeEntryListener(listenerId);
         log.info("Stopped");
     }