blob: a7bbe68021192e69b114aa9893eb557d062b835f [file] [log] [blame]
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Jon Hall7a8bfc62016-05-26 17:59:04 -070044 // while still remaining a candidate
Madan Jampani620f70d2016-01-30 22:22:47 -080045 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 */
Jon Hall7a8bfc62016-05-26 17:59:04 -070050 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 Radoslavova6b754c2014-11-18 13:55:37 -080061 }
62
63 /**
64 * Creates an event of a given type and for the specified instance and the
65 * current time.
66 *
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 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080070 public LeadershipEvent(Type type, Leadership leadership) {
71 super(type, leadership);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080072 }
73
74 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080075 * Creates an event of a given type and for the specified subject and time.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080076 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080077 * @param type leadership event type
78 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080079 * @param time occurrence time
80 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080081 public LeadershipEvent(Type type, Leadership leadership, long time) {
82 super(type, leadership, time);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080083 }
84
Madan Jampani1d3494e2014-11-20 11:24:22 -080085 @Override
86 public int hashCode() {
87 return Objects.hash(type(), subject(), time());
88 }
89
90 @Override
Ray Milkey30edb162014-11-24 15:02:14 -080091 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 Jampani1d3494e2014-11-20 11:24:22 -0800105 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 Radoslavova6b754c2014-11-18 13:55:37 -0800112}