blob: eccc14d6c64a8512c9d8729d275e90534ed81d16 [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
18import org.onlab.onos.event.AbstractEvent;
19import org.onlab.onos.net.Device;
tom29df6f42014-09-05 08:14:14 -070020import org.onlab.onos.net.Port;
tome33cc1a2014-08-25 21:59:41 -070021
Pavlin Radoslavov156c2ff2014-10-22 22:00:15 -070022import static com.google.common.base.MoreObjects.toStringHelper;
23
tome33cc1a2014-08-25 21:59:41 -070024/**
25 * Describes infrastructure device event.
26 */
27public class DeviceEvent extends AbstractEvent<DeviceEvent.Type, Device> {
28
tom29df6f42014-09-05 08:14:14 -070029 private final Port port;
30
tome33cc1a2014-08-25 21:59:41 -070031 /**
32 * Type of device events.
33 */
34 public enum Type {
tom64b7aac2014-08-26 00:18:21 -070035 /**
36 * Signifies that a new device has been detected.
37 */
tome33cc1a2014-08-25 21:59:41 -070038 DEVICE_ADDED,
39
tom64b7aac2014-08-26 00:18:21 -070040 /**
tome5ec3fd2014-09-04 15:18:06 -070041 * Signifies that some device attributes have changed; excludes
42 * availability changes.
43 */
44 DEVICE_UPDATED,
45
46 /**
tom64b7aac2014-08-26 00:18:21 -070047 * Signifies that a device has been removed.
48 */
tome33cc1a2014-08-25 21:59:41 -070049 DEVICE_REMOVED,
50
tom64b7aac2014-08-26 00:18:21 -070051 /**
52 * Signifies that a device has been administratively suspended.
53 */
tome33cc1a2014-08-25 21:59:41 -070054 DEVICE_SUSPENDED,
55
tom64b7aac2014-08-26 00:18:21 -070056 /**
57 * Signifies that a device has come online or has gone offline.
58 */
tome33cc1a2014-08-25 21:59:41 -070059 DEVICE_AVAILABILITY_CHANGED,
60
61 /**
62 * Signifies that the current controller instance relationship has
63 * changed with respect to a device.
64 */
tom29df6f42014-09-05 08:14:14 -070065 DEVICE_MASTERSHIP_CHANGED,
66
67 /**
68 * Signifies that a port has been added.
69 */
70 PORT_ADDED,
71
72 /**
73 * Signifies that a port has been updated.
74 */
75 PORT_UPDATED,
76
77 /**
78 * Signifies that a port has been removed.
79 */
80 PORT_REMOVED
tome33cc1a2014-08-25 21:59:41 -070081 }
82
83 /**
tom64b7aac2014-08-26 00:18:21 -070084 * Creates an event of a given type and for the specified device and the
tome33cc1a2014-08-25 21:59:41 -070085 * current time.
86 *
tom64b7aac2014-08-26 00:18:21 -070087 * @param type device event type
88 * @param device event device subject
tome33cc1a2014-08-25 21:59:41 -070089 */
tom64b7aac2014-08-26 00:18:21 -070090 public DeviceEvent(Type type, Device device) {
tom29df6f42014-09-05 08:14:14 -070091 this(type, device, null);
92 }
93
94 /**
95 * Creates an event of a given type and for the specified device, port
96 * and the current time.
97 *
98 * @param type device event type
99 * @param device event device subject
100 * @param port optional port subject
101 */
102 public DeviceEvent(Type type, Device device, Port port) {
tom64b7aac2014-08-26 00:18:21 -0700103 super(type, device);
tom29df6f42014-09-05 08:14:14 -0700104 this.port = port;
tome33cc1a2014-08-25 21:59:41 -0700105 }
106
107 /**
tom64b7aac2014-08-26 00:18:21 -0700108 * Creates an event of a given type and for the specified device and time.
tome33cc1a2014-08-25 21:59:41 -0700109 *
tom64b7aac2014-08-26 00:18:21 -0700110 * @param type device event type
111 * @param device event device subject
tom29df6f42014-09-05 08:14:14 -0700112 * @param port optional port subject
tom64b7aac2014-08-26 00:18:21 -0700113 * @param time occurrence time
tome33cc1a2014-08-25 21:59:41 -0700114 */
tom29df6f42014-09-05 08:14:14 -0700115 public DeviceEvent(Type type, Device device, Port port, long time) {
tom64b7aac2014-08-26 00:18:21 -0700116 super(type, device, time);
tom29df6f42014-09-05 08:14:14 -0700117 this.port = port;
118 }
119
120 /**
121 * Returns the port subject.
122 *
123 * @return port subject or null if the event is not port specific.
124 */
tom0efbb1d2014-09-09 11:54:28 -0700125 public Port port() {
tom29df6f42014-09-05 08:14:14 -0700126 return port;
tome33cc1a2014-08-25 21:59:41 -0700127 }
128
Pavlin Radoslavov156c2ff2014-10-22 22:00:15 -0700129 @Override
130 public String toString() {
131 if (port == null) {
132 return super.toString();
133 }
134 return toStringHelper(this).add("time", time()).add("type", type())
135 .add("subject", subject()).add("port", port).toString();
136 }
tome33cc1a2014-08-25 21:59:41 -0700137}