blob: bf202704b670e6824daf312eb044d00870e0aec2 [file] [log] [blame]
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
16package org.onlab.onos.cluster;
17
Madan Jampani1d3494e2014-11-20 11:24:22 -080018import java.util.Objects;
19
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080020import org.onlab.onos.event.AbstractEvent;
21
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 /**
46 * Signifies that the leader has been booted and lost leadership. The event subject is the
47 * former leader.
48 */
49 LEADER_BOOTED
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080050 }
51
52 /**
53 * Creates an event of a given type and for the specified instance and the
54 * current time.
55 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080056 * @param type leadership event type
57 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080058 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080059 public LeadershipEvent(Type type, Leadership leadership) {
60 super(type, leadership);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080061 }
62
63 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080064 * Creates an event of a given type and for the specified subject and time.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080065 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080066 * @param type leadership event type
67 * @param leadership event subject
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080068 * @param time occurrence time
69 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080070 public LeadershipEvent(Type type, Leadership leadership, long time) {
71 super(type, leadership, time);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080072 }
73
Madan Jampani1d3494e2014-11-20 11:24:22 -080074 @Override
75 public int hashCode() {
76 return Objects.hash(type(), subject(), time());
77 }
78
79 @Override
Ray Milkey30edb162014-11-24 15:02:14 -080080 public boolean equals(Object obj) {
81 if (this == obj) {
82 return true;
83 }
84 if (obj instanceof LeadershipEvent) {
85 final LeadershipEvent other = (LeadershipEvent) obj;
86 return Objects.equals(this.type(), other.type()) &&
87 Objects.equals(this.subject(), other.subject()) &&
88 Objects.equals(this.time(), other.time());
89 }
90 return false;
91 }
92
93 @Override
Madan Jampani1d3494e2014-11-20 11:24:22 -080094 public String toString() {
95 return MoreObjects.toStringHelper(this.getClass())
96 .add("type", type())
97 .add("subject", subject())
98 .add("time", time())
99 .toString();
100 }
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800101}