Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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.mastership; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.cluster.RoleInfo; |
| 20 | import org.onosproject.event.AbstractEvent; |
| 21 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 22 | |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 23 | import com.google.common.base.MoreObjects; |
| 24 | |
Jon Hall | 672a3ed | 2017-04-05 13:03:37 -0700 | [diff] [blame] | 25 | import java.util.Objects; |
| 26 | |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 27 | /** |
Ayaka Koshibe | 8d504a9 | 2014-09-22 17:07:36 -0700 | [diff] [blame] | 28 | * Describes a device mastership event. |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 29 | */ |
| 30 | public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> { |
| 31 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 32 | //Contains master and standby information. |
| 33 | RoleInfo roleInfo; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Type of mastership events. |
| 37 | */ |
| 38 | public enum Type { |
| 39 | /** |
| 40 | * Signifies that the master for a device has changed. |
| 41 | */ |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 42 | MASTER_CHANGED, |
| 43 | |
| 44 | /** |
Ayaka Koshibe | 98bd12f | 2014-11-01 20:13:37 -0700 | [diff] [blame] | 45 | * 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 Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 48 | */ |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 49 | BACKUPS_CHANGED, |
| 50 | |
| 51 | /** |
| 52 | * Signifies that the underlying storage for the Mastership state |
| 53 | * of this device is unavailable. |
| 54 | */ |
| 55 | SUSPENDED |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 59 | * Creates an event of a given type and for the specified device, |
| 60 | * role information, and the current time. |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 61 | * |
Yuta HIGUCHI | 8cc4378 | 2014-10-30 18:06:43 -0700 | [diff] [blame] | 62 | * @param type mastership event type |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 63 | * @param device event device subject |
Yuta HIGUCHI | 8cc4378 | 2014-10-30 18:06:43 -0700 | [diff] [blame] | 64 | * @param info mastership role information |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 65 | */ |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 66 | public MastershipEvent(Type type, DeviceId device, RoleInfo info) { |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 67 | super(type, device); |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 68 | this.roleInfo = info; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 69 | } |
| 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 Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 77 | * @param info role information |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 78 | * @param time occurrence time |
| 79 | */ |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 80 | public MastershipEvent(Type type, DeviceId device, RoleInfo info, long time) { |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 81 | super(type, device, time); |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 82 | this.roleInfo = info; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 86 | * 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 Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 92 | } |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 93 | |
| 94 | @Override |
Jon Hall | 672a3ed | 2017-04-05 13:03:37 -0700 | [diff] [blame] | 95 | 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 HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 115 | public String toString() { |
| 116 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 117 | .add("time", Tools.defaultOffsetDataTime(time())) |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 118 | .add("type", type()) |
| 119 | .add("subject", subject()) |
| 120 | .add("roleInfo", roleInfo) |
| 121 | .toString(); |
| 122 | } |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 123 | } |