blob: 4b7de5254b0b7d6ec901758d591519c3d91d7861 [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 HIGUCHIbfc77f02014-07-14 22:50:25 -070011import net.onrc.onos.core.topology.web.serializers.HostEventSerializer;
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 HIGUCHIbfc77f02014-07-14 22:50:25 -070019 * Self-contained Host event(s) Object
Ray Milkey269ffb92014-04-03 14:43:30 -070020 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070021 * Host event differ from other events.
22 * Host Event represent add/remove of attachmentPoint.
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070023 * Not add/remove of the Host Object itself.
Ray Milkey269ffb92014-04-03 14:43:30 -070024 * <p/>
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080025 * Multiple attachmentPoints can be specified to batch events into 1 object.
26 * Each should be treated as independent events.
Ray Milkey269ffb92014-04-03 14:43:30 -070027 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070028 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070029 * FIXME: Current implementation directly use this object as
30 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080031 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070032@JsonSerialize(using = HostEventSerializer.class)
33public class HostEvent extends TopologyElement<HostEvent> {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070034
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080035 private final MACAddress mac;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070036 private List<SwitchPort> attachmentPoints;
TeruUd1c5b652014-03-24 13:58:46 -070037 private long lastSeenTime;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080038
39 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080040 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080041 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080042 @Deprecated
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070043 protected HostEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070044 mac = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080045 }
46
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070047 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070048 * Constructor for a given host MAC address.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070049 *
50 * @param mac the MAC address to identify the host
51 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070052 public HostEvent(MACAddress mac) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070053 this.mac = checkNotNull(mac);
Ray Milkey269ffb92014-04-03 14:43:30 -070054 this.attachmentPoints = new LinkedList<>();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080055 }
56
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070057 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070058 * Copy constructor.
59 * <p>
60 * Creates an unfrozen copy of the given HostEvent object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070062 * @param original the object to make copy of
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070064 public HostEvent(HostEvent original) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070065 super(original);
66 this.mac = original.mac;
67 this.attachmentPoints = new ArrayList<>(original.attachmentPoints);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070068 this.lastSeenTime = original.lastSeenTime;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070069 }
70
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070071 /**
72 * Gets the host MAC address.
73 *
74 * @return the MAC address.
75 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080076 public MACAddress getMac() {
Ray Milkey269ffb92014-04-03 14:43:30 -070077 return mac;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080078 }
79
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070080 /**
81 * Gets the switch attachment points.
82 *
83 * @return the switch attachment points
84 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080085 public List<SwitchPort> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070086 return Collections.unmodifiableList(attachmentPoints);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080087 }
88
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070089 /**
90 * Sets the switch attachment points.
91 *
92 * @param attachmentPoints the switch attachment points to set
93 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080094 public void setAttachmentPoints(List<SwitchPort> attachmentPoints) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070095 if (isFrozen()) {
96 throw new IllegalStateException("Tried to modify frozen instance: " + this);
97 }
Ray Milkey269ffb92014-04-03 14:43:30 -070098 this.attachmentPoints = attachmentPoints;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080099 }
100
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700101 /**
102 * Adds a switch attachment point.
103 *
104 * @param attachmentPoint the switch attachment point to add
105 */
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800106 public void addAttachmentPoint(SwitchPort attachmentPoint) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700107 if (isFrozen()) {
108 throw new IllegalStateException("Tried to modify frozen instance: " + this);
109 }
110 // may need to maintain uniqueness
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 this.attachmentPoints.add(0, attachmentPoint);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800112 }
113
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700114 /**
115 * Removes a switch attachment point.
116 *
117 * @param attachmentPoint the switch attachment point to remove
118 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700119 public boolean removeAttachmentPoint(SwitchPort attachmentPoint) {
120 if (isFrozen()) {
121 throw new IllegalStateException("Tried to modify frozen instance: " + this);
122 }
123 return this.attachmentPoints.remove(attachmentPoint);
124 }
125
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700126 /**
127 * Gets the host last seen time in milliseconds since the epoch.
128 *
129 * @return the host last seen time in milliseconds since the epoch
130 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700131 public long getLastSeenTime() {
132 return lastSeenTime;
133 }
134
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700135 /**
136 * Sets the host last seen time in milliseconds since the epoch.
137 *
138 * @param lastSeenTime the host last seen time in milliseconds since the
139 * epoch
140 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700141 public void setLastSeenTime(long lastSeenTime) {
142 if (isFrozen()) {
143 throw new IllegalStateException("Tried to modify frozen instance: " + this);
144 }
145 this.lastSeenTime = lastSeenTime;
146 }
147
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700148 /**
149 * Computes the host ID for a given host MAC address.
150 * <p>
151 * TODO: This method should be replaced with a method that uses
152 * MACAddress as an argument instead of a byte array.
153 *
154 * @param mac the host MAC address to use
155 * @return the host ID as a ByteBuffer
156 */
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700157 public static ByteBuffer getHostID(final byte[] mac) {
158 return (ByteBuffer) ByteBuffer.allocate(2 + mac.length)
159 .putChar('H').put(mac).flip();
160 }
161
162 @Override
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700163 public Dpid getOriginDpid() {
164 // TODO: Eventually, we should return a collection of Dpid values
165 for (SwitchPort sp : attachmentPoints) {
166 return sp.getDpid();
167 }
168 return null;
169 }
170
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800171 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700172 public ByteBuffer getIDasByteBuffer() {
173 return getHostID(mac.toBytes());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800174 }
175
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700176 @Override
177 public int hashCode() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700178 return 31 * super.hashCode() + Objects.hash(attachmentPoints, mac);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700179 }
180
181 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700182 public boolean equals(Object o) {
183 if (this == o) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700184 return true;
185 }
186
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700187 if (o == null || getClass() != o.getClass()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700188 return false;
189 }
190
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700191 // Compare attributes
192 if (!super.equals(o)) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700193 return false;
194 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700195
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700196 HostEvent other = (HostEvent) o;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700197 // XXX lastSeenTime excluded from Equality condition, is it OK?
198 return Objects.equals(mac, other.mac) &&
199 Objects.equals(this.attachmentPoints, other.attachmentPoints);
200 }
201
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700202 @Override
203 public String toString() {
204 return "[HostEvent " + mac + " attachmentPoints:" + attachmentPoints + "]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800205 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800206}