blob: 33f3803c79c8c3e961e7db210c3180e988af8b00 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Sangho Shin2f263692014-09-15 14:09:41 -07003import static com.google.common.base.Preconditions.checkNotNull;
4
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08005import java.nio.ByteBuffer;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07006import java.util.ArrayList;
7import java.util.Collections;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08008import java.util.LinkedList;
9import java.util.List;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070010import java.util.Objects;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080011
12import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070013import net.onrc.onos.core.topology.web.serializers.HostDataSerializer;
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070014import net.onrc.onos.core.util.Dpid;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070015import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080016
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070017import org.codehaus.jackson.map.annotate.JsonSerialize;
18
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080019/**
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070020 * Self-contained Host object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080021 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070022@JsonSerialize(using = HostDataSerializer.class)
23public class HostData extends TopologyElement<HostData> {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070024
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080025 private final MACAddress mac;
Sangho Shin2f263692014-09-15 14:09:41 -070026 private final int ip;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 private List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070028 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080029
30 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080031 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080032 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080033 @Deprecated
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070034 protected HostData() {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 mac = null;
Sangho Shin2f263692014-09-15 14:09:41 -070036 ip = 0;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080037 }
38
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070039 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070040 * Constructor for a given host MAC address.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070041 *
42 * @param mac the MAC address to identify the host
43 */
Sangho Shin2ed83dd2014-09-15 14:37:40 -070044 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 Shin2f263692014-09-15 14:09:41 -070055 public HostData(MACAddress mac, int ip) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070056 this.mac = checkNotNull(mac);
Sangho Shin2f263692014-09-15 14:09:41 -070057 this.ip = ip;
Ray Milkey269ffb92014-04-03 14:43:30 -070058 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080059 }
60
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070062 * Copy constructor.
63 * <p>
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070064 * Creates an unfrozen copy of the given HostData object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070065 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070066 * @param original the object to make copy of
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070067 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070068 public HostData(HostData original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070069 super(original);
70 this.mac = original.mac;
Sangho Shin2f263692014-09-15 14:09:41 -070071 this.ip = original.ip;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070072 this.attachmentPoints = new ArrayList<>(original.attachmentPoints);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070073 this.lastSeenTime = original.lastSeenTime;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070074 }
75
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070076 /**
77 * Gets the host MAC address.
78 *
79 * @return the MAC address.
80 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080081 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070082 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080083 }
84
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070085 /**
Sangho Shin2f263692014-09-15 14:09:41 -070086 * Gets the host IP address.
87 *
88 * @return the IP address.
89 */
90 public int getIp() {
91 return ip;
92 }
93
94 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070095 * Gets the switch attachment points.
96 *
97 * @return the switch attachment points
98 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080099 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700100 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800101 }
102
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700103 /**
104 * Sets the switch attachment points.
105 *
106 * @param attachmentPoints the switch attachment points to set
107 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800108 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700109 if (isFrozen()) {
110 throw new IllegalStateException("Tried to modify frozen instance: " + this);
111 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800113 }
114
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700115 /**
116 * Adds a switch attachment point.
117 *
118 * @param attachmentPoint the switch attachment point to add
119 */
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800120 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700121 if (isFrozen()) {
122 throw new IllegalStateException("Tried to modify frozen instance: " + this);
123 }
124 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800126 }
127
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700128 /**
129 * Removes a switch attachment point.
130 *
131 * @param attachmentPoint the switch attachment point to remove
132 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700133 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 Radoslavov5317ac92014-08-18 12:59:33 -0700140 /**
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 HIGUCHIbf0a8712014-06-30 18:59:46 -0700145 public long getLastSeenTime() {
146 return lastSeenTime;
147 }
148
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700149 /**
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 HIGUCHIbf0a8712014-06-30 18:59:46 -0700155 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 Radoslavov5317ac92014-08-18 12:59:33 -0700162 /**
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 Radoslavov31f85102014-08-15 13:55:44 -0700171 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 Radoslavovd7b792e2014-08-01 02:47:47 -0700177 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 HIGUCHI54ab8cd2014-02-11 09:43:34 -0800185 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700186 public ByteBuffer getIDasByteBuffer() {
187 return getHostID(mac.toBytes());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800188 }
189
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700190 @Override
191 public int hashCode() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700192 return 31 * super.hashCode() + Objects.hash(attachmentPoints, mac);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700193 }
194
195 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700196 public boolean equals(Object o) {
197 if (this == o) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700198 return true;
199 }
200
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700201 if (o == null || getClass() != o.getClass()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700202 return false;
203 }
204
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700205 // Compare attributes
206 if (!super.equals(o)) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700207 return false;
208 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700209
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700210 HostData other = (HostData) o;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700211 // XXX lastSeenTime excluded from Equality condition, is it OK?
212 return Objects.equals(mac, other.mac) &&
Sangho Shin2f263692014-09-15 14:09:41 -0700213 ip == other.ip &&
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700214 Objects.equals(this.attachmentPoints, other.attachmentPoints);
215 }
216
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700217 @Override
218 public String toString() {
Sangho Shin2f263692014-09-15 14:09:41 -0700219 return "[HostData " + mac + "(ip:" + ip + ")" + " attachmentPoints:" + attachmentPoints + "]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800220 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800221}