blob: 6f328f07f677e63d65fe5a6078010ba3c9bfe1f7 [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/**
Yuta HIGUCHIdac4caa2014-02-11 18:51:35 -08008 * Self-contained Link event Object
Ray Milkey269ffb92014-04-03 14:43:30 -07009 * <p/>
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080010 * 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
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080025 public LinkEvent(Long src_dpid, Long src_port_no, Long dst_dpid,
Ray Milkey269ffb92014-04-03 14:43:30 -070026 Long dst_port_no) {
27 src = new SwitchPort(src_dpid, src_port_no);
28 dst = new SwitchPort(dst_dpid, dst_port_no);
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
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080053 public static ByteBuffer getLinkID(Long src_dpid, Long src_port_no,
Ray Milkey269ffb92014-04-03 14:43:30 -070054 Long dst_dpid, Long dst_port_no) {
55 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES).putChar('L')
56 .put(PortEvent.getPortID(src_dpid, src_port_no))
57 .put(PortEvent.getPortID(dst_dpid, dst_port_no)).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}