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