blob: 7fa981ec0c44287fd028f13336fee6c720aca40b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
tome33cc1a2014-08-25 21:59:41 -070016package org.onlab.onos.net.device;
17
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -070018import org.joda.time.LocalDateTime;
tome33cc1a2014-08-25 21:59:41 -070019import org.onlab.onos.event.AbstractEvent;
20import org.onlab.onos.net.Device;
tom29df6f42014-09-05 08:14:14 -070021import org.onlab.onos.net.Port;
tome33cc1a2014-08-25 21:59:41 -070022
Pavlin Radoslavov156c2ff2014-10-22 22:00:15 -070023import static com.google.common.base.MoreObjects.toStringHelper;
24
tome33cc1a2014-08-25 21:59:41 -070025/**
26 * Describes infrastructure device event.
27 */
28public class DeviceEvent extends AbstractEvent<DeviceEvent.Type, Device> {
29
tom29df6f42014-09-05 08:14:14 -070030 private final Port port;
31
tome33cc1a2014-08-25 21:59:41 -070032 /**
33 * Type of device events.
34 */
35 public enum Type {
tom64b7aac2014-08-26 00:18:21 -070036 /**
37 * Signifies that a new device has been detected.
38 */
tome33cc1a2014-08-25 21:59:41 -070039 DEVICE_ADDED,
40
tom64b7aac2014-08-26 00:18:21 -070041 /**
tome5ec3fd2014-09-04 15:18:06 -070042 * Signifies that some device attributes have changed; excludes
43 * availability changes.
44 */
45 DEVICE_UPDATED,
46
47 /**
tom64b7aac2014-08-26 00:18:21 -070048 * Signifies that a device has been removed.
49 */
tome33cc1a2014-08-25 21:59:41 -070050 DEVICE_REMOVED,
51
tom64b7aac2014-08-26 00:18:21 -070052 /**
53 * Signifies that a device has been administratively suspended.
54 */
tome33cc1a2014-08-25 21:59:41 -070055 DEVICE_SUSPENDED,
56
tom64b7aac2014-08-26 00:18:21 -070057 /**
58 * Signifies that a device has come online or has gone offline.
59 */
tome33cc1a2014-08-25 21:59:41 -070060 DEVICE_AVAILABILITY_CHANGED,
61
62 /**
63 * Signifies that the current controller instance relationship has
64 * changed with respect to a device.
65 */
tom29df6f42014-09-05 08:14:14 -070066 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
tome33cc1a2014-08-25 21:59:41 -070082 }
83
84 /**
tom64b7aac2014-08-26 00:18:21 -070085 * Creates an event of a given type and for the specified device and the
tome33cc1a2014-08-25 21:59:41 -070086 * current time.
87 *
tom64b7aac2014-08-26 00:18:21 -070088 * @param type device event type
89 * @param device event device subject
tome33cc1a2014-08-25 21:59:41 -070090 */
tom64b7aac2014-08-26 00:18:21 -070091 public DeviceEvent(Type type, Device device) {
tom29df6f42014-09-05 08:14:14 -070092 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) {
tom64b7aac2014-08-26 00:18:21 -0700104 super(type, device);
tom29df6f42014-09-05 08:14:14 -0700105 this.port = port;
tome33cc1a2014-08-25 21:59:41 -0700106 }
107
108 /**
tom64b7aac2014-08-26 00:18:21 -0700109 * Creates an event of a given type and for the specified device and time.
tome33cc1a2014-08-25 21:59:41 -0700110 *
tom64b7aac2014-08-26 00:18:21 -0700111 * @param type device event type
112 * @param device event device subject
tom29df6f42014-09-05 08:14:14 -0700113 * @param port optional port subject
tom64b7aac2014-08-26 00:18:21 -0700114 * @param time occurrence time
tome33cc1a2014-08-25 21:59:41 -0700115 */
tom29df6f42014-09-05 08:14:14 -0700116 public DeviceEvent(Type type, Device device, Port port, long time) {
tom64b7aac2014-08-26 00:18:21 -0700117 super(type, device, time);
tom29df6f42014-09-05 08:14:14 -0700118 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 */
tom0efbb1d2014-09-09 11:54:28 -0700126 public Port port() {
tom29df6f42014-09-05 08:14:14 -0700127 return port;
tome33cc1a2014-08-25 21:59:41 -0700128 }
129
Pavlin Radoslavov156c2ff2014-10-22 22:00:15 -0700130 @Override
131 public String toString() {
132 if (port == null) {
133 return super.toString();
134 }
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -0700135 return toStringHelper(this)
136 .add("time", new LocalDateTime(time()))
137 .add("type", type())
138 .add("subject", subject())
139 .add("port", port)
140 .toString();
Pavlin Radoslavov156c2ff2014-10-22 22:00:15 -0700141 }
tome33cc1a2014-08-25 21:59:41 -0700142}