blob: 59f69bd1dd0dead93fb772e29956a372c4769ac1 [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
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -07006import net.onrc.onos.core.topology.web.serializers.LinkDataSerializer;
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/**
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070016 * Self-contained Link object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080017 */
Ray Milkey2fa6ca42014-06-13 15:38:20 -070018
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070019@JsonSerialize(using = LinkDataSerializer.class)
20public class LinkData extends TopologyElement<LinkData> {
21 public static final int LINKID_BYTES = 2 + PortData.PORTID_BYTES * 2;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070022
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070023 private final LinkTuple id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070024 // TODO add LastSeenTime, Capacity if appropriate
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -070025 protected static final Double DEFAULT_CAPACITY = Double.POSITIVE_INFINITY;
26 private Double capacity = DEFAULT_CAPACITY;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080027
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080028 /**
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 LinkData() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070033 id = null;
34 }
35
36 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070037 * Constructor for given LinkTuple.
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070038 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070039 * @param id the link tuple to identify the link
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070040 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070041 public LinkData(LinkTuple id) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070042 this.id = checkNotNull(id);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080043 }
44
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070045 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070046 * Constructor for given source and destination switch ports.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070047 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070048 * @param src the source SwitchPort to use
49 * @param dst the destination SwitchPort to use
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070050 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070051 public LinkData(SwitchPort src, SwitchPort dst) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070052 this(new LinkTuple(src, dst));
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070053 }
54
55 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070056 * Constructor for a given Link object.
57 * <p>
58 * TODO: This constructor should probably be removed.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070059 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070060 * @param link the Link object to use.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070062 public LinkData(Link link) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070063 this(link.getLinkTuple());
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070064 // FIXME losing attributes here
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080065 }
66
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070067 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070068 * Copy constructor.
69 * <p>
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070070 * Creates an unfrozen copy of the given LinkData object.
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070071 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070072 * @param original the object ot make copy of
73 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070074 public LinkData(LinkData original) {
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070075 super(original);
76 this.id = original.id;
77 }
78
79 /**
80 * Gets the LinkTuple that identifies this link.
81 *
82 * @return the LinkTuple identifying this link
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070083 */
84 public LinkTuple getLinkTuple() {
85 return id;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070086 }
87
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070088 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070089 * Gets the link source SwitchPort.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070090 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070091 * @return the link source SwitchPort
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070092 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080093 public SwitchPort getSrc() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070094 return getLinkTuple().getSrc();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080095 }
96
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070097 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070098 * Gets the link destination SwitchPort.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070099 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700100 * @return the link destination SwitchPort
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700101 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800102 public SwitchPort getDst() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700103 return getLinkTuple().getDst();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800104 }
105
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700106 /**
107 * Gets the link capacity.
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700108 * <p>
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700109 * TODO: What is the unit?
110 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700111 * @return the link capacity
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700112 */
113 public Double getCapacity() {
114 return capacity;
115 }
116
117 /**
118 * Sets the link capacity.
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700119 * <p>
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700120 * TODO: What is the unit?
121 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700122 * @param capacity the link capacity to set
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700123 */
124 void setCapacity(Double capacity) {
125 if (isFrozen()) {
126 throw new IllegalStateException("Tried to modify frozen instance: " + this);
127 }
128
129 this.capacity = capacity;
130 }
131
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700132 /**
133 * Computes the link ID for given source and destination switch DPID and
134 * port numbers.
135 *
136 * @param srcDpid the source switch DPID to use
137 * @param srcPortNo the source port number to use
138 * @param dstDpid the destination switch DPID to use
139 * @param dstPortNo the destination port number to use
140 * @return the link ID as a ByteBuffer
141 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700142 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
143 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700144 return getLinkID(srcDpid.value(), srcPortNo.value(),
145 dstDpid.value(), dstPortNo.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700146 }
147
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700148 /**
149 * Computes the link ID for given source and destination switch DPID and
150 * port numbers.
151 * <p>
152 * TODO: This method should be removed and replaced with the corresponding
153 * getLinkID(Dpid, PortNumber, Dpid, PortNumber) method.
154 *
155 * @param srcDpid the source switch DPID to use
156 * @param srcPortNo the source port number to use
157 * @param dstDpid the destination switch DPID to use
158 * @param dstPortNo the destination port number to use
159 * @return the link ID as a ByteBuffer
160 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700161 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
162 Long dstDpid, Long dstPortNo) {
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700163 return (ByteBuffer) ByteBuffer.allocate(LinkData.LINKID_BYTES)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700164 .putChar('L')
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700165 .put(PortData.getPortID(srcDpid, srcPortNo))
166 .put(PortData.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800167 }
168
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700169 @Override
170 public Dpid getOriginDpid() {
171 return this.id.getDst().getDpid();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800172 }
173
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700174 @Override
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800175 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700176 return getLinkID(getSrc().getDpid(), getSrc().getPortNumber(),
177 getDst().getDpid(), getDst().getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800178 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800179
Toshio Koide0c9106d2014-02-19 15:26:38 -0800180 @Override
181 public int hashCode() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700182 return 31 * super.hashCode() + Objects.hashCode(id);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800183 }
184
185 @Override
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700186 public boolean equals(Object o) {
187 if (this == o) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700188 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700189 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700190
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700191 if (o == null || getClass() != o.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700193 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700194
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700195 // Compare attributes
196 if (!super.equals(o)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700198 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700199
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700200 LinkData other = (LinkData) o;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700201 return Objects.equals(this.id, other.id);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800202 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700203
204 @Override
205 public String toString() {
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700206 return "[LinkData " + getSrc() + "->" + getDst() + "]";
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700207 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700208}