Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame^] | 1 | package org.onlab.onos.store.trivial.impl; |
| 2 | |
| 3 | import java.util.Map; |
| 4 | import java.util.Set; |
| 5 | import java.util.concurrent.ConcurrentHashMap; |
| 6 | import java.util.concurrent.CopyOnWriteArraySet; |
| 7 | |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Reference; |
| 10 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 11 | import org.apache.felix.scr.annotations.Service; |
| 12 | import org.onlab.onos.cluster.ClusterService; |
| 13 | import org.onlab.onos.cluster.ControllerNode; |
| 14 | import org.onlab.onos.cluster.Leadership; |
| 15 | import org.onlab.onos.cluster.LeadershipEvent; |
| 16 | import org.onlab.onos.cluster.LeadershipEvent.Type; |
| 17 | import org.onlab.onos.cluster.LeadershipEventListener; |
| 18 | import org.onlab.onos.cluster.LeadershipService; |
| 19 | |
| 20 | /** |
| 21 | * A trivial implementation of the leadership service. |
| 22 | * <p></p> |
| 23 | * The service is not distributed, so it can assume there's a single leadership |
| 24 | * contender. This contender is always granted leadership whenever it asks. |
| 25 | */ |
| 26 | @Component(immediate = true) |
| 27 | @Service |
| 28 | public class SimpleLeadershipManager implements LeadershipService { |
| 29 | |
| 30 | private Set<LeadershipEventListener> listeners = new CopyOnWriteArraySet<>(); |
| 31 | |
| 32 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 33 | private ClusterService clusterService; |
| 34 | |
| 35 | private Map<String, Boolean> elections = new ConcurrentHashMap<>(); |
| 36 | |
| 37 | @Override |
| 38 | public ControllerNode getLeader(String path) { |
| 39 | return elections.get(path) ? clusterService.getLocalNode() : null; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void runForLeadership(String path) { |
| 44 | elections.put(path, true); |
| 45 | for (LeadershipEventListener listener : listeners) { |
| 46 | listener.event(new LeadershipEvent(Type.LEADER_ELECTED, |
| 47 | new Leadership(path, clusterService.getLocalNode(), 0))); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void withdraw(String path) { |
| 53 | elections.remove(path); |
| 54 | for (LeadershipEventListener listener : listeners) { |
| 55 | listener.event(new LeadershipEvent(Type.LEADER_BOOTED, |
| 56 | new Leadership(path, clusterService.getLocalNode(), 0))); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Map<String, Leadership> getLeaderBoard() { |
| 62 | throw new UnsupportedOperationException("I don't know what to do." + |
| 63 | " I wish you luck."); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void addListener(LeadershipEventListener listener) { |
| 68 | listeners.add(listener); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void removeListener(LeadershipEventListener listener) { |
| 73 | listeners.remove(listener); |
| 74 | } |
| 75 | |
| 76 | } |