Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster; |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 17 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 18 | import java.util.Objects; |
| 19 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.event.AbstractEvent; |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 21 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 22 | import com.google.common.base.MoreObjects; |
| 23 | |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Describes leadership-related event. |
| 26 | */ |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 27 | public class LeadershipEvent extends AbstractEvent<LeadershipEvent.Type, Leadership> { |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Type of leadership-related events. |
| 31 | */ |
| 32 | public enum Type { |
| 33 | /** |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 34 | * 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 Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 37 | */ |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 38 | LEADER_ELECTED, |
| 39 | |
| 40 | /** |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 41 | * 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 Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 44 | */ |
| 45 | LEADER_REELECTED, |
| 46 | |
| 47 | /** |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 48 | * 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 Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 51 | */ |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 52 | LEADER_BOOTED, |
| 53 | |
| 54 | /** |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 55 | * Signifies that the list of candidates for leadership for a topic has |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 56 | * changed. This event does not guarantee accurate leader information. |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 57 | */ |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 58 | CANDIDATES_CHANGED |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates an event of a given type and for the specified instance and the |
| 63 | * current time. |
| 64 | * |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 65 | * @param type leadership event type |
| 66 | * @param leadership event subject |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 67 | */ |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 68 | public LeadershipEvent(Type type, Leadership leadership) { |
| 69 | super(type, leadership); |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 73 | * Creates an event of a given type and for the specified subject and time. |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 74 | * |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 75 | * @param type leadership event type |
| 76 | * @param leadership event subject |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 77 | * @param time occurrence time |
| 78 | */ |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 79 | public LeadershipEvent(Type type, Leadership leadership, long time) { |
| 80 | super(type, leadership, time); |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 83 | @Override |
| 84 | public int hashCode() { |
| 85 | return Objects.hash(type(), subject(), time()); |
| 86 | } |
| 87 | |
| 88 | @Override |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 89 | 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 Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 103 | 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 Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 110 | } |