blob: 02041ebb09d3a13d4e46b555d073d4e1eaa23e52 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08003import java.nio.ByteBuffer;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08004import java.util.LinkedList;
5import java.util.List;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08006
7import net.floodlightcontroller.util.MACAddress;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07008import net.onrc.onos.core.topology.web.serializers.DeviceEventSerializer;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07009import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080010
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070011import org.codehaus.jackson.map.annotate.JsonSerialize;
12
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080013/**
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080014 * Self-contained Device event(s) Object
Ray Milkey269ffb92014-04-03 14:43:30 -070015 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080016 * Device event differ from other events.
TeruU28adcc32014-04-15 17:57:35 -070017 * Device Event represent add/remove of attachmentPoint.
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080018 * Not add/remove of the DeviceObject itself.
Ray Milkey269ffb92014-04-03 14:43:30 -070019 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080020 * Multiple attachmentPoints can be specified to batch events into 1 object.
21 * Each should be treated as independent events.
Ray Milkey269ffb92014-04-03 14:43:30 -070022 * <p/>
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080023 * TODO: We probably want common base class/interface for Self-Contained Event Object
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080024 */
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070025@JsonSerialize(using = DeviceEventSerializer.class)
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080026public class DeviceEvent {
27 private final MACAddress mac;
28 protected List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070029 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080030
31 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080032 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080033 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080034 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080035 public DeviceEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 mac = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080037 }
38
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080039 public DeviceEvent(MACAddress mac) {
Ray Milkey269ffb92014-04-03 14:43:30 -070040 if (mac == null) {
41 throw new IllegalArgumentException("Device mac cannot be null");
42 }
43 this.mac = mac;
44 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080045 }
46
47 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070048 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080049 }
50
51 public List<SwitchPort> getAttachmentPoints() {
Ray Milkey269ffb92014-04-03 14:43:30 -070052 return attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080053 }
54
55 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080057 }
58
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080059 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Ray Milkey269ffb92014-04-03 14:43:30 -070060 // may need to maintain uniqness
61 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080062 }
63
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080064 @Override
65 public String toString() {
TeruU28adcc32014-04-15 17:57:35 -070066 return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080067 }
68
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080069 // Assuming mac is unique cluster-wide
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080070 public static ByteBuffer getDeviceID(final byte[] mac) {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080072 }
73
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080074 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 return getDeviceID(mac.toBytes()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080076 }
77
78 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -070079 return getDeviceID(mac.toBytes());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080080 }
TeruUd1c5b652014-03-24 13:58:46 -070081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public long getLastSeenTime() {
83 return lastSeenTime;
84 }
TeruUd1c5b652014-03-24 13:58:46 -070085
Ray Milkey269ffb92014-04-03 14:43:30 -070086 public void setLastSeenTime(long lastSeenTime) {
87 this.lastSeenTime = lastSeenTime;
88 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080089}