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