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 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.device; |
| 17 | |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 18 | import org.joda.time.LocalDateTime; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 19 | import org.onlab.onos.event.AbstractEvent; |
| 20 | import org.onlab.onos.net.Device; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.Port; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 22 | |
Pavlin Radoslavov | 156c2ff | 2014-10-22 22:00:15 -0700 | [diff] [blame] | 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 24 | |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Describes infrastructure device event. |
| 27 | */ |
| 28 | public class DeviceEvent extends AbstractEvent<DeviceEvent.Type, Device> { |
| 29 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 30 | private final Port port; |
| 31 | |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Type of device events. |
| 34 | */ |
| 35 | public enum Type { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Signifies that a new device has been detected. |
| 38 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 39 | DEVICE_ADDED, |
| 40 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 41 | /** |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 42 | * Signifies that some device attributes have changed; excludes |
| 43 | * availability changes. |
| 44 | */ |
| 45 | DEVICE_UPDATED, |
| 46 | |
| 47 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 48 | * Signifies that a device has been removed. |
| 49 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 50 | DEVICE_REMOVED, |
| 51 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Signifies that a device has been administratively suspended. |
| 54 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 55 | DEVICE_SUSPENDED, |
| 56 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Signifies that a device has come online or has gone offline. |
| 59 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 60 | DEVICE_AVAILABILITY_CHANGED, |
| 61 | |
| 62 | /** |
| 63 | * Signifies that the current controller instance relationship has |
| 64 | * changed with respect to a device. |
| 65 | */ |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 66 | DEVICE_MASTERSHIP_CHANGED, |
| 67 | |
| 68 | /** |
| 69 | * Signifies that a port has been added. |
| 70 | */ |
| 71 | PORT_ADDED, |
| 72 | |
| 73 | /** |
| 74 | * Signifies that a port has been updated. |
| 75 | */ |
| 76 | PORT_UPDATED, |
| 77 | |
| 78 | /** |
| 79 | * Signifies that a port has been removed. |
| 80 | */ |
| 81 | PORT_REMOVED |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 85 | * Creates an event of a given type and for the specified device and the |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 86 | * current time. |
| 87 | * |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 88 | * @param type device event type |
| 89 | * @param device event device subject |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 90 | */ |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 91 | public DeviceEvent(Type type, Device device) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 92 | this(type, device, null); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates an event of a given type and for the specified device, port |
| 97 | * and the current time. |
| 98 | * |
| 99 | * @param type device event type |
| 100 | * @param device event device subject |
| 101 | * @param port optional port subject |
| 102 | */ |
| 103 | public DeviceEvent(Type type, Device device, Port port) { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 104 | super(type, device); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 105 | this.port = port; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 109 | * Creates an event of a given type and for the specified device and time. |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 110 | * |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 111 | * @param type device event type |
| 112 | * @param device event device subject |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 113 | * @param port optional port subject |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 114 | * @param time occurrence time |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 115 | */ |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 116 | public DeviceEvent(Type type, Device device, Port port, long time) { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 117 | super(type, device, time); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 118 | this.port = port; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the port subject. |
| 123 | * |
| 124 | * @return port subject or null if the event is not port specific. |
| 125 | */ |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 126 | public Port port() { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 127 | return port; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Pavlin Radoslavov | 156c2ff | 2014-10-22 22:00:15 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public String toString() { |
| 132 | if (port == null) { |
| 133 | return super.toString(); |
| 134 | } |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 135 | return toStringHelper(this) |
| 136 | .add("time", new LocalDateTime(time())) |
| 137 | .add("type", type()) |
| 138 | .add("subject", subject()) |
| 139 | .add("port", port) |
| 140 | .toString(); |
Pavlin Radoslavov | 156c2ff | 2014-10-22 22:00:15 -0700 | [diff] [blame] | 141 | } |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 142 | } |