blob: d1fe6705614552e2e2ac3da4969c4878acdc5cb5 [file] [log] [blame]
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 /**
30 * Type of leadership-related events.
31 */
32 public enum Type {
33 /**
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080034 * Signifies that the leader has been elected.
35 * The event subject is the new leader.
36 * This event does not guarantee accurate candidate information.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080037 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080038 LEADER_ELECTED,
39
40 /**
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080041 * Signifies that the leader has been re-elected.
42 * The event subject is the leader.
43 * This event does not guarantee accurate candidate information.
Madan Jampani1d3494e2014-11-20 11:24:22 -080044 */
45 LEADER_REELECTED,
46
47 /**
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080048 * Signifies that the leader has been booted and lost leadership.
49 * The event subject is the former leader.
50 * This event does not guarantee accurate candidate information.
Madan Jampani1d3494e2014-11-20 11:24:22 -080051 */
Ayaka Koshibea48f7522015-04-01 17:06:09 -070052 LEADER_BOOTED,
53
54 /**
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070055 * Signifies that the list of candidates for leadership for a topic has
Ayaka Koshibefd26a302015-04-13 13:59:54 -070056 * changed. This event does not guarantee accurate leader information.
Ayaka Koshibea48f7522015-04-01 17:06:09 -070057 */
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070058 CANDIDATES_CHANGED
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080059 }
60
61 /**
62 * Creates an event of a given type and for the specified instance and the
63 * current time.
64 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080065 * @param type leadership event type
66 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080067 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080068 public LeadershipEvent(Type type, Leadership leadership) {
69 super(type, leadership);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080070 }
71
72 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080073 * Creates an event of a given type and for the specified subject and time.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080074 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080075 * @param type leadership event type
76 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080077 * @param time occurrence time
78 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080079 public LeadershipEvent(Type type, Leadership leadership, long time) {
80 super(type, leadership, time);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080081 }
82
Madan Jampani1d3494e2014-11-20 11:24:22 -080083 @Override
84 public int hashCode() {
85 return Objects.hash(type(), subject(), time());
86 }
87
88 @Override
Ray Milkey30edb162014-11-24 15:02:14 -080089 public boolean equals(Object obj) {
90 if (this == obj) {
91 return true;
92 }
93 if (obj instanceof LeadershipEvent) {
94 final LeadershipEvent other = (LeadershipEvent) obj;
95 return Objects.equals(this.type(), other.type()) &&
96 Objects.equals(this.subject(), other.subject()) &&
97 Objects.equals(this.time(), other.time());
98 }
99 return false;
100 }
101
102 @Override
Madan Jampani1d3494e2014-11-20 11:24:22 -0800103 public String toString() {
104 return MoreObjects.toStringHelper(this.getClass())
105 .add("type", type())
106 .add("subject", subject())
107 .add("time", time())
108 .toString();
109 }
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800110}