blob: 4242e61f9e0fcc4cd31ea7a045a5e6992f4f0bc7 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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.mastership;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070017
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -070018import org.joda.time.LocalDateTime;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.cluster.RoleInfo;
20import org.onosproject.event.AbstractEvent;
21import org.onosproject.net.DeviceId;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070022
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -070023import com.google.common.base.MoreObjects;
24
Jon Hall672a3ed2017-04-05 13:03:37 -070025import java.util.Objects;
26
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070027/**
Ayaka Koshibe8d504a92014-09-22 17:07:36 -070028 * Describes a device mastership event.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070029 */
30public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> {
31
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070032 //Contains master and standby information.
33 RoleInfo roleInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070034
35 /**
36 * Type of mastership events.
37 */
38 public enum Type {
39 /**
40 * Signifies that the master for a device has changed.
41 */
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070042 MASTER_CHANGED,
43
44 /**
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -070045 * Signifies that the list of backup nodes has changed. If
46 * the change in the backups list is accompanied by a change in
47 * master, the event is subsumed by MASTER_CHANGED.
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070048 */
Jon Hall7a8bfc62016-05-26 17:59:04 -070049 BACKUPS_CHANGED,
50
51 /**
52 * Signifies that the underlying storage for the Mastership state
53 * of this device is unavailable.
54 */
55 SUSPENDED
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070056 }
57
58 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070059 * Creates an event of a given type and for the specified device,
60 * role information, and the current time.
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070061 *
Yuta HIGUCHI8cc43782014-10-30 18:06:43 -070062 * @param type mastership event type
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070063 * @param device event device subject
Yuta HIGUCHI8cc43782014-10-30 18:06:43 -070064 * @param info mastership role information
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070065 */
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070066 public MastershipEvent(Type type, DeviceId device, RoleInfo info) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070067 super(type, device);
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070068 this.roleInfo = info;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070069 }
70
71 /**
72 * Creates an event of a given type and for the specified device, master,
73 * and time.
74 *
75 * @param type mastership event type
76 * @param device event device subject
Thomas Vachuska4b420772014-10-30 16:46:17 -070077 * @param info role information
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070078 * @param time occurrence time
79 */
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070080 public MastershipEvent(Type type, DeviceId device, RoleInfo info, long time) {
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070081 super(type, device, time);
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070082 this.roleInfo = info;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070083 }
84
85 /**
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070086 * Returns the current role state for the subject.
87 *
88 * @return RoleInfo associated with Device ID subject
89 */
90 public RoleInfo roleInfo() {
91 return roleInfo;
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -070092 }
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -070093
94 @Override
Jon Hall672a3ed2017-04-05 13:03:37 -070095 public int hashCode() {
96 return Objects.hash(type(), subject(), roleInfo(), time());
97 }
98
99 @Override
100 public boolean equals(Object obj) {
101 if (this == obj) {
102 return true;
103 }
104 if (obj instanceof MastershipEvent) {
105 final MastershipEvent other = (MastershipEvent) obj;
106 return Objects.equals(this.type(), other.type()) &&
107 Objects.equals(this.subject(), other.subject()) &&
108 Objects.equals(this.roleInfo(), other.roleInfo()) &&
109 Objects.equals(this.time(), other.time());
110 }
111 return false;
112 }
113
114 @Override
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700115 public String toString() {
116 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -0700117 .add("time", new LocalDateTime(time()))
Yuta HIGUCHIfd5cdf12014-10-26 13:28:48 -0700118 .add("type", type())
119 .add("subject", subject())
120 .add("roleInfo", roleInfo)
121 .toString();
122 }
Ayaka Koshibe8b3270f2014-09-22 13:44:42 -0700123}