blob: 01b4efeb7e1d237813105335b693404d60755e09 [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;
8import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07009import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070010
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070011import org.apache.commons.lang.Validate;
Ray Milkey2fa6ca42014-06-13 15:38:20 -070012import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080013
14/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070015 * Self-contained Link event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070016 * <p/>
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070017 * TODO: Rename to match what it is. (Switch/Port/Link/Device)Snapshot?
18 * FIXME: Current implementation directly use this object as
19 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080020 */
Ray Milkey2fa6ca42014-06-13 15:38:20 -070021
22@JsonSerialize(using = LinkEventSerializer.class)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070023public class LinkEvent extends TopologyElement<LinkEvent> {
24
25 private final SwitchPort src;
26 private final SwitchPort dst;
27 // TODO add LastSeenTime, Capacity if appropriate
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080028
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080029 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080030 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080032 @Deprecated
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070033 protected LinkEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070034 src = null;
35 dst = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080036 }
37
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070038 /**
39 * Creates the Link object.
40 *
41 * @param src source SwitchPort
42 * @param dst destination SwitchPort
43 */
44 public LinkEvent(SwitchPort src, SwitchPort dst) {
45 Validate.notNull(src);
46 Validate.notNull(dst);
47
48 this.src = src;
49 this.dst = dst;
50 }
51
52 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070053 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070054 *
55 * @param original to make copy of.
56 */
57 public LinkEvent(LinkEvent original) {
58 super(original);
59 this.src = original.src;
60 this.dst = original.dst;
61 }
62
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070063 // TODO probably want to remove this
Toshio Koide0c9106d2014-02-19 15:26:38 -080064 public LinkEvent(Link link) {
Ray Milkey269ffb92014-04-03 14:43:30 -070065 src = new SwitchPort(link.getSrcSwitch().getDpid(),
66 link.getSrcPort().getNumber());
67 dst = new SwitchPort(link.getDstSwitch().getDpid(),
68 link.getDstPort().getNumber());
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070069 // FIXME losing attributes here
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080070 }
71
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070072 /**
73 * Creates the Link object.
74 *
75 * @param srcDpid source switch DPID
76 * @param srcPortNo source port number
77 * @param dstDpid destination switch DPID
78 * @param dstPortNo destination port number
79 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070080 public LinkEvent(Dpid srcDpid, PortNumber srcPortNo,
81 Dpid dstDpid, PortNumber dstPortNo) {
82 src = new SwitchPort(srcDpid, srcPortNo);
83 dst = new SwitchPort(dstDpid, dstPortNo);
84 }
85
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070086 /**
87 * Gets the source SwitchPort.
88 *
89 * @return source SwitchPort.
90 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080091 public SwitchPort getSrc() {
Ray Milkey269ffb92014-04-03 14:43:30 -070092 return src;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080093 }
94
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070095 /**
96 * Gets the destination SwitchPort.
97 *
98 * @return destination SwitchPort.
99 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800100 public SwitchPort getDst() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 return dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800102 }
103
104 @Override
105 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 return "[LinkEvent " + src + "->" + dst + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800107 }
108
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800109 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
110
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700111 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
112 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700113 return getLinkID(srcDpid.value(), (long) srcPortNo.value(),
114 dstDpid.value(), (long) dstPortNo.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700115 }
116
Ray Milkey9526d6f2014-04-10 14:54:15 -0700117 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
118 Long dstDpid, Long dstPortNo) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700119 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES)
120 .putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -0700121 .put(PortEvent.getPortID(srcDpid, srcPortNo))
122 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800123 }
124
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800125 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700126 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800127 }
128
129 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700130 return getLinkID(src.getDpid(), src.getPortNumber(),
131 dst.getDpid(), dst.getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800132 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800133
Toshio Koide0c9106d2014-02-19 15:26:38 -0800134 @Override
135 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 final int prime = 31;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700137 int result = super.hashCode();
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 result = prime * result + ((dst == null) ? 0 : dst.hashCode());
139 result = prime * result + ((src == null) ? 0 : src.hashCode());
140 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800141 }
142
143 @Override
144 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700145 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700147 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700148
Ray Milkeyb29e6262014-04-09 16:02:14 -0700149 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700151 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700152
Ray Milkeyb29e6262014-04-09 16:02:14 -0700153 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700155 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700156 LinkEvent other = (LinkEvent) obj;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700157
158 // compare attributes
159 if (!super.equals(obj)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700161 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700162
163 return Objects.equals(this.src, other.src) &&
164 Objects.equals(this.dst, other.dst);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800165 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700166}