blob: ae690a5726cc33f422b9daf998c2213cd649f49d [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
alshabibab984662014-12-04 18:56:18 -08003 *
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.cluster;
Madan Jampani1d3494e2014-11-20 11:24:22 -080017
18import java.util.Objects;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070019import java.util.List;
Madan Jampani30a57f82015-03-02 12:19:41 -080020
Madan Jampani1d3494e2014-11-20 11:24:22 -080021import com.google.common.base.MoreObjects;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070022import com.google.common.collect.ImmutableList;
Madan Jampani1d3494e2014-11-20 11:24:22 -080023
24/**
Madan Jampani620f70d2016-01-30 22:22:47 -080025 * 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 Jampani1d3494e2014-11-20 11:24:22 -080030 */
31public class Leadership {
32
33 private final String topic;
Madan Jampani620f70d2016-01-30 22:22:47 -080034 private final Leader leader;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070035 private final List<NodeId> candidates;
Madan Jampani1d3494e2014-11-20 11:24:22 -080036
Madan Jampani620f70d2016-01-30 22:22:47 -080037 public Leadership(String topic, Leader leader, List<NodeId> candidates) {
Madan Jampani1d3494e2014-11-20 11:24:22 -080038 this.topic = topic;
Madan Jampani620f70d2016-01-30 22:22:47 -080039 this.leader = leader;
Ayaka Koshibefd26a302015-04-13 13:59:54 -070040 this.candidates = ImmutableList.copyOf(candidates);
Madan Jampani1d3494e2014-11-20 11:24:22 -080041 }
42
43 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080044 * Returns the leadership topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070045 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080046 * @return leadership topic.
47 */
48 public String topic() {
49 return topic;
50 }
51
52 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080053 * Returns the {@link NodeId nodeId} of the leader.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070054 *
Madan Jampani620f70d2016-01-30 22:22:47 -080055 * @return leader node identifier; will be null if there is no leader
Madan Jampani1d3494e2014-11-20 11:24:22 -080056 */
Madan Jampani620f70d2016-01-30 22:22:47 -080057 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 Jampani1d3494e2014-11-20 11:24:22 -080068 }
69
70 /**
Ayaka Koshibea48f7522015-04-01 17:06:09 -070071 * 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 Jampani1d3494e2014-11-20 11:24:22 -080080 @Override
81 public int hashCode() {
Madan Jampani620f70d2016-01-30 22:22:47 -080082 return Objects.hash(topic, leader, candidates);
Madan Jampani1d3494e2014-11-20 11:24:22 -080083 }
84
85 @Override
Ray Milkey30edb162014-11-24 15:02:14 -080086 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 Milkey50510402015-06-11 14:36:01 -070093 Objects.equals(this.leader, other.leader) &&
Madan Jampani620f70d2016-01-30 22:22:47 -080094 Objects.equals(this.candidates, other.candidates);
Ray Milkey30edb162014-11-24 15:02:14 -080095 }
96 return false;
97 }
98
99 @Override
Madan Jampani1d3494e2014-11-20 11:24:22 -0800100 public String toString() {
101 return MoreObjects.toStringHelper(this.getClass())
102 .add("topic", topic)
103 .add("leader", leader)
Ayaka Koshibea48f7522015-04-01 17:06:09 -0700104 .add("candidates", candidates)
Madan Jampani1d3494e2014-11-20 11:24:22 -0800105 .toString();
106 }
Ray Milkey30edb162014-11-24 15:02:14 -0800107}