Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.mastership; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 18 | import org.joda.time.LocalDateTime; |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 19 | import org.onlab.onos.cluster.RoleInfo; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 20 | import org.onlab.onos.event.AbstractEvent; |
| 21 | import org.onlab.onos.net.DeviceId; |
| 22 | |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 23 | import com.google.common.base.MoreObjects; |
| 24 | |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 25 | /** |
Ayaka Koshibe | 8d504a9 | 2014-09-22 17:07:36 -0700 | [diff] [blame] | 26 | * Describes a device mastership event. |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 27 | */ |
| 28 | public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> { |
| 29 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 30 | //Contains master and standby information. |
| 31 | RoleInfo roleInfo; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Type of mastership events. |
| 35 | */ |
| 36 | public enum Type { |
| 37 | /** |
| 38 | * Signifies that the master for a device has changed. |
| 39 | */ |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 40 | MASTER_CHANGED, |
| 41 | |
| 42 | /** |
| 43 | * Signifies that the list of backup nodes has changed. |
| 44 | */ |
| 45 | BACKUPS_CHANGED |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 49 | * Creates an event of a given type and for the specified device, |
| 50 | * role information, and the current time. |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 51 | * |
| 52 | * @param type device event type |
| 53 | * @param device event device subject |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 54 | * @param info mastership role information subject |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 55 | */ |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 56 | public MastershipEvent(Type type, DeviceId device, RoleInfo info) { |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 57 | super(type, device); |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 58 | this.roleInfo = info; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates an event of a given type and for the specified device, master, |
| 63 | * and time. |
| 64 | * |
| 65 | * @param type mastership event type |
| 66 | * @param device event device subject |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 67 | * @param info role information |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 68 | * @param time occurrence time |
| 69 | */ |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 70 | public MastershipEvent(Type type, DeviceId device, RoleInfo info, long time) { |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 71 | super(type, device, time); |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 72 | this.roleInfo = info; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 76 | * Returns the current role state for the subject. |
| 77 | * |
| 78 | * @return RoleInfo associated with Device ID subject |
| 79 | */ |
| 80 | public RoleInfo roleInfo() { |
| 81 | return roleInfo; |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 82 | } |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 83 | |
| 84 | @Override |
| 85 | public String toString() { |
| 86 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 87 | .add("time", new LocalDateTime(time())) |
Yuta HIGUCHI | fd5cdf1 | 2014-10-26 13:28:48 -0700 | [diff] [blame] | 88 | .add("type", type()) |
| 89 | .add("subject", subject()) |
| 90 | .add("roleInfo", roleInfo) |
| 91 | .toString(); |
| 92 | } |
Ayaka Koshibe | 8b3270f | 2014-09-22 13:44:42 -0700 | [diff] [blame] | 93 | } |