blob: 48d50e41158a6a75b1fbaaf63eec54d5ebd7dbc1 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
3import java.net.InetAddress;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08004import java.nio.ByteBuffer;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08005import java.util.HashSet;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Set;
9
10import net.floodlightcontroller.util.MACAddress;
Jonathan Hart472062d2014-04-03 10:56:48 -070011import net.onrc.onos.core.topology.PortEvent.SwitchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080012
13/**
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.
17 * Device Event represent add/remove of attachmentPoint or ipAddress.
18 * 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 */
25public class DeviceEvent {
26 private final MACAddress mac;
27 protected List<SwitchPort> attachmentPoints;
28 protected Set<InetAddress> ipAddresses;
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<>();
45 this.ipAddresses = new HashSet<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080046 }
47
48 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070049 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080050 }
51
52 public List<SwitchPort> getAttachmentPoints() {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 return attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080054 }
55
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080056 public Set<InetAddress> getIpAddresses() {
57 return ipAddresses;
58 }
59
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080060 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Ray Milkey269ffb92014-04-03 14:43:30 -070061 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080062 }
63
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080064 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Ray Milkey269ffb92014-04-03 14:43:30 -070065 // may need to maintain uniqness
66 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080067 }
68
69
TeruUd1c5b652014-03-24 13:58:46 -070070 public boolean addIpAddress(InetAddress addr) {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return this.ipAddresses.add(addr);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080072 }
73
TeruUd1c5b652014-03-24 13:58:46 -070074 public boolean removeIpAddress(InetAddress addr) {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 return this.ipAddresses.remove(addr);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080076 }
77
78 @Override
79 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070080 return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + " ipAddr:" + ipAddresses + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080081 }
82
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080083 // Assuming mac is unique cluster-wide
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080084 public static ByteBuffer getDeviceID(final byte[] mac) {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080086 }
87
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080088 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -070089 return getDeviceID(mac.toBytes()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080090 }
91
92 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -070093 return getDeviceID(mac.toBytes());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080094 }
TeruUd1c5b652014-03-24 13:58:46 -070095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 public long getLastSeenTime() {
97 return lastSeenTime;
98 }
TeruUd1c5b652014-03-24 13:58:46 -070099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 public void setLastSeenTime(long lastSeenTime) {
101 this.lastSeenTime = lastSeenTime;
102 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800103}