Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collections; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 8 | import java.util.Objects; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 9 | |
| 10 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.topology.web.serializers.DeviceEventSerializer; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 13 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 14 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 15 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 16 | /** |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 17 | * Self-contained Device event(s) Object |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | * <p/> |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 19 | * Device event differ from other events. |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 20 | * Device Event represent add/remove of attachmentPoint. |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 21 | * Not add/remove of the DeviceObject itself. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | * <p/> |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 23 | * Multiple attachmentPoints can be specified to batch events into 1 object. |
| 24 | * Each should be treated as independent events. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 25 | * <p/> |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 26 | * TODO: Rename to match what it is. (Switch/Port/Link/Device)Snapshot? |
| 27 | * FIXME: Current implementation directly use this object as |
| 28 | * Replication message, but should be sending update operation info. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 29 | */ |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 30 | @JsonSerialize(using = DeviceEventSerializer.class) |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 31 | public class DeviceEvent extends TopologyElement<DeviceEvent> { |
| 32 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 33 | private final MACAddress mac; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 34 | private List<SwitchPort> attachmentPoints; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 35 | private long lastSeenTime; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 38 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 39 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 40 | @Deprecated |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 41 | protected DeviceEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | mac = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 45 | public DeviceEvent(MACAddress mac) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | if (mac == null) { |
| 47 | throw new IllegalArgumentException("Device mac cannot be null"); |
| 48 | } |
| 49 | this.mac = mac; |
| 50 | this.attachmentPoints = new LinkedList<>(); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Create an unfrozen copy of given Object. |
| 55 | * |
| 56 | * @param original to make copy of. |
| 57 | */ |
| 58 | public DeviceEvent(DeviceEvent original) { |
| 59 | super(original); |
| 60 | this.mac = original.mac; |
| 61 | this.attachmentPoints = new ArrayList<>(original.attachmentPoints); |
| 62 | } |
| 63 | |
| 64 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 65 | public MACAddress getMac() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | return mac; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public List<SwitchPort> getAttachmentPoints() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 70 | return Collections.unmodifiableList(attachmentPoints); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public void setAttachmentPoints(List<SwitchPort> attachmentPoints) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 74 | if (isFrozen()) { |
| 75 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 76 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | this.attachmentPoints = attachmentPoints; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 80 | public void addAttachmentPoint(SwitchPort attachmentPoint) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 81 | if (isFrozen()) { |
| 82 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 83 | } |
| 84 | // may need to maintain uniqueness |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | this.attachmentPoints.add(0, attachmentPoint); |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 88 | public boolean removeAttachmentPoint(SwitchPort attachmentPoint) { |
| 89 | if (isFrozen()) { |
| 90 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 91 | } |
| 92 | return this.attachmentPoints.remove(attachmentPoint); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | public long getLastSeenTime() { |
| 97 | return lastSeenTime; |
| 98 | } |
| 99 | |
| 100 | public void setLastSeenTime(long lastSeenTime) { |
| 101 | if (isFrozen()) { |
| 102 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 103 | } |
| 104 | this.lastSeenTime = lastSeenTime; |
| 105 | } |
| 106 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 107 | @Override |
| 108 | public String toString() { |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 109 | return "[DeviceEvent " + mac + " attachmentPoints:" + attachmentPoints + "]"; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public int hashCode() { |
| 114 | final int prime = 31; |
| 115 | int result = super.hashCode(); |
| 116 | result = prime * result |
| 117 | + ((attachmentPoints == null) ? 0 : attachmentPoints.hashCode()); |
| 118 | result = prime * result + ((mac == null) ? 0 : mac.hashCode()); |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean equals(Object obj) { |
| 124 | if (this == obj) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | if (!super.equals(obj)) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | if (getClass() != obj.getClass()) { |
| 133 | return false; |
| 134 | } |
| 135 | DeviceEvent other = (DeviceEvent) obj; |
| 136 | |
| 137 | if (!super.equals(obj)) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | // XXX lastSeenTime excluded from Equality condition, is it OK? |
| 142 | return Objects.equals(mac, other.mac) && |
| 143 | Objects.equals(this.attachmentPoints, other.attachmentPoints); |
| 144 | } |
| 145 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 146 | // Assuming mac is unique cluster-wide |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 147 | public static ByteBuffer getDeviceID(final byte[] mac) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 148 | return (ByteBuffer) ByteBuffer.allocate(2 + mac.length) |
| 149 | .putChar('D').put(mac).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 152 | public byte[] getID() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 153 | return getDeviceID(mac.toBytes()).array(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | public ByteBuffer getIDasByteBuffer() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | return getDeviceID(mac.toBytes()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 158 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 159 | } |