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 | /** |
| 33 | * Default constructor. |
| 34 | */ |
| 35 | public DeviceEvent() { |
| 36 | mac = null; |
| 37 | } |
| 38 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 39 | public DeviceEvent(MACAddress mac) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 40 | if (mac == null) { |
| 41 | throw new IllegalArgumentException("Device mac cannot be null"); |
| 42 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 43 | 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 HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 56 | public Set<InetAddress> getIpAddresses() { |
| 57 | return ipAddresses; |
| 58 | } |
| 59 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 60 | public void setAttachmentPoints(List<SwitchPort> attachmentPoints) { |
| 61 | this.attachmentPoints = attachmentPoints; |
| 62 | } |
| 63 | |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 64 | public void addAttachmentPoint(SwitchPort attachmentPoint) { |
| 65 | // may need to maintain uniqness |
| 66 | this.attachmentPoints.add(0, attachmentPoint); |
| 67 | } |
| 68 | |
| 69 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 70 | 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 HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 83 | // Assuming mac is unique cluster-wide |
| 84 | public static byte[] getDeviceID(final byte[] mac) { |
| 85 | return ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac) |
| 86 | .array(); |
| 87 | } |
| 88 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 89 | public byte[] getID() { |
| 90 | return getDeviceID(mac.toBytes()); |
| 91 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 92 | } |