alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 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.cluster; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Objects; |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 19 | import java.util.List; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 20 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 21 | import com.google.common.base.MoreObjects; |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 25 | * State of leadership for topic. |
| 26 | * <p> |
| 27 | * Provided by this construct is the current {@link Leader leader} and the list of |
| 28 | * {@link NodeId nodeId}s currently registered as candidates for election for the topic. |
| 29 | * Keep in mind that only registered candidates can become leaders. |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 30 | */ |
| 31 | public class Leadership { |
| 32 | |
| 33 | private final String topic; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 34 | private final Leader leader; |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 35 | private final List<NodeId> candidates; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 36 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 37 | public Leadership(String topic, Leader leader, List<NodeId> candidates) { |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 38 | this.topic = topic; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 39 | this.leader = leader; |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 40 | this.candidates = ImmutableList.copyOf(candidates); |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 44 | * Returns the leadership topic. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 45 | * |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 46 | * @return leadership topic. |
| 47 | */ |
| 48 | public String topic() { |
| 49 | return topic; |
| 50 | } |
| 51 | |
| 52 | /** |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 53 | * Returns the {@link NodeId nodeId} of the leader. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 54 | * |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 55 | * @return leader node identifier; will be null if there is no leader |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 56 | */ |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 57 | public NodeId leaderNodeId() { |
| 58 | return leader == null ? null : leader.nodeId(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the leader for this topic. |
| 63 | * |
| 64 | * @return leader; will be null if there is no leader for topic |
| 65 | */ |
| 66 | public Leader leader() { |
| 67 | return leader; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 71 | * Returns an preference-ordered list of nodes that are in the leadership |
| 72 | * race for this topic. |
| 73 | * |
| 74 | * @return a list of NodeIds in priority-order, or an empty list. |
| 75 | */ |
| 76 | public List<NodeId> candidates() { |
| 77 | return candidates; |
| 78 | } |
| 79 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 80 | @Override |
| 81 | public int hashCode() { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 82 | return Objects.hash(topic, leader, candidates); |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 86 | public boolean equals(Object obj) { |
| 87 | if (this == obj) { |
| 88 | return true; |
| 89 | } |
| 90 | if (obj instanceof Leadership) { |
| 91 | final Leadership other = (Leadership) obj; |
| 92 | return Objects.equals(this.topic, other.topic) && |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 93 | Objects.equals(this.leader, other.leader) && |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 94 | Objects.equals(this.candidates, other.candidates); |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | @Override |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 100 | public String toString() { |
| 101 | return MoreObjects.toStringHelper(this.getClass()) |
| 102 | .add("topic", topic) |
| 103 | .add("leader", leader) |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 104 | .add("candidates", candidates) |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 105 | .toString(); |
| 106 | } |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 107 | } |