blob: 86c951eb64a71b2ce4eb3ee2c1d476504f586585 [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.cluster;
Madan Jampani1d3494e2014-11-20 11:24:22 -080017
18import java.util.Objects;
19
20import com.google.common.base.MoreObjects;
21
22/**
23 * Abstract leadership concept.
24 */
25public class Leadership {
26
27 private final String topic;
Madan Jampani8d21c792014-12-01 16:31:07 -080028 private final NodeId leader;
Madan Jampani1ee91782014-11-20 20:24:24 -080029 private final long epoch;
Madan Jampani1d3494e2014-11-20 11:24:22 -080030
Madan Jampani8d21c792014-12-01 16:31:07 -080031 public Leadership(String topic, NodeId leader, long epoch) {
Madan Jampani1d3494e2014-11-20 11:24:22 -080032 this.topic = topic;
33 this.leader = leader;
Madan Jampani1ee91782014-11-20 20:24:24 -080034 this.epoch = epoch;
Madan Jampani1d3494e2014-11-20 11:24:22 -080035 }
36
37 /**
38 * The topic for which this leadership applies.
39 * @return leadership topic.
40 */
41 public String topic() {
42 return topic;
43 }
44
45 /**
Madan Jampani8d21c792014-12-01 16:31:07 -080046 * The nodeId of leader for this topic.
Madan Jampani1d3494e2014-11-20 11:24:22 -080047 * @return leader node.
48 */
Madan Jampani8d21c792014-12-01 16:31:07 -080049 public NodeId leader() {
Madan Jampani1d3494e2014-11-20 11:24:22 -080050 return leader;
51 }
52
53 /**
Madan Jampani1ee91782014-11-20 20:24:24 -080054 * The epoch when the leadership was assumed.
55 * @return leadership epoch
Madan Jampani1d3494e2014-11-20 11:24:22 -080056 */
Madan Jampani1ee91782014-11-20 20:24:24 -080057 public long epoch() {
58 return epoch;
Madan Jampani1d3494e2014-11-20 11:24:22 -080059 }
60
61 @Override
62 public int hashCode() {
Madan Jampani1ee91782014-11-20 20:24:24 -080063 return Objects.hash(topic, leader, epoch);
Madan Jampani1d3494e2014-11-20 11:24:22 -080064 }
65
66 @Override
Ray Milkey30edb162014-11-24 15:02:14 -080067 public boolean equals(Object obj) {
68 if (this == obj) {
69 return true;
70 }
71 if (obj instanceof Leadership) {
72 final Leadership other = (Leadership) obj;
73 return Objects.equals(this.topic, other.topic) &&
74 Objects.equals(this.leader, other.leader) &&
75 Objects.equals(this.epoch, other.epoch);
76 }
77 return false;
78 }
79
80 @Override
Madan Jampani1d3494e2014-11-20 11:24:22 -080081 public String toString() {
82 return MoreObjects.toStringHelper(this.getClass())
83 .add("topic", topic)
84 .add("leader", leader)
Madan Jampani1ee91782014-11-20 20:24:24 -080085 .add("epoch", epoch)
Madan Jampani1d3494e2014-11-20 11:24:22 -080086 .toString();
87 }
Ray Milkey30edb162014-11-24 15:02:14 -080088}