Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 2 | |
| 3 | import java.net.InetAddress; |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 4 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 5 | import java.util.HashSet; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | import java.util.Set; |
| 9 | |
| 10 | import net.floodlightcontroller.util.MACAddress; |
| 11 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort; |
| 12 | |
| 13 | /** |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 14 | * 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 HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 22 | * |
| 23 | * TODO: We probably want common base class/interface for Self-Contained Event Object |
| 24 | * |
| 25 | */ |
| 26 | public class DeviceEvent { |
| 27 | private final MACAddress mac; |
| 28 | protected List<SwitchPort> attachmentPoints; |
| 29 | protected Set<InetAddress> ipAddresses; |
| 30 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 33 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 34 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 35 | @Deprecated |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 36 | public DeviceEvent() { |
| 37 | mac = null; |
| 38 | } |
| 39 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 40 | public DeviceEvent(MACAddress mac) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 41 | if (mac == null) { |
| 42 | throw new IllegalArgumentException("Device mac cannot be null"); |
| 43 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 44 | 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 HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 57 | public Set<InetAddress> getIpAddresses() { |
| 58 | return ipAddresses; |
| 59 | } |
| 60 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 61 | public void setAttachmentPoints(List<SwitchPort> attachmentPoints) { |
| 62 | this.attachmentPoints = attachmentPoints; |
| 63 | } |
| 64 | |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 65 | public void addAttachmentPoint(SwitchPort attachmentPoint) { |
| 66 | // may need to maintain uniqness |
| 67 | this.attachmentPoints.add(0, attachmentPoint); |
| 68 | } |
| 69 | |
| 70 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 71 | boolean addIpAddress(InetAddress addr) { |
| 72 | return this.ipAddresses.add(addr); |
| 73 | } |
| 74 | |
| 75 | boolean removeIpAddress(InetAddress addr) { |
| 76 | return this.ipAddresses.remove(addr); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
| 81 | return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + " ipAddr:" + ipAddresses + "]"; |
| 82 | } |
| 83 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 84 | // Assuming mac is unique cluster-wide |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 85 | public static ByteBuffer getDeviceID(final byte[] mac) { |
Yuta HIGUCHI | 9e873bd | 2014-02-24 13:29:13 -0800 | [diff] [blame] | 86 | return (ByteBuffer) ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 89 | public byte[] getID() { |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 90 | return getDeviceID(mac.toBytes()).array(); |
| 91 | } |
| 92 | |
| 93 | public ByteBuffer getIDasByteBuffer() { |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 94 | return getDeviceID(mac.toBytes()); |
| 95 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 96 | } |