blob: baf37045a513bd09719945b717818042031f8d0e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.device;
tome33cc1a2014-08-25 21:59:41 -070017
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -070018import org.joda.time.LocalDateTime;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.event.AbstractEvent;
20import org.onosproject.net.Device;
21import org.onosproject.net.Port;
tome33cc1a2014-08-25 21:59:41 -070022
Thomas Vachuskad46abf22016-06-07 19:57:03 -070023import java.util.Optional;
24
Pavlin Radoslavov156c2ff2014-10-22 22:00:15 -070025import static com.google.common.base.MoreObjects.toStringHelper;
Thomas Vachuskad46abf22016-06-07 19:57:03 -070026import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
Pavlin Radoslavov156c2ff2014-10-22 22:00:15 -070027
tome33cc1a2014-08-25 21:59:41 -070028/**
29 * Describes infrastructure device event.
30 */
31public class DeviceEvent extends AbstractEvent<DeviceEvent.Type, Device> {
32
tom29df6f42014-09-05 08:14:14 -070033 private final Port port;
Thomas Vachuskad46abf22016-06-07 19:57:03 -070034 private final boolean isAvailable;
tom29df6f42014-09-05 08:14:14 -070035
tome33cc1a2014-08-25 21:59:41 -070036 /**
37 * Type of device events.
38 */
39 public enum Type {
tom64b7aac2014-08-26 00:18:21 -070040 /**
41 * Signifies that a new device has been detected.
42 */
tome33cc1a2014-08-25 21:59:41 -070043 DEVICE_ADDED,
44
tom64b7aac2014-08-26 00:18:21 -070045 /**
tome5ec3fd2014-09-04 15:18:06 -070046 * Signifies that some device attributes have changed; excludes
47 * availability changes.
48 */
49 DEVICE_UPDATED,
50
51 /**
tom64b7aac2014-08-26 00:18:21 -070052 * Signifies that a device has been removed.
53 */
tome33cc1a2014-08-25 21:59:41 -070054 DEVICE_REMOVED,
55
tom64b7aac2014-08-26 00:18:21 -070056 /**
57 * Signifies that a device has been administratively suspended.
58 */
tome33cc1a2014-08-25 21:59:41 -070059 DEVICE_SUSPENDED,
60
tom64b7aac2014-08-26 00:18:21 -070061 /**
62 * Signifies that a device has come online or has gone offline.
63 */
tome33cc1a2014-08-25 21:59:41 -070064 DEVICE_AVAILABILITY_CHANGED,
65
66 /**
tom29df6f42014-09-05 08:14:14 -070067 * 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 */
sangho538108b2015-04-08 14:29:20 -070079 PORT_REMOVED,
80
Thomas Vachuskacb5016f2015-05-18 14:11:43 -070081 /**
sangho538108b2015-04-08 14:29:20 -070082 * Signifies that port statistics has been updated.
83 */
84 PORT_STATS_UPDATED
tome33cc1a2014-08-25 21:59:41 -070085 }
86
87 /**
tom64b7aac2014-08-26 00:18:21 -070088 * Creates an event of a given type and for the specified device and the
tome33cc1a2014-08-25 21:59:41 -070089 * current time.
90 *
tom64b7aac2014-08-26 00:18:21 -070091 * @param type device event type
92 * @param device event device subject
tome33cc1a2014-08-25 21:59:41 -070093 */
tom64b7aac2014-08-26 00:18:21 -070094 public DeviceEvent(Type type, Device device) {
tom29df6f42014-09-05 08:14:14 -070095 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) {
tom64b7aac2014-08-26 00:18:21 -0700107 super(type, device);
tom29df6f42014-09-05 08:14:14 -0700108 this.port = port;
Thomas Vachuskad46abf22016-06-07 19:57:03 -0700109 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;
tome33cc1a2014-08-25 21:59:41 -0700123 }
124
125 /**
tom64b7aac2014-08-26 00:18:21 -0700126 * Creates an event of a given type and for the specified device and time.
tome33cc1a2014-08-25 21:59:41 -0700127 *
tom64b7aac2014-08-26 00:18:21 -0700128 * @param type device event type
129 * @param device event device subject
tom29df6f42014-09-05 08:14:14 -0700130 * @param port optional port subject
tom64b7aac2014-08-26 00:18:21 -0700131 * @param time occurrence time
tome33cc1a2014-08-25 21:59:41 -0700132 */
tom29df6f42014-09-05 08:14:14 -0700133 public DeviceEvent(Type type, Device device, Port port, long time) {
tom64b7aac2014-08-26 00:18:21 -0700134 super(type, device, time);
tom29df6f42014-09-05 08:14:14 -0700135 this.port = port;
Thomas Vachuskad46abf22016-06-07 19:57:03 -0700136 this.isAvailable = false;
tom29df6f42014-09-05 08:14:14 -0700137 }
138
139 /**
140 * Returns the port subject.
141 *
142 * @return port subject or null if the event is not port specific.
143 */
tom0efbb1d2014-09-09 11:54:28 -0700144 public Port port() {
tom29df6f42014-09-05 08:14:14 -0700145 return port;
tome33cc1a2014-08-25 21:59:41 -0700146 }
147
Thomas Vachuskad46abf22016-06-07 19:57:03 -0700148 /**
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 Radoslavov156c2ff2014-10-22 22:00:15 -0700157 @Override
158 public String toString() {
159 if (port == null) {
160 return super.toString();
161 }
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -0700162 return toStringHelper(this)
163 .add("time", new LocalDateTime(time()))
164 .add("type", type())
165 .add("subject", subject())
166 .add("port", port)
167 .toString();
Thomas Vachuskad46abf22016-06-07 19:57:03 -0700168 }
tome33cc1a2014-08-25 21:59:41 -0700169}