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; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.topology.web.serializers.HostEventSerializer; |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 14 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 15 | import static com.google.common.base.Preconditions.checkNotNull; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 16 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 17 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 18 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 19 | * Self-contained Host event(s) Object |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 20 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 21 | * Host event differ from other events. |
| 22 | * Host Event represent add/remove of attachmentPoint. |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 23 | * Not add/remove of the Host Object itself. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | * <p/> |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 25 | * Multiple attachmentPoints can be specified to batch events into 1 object. |
| 26 | * Each should be treated as independent events. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 28 | * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot? |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 29 | * FIXME: Current implementation directly use this object as |
| 30 | * Replication message, but should be sending update operation info. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 31 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 32 | @JsonSerialize(using = HostEventSerializer.class) |
| 33 | public class HostEvent extends TopologyElement<HostEvent> { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 34 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 35 | private final MACAddress mac; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 36 | private List<SwitchPort> attachmentPoints; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 37 | private long lastSeenTime; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 40 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 41 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 42 | @Deprecated |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 43 | protected HostEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | mac = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 47 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 48 | * Constructor for a given host MAC address. |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 49 | * |
| 50 | * @param mac the MAC address to identify the host |
| 51 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 52 | public HostEvent(MACAddress mac) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 53 | this.mac = checkNotNull(mac); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | this.attachmentPoints = new LinkedList<>(); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 57 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 58 | * Copy constructor. |
| 59 | * <p> |
| 60 | * Creates an unfrozen copy of the given HostEvent object. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 61 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 62 | * @param original the object to make copy of |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 63 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 64 | public HostEvent(HostEvent original) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 65 | super(original); |
| 66 | this.mac = original.mac; |
| 67 | this.attachmentPoints = new ArrayList<>(original.attachmentPoints); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 68 | this.lastSeenTime = original.lastSeenTime; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Gets the host MAC address. |
| 73 | * |
| 74 | * @return the MAC address. |
| 75 | */ |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 76 | public MACAddress getMac() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | return mac; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Gets the switch attachment points. |
| 82 | * |
| 83 | * @return the switch attachment points |
| 84 | */ |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 85 | public List<SwitchPort> getAttachmentPoints() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 86 | return Collections.unmodifiableList(attachmentPoints); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Sets the switch attachment points. |
| 91 | * |
| 92 | * @param attachmentPoints the switch attachment points to set |
| 93 | */ |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 94 | public void setAttachmentPoints(List<SwitchPort> attachmentPoints) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 95 | if (isFrozen()) { |
| 96 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 97 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | this.attachmentPoints = attachmentPoints; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Adds a switch attachment point. |
| 103 | * |
| 104 | * @param attachmentPoint the switch attachment point to add |
| 105 | */ |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 106 | public void addAttachmentPoint(SwitchPort attachmentPoint) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 107 | if (isFrozen()) { |
| 108 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 109 | } |
| 110 | // may need to maintain uniqueness |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | this.attachmentPoints.add(0, attachmentPoint); |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Removes a switch attachment point. |
| 116 | * |
| 117 | * @param attachmentPoint the switch attachment point to remove |
| 118 | */ |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 119 | public boolean removeAttachmentPoint(SwitchPort attachmentPoint) { |
| 120 | if (isFrozen()) { |
| 121 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 122 | } |
| 123 | return this.attachmentPoints.remove(attachmentPoint); |
| 124 | } |
| 125 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Gets the host last seen time in milliseconds since the epoch. |
| 128 | * |
| 129 | * @return the host last seen time in milliseconds since the epoch |
| 130 | */ |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 131 | public long getLastSeenTime() { |
| 132 | return lastSeenTime; |
| 133 | } |
| 134 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Sets the host last seen time in milliseconds since the epoch. |
| 137 | * |
| 138 | * @param lastSeenTime the host last seen time in milliseconds since the |
| 139 | * epoch |
| 140 | */ |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 141 | public void setLastSeenTime(long lastSeenTime) { |
| 142 | if (isFrozen()) { |
| 143 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 144 | } |
| 145 | this.lastSeenTime = lastSeenTime; |
| 146 | } |
| 147 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Computes the host ID for a given host MAC address. |
| 150 | * <p> |
| 151 | * TODO: This method should be replaced with a method that uses |
| 152 | * MACAddress as an argument instead of a byte array. |
| 153 | * |
| 154 | * @param mac the host MAC address to use |
| 155 | * @return the host ID as a ByteBuffer |
| 156 | */ |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 157 | public static ByteBuffer getHostID(final byte[] mac) { |
| 158 | return (ByteBuffer) ByteBuffer.allocate(2 + mac.length) |
| 159 | .putChar('H').put(mac).flip(); |
| 160 | } |
| 161 | |
| 162 | @Override |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 163 | public Dpid getOriginDpid() { |
| 164 | // TODO: Eventually, we should return a collection of Dpid values |
| 165 | for (SwitchPort sp : attachmentPoints) { |
| 166 | return sp.getDpid(); |
| 167 | } |
| 168 | return null; |
| 169 | } |
| 170 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 171 | @Override |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 172 | public ByteBuffer getIDasByteBuffer() { |
| 173 | return getHostID(mac.toBytes()); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public int hashCode() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 178 | return 31 * super.hashCode() + Objects.hash(attachmentPoints, mac); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 182 | public boolean equals(Object o) { |
| 183 | if (this == o) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 187 | if (o == null || getClass() != o.getClass()) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 191 | // Compare attributes |
| 192 | if (!super.equals(o)) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 193 | return false; |
| 194 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 195 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 196 | HostEvent other = (HostEvent) o; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 197 | // XXX lastSeenTime excluded from Equality condition, is it OK? |
| 198 | return Objects.equals(mac, other.mac) && |
| 199 | Objects.equals(this.attachmentPoints, other.attachmentPoints); |
| 200 | } |
| 201 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 202 | @Override |
| 203 | public String toString() { |
| 204 | return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]"; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 205 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 206 | } |