blob: faf6dd45f4d531ab343088550fa45dc2dc11a49f [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 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080034 * Signifies that the leader has been elected. The event subject is the
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080035 * new leader.
36 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080037 LEADER_ELECTED,
38
39 /**
40 * Signifies that the leader has been re-elected. The event subject is the
41 * leader.
42 */
43 LEADER_REELECTED,
44
45 /**
Ayaka Koshibefd26a302015-04-13 13:59:54 -070046 * Signifies that the leader has been booted and lost leadership. The
47 * event subject is the former leader.
Madan Jampani1d3494e2014-11-20 11:24:22 -080048 */
Ayaka Koshibea48f7522015-04-01 17:06:09 -070049 LEADER_BOOTED,
50
51 /**
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070052 * Signifies that the list of candidates for leadership for a topic has
Ayaka Koshibefd26a302015-04-13 13:59:54 -070053 * changed. This event does not guarantee accurate leader information.
Ayaka Koshibea48f7522015-04-01 17:06:09 -070054 */
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070055 CANDIDATES_CHANGED
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080056 }
57
58 /**
59 * Creates an event of a given type and for the specified instance and the
60 * current time.
61 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080062 * @param type leadership event type
63 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080064 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080065 public LeadershipEvent(Type type, Leadership leadership) {
66 super(type, leadership);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080067 }
68
69 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080070 * Creates an event of a given type and for the specified subject and time.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080071 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080072 * @param type leadership event type
73 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080074 * @param time occurrence time
75 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080076 public LeadershipEvent(Type type, Leadership leadership, long time) {
77 super(type, leadership, time);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080078 }
79
Madan Jampani1d3494e2014-11-20 11:24:22 -080080 @Override
81 public int hashCode() {
82 return Objects.hash(type(), subject(), time());
83 }
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 LeadershipEvent) {
91 final LeadershipEvent other = (LeadershipEvent) obj;
92 return Objects.equals(this.type(), other.type()) &&
93 Objects.equals(this.subject(), other.subject()) &&
94 Objects.equals(this.time(), other.time());
95 }
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("type", type())
103 .add("subject", subject())
104 .add("time", time())
105 .toString();
106 }
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800107}