blob: 40d7400a4344323dd05feac5e95ac30c48b83a14 [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;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08006
7/**
Ray Milkeyb41100a2014-04-10 10:42:15 -07008 * Self-contained Link event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -07009 * <p/>
Ray Milkeyb41100a2014-04-10 10:42:15 -070010 * TODO: We probably want common base class/interface for Self-Contained Event Object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080011 */
12public class LinkEvent {
Yuta HIGUCHIf65bcd52014-02-23 15:09:57 -080013 protected final SwitchPort src;
14 protected final SwitchPort dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080015
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080016 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080017 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080018 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080019 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080020 public LinkEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070021 src = null;
22 dst = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080023 }
24
Ray Milkey9526d6f2014-04-10 14:54:15 -070025 public LinkEvent(Long srcDpid, Long srcPortNo, Long dstDpid,
26 Long dstPortNo) {
27 src = new SwitchPort(srcDpid, srcPortNo);
28 dst = new SwitchPort(dstDpid, dstPortNo);
Toshio Koide0c9106d2014-02-19 15:26:38 -080029 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080030
Toshio Koide0c9106d2014-02-19 15:26:38 -080031 public LinkEvent(Link link) {
Ray Milkey269ffb92014-04-03 14:43:30 -070032 src = new SwitchPort(link.getSrcSwitch().getDpid(),
33 link.getSrcPort().getNumber());
34 dst = new SwitchPort(link.getDstSwitch().getDpid(),
35 link.getDstPort().getNumber());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080036 }
37
38 public SwitchPort getSrc() {
Ray Milkey269ffb92014-04-03 14:43:30 -070039 return src;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080040 }
41
42 public SwitchPort getDst() {
Ray Milkey269ffb92014-04-03 14:43:30 -070043 return dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080044 }
45
46 @Override
47 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070048 return "[LinkEvent " + src + "->" + dst + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080049 }
50
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080051 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
52
Ray Milkey9526d6f2014-04-10 14:54:15 -070053 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
54 Long dstDpid, Long dstPortNo) {
Ray Milkey269ffb92014-04-03 14:43:30 -070055 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES).putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -070056 .put(PortEvent.getPortID(srcDpid, srcPortNo))
57 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080058 }
59
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080060 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -070061 return getLinkID(src.getDpid(), src.getNumber(),
62 dst.getDpid(), dst.getNumber()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080063 }
64
65 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -070066 return getLinkID(src.getDpid(), src.getNumber(),
67 dst.getDpid(), dst.getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080068 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080069
Toshio Koide0c9106d2014-02-19 15:26:38 -080070 @Override
71 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -070072 final int prime = 31;
73 int result = 1;
74 result = prime * result + ((dst == null) ? 0 : dst.hashCode());
75 result = prime * result + ((src == null) ? 0 : src.hashCode());
76 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -080077 }
78
79 @Override
80 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070081 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -070082 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070083 }
84 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070086 }
87 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -070088 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070089 }
Ray Milkey269ffb92014-04-03 14:43:30 -070090 LinkEvent other = (LinkEvent) obj;
91 if (dst == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070092 if (other.dst != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070093 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070094 }
95 } else if (!dst.equals(other.dst)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070096 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070097 }
Ray Milkey269ffb92014-04-03 14:43:30 -070098 if (src == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070099 if (other.src != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 }
102 } else if (!src.equals(other.src)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700104 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 return true;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800106 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700107}