blob: f7331a0ee4e8100a2b6639e73ae637d2c431456a [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
15 *
16 * Device event differ from other events.
17 * Device Event represent add/remove of attachmentPoint or ipAddress.
18 * Not add/remove of the DeviceObject itself.
19 *
20 * Multiple attachmentPoints can be specified to batch events into 1 object.
21 * Each should be treated as independent events.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080022 *
23 * TODO: We probably want common base class/interface for Self-Contained Event Object
24 *
25 */
26public class DeviceEvent {
27 private final MACAddress mac;
28 protected List<SwitchPort> attachmentPoints;
29 protected Set<InetAddress> ipAddresses;
TeruUd1c5b652014-03-24 13:58:46 -070030 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031
32 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080033 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080034 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080035 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080036 public DeviceEvent() {
37 mac = null;
38 }
39
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080040 public DeviceEvent(MACAddress mac) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080041 if (mac == null) {
42 throw new IllegalArgumentException("Device mac cannot be null");
43 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080044 this.mac = mac;
45 this.attachmentPoints = new LinkedList<>();
46 this.ipAddresses = new HashSet<>();
47 }
48
49 public MACAddress getMac() {
50 return mac;
51 }
52
53 public List<SwitchPort> getAttachmentPoints() {
54 return attachmentPoints;
55 }
56
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080057 public Set<InetAddress> getIpAddresses() {
58 return ipAddresses;
59 }
60
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080061 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
62 this.attachmentPoints = attachmentPoints;
63 }
64
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080065 public void addAttachmentPoint(SwitchPort attachmentPoint) {
66 // may need to maintain uniqness
67 this.attachmentPoints.add(0, attachmentPoint);
68 }
69
70
TeruUd1c5b652014-03-24 13:58:46 -070071 public boolean addIpAddress(InetAddress addr) {
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080072 return this.ipAddresses.add(addr);
73 }
74
TeruUd1c5b652014-03-24 13:58:46 -070075 public boolean removeIpAddress(InetAddress addr) {
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080076 return this.ipAddresses.remove(addr);
77 }
78
79 @Override
80 public String toString() {
81 return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + " ipAddr:" + ipAddresses + "]";
82 }
83
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080084 // Assuming mac is unique cluster-wide
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080085 public static ByteBuffer getDeviceID(final byte[] mac) {
Yuta HIGUCHI9e873bd2014-02-24 13:29:13 -080086 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080087 }
88
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080089 public byte[] getID() {
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080090 return getDeviceID(mac.toBytes()).array();
91 }
92
93 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080094 return getDeviceID(mac.toBytes());
95 }
TeruUd1c5b652014-03-24 13:58:46 -070096
97 public long getLastSeenTime() {
98 return lastSeenTime;
99 }
100
101 public void setLastSeenTime(long lastSeenTime) {
102 this.lastSeenTime = lastSeenTime;
103 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800104}