alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.trivial.impl; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 17 | |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 20 | import java.util.Map; |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 21 | import java.util.Map.Entry; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.CopyOnWriteArraySet; |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 25 | import java.util.stream.Collectors; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 26 | |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.Leadership; |
| 33 | import org.onosproject.cluster.LeadershipEvent; |
| 34 | import org.onosproject.cluster.LeadershipEvent.Type; |
| 35 | import org.onosproject.cluster.LeadershipEventListener; |
| 36 | import org.onosproject.cluster.LeadershipService; |
| 37 | import org.onosproject.cluster.NodeId; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * A trivial implementation of the leadership service. |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 41 | * <p> |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 42 | * The service is not distributed, so it can assume there's a single leadership |
| 43 | * contender. This contender is always granted leadership whenever it asks. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | @Service |
| 47 | public class SimpleLeadershipManager implements LeadershipService { |
| 48 | |
| 49 | private Set<LeadershipEventListener> listeners = new CopyOnWriteArraySet<>(); |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | private ClusterService clusterService; |
| 53 | |
| 54 | private Map<String, Boolean> elections = new ConcurrentHashMap<>(); |
| 55 | |
| 56 | @Override |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 57 | public NodeId getLeader(String path) { |
| 58 | return elections.get(path) ? clusterService.getLocalNode().id() : null; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 62 | public Leadership getLeadership(String path) { |
| 63 | checkArgument(path != null); |
| 64 | return elections.get(path) ? new Leadership(path, clusterService.getLocalNode().id(), 0) : null; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Set<String> ownedTopics(NodeId nodeId) { |
| 69 | checkArgument(nodeId != null); |
| 70 | return elections.entrySet() |
| 71 | .stream() |
| 72 | .filter(Entry::getValue) |
| 73 | .map(Entry::getKey) |
| 74 | .collect(Collectors.toSet()); |
| 75 | } |
| 76 | |
| 77 | @Override |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 78 | public void runForLeadership(String path) { |
| 79 | elections.put(path, true); |
| 80 | for (LeadershipEventListener listener : listeners) { |
| 81 | listener.event(new LeadershipEvent(Type.LEADER_ELECTED, |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 82 | new Leadership(path, clusterService.getLocalNode().id(), 0))); |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void withdraw(String path) { |
| 88 | elections.remove(path); |
| 89 | for (LeadershipEventListener listener : listeners) { |
| 90 | listener.event(new LeadershipEvent(Type.LEADER_BOOTED, |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 91 | new Leadership(path, clusterService.getLocalNode().id(), 0))); |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Map<String, Leadership> getLeaderBoard() { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 97 | //FIXME |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 98 | throw new UnsupportedOperationException("I don't know what to do." + |
| 99 | " I wish you luck."); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void addListener(LeadershipEventListener listener) { |
| 104 | listeners.add(listener); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void removeListener(LeadershipEventListener listener) { |
| 109 | listeners.remove(listener); |
| 110 | } |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 111 | } |