Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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.net.device; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 18 | import org.joda.time.LocalDateTime; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.event.AbstractEvent; |
| 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.Port; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 22 | |
Thomas Vachuska | d46abf2 | 2016-06-07 19:57:03 -0700 | [diff] [blame] | 23 | import java.util.Optional; |
| 24 | |
Pavlin Radoslavov | 156c2ff | 2014-10-22 22:00:15 -0700 | [diff] [blame] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thomas Vachuska | d46abf2 | 2016-06-07 19:57:03 -0700 | [diff] [blame] | 26 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
Pavlin Radoslavov | 156c2ff | 2014-10-22 22:00:15 -0700 | [diff] [blame] | 27 | |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Describes infrastructure device event. |
| 30 | */ |
| 31 | public class DeviceEvent extends AbstractEvent<DeviceEvent.Type, Device> { |
| 32 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 33 | private final Port port; |
Thomas Vachuska | d46abf2 | 2016-06-07 19:57:03 -0700 | [diff] [blame] | 34 | private final boolean isAvailable; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 35 | |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Type of device events. |
| 38 | */ |
| 39 | public enum Type { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Signifies that a new device has been detected. |
| 42 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 43 | DEVICE_ADDED, |
| 44 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 45 | /** |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 46 | * Signifies that some device attributes have changed; excludes |
| 47 | * availability changes. |
| 48 | */ |
| 49 | DEVICE_UPDATED, |
| 50 | |
| 51 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 52 | * Signifies that a device has been removed. |
| 53 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 54 | DEVICE_REMOVED, |
| 55 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Signifies that a device has been administratively suspended. |
| 58 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 59 | DEVICE_SUSPENDED, |
| 60 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Signifies that a device has come online or has gone offline. |
| 63 | */ |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 64 | DEVICE_AVAILABILITY_CHANGED, |
| 65 | |
| 66 | /** |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 67 | * Signifies that a port has been added. |
| 68 | */ |
| 69 | PORT_ADDED, |
| 70 | |
| 71 | /** |
| 72 | * Signifies that a port has been updated. |
| 73 | */ |
| 74 | PORT_UPDATED, |
| 75 | |
| 76 | /** |
| 77 | * Signifies that a port has been removed. |
| 78 | */ |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 79 | PORT_REMOVED, |
| 80 | |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 81 | /** |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 82 | * Signifies that port statistics has been updated. |
| 83 | */ |
| 84 | PORT_STATS_UPDATED |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 88 | * 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] | 89 | * current time. |
| 90 | * |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 91 | * @param type device event type |
| 92 | * @param device event device subject |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 93 | */ |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 94 | public DeviceEvent(Type type, Device device) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 95 | this(type, device, null); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates an event of a given type and for the specified device, port |
| 100 | * and the current time. |
| 101 | * |
| 102 | * @param type device event type |
| 103 | * @param device event device subject |
| 104 | * @param port optional port subject |
| 105 | */ |
| 106 | public DeviceEvent(Type type, Device device, Port port) { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 107 | super(type, device); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 108 | this.port = port; |
Thomas Vachuska | d46abf2 | 2016-06-07 19:57:03 -0700 | [diff] [blame] | 109 | this.isAvailable = false; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Creates an event for change of device availability for the given device |
| 114 | * and the current time. |
| 115 | * |
| 116 | * @param device event device subject |
| 117 | * @param isAvailable true if device became available; false otherwise |
| 118 | */ |
| 119 | public DeviceEvent(Device device, boolean isAvailable) { |
| 120 | super(DEVICE_AVAILABILITY_CHANGED, device); |
| 121 | this.port = null; |
| 122 | this.isAvailable = isAvailable; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 126 | * 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] | 127 | * |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 128 | * @param type device event type |
| 129 | * @param device event device subject |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 130 | * @param port optional port subject |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 131 | * @param time occurrence time |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 132 | */ |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 133 | public DeviceEvent(Type type, Device device, Port port, long time) { |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 134 | super(type, device, time); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 135 | this.port = port; |
Thomas Vachuska | d46abf2 | 2016-06-07 19:57:03 -0700 | [diff] [blame] | 136 | this.isAvailable = false; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns the port subject. |
| 141 | * |
| 142 | * @return port subject or null if the event is not port specific. |
| 143 | */ |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 144 | public Port port() { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 145 | return port; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Thomas Vachuska | d46abf2 | 2016-06-07 19:57:03 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Indicates whether device became available or unavailable. |
| 150 | * |
| 151 | * @return if present, true indicates device came online; false if device went offline |
| 152 | */ |
| 153 | public Optional<Boolean> isAvailable() { |
| 154 | return type() == DEVICE_AVAILABILITY_CHANGED ? Optional.of(isAvailable) : Optional.empty(); |
| 155 | } |
| 156 | |
Pavlin Radoslavov | 156c2ff | 2014-10-22 22:00:15 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public String toString() { |
| 159 | if (port == null) { |
| 160 | return super.toString(); |
| 161 | } |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 162 | return toStringHelper(this) |
| 163 | .add("time", new LocalDateTime(time())) |
| 164 | .add("type", type()) |
| 165 | .add("subject", subject()) |
| 166 | .add("port", port) |
| 167 | .toString(); |
Thomas Vachuska | d46abf2 | 2016-06-07 19:57:03 -0700 | [diff] [blame] | 168 | } |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 169 | } |