blob: d73f80f32081de98972565f487e0987eeb5fcf03 [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 /**
53 * Create an unfrozen copy of given Object.
54 *
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
Ray Milkey9526d6f2014-04-10 14:54:15 -070063 public LinkEvent(Long srcDpid, Long srcPortNo, Long dstDpid,
64 Long dstPortNo) {
65 src = new SwitchPort(srcDpid, srcPortNo);
66 dst = new SwitchPort(dstDpid, dstPortNo);
Toshio Koide0c9106d2014-02-19 15:26:38 -080067 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080068
Toshio Koide0c9106d2014-02-19 15:26:38 -080069 public LinkEvent(Link link) {
Ray Milkey269ffb92014-04-03 14:43:30 -070070 src = new SwitchPort(link.getSrcSwitch().getDpid(),
71 link.getSrcPort().getNumber());
72 dst = new SwitchPort(link.getDstSwitch().getDpid(),
73 link.getDstPort().getNumber());
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) {
86 src = new SwitchPort(srcDpid, srcPortNo);
87 dst = new SwitchPort(dstDpid, dstPortNo);
88 }
89
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070090 /**
91 * Gets the source SwitchPort.
92 *
93 * @return source SwitchPort.
94 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080095 public SwitchPort getSrc() {
Ray Milkey269ffb92014-04-03 14:43:30 -070096 return src;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080097 }
98
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070099 /**
100 * Gets the destination SwitchPort.
101 *
102 * @return destination SwitchPort.
103 */
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800104 public SwitchPort getDst() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 return dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800106 }
107
108 @Override
109 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 return "[LinkEvent " + src + "->" + dst + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800111 }
112
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800113 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
114
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700115 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
116 Dpid dstDpid, PortNumber dstPortNo) {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700117 return getLinkID(srcDpid.value(), (long) srcPortNo.value(),
118 dstDpid.value(), (long) dstPortNo.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700119 }
120
Ray Milkey9526d6f2014-04-10 14:54:15 -0700121 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
122 Long dstDpid, Long dstPortNo) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700123 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES)
124 .putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -0700125 .put(PortEvent.getPortID(srcDpid, srcPortNo))
126 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800127 }
128
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800129 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700130 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800131 }
132
133 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700134 return getLinkID(src.getDpid(), src.getPortNumber(),
135 dst.getDpid(), dst.getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800136 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800137
Toshio Koide0c9106d2014-02-19 15:26:38 -0800138 @Override
139 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 final int prime = 31;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700141 int result = super.hashCode();
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 result = prime * result + ((dst == null) ? 0 : dst.hashCode());
143 result = prime * result + ((src == null) ? 0 : src.hashCode());
144 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800145 }
146
147 @Override
148 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700149 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 return true;
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 (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700155 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700156
Ray Milkeyb29e6262014-04-09 16:02:14 -0700157 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700159 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 LinkEvent other = (LinkEvent) obj;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700161
162 // compare attributes
163 if (!super.equals(obj)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700164 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700165 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700166
167 return Objects.equals(this.src, other.src) &&
168 Objects.equals(this.dst, other.dst);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800169 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700170}