blob: d799113f620fabbedd9073d1cade8903bb992285 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08003import java.nio.ByteBuffer;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07004import java.util.ArrayList;
5import java.util.Collections;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08006import java.util.LinkedList;
7import java.util.List;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07008import java.util.Objects;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08009
10import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070011import net.onrc.onos.core.topology.web.serializers.HostDataSerializer;
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070012import net.onrc.onos.core.util.Dpid;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070013import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070015import static com.google.common.base.Preconditions.checkNotNull;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070016import org.codehaus.jackson.map.annotate.JsonSerialize;
17
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080018/**
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070019 * Self-contained Host object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080020 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070021@JsonSerialize(using = HostDataSerializer.class)
22public class HostData extends TopologyElement<HostData> {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070023
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080024 private final MACAddress mac;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025 private List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070026 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080027
28 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080029 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080030 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080031 @Deprecated
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070032 protected HostData() {
Ray Milkey269ffb92014-04-03 14:43:30 -070033 mac = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080034 }
35
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070036 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070037 * Constructor for a given host MAC address.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070038 *
39 * @param mac the MAC address to identify the host
40 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070041 public HostData(MACAddress mac) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070042 this.mac = checkNotNull(mac);
Ray Milkey269ffb92014-04-03 14:43:30 -070043 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080044 }
45
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070046 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070047 * Copy constructor.
48 * <p>
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070049 * Creates an unfrozen copy of the given HostData object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070050 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070051 * @param original the object to make copy of
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070052 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070053 public HostData(HostData original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070054 super(original);
55 this.mac = original.mac;
56 this.attachmentPoints = new ArrayList<>(original.attachmentPoints);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070057 this.lastSeenTime = original.lastSeenTime;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070058 }
59
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070060 /**
61 * Gets the host MAC address.
62 *
63 * @return the MAC address.
64 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080065 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070066 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080067 }
68
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070069 /**
70 * Gets the switch attachment points.
71 *
72 * @return the switch attachment points
73 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080074 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070075 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080076 }
77
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070078 /**
79 * Sets the switch attachment points.
80 *
81 * @param attachmentPoints the switch attachment points to set
82 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080083 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070084 if (isFrozen()) {
85 throw new IllegalStateException("Tried to modify frozen instance: " + this);
86 }
Ray Milkey269ffb92014-04-03 14:43:30 -070087 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080088 }
89
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070090 /**
91 * Adds a switch attachment point.
92 *
93 * @param attachmentPoint the switch attachment point to add
94 */
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080095 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070096 if (isFrozen()) {
97 throw new IllegalStateException("Tried to modify frozen instance: " + this);
98 }
99 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800101 }
102
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700103 /**
104 * Removes a switch attachment point.
105 *
106 * @param attachmentPoint the switch attachment point to remove
107 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700108 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 Radoslavov5317ac92014-08-18 12:59:33 -0700115 /**
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 HIGUCHIbf0a8712014-06-30 18:59:46 -0700120 public long getLastSeenTime() {
121 return lastSeenTime;
122 }
123
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700124 /**
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 HIGUCHIbf0a8712014-06-30 18:59:46 -0700130 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 Radoslavov5317ac92014-08-18 12:59:33 -0700137 /**
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 Radoslavov31f85102014-08-15 13:55:44 -0700146 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 Radoslavovd7b792e2014-08-01 02:47:47 -0700152 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 HIGUCHI54ab8cd2014-02-11 09:43:34 -0800160 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700161 public ByteBuffer getIDasByteBuffer() {
162 return getHostID(mac.toBytes());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800163 }
164
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700165 @Override
166 public int hashCode() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700167 return 31 * super.hashCode() + Objects.hash(attachmentPoints, mac);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700168 }
169
170 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700171 public boolean equals(Object o) {
172 if (this == o) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700173 return true;
174 }
175
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700176 if (o == null || getClass() != o.getClass()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700177 return false;
178 }
179
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700180 // Compare attributes
181 if (!super.equals(o)) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700182 return false;
183 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700184
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700185 HostData other = (HostData) o;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700186 // 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 Radoslavov31f85102014-08-15 13:55:44 -0700191 @Override
192 public String toString() {
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700193 return "[HostData " + mac + " attachmentPoints:" + attachmentPoints + "]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800194 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800195}