blob: e9ba12e42b3958317cbeee745f6559a3adaec363 [file] [log] [blame]
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
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;
11import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort;
12
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;
30
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031
32 /**
33 * Default constructor.
34 */
35 public DeviceEvent() {
36 mac = null;
37 }
38
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080039 public DeviceEvent(MACAddress mac) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080040 if (mac == null) {
41 throw new IllegalArgumentException("Device mac cannot be null");
42 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080043 this.mac = mac;
44 this.attachmentPoints = new LinkedList<>();
45 this.ipAddresses = new HashSet<>();
46 }
47
48 public MACAddress getMac() {
49 return mac;
50 }
51
52 public List<SwitchPort> getAttachmentPoints() {
53 return attachmentPoints;
54 }
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) {
61 this.attachmentPoints = attachmentPoints;
62 }
63
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080064 public void addAttachmentPoint(SwitchPort attachmentPoint) {
65 // may need to maintain uniqness
66 this.attachmentPoints.add(0, attachmentPoint);
67 }
68
69
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080070 boolean addIpAddress(InetAddress addr) {
71 return this.ipAddresses.add(addr);
72 }
73
74 boolean removeIpAddress(InetAddress addr) {
75 return this.ipAddresses.remove(addr);
76 }
77
78 @Override
79 public String toString() {
80 return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + " ipAddr:" + ipAddresses + "]";
81 }
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) {
85 return ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac);
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080086 }
87
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080088 public byte[] getID() {
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080089 return getDeviceID(mac.toBytes()).array();
90 }
91
92 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080093 return getDeviceID(mac.toBytes());
94 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080095}