Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | /** |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 30 | * Type of leadership events. |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 31 | */ |
| 32 | public enum Type { |
| 33 | /** |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 34 | * 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 Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 37 | */ |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 38 | LEADER_AND_CANDIDATES_CHANGED, |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 41 | * Signifies that the leader for a topic has changed. |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 42 | */ |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 43 | // TODO: We may not need this. We currently do not support a way for a current leader to step down |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 44 | // while still remaining a candidate |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 45 | LEADER_CHANGED, |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 48 | * Signifies a change in the list of candidates for a topic. |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 49 | */ |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 50 | CANDIDATES_CHANGED, |
| 51 | |
| 52 | /** |
| 53 | * Signifies the Leadership Elector is unavailable. |
| 54 | */ |
| 55 | SERVICE_DISRUPTED, |
| 56 | |
| 57 | /** |
| 58 | * Signifies the Leadership Elector is available again. |
| 59 | */ |
| 60 | SERVICE_RESTORED |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates an event of a given type and for the specified instance and the |
| 65 | * current time. |
| 66 | * |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 67 | * @param type leadership event type |
| 68 | * @param leadership event subject |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 69 | */ |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 70 | public LeadershipEvent(Type type, Leadership leadership) { |
| 71 | super(type, leadership); |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 75 | * 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] | 76 | * |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 77 | * @param type leadership event type |
| 78 | * @param leadership event subject |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 79 | * @param time occurrence time |
| 80 | */ |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 81 | public LeadershipEvent(Type type, Leadership leadership, long time) { |
| 82 | super(type, leadership, time); |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 85 | @Override |
| 86 | public int hashCode() { |
| 87 | return Objects.hash(type(), subject(), time()); |
| 88 | } |
| 89 | |
| 90 | @Override |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 91 | public boolean equals(Object obj) { |
| 92 | if (this == obj) { |
| 93 | return true; |
| 94 | } |
| 95 | if (obj instanceof LeadershipEvent) { |
| 96 | final LeadershipEvent other = (LeadershipEvent) obj; |
| 97 | return Objects.equals(this.type(), other.type()) && |
| 98 | Objects.equals(this.subject(), other.subject()) && |
| 99 | Objects.equals(this.time(), other.time()); |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | @Override |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 105 | public String toString() { |
| 106 | return MoreObjects.toStringHelper(this.getClass()) |
| 107 | .add("type", type()) |
| 108 | .add("subject", subject()) |
| 109 | .add("time", time()) |
| 110 | .toString(); |
| 111 | } |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 112 | } |