blob: 79bd717290a86e685cbba79377436b095c51539f [file] [log] [blame]
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -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;
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080017
Madan Jampani1d3494e2014-11-20 11:24:22 -080018import java.util.Objects;
19
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.event.AbstractEvent;
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080021
Madan Jampani1d3494e2014-11-20 11:24:22 -080022import com.google.common.base.MoreObjects;
23
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080024/**
25 * Describes leadership-related event.
26 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080027public class LeadershipEvent extends AbstractEvent<LeadershipEvent.Type, Leadership> {
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080028
29 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080030 * Type of leadership events.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080031 */
32 public enum Type {
33 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080034 * Signifies a change in both the leader as well as change to the list of candidates. Keep in mind though that
35 * the first node entering the race will trigger this event as it will become a candidate and automatically get
36 * promoted to become leader.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080037 */
Madan Jampani620f70d2016-01-30 22:22:47 -080038 LEADER_AND_CANDIDATES_CHANGED,
Madan Jampani1d3494e2014-11-20 11:24:22 -080039
40 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080041 * Signifies that the leader for a topic has changed.
Madan Jampani1d3494e2014-11-20 11:24:22 -080042 */
Madan Jampani620f70d2016-01-30 22:22:47 -080043 // TODO: We may not need this. We currently do not support a way for a current leader to step down
44 // while still reamining a candidate
45 LEADER_CHANGED,
Madan Jampani1d3494e2014-11-20 11:24:22 -080046
47 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080048 * Signifies a change in the list of candidates for a topic.
Ayaka Koshibea48f7522015-04-01 17:06:09 -070049 */
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070050 CANDIDATES_CHANGED
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080051 }
52
53 /**
54 * Creates an event of a given type and for the specified instance and the
55 * current time.
56 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080057 * @param type leadership event type
58 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080059 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080060 public LeadershipEvent(Type type, Leadership leadership) {
61 super(type, leadership);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080062 }
63
64 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080065 * Creates an event of a given type and for the specified subject and time.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080066 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080067 * @param type leadership event type
68 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080069 * @param time occurrence time
70 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080071 public LeadershipEvent(Type type, Leadership leadership, long time) {
72 super(type, leadership, time);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080073 }
74
Madan Jampani1d3494e2014-11-20 11:24:22 -080075 @Override
76 public int hashCode() {
77 return Objects.hash(type(), subject(), time());
78 }
79
80 @Override
Ray Milkey30edb162014-11-24 15:02:14 -080081 public boolean equals(Object obj) {
82 if (this == obj) {
83 return true;
84 }
85 if (obj instanceof LeadershipEvent) {
86 final LeadershipEvent other = (LeadershipEvent) obj;
87 return Objects.equals(this.type(), other.type()) &&
88 Objects.equals(this.subject(), other.subject()) &&
89 Objects.equals(this.time(), other.time());
90 }
91 return false;
92 }
93
94 @Override
Madan Jampani1d3494e2014-11-20 11:24:22 -080095 public String toString() {
96 return MoreObjects.toStringHelper(this.getClass())
97 .add("type", type())
98 .add("subject", subject())
99 .add("time", time())
100 .toString();
101 }
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800102}