blob: d61772e6b06024eb594df4f50b3819e4e5da996e [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.trivial.impl;
Jonathan Hart949c2842014-11-28 23:44:09 -080017
Madan Jampani59610512015-02-25 15:25:43 -080018import static com.google.common.base.Preconditions.checkArgument;
19
Jonathan Hart949c2842014-11-28 23:44:09 -080020import java.util.Map;
Madan Jampani59610512015-02-25 15:25:43 -080021import java.util.Map.Entry;
Jonathan Hart949c2842014-11-28 23:44:09 -080022import java.util.Set;
23import java.util.concurrent.ConcurrentHashMap;
24import java.util.concurrent.CopyOnWriteArraySet;
Madan Jampani59610512015-02-25 15:25:43 -080025import java.util.stream.Collectors;
Jonathan Hart949c2842014-11-28 23:44:09 -080026
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.cluster.ClusterService;
32import org.onosproject.cluster.Leadership;
33import org.onosproject.cluster.LeadershipEvent;
34import org.onosproject.cluster.LeadershipEvent.Type;
35import org.onosproject.cluster.LeadershipEventListener;
36import org.onosproject.cluster.LeadershipService;
37import org.onosproject.cluster.NodeId;
Jonathan Hart949c2842014-11-28 23:44:09 -080038
39/**
40 * A trivial implementation of the leadership service.
alshabiba9819bf2014-11-30 18:15:52 -080041 * <p>
Jonathan Hart949c2842014-11-28 23:44:09 -080042 * 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
47public 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 Jampani8d21c792014-12-01 16:31:07 -080057 public NodeId getLeader(String path) {
58 return elections.get(path) ? clusterService.getLocalNode().id() : null;
Jonathan Hart949c2842014-11-28 23:44:09 -080059 }
60
61 @Override
Madan Jampani59610512015-02-25 15:25:43 -080062 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 Hart949c2842014-11-28 23:44:09 -080078 public void runForLeadership(String path) {
79 elections.put(path, true);
80 for (LeadershipEventListener listener : listeners) {
81 listener.event(new LeadershipEvent(Type.LEADER_ELECTED,
Madan Jampani8d21c792014-12-01 16:31:07 -080082 new Leadership(path, clusterService.getLocalNode().id(), 0)));
Jonathan Hart949c2842014-11-28 23:44:09 -080083 }
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 Jampani8d21c792014-12-01 16:31:07 -080091 new Leadership(path, clusterService.getLocalNode().id(), 0)));
Jonathan Hart949c2842014-11-28 23:44:09 -080092 }
93 }
94
95 @Override
96 public Map<String, Leadership> getLeaderBoard() {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080097 //FIXME
Jonathan Hart949c2842014-11-28 23:44:09 -080098 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 Hart949c2842014-11-28 23:44:09 -0800111}