blob: 831b407554888cebd041ba1487a1492bb1d74429 [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
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070012import org.apache.commons.lang.Validate;
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) {
45 Validate.notNull(id);
46
47 this.id = id;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080048 }
49
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070050 /**
51 * Creates the Link object.
52 *
53 * @param src source SwitchPort
54 * @param dst destination SwitchPort
55 */
56 public LinkEvent(SwitchPort src, SwitchPort dst) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070057 this(new LinkTuple(src, dst));
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070058 }
59
60 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070061 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070062 *
63 * @param original to make copy of.
64 */
65 public LinkEvent(LinkEvent original) {
66 super(original);
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070067 this.id = original.id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070068 }
69
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070070 // TODO probably want to remove this
Toshio Koide0c9106d2014-02-19 15:26:38 -080071 public LinkEvent(Link link) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070072 this(link.getLinkTuple());
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070073 // FIXME losing attributes here
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080074 }
75
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070076 /**
77 * Creates the Link object.
78 *
79 * @param srcDpid source switch DPID
80 * @param srcPortNo source port number
81 * @param dstDpid destination switch DPID
82 * @param dstPortNo destination port number
83 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070084 public LinkEvent(Dpid srcDpid, PortNumber srcPortNo,
85 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070086 this(new LinkTuple(srcDpid, srcPortNo, dstDpid, dstPortNo));
87 }
88
89 /**
90 * Gets a {@link LinkTuple} that identifies this link.
91 *
92 * @return a LinkTuple representing the Port
93 */
94 public LinkTuple getLinkTuple() {
95 return id;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070096 }
97
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070098 /**
99 * Gets the source SwitchPort.
100 *
101 * @return source SwitchPort.
102 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800103 public SwitchPort getSrc() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700104 return getLinkTuple().getSrc();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800105 }
106
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700107 /**
108 * Gets the destination SwitchPort.
109 *
110 * @return destination SwitchPort.
111 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800112 public SwitchPort getDst() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700113 return getLinkTuple().getDst();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800114 }
115
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700116 /**
117 * Gets the link capacity.
118 * TODO: What is the unit?
119 *
120 * @return capacity
121 */
122 public Double getCapacity() {
123 return capacity;
124 }
125
126 /**
127 * Sets the link capacity.
128 * TODO: What is the unit?
129 *
130 * @param capacity capacity
131 */
132 void setCapacity(Double capacity) {
133 if (isFrozen()) {
134 throw new IllegalStateException("Tried to modify frozen instance: " + this);
135 }
136
137 this.capacity = capacity;
138 }
139
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800140 @Override
141 public String toString() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700142 return "[LinkEvent " + getSrc() + "->" + getDst() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800143 }
144
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800145 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
146
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700147 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
148 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700149 return getLinkID(srcDpid.value(), (long) srcPortNo.value(),
150 dstDpid.value(), (long) dstPortNo.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700151 }
152
Ray Milkey9526d6f2014-04-10 14:54:15 -0700153 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
154 Long dstDpid, Long dstPortNo) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700155 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES)
156 .putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -0700157 .put(PortEvent.getPortID(srcDpid, srcPortNo))
158 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800159 }
160
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800161 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700162 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800163 }
164
165 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700166 return getLinkID(getSrc().getDpid(), getSrc().getPortNumber(),
167 getDst().getDpid(), getDst().getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800168 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800169
Toshio Koide0c9106d2014-02-19 15:26:38 -0800170 @Override
171 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700172 final int prime = 31;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700173 int result = super.hashCode();
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700174 result = prime * result + Objects.hashCode(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700175 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800176 }
177
178 @Override
179 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700180 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700181 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700182 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700183
Ray Milkeyb29e6262014-04-09 16:02:14 -0700184 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700185 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700186 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700187
Ray Milkeyb29e6262014-04-09 16:02:14 -0700188 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700189 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700190 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700191 LinkEvent other = (LinkEvent) obj;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700192
193 // compare attributes
194 if (!super.equals(obj)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700196 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700197
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700198 return Objects.equals(this.id, other.id);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800199 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700200}