blob: 0030516334d2117e494c45a898b36b6bc5e2af31 [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;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07008import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08009
10/**
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080011 * Self-contained Device event(s) Object
Ray Milkey269ffb92014-04-03 14:43:30 -070012 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080013 * Device event differ from other events.
TeruU28adcc32014-04-15 17:57:35 -070014 * Device Event represent add/remove of attachmentPoint.
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080015 * Not add/remove of the DeviceObject itself.
Ray Milkey269ffb92014-04-03 14:43:30 -070016 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080017 * Multiple attachmentPoints can be specified to batch events into 1 object.
18 * Each should be treated as independent events.
Ray Milkey269ffb92014-04-03 14:43:30 -070019 * <p/>
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080020 * TODO: We probably want common base class/interface for Self-Contained Event Object
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080021 */
22public class DeviceEvent {
23 private final MACAddress mac;
24 protected List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070025 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080026
27 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080028 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080029 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080030 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031 public DeviceEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070032 mac = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080033 }
34
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080035 public DeviceEvent(MACAddress mac) {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 if (mac == null) {
37 throw new IllegalArgumentException("Device mac cannot be null");
38 }
39 this.mac = mac;
40 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080041 }
42
43 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070044 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080045 }
46
47 public List<SwitchPort> getAttachmentPoints() {
Ray Milkey269ffb92014-04-03 14:43:30 -070048 return attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080049 }
50
51 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Ray Milkey269ffb92014-04-03 14:43:30 -070052 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080053 }
54
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080055 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 // may need to maintain uniqness
57 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080058 }
59
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080060 @Override
61 public String toString() {
TeruU28adcc32014-04-15 17:57:35 -070062 return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080063 }
64
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080065 // Assuming mac is unique cluster-wide
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080066 public static ByteBuffer getDeviceID(final byte[] mac) {
Ray Milkey269ffb92014-04-03 14:43:30 -070067 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080068 }
69
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080070 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return getDeviceID(mac.toBytes()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080072 }
73
74 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 return getDeviceID(mac.toBytes());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080076 }
TeruUd1c5b652014-03-24 13:58:46 -070077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 public long getLastSeenTime() {
79 return lastSeenTime;
80 }
TeruUd1c5b652014-03-24 13:58:46 -070081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public void setLastSeenTime(long lastSeenTime) {
83 this.lastSeenTime = lastSeenTime;
84 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080085}