blob: 4296fee3140ecfd5a48294a3c8aa239cd18fe749 [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.Objects;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08005
Ray Milkey2fa6ca42014-06-13 15:38:20 -07006import net.onrc.onos.core.topology.web.serializers.LinkEventSerializer;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07007import net.onrc.onos.core.util.Dpid;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -07008import net.onrc.onos.core.util.LinkTuple;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07009import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070010import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070011
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070012import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkey2fa6ca42014-06-13 15:38:20 -070013import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014
15/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070016 * Self-contained Link event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070017 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070018 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070019 * FIXME: Current implementation directly use this object as
20 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080021 */
Ray Milkey2fa6ca42014-06-13 15:38:20 -070022
23@JsonSerialize(using = LinkEventSerializer.class)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070024public class LinkEvent extends TopologyElement<LinkEvent> {
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070025 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070026
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070027 private final LinkTuple id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070028 // TODO add LastSeenTime, Capacity if appropriate
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -070029 protected static final Double DEFAULT_CAPACITY = Double.POSITIVE_INFINITY;
30 private Double capacity = DEFAULT_CAPACITY;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080031
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080032 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080033 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080034 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080035 @Deprecated
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070036 protected LinkEvent() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070037 id = null;
38 }
39
40 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070041 * Constructor for given LinkTuple.
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070042 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070043 * @param id the link tuple to identify the link
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070044 */
45 public LinkEvent(LinkTuple id) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070046 this.id = checkNotNull(id);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080047 }
48
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070049 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070050 * Constructor for given source and destination switch ports.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070051 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070052 * @param src the source SwitchPort to use
53 * @param dst the destination SwitchPort to use
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070054 */
55 public LinkEvent(SwitchPort src, SwitchPort dst) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070056 this(new LinkTuple(src, dst));
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070057 }
58
59 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070060 * Constructor for a given Link object.
61 * <p>
62 * TODO: This constructor should probably be removed.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070064 * @param link the Link object to use.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070065 */
Toshio Koide0c9106d2014-02-19 15:26:38 -080066 public LinkEvent(Link link) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070067 this(link.getLinkTuple());
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070068 // FIXME losing attributes here
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080069 }
70
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070071 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070072 * Copy constructor.
73 * <p>
74 * Creates an unfrozen copy of the given LinkEvent object.
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070075 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070076 * @param original the object ot make copy of
77 */
78 public LinkEvent(LinkEvent original) {
79 super(original);
80 this.id = original.id;
81 }
82
83 /**
84 * Gets the LinkTuple that identifies this link.
85 *
86 * @return the LinkTuple identifying this link
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070087 */
88 public LinkTuple getLinkTuple() {
89 return id;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070090 }
91
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070092 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070093 * Gets the link source SwitchPort.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070094 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070095 * @return the link source SwitchPort
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070096 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080097 public SwitchPort getSrc() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070098 return getLinkTuple().getSrc();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080099 }
100
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700101 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700102 * Gets the link destination SwitchPort.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700103 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700104 * @return the link destination SwitchPort
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700105 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800106 public SwitchPort getDst() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700107 return getLinkTuple().getDst();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800108 }
109
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700110 /**
111 * Gets the link capacity.
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700112 * <p>
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700113 * TODO: What is the unit?
114 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700115 * @return the link capacity
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700116 */
117 public Double getCapacity() {
118 return capacity;
119 }
120
121 /**
122 * Sets the link capacity.
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700123 * <p>
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700124 * TODO: What is the unit?
125 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700126 * @param capacity the link capacity to set
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700127 */
128 void setCapacity(Double capacity) {
129 if (isFrozen()) {
130 throw new IllegalStateException("Tried to modify frozen instance: " + this);
131 }
132
133 this.capacity = capacity;
134 }
135
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700136 /**
137 * Computes the link ID for given source and destination switch DPID and
138 * port numbers.
139 *
140 * @param srcDpid the source switch DPID to use
141 * @param srcPortNo the source port number to use
142 * @param dstDpid the destination switch DPID to use
143 * @param dstPortNo the destination port number to use
144 * @return the link ID as a ByteBuffer
145 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700146 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
147 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700148 return getLinkID(srcDpid.value(), srcPortNo.value(),
149 dstDpid.value(), dstPortNo.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700150 }
151
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700152 /**
153 * Computes the link ID for given source and destination switch DPID and
154 * port numbers.
155 * <p>
156 * TODO: This method should be removed and replaced with the corresponding
157 * getLinkID(Dpid, PortNumber, Dpid, PortNumber) method.
158 *
159 * @param srcDpid the source switch DPID to use
160 * @param srcPortNo the source port number to use
161 * @param dstDpid the destination switch DPID to use
162 * @param dstPortNo the destination port number to use
163 * @return the link ID as a ByteBuffer
164 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700165 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
166 Long dstDpid, Long dstPortNo) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700167 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES)
168 .putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -0700169 .put(PortEvent.getPortID(srcDpid, srcPortNo))
170 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800171 }
172
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700173 @Override
174 public Dpid getOriginDpid() {
175 return this.id.getDst().getDpid();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800176 }
177
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700178 @Override
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800179 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700180 return getLinkID(getSrc().getDpid(), getSrc().getPortNumber(),
181 getDst().getDpid(), getDst().getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800182 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800183
Toshio Koide0c9106d2014-02-19 15:26:38 -0800184 @Override
185 public int hashCode() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700186 return 31 * super.hashCode() + Objects.hashCode(id);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800187 }
188
189 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700190 public boolean equals(Object o) {
191 if (this == o) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700193 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700194
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700195 if (o == null || getClass() != o.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700197 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700198
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700199 // Compare attributes
200 if (!super.equals(o)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700201 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700202 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700203
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700204 LinkEvent other = (LinkEvent) o;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700205 return Objects.equals(this.id, other.id);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800206 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700207
208 @Override
209 public String toString() {
210 return "[LinkEvent " + getSrc() + "->" + getDst() + "]";
211 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700212}