blob: f7ae6d3fcfb1d50b31275544252ad91019b0d56d [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> {
25
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070026 private final LinkTuple id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 // TODO add LastSeenTime, Capacity if appropriate
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -070028 protected static final Double DEFAULT_CAPACITY = Double.POSITIVE_INFINITY;
29 private Double capacity = DEFAULT_CAPACITY;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080030
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080032 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080033 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080034 @Deprecated
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070035 protected LinkEvent() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070036 id = null;
37 }
38
39 /**
40 * Creates the Link object.
41 *
42 * @param id link tuple to identify this link
43 */
44 public LinkEvent(LinkTuple id) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070045 this.id = checkNotNull(id);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080046 }
47
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070048 /**
49 * Creates the Link object.
50 *
51 * @param src source SwitchPort
52 * @param dst destination SwitchPort
53 */
54 public LinkEvent(SwitchPort src, SwitchPort dst) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070055 this(new LinkTuple(src, dst));
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070056 }
57
58 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070059 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070060 *
61 * @param original to make copy of.
62 */
63 public LinkEvent(LinkEvent original) {
64 super(original);
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070065 this.id = original.id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070066 }
67
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070068 // TODO probably want to remove this
Toshio Koide0c9106d2014-02-19 15:26:38 -080069 public LinkEvent(Link link) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070070 this(link.getLinkTuple());
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070071 // FIXME losing attributes here
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080072 }
73
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070074 /**
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070075 * Gets a {@link LinkTuple} that identifies this link.
76 *
77 * @return a LinkTuple representing the Port
78 */
79 public LinkTuple getLinkTuple() {
80 return id;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070081 }
82
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070083 /**
84 * Gets the source SwitchPort.
85 *
86 * @return source SwitchPort.
87 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080088 public SwitchPort getSrc() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070089 return getLinkTuple().getSrc();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080090 }
91
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070092 /**
93 * Gets the destination SwitchPort.
94 *
95 * @return destination SwitchPort.
96 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080097 public SwitchPort getDst() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070098 return getLinkTuple().getDst();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080099 }
100
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700101 /**
102 * Gets the link capacity.
103 * TODO: What is the unit?
104 *
105 * @return capacity
106 */
107 public Double getCapacity() {
108 return capacity;
109 }
110
111 /**
112 * Sets the link capacity.
113 * TODO: What is the unit?
114 *
115 * @param capacity capacity
116 */
117 void setCapacity(Double capacity) {
118 if (isFrozen()) {
119 throw new IllegalStateException("Tried to modify frozen instance: " + this);
120 }
121
122 this.capacity = capacity;
123 }
124
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800125 @Override
126 public String toString() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700127 return "[LinkEvent " + getSrc() + "->" + getDst() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800128 }
129
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800130 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
131
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700132 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
133 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700134 return getLinkID(srcDpid.value(), srcPortNo.value(),
135 dstDpid.value(), dstPortNo.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700136 }
137
Ray Milkey9526d6f2014-04-10 14:54:15 -0700138 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
139 Long dstDpid, Long dstPortNo) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700140 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES)
141 .putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -0700142 .put(PortEvent.getPortID(srcDpid, srcPortNo))
143 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800144 }
145
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800146 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700147 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800148 }
149
150 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700151 return getLinkID(getSrc().getDpid(), getSrc().getPortNumber(),
152 getDst().getDpid(), getDst().getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800153 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800154
Toshio Koide0c9106d2014-02-19 15:26:38 -0800155 @Override
156 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700157 final int prime = 31;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700158 int result = super.hashCode();
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700159 result = prime * result + Objects.hashCode(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800161 }
162
163 @Override
164 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700165 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700166 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700167 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700168
Ray Milkeyb29e6262014-04-09 16:02:14 -0700169 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700170 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700171 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700172
Ray Milkeyb29e6262014-04-09 16:02:14 -0700173 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700175 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 LinkEvent other = (LinkEvent) obj;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700177
178 // compare attributes
179 if (!super.equals(obj)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700180 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700181 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700182
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700183 return Objects.equals(this.id, other.id);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800184 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700185}