blob: e790134a88e74946014da95f7d8047e45dca94b7 [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 Shin2f263692014-09-15 14:09:41 -070044 public HostData(MACAddress mac, int ip) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070045 this.mac = checkNotNull(mac);
Sangho Shin2f263692014-09-15 14:09:41 -070046 this.ip = ip;
Ray Milkey269ffb92014-04-03 14:43:30 -070047 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080048 }
49
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070050 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070051 * Copy constructor.
52 * <p>
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070053 * Creates an unfrozen copy of the given HostData object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070054 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070055 * @param original the object to make copy of
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070056 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070057 public HostData(HostData original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070058 super(original);
59 this.mac = original.mac;
Sangho Shin2f263692014-09-15 14:09:41 -070060 this.ip = original.ip;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 this.attachmentPoints = new ArrayList<>(original.attachmentPoints);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070062 this.lastSeenTime = original.lastSeenTime;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 }
64
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070065 /**
66 * Gets the host MAC address.
67 *
68 * @return the MAC address.
69 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080070 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080072 }
73
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070074 /**
Sangho Shin2f263692014-09-15 14:09:41 -070075 * Gets the host IP address.
76 *
77 * @return the IP address.
78 */
79 public int getIp() {
80 return ip;
81 }
82
83 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070084 * Gets the switch attachment points.
85 *
86 * @return the switch attachment points
87 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080088 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070089 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080090 }
91
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070092 /**
93 * Sets the switch attachment points.
94 *
95 * @param attachmentPoints the switch attachment points to set
96 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080097 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070098 if (isFrozen()) {
99 throw new IllegalStateException("Tried to modify frozen instance: " + this);
100 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800102 }
103
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700104 /**
105 * Adds a switch attachment point.
106 *
107 * @param attachmentPoint the switch attachment point to add
108 */
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800109 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700110 if (isFrozen()) {
111 throw new IllegalStateException("Tried to modify frozen instance: " + this);
112 }
113 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800115 }
116
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700117 /**
118 * Removes a switch attachment point.
119 *
120 * @param attachmentPoint the switch attachment point to remove
121 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700122 public boolean removeAttachmentPoint(SwitchPort attachmentPoint) {
123 if (isFrozen()) {
124 throw new IllegalStateException("Tried to modify frozen instance: " + this);
125 }
126 return this.attachmentPoints.remove(attachmentPoint);
127 }
128
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700129 /**
130 * Gets the host last seen time in milliseconds since the epoch.
131 *
132 * @return the host last seen time in milliseconds since the epoch
133 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700134 public long getLastSeenTime() {
135 return lastSeenTime;
136 }
137
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700138 /**
139 * Sets the host last seen time in milliseconds since the epoch.
140 *
141 * @param lastSeenTime the host last seen time in milliseconds since the
142 * epoch
143 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700144 public void setLastSeenTime(long lastSeenTime) {
145 if (isFrozen()) {
146 throw new IllegalStateException("Tried to modify frozen instance: " + this);
147 }
148 this.lastSeenTime = lastSeenTime;
149 }
150
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700151 /**
152 * Computes the host ID for a given host MAC address.
153 * <p>
154 * TODO: This method should be replaced with a method that uses
155 * MACAddress as an argument instead of a byte array.
156 *
157 * @param mac the host MAC address to use
158 * @return the host ID as a ByteBuffer
159 */
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700160 public static ByteBuffer getHostID(final byte[] mac) {
161 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length)
162 .putChar('H').put(mac).flip();
163 }
164
165 @Override
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700166 public Dpid getOriginDpid() {
167 // TODO: Eventually, we should return a collection of Dpid values
168 for (SwitchPort sp : attachmentPoints) {
169 return sp.getDpid();
170 }
171 return null;
172 }
173
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800174 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700175 public ByteBuffer getIDasByteBuffer() {
176 return getHostID(mac.toBytes());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800177 }
178
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700179 @Override
180 public int hashCode() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700181 return 31 * super.hashCode() + Objects.hash(attachmentPoints, mac);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700182 }
183
184 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700185 public boolean equals(Object o) {
186 if (this == o) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700187 return true;
188 }
189
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700190 if (o == null || getClass() != o.getClass()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700191 return false;
192 }
193
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700194 // Compare attributes
195 if (!super.equals(o)) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700196 return false;
197 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700198
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700199 HostData other = (HostData) o;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700200 // XXX lastSeenTime excluded from Equality condition, is it OK?
201 return Objects.equals(mac, other.mac) &&
Sangho Shin2f263692014-09-15 14:09:41 -0700202 ip == other.ip &&
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700203 Objects.equals(this.attachmentPoints, other.attachmentPoints);
204 }
205
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700206 @Override
207 public String toString() {
Sangho Shin2f263692014-09-15 14:09:41 -0700208 return "[HostData " + mac + "(ip:" + ip + ")" + " attachmentPoints:" + attachmentPoints + "]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800209 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800210}