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 | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Type of mastership events. |
| 34 | */ |
| 35 | public enum Type { |
| 36 | /** |
| 37 | * Signifies that the master for a device has changed. |
| 38 | */ |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 39 | MASTER_CHANGED, |
| 40 | |
| 41 | /** |
Ayaka Koshibe | 98bd12f | 2014-11-01 20:13:37 -0700 | [diff] [blame] | 42 | * Signifies that the list of backup nodes has changed. If |
| 43 | * the change in the backups list is accompanied by a change in |
| 44 | * master, the event is subsumed by MASTER_CHANGED. |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 45 | */ |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 46 | BACKUPS_CHANGED, |
| 47 | |
| 48 | /** |
| 49 | * Signifies that the underlying storage for the Mastership state |
| 50 | * of this device is unavailable. |
| 51 | */ |
Andrea Campanella | 80b296d | 2018-09-18 10:48:15 +0200 | [diff] [blame] | 52 | SUSPENDED, |
| 53 | |
| 54 | /** |
| 55 | * Signifies that the underlying storage for the Mastership state |
| 56 | * of this device became available again. |
| 57 | */ |
| 58 | RESTORED |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 61 | private final MastershipInfo mastershipInfo; |
| 62 | |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 63 | /** |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 64 | * Creates an event of a given type and for the specified device, |
| 65 | * role information, and the current time. |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 66 | * |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 67 | * @param type mastership event type |
| 68 | * @param device event device subject |
| 69 | * @param mastershipInfo mastership info |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 70 | */ |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 71 | public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo) { |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 72 | super(type, device); |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 73 | this.mastershipInfo = mastershipInfo; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Creates an event of a given type and for the specified device, master, |
| 78 | * and time. |
| 79 | * |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 80 | * @param type mastership event type |
| 81 | * @param device event device subject |
| 82 | * @param mastershipInfo mastership information |
| 83 | * @param time occurrence time |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 84 | */ |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 85 | public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo, long time) { |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 86 | super(type, device, time); |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 87 | this.mastershipInfo = mastershipInfo; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the mastership info. |
| 92 | * |
| 93 | * @return the mastership info |
| 94 | */ |
| 95 | public MastershipInfo mastershipInfo() { |
| 96 | return mastershipInfo; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 100 | * Returns the current role state for the subject. |
| 101 | * |
| 102 | * @return RoleInfo associated with Device ID subject |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 103 | * @deprecated since 1.14 |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 104 | */ |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 105 | @Deprecated |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 106 | public RoleInfo roleInfo() { |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 107 | return new RoleInfo(mastershipInfo.master().orElse(null), mastershipInfo.backups()); |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 108 | } |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 109 | |
| 110 | @Override |
Jon Hall | 672a3ed | 2017-04-05 13:03:37 -0700 | [diff] [blame] | 111 | public int hashCode() { |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 112 | return Objects.hash(type(), subject(), mastershipInfo(), time()); |
Jon Hall | 672a3ed | 2017-04-05 13:03:37 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean equals(Object obj) { |
| 117 | if (this == obj) { |
| 118 | return true; |
| 119 | } |
| 120 | if (obj instanceof MastershipEvent) { |
| 121 | final MastershipEvent other = (MastershipEvent) obj; |
| 122 | return Objects.equals(this.type(), other.type()) && |
| 123 | Objects.equals(this.subject(), other.subject()) && |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 124 | Objects.equals(this.mastershipInfo(), other.mastershipInfo()) && |
Jon Hall | 672a3ed | 2017-04-05 13:03:37 -0700 | [diff] [blame] | 125 | Objects.equals(this.time(), other.time()); |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | @Override |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 133 | .add("time", Tools.defaultOffsetDataTime(time())) |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 134 | .add("type", type()) |
| 135 | .add("subject", subject()) |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 136 | .add("mastershipInfo", mastershipInfo()) |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 137 | .toString(); |
| 138 | } |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 139 | } |