blob: c54140d9c289991437466100110ed9c982f1bc53 [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;
4
Jonathan Hart472062d2014-04-03 10:56:48 -07005import net.onrc.onos.core.topology.PortEvent.SwitchPort;
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;
9
Ray Milkey2fa6ca42014-06-13 15:38:20 -070010import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080011
12/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070013 * Self-contained Link event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070014 * <p/>
Ray Milkeyb41100a2014-04-10 10:42:15 -070015 * TODO: We probably want common base class/interface for Self-Contained Event Object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080016 */
Ray Milkey2fa6ca42014-06-13 15:38:20 -070017
18@JsonSerialize(using = LinkEventSerializer.class)
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080019public class LinkEvent {
Yuta HIGUCHIf65bcd52014-02-23 15:09:57 -080020 protected final SwitchPort src;
21 protected final SwitchPort dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080022
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080023 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080024 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080025 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080026 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080027 public LinkEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070028 src = null;
29 dst = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080030 }
31
Ray Milkey9526d6f2014-04-10 14:54:15 -070032 public LinkEvent(Long srcDpid, Long srcPortNo, Long dstDpid,
33 Long dstPortNo) {
34 src = new SwitchPort(srcDpid, srcPortNo);
35 dst = new SwitchPort(dstDpid, dstPortNo);
Toshio Koide0c9106d2014-02-19 15:26:38 -080036 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080037
Toshio Koide0c9106d2014-02-19 15:26:38 -080038 public LinkEvent(Link link) {
Ray Milkey269ffb92014-04-03 14:43:30 -070039 src = new SwitchPort(link.getSrcSwitch().getDpid(),
40 link.getSrcPort().getNumber());
41 dst = new SwitchPort(link.getDstSwitch().getDpid(),
42 link.getDstPort().getNumber());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080043 }
44
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070045 public LinkEvent(Dpid srcDpid, PortNumber srcPortNo,
46 Dpid dstDpid, PortNumber dstPortNo) {
47 src = new SwitchPort(srcDpid, srcPortNo);
48 dst = new SwitchPort(dstDpid, dstPortNo);
49 }
50
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080051 public SwitchPort getSrc() {
Ray Milkey269ffb92014-04-03 14:43:30 -070052 return src;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080053 }
54
55 public SwitchPort getDst() {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 return dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080057 }
58
59 @Override
60 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070061 return "[LinkEvent " + src + "->" + dst + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080062 }
63
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080064 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
65
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070066 public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo,
67 Dpid dstDpid, PortNumber dstPortNo) {
68 return getLinkID(srcDpid.value(), (long) srcPortNo.value(),
69 dstDpid.value(), (long) dstPortNo.value());
70 }
71
Ray Milkey9526d6f2014-04-10 14:54:15 -070072 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
73 Long dstDpid, Long dstPortNo) {
Ray Milkey269ffb92014-04-03 14:43:30 -070074 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES).putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -070075 .put(PortEvent.getPortID(srcDpid, srcPortNo))
76 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080077 }
78
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080079 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -070080 return getLinkID(src.getDpid(), src.getNumber(),
81 dst.getDpid(), dst.getNumber()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080082 }
83
84 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return getLinkID(src.getDpid(), src.getNumber(),
86 dst.getDpid(), dst.getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080087 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080088
Toshio Koide0c9106d2014-02-19 15:26:38 -080089 @Override
90 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -070091 final int prime = 31;
92 int result = 1;
93 result = prime * result + ((dst == null) ? 0 : dst.hashCode());
94 result = prime * result + ((src == null) ? 0 : src.hashCode());
95 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -080096 }
97
98 @Override
99 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700100 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700102 }
103 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700105 }
106 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700108 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 LinkEvent other = (LinkEvent) obj;
110 if (dst == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700111 if (other.dst != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700113 }
114 } else if (!dst.equals(other.dst)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700116 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 if (src == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700118 if (other.src != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700120 }
121 } else if (!src.equals(other.src)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700123 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 return true;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800125 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700126}