Madan Jampani

Ensure one can add/remove status change listeners for LeaderElectors

Change-Id: Ief362489fe9b563606f07d52c300629af23b6372
......@@ -15,6 +15,7 @@
*/
package org.onosproject.store.primitives;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
......@@ -89,6 +90,21 @@ public class DefaultLeaderElector extends Synchronous<AsyncLeaderElector> implem
complete(asyncElector.removeChangeListener(consumer));
}
@Override
public void addStatusChangeListener(Consumer<Status> listener) {
asyncElector.addStatusChangeListener(listener);
}
@Override
public void removeStatusChangeListener(Consumer<Status> listener) {
asyncElector.removeStatusChangeListener(listener);
}
@Override
public Collection<Consumer<Status>> statusChangeListeners() {
return asyncElector.statusChangeListeners();
}
private <T> T complete(CompletableFuture<T> future) {
try {
return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
......
......@@ -19,6 +19,7 @@ import io.atomix.copycat.client.CopycatClient;
import io.atomix.resource.AbstractResource;
import io.atomix.resource.ResourceTypeInfo;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
......@@ -40,6 +41,7 @@ import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorComman
import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Withdraw;
import org.onosproject.store.service.AsyncLeaderElector;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
/**
......@@ -48,8 +50,10 @@ import com.google.common.collect.Sets;
@ResourceTypeInfo(id = -152, factory = AtomixLeaderElectorFactory.class)
public class AtomixLeaderElector extends AbstractResource<AtomixLeaderElector>
implements AsyncLeaderElector {
private final Set<Consumer<Status>> statusChangeListeners =
Sets.newCopyOnWriteArraySet();
private final Set<Consumer<Change<Leadership>>> leadershipChangeListeners =
Sets.newIdentityHashSet();
Sets.newCopyOnWriteArraySet();
public static final String CHANGE_SUBJECT = "leadershipChangeEvents";
......@@ -130,4 +134,19 @@ public class AtomixLeaderElector extends AbstractResource<AtomixLeaderElector>
}
return CompletableFuture.completedFuture(null);
}
@Override
public void addStatusChangeListener(Consumer<Status> listener) {
statusChangeListeners.add(listener);
}
@Override
public void removeStatusChangeListener(Consumer<Status> listener) {
statusChangeListeners.remove(listener);
}
@Override
public Collection<Consumer<Status>> statusChangeListeners() {
return ImmutableSet.copyOf(statusChangeListeners);
}
}
......