blob: fa7a76195e842edae1f26ac31bf9855f4290683a [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
31 public DeviceEvent(MACAddress mac) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080032 if (mac == null) {
33 throw new IllegalArgumentException("Device mac cannot be null");
34 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080035 this.mac = mac;
36 this.attachmentPoints = new LinkedList<>();
37 this.ipAddresses = new HashSet<>();
38 }
39
40 public MACAddress getMac() {
41 return mac;
42 }
43
44 public List<SwitchPort> getAttachmentPoints() {
45 return attachmentPoints;
46 }
47
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080048 public Set<InetAddress> getIpAddresses() {
49 return ipAddresses;
50 }
51
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080052 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
53 this.attachmentPoints = attachmentPoints;
54 }
55
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080056 public void addAttachmentPoint(SwitchPort attachmentPoint) {
57 // may need to maintain uniqness
58 this.attachmentPoints.add(0, attachmentPoint);
59 }
60
61
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080062 boolean addIpAddress(InetAddress addr) {
63 return this.ipAddresses.add(addr);
64 }
65
66 boolean removeIpAddress(InetAddress addr) {
67 return this.ipAddresses.remove(addr);
68 }
69
70 @Override
71 public String toString() {
72 return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + " ipAddr:" + ipAddresses + "]";
73 }
74
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080075 // Assuming mac is unique cluster-wide
76 public static byte[] getDeviceID(final byte[] mac) {
77 return ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac)
78 .array();
79 }
80
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080081
82}