Madan Jampani
Committed by Gerrit Code Review

Ensure one can add/remove status change listeners for LeaderElectors

Change-Id: Ief362489fe9b563606f07d52c300629af23b6372
(cherry picked from commit d7ff34d6)
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.store.primitives; 16 package org.onosproject.store.primitives;
17 17
18 +import java.util.Collection;
18 import java.util.Map; 19 import java.util.Map;
19 import java.util.concurrent.CompletableFuture; 20 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.ExecutionException; 21 import java.util.concurrent.ExecutionException;
...@@ -89,6 +90,21 @@ public class DefaultLeaderElector extends Synchronous<AsyncLeaderElector> implem ...@@ -89,6 +90,21 @@ public class DefaultLeaderElector extends Synchronous<AsyncLeaderElector> implem
89 complete(asyncElector.removeChangeListener(consumer)); 90 complete(asyncElector.removeChangeListener(consumer));
90 } 91 }
91 92
93 + @Override
94 + public void addStatusChangeListener(Consumer<Status> listener) {
95 + asyncElector.addStatusChangeListener(listener);
96 + }
97 +
98 + @Override
99 + public void removeStatusChangeListener(Consumer<Status> listener) {
100 + asyncElector.removeStatusChangeListener(listener);
101 + }
102 +
103 + @Override
104 + public Collection<Consumer<Status>> statusChangeListeners() {
105 + return asyncElector.statusChangeListeners();
106 + }
107 +
92 private <T> T complete(CompletableFuture<T> future) { 108 private <T> T complete(CompletableFuture<T> future) {
93 try { 109 try {
94 return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); 110 return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
......
...@@ -19,6 +19,7 @@ import io.atomix.copycat.client.CopycatClient; ...@@ -19,6 +19,7 @@ import io.atomix.copycat.client.CopycatClient;
19 import io.atomix.resource.AbstractResource; 19 import io.atomix.resource.AbstractResource;
20 import io.atomix.resource.ResourceTypeInfo; 20 import io.atomix.resource.ResourceTypeInfo;
21 21
22 +import java.util.Collection;
22 import java.util.List; 23 import java.util.List;
23 import java.util.Map; 24 import java.util.Map;
24 import java.util.Properties; 25 import java.util.Properties;
...@@ -40,6 +41,7 @@ import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorComman ...@@ -40,6 +41,7 @@ import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorComman
40 import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Withdraw; 41 import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Withdraw;
41 import org.onosproject.store.service.AsyncLeaderElector; 42 import org.onosproject.store.service.AsyncLeaderElector;
42 43
44 +import com.google.common.collect.ImmutableSet;
43 import com.google.common.collect.Sets; 45 import com.google.common.collect.Sets;
44 46
45 /** 47 /**
...@@ -48,8 +50,10 @@ import com.google.common.collect.Sets; ...@@ -48,8 +50,10 @@ import com.google.common.collect.Sets;
48 @ResourceTypeInfo(id = -152, factory = AtomixLeaderElectorFactory.class) 50 @ResourceTypeInfo(id = -152, factory = AtomixLeaderElectorFactory.class)
49 public class AtomixLeaderElector extends AbstractResource<AtomixLeaderElector> 51 public class AtomixLeaderElector extends AbstractResource<AtomixLeaderElector>
50 implements AsyncLeaderElector { 52 implements AsyncLeaderElector {
53 + private final Set<Consumer<Status>> statusChangeListeners =
54 + Sets.newCopyOnWriteArraySet();
51 private final Set<Consumer<Change<Leadership>>> leadershipChangeListeners = 55 private final Set<Consumer<Change<Leadership>>> leadershipChangeListeners =
52 - Sets.newIdentityHashSet(); 56 + Sets.newCopyOnWriteArraySet();
53 57
54 public static final String CHANGE_SUBJECT = "leadershipChangeEvents"; 58 public static final String CHANGE_SUBJECT = "leadershipChangeEvents";
55 59
...@@ -130,4 +134,19 @@ public class AtomixLeaderElector extends AbstractResource<AtomixLeaderElector> ...@@ -130,4 +134,19 @@ public class AtomixLeaderElector extends AbstractResource<AtomixLeaderElector>
130 } 134 }
131 return CompletableFuture.completedFuture(null); 135 return CompletableFuture.completedFuture(null);
132 } 136 }
137 +
138 + @Override
139 + public void addStatusChangeListener(Consumer<Status> listener) {
140 + statusChangeListeners.add(listener);
141 + }
142 +
143 + @Override
144 + public void removeStatusChangeListener(Consumer<Status> listener) {
145 + statusChangeListeners.remove(listener);
146 + }
147 +
148 + @Override
149 + public Collection<Consumer<Status>> statusChangeListeners() {
150 + return ImmutableSet.copyOf(statusChangeListeners);
151 + }
133 } 152 }
......