blob: 4d060de93723548020de9aa276ce74a8f235ff7a [file] [log] [blame]
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08003import java.nio.ByteBuffer;
4
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08005import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort;
6
7/**
Yuta HIGUCHIdac4caa2014-02-11 18:51:35 -08008 * Self-contained Link event Object
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08009 *
10 * TODO: We probably want common base class/interface for Self-Contained Event Object
11 *
12 */
13public class LinkEvent {
Yuta HIGUCHIf65bcd52014-02-23 15:09:57 -080014 protected final SwitchPort src;
15 protected final SwitchPort dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080016
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080017 /**
18 * Default constructor.
19 */
20 public LinkEvent() {
21 src = null;
22 dst = null;
23 }
24
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080025 public LinkEvent(Long src_dpid, Long src_port_no, Long dst_dpid,
26 Long dst_port_no) {
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080027 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) {
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080032 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() {
Toshio Koide0c9106d2014-02-19 15:26:38 -080039 return src;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080040 }
41
42 public SwitchPort getDst() {
Toshio Koide0c9106d2014-02-19 15:26:38 -080043 return dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080044 }
45
46 @Override
47 public String toString() {
48 return "[LinkEvent " + src + "->" + dst + "]";
49 }
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,
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080054 Long dst_dpid, Long dst_port_no) {
55 return ByteBuffer.allocate(LinkEvent.LINKID_BYTES).putChar('L')
56 .put(PortEvent.getPortID(src_dpid, src_port_no))
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080057 .put(PortEvent.getPortID(dst_dpid, dst_port_no));
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080058 }
59
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080060 public byte[] getID() {
61 return getLinkID(src.getDpid(), src.getNumber(),
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080062 dst.getDpid(), dst.getNumber()).array();
63 }
64
65 public ByteBuffer getIDasByteBuffer() {
66 return getLinkID(src.getDpid(), src.getNumber(),
Toshio Koide0c9106d2014-02-19 15:26:38 -080067 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() {
72 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;
77 }
78
79 @Override
80 public boolean equals(Object obj) {
81 if (this == obj)
82 return true;
83 if (obj == null)
84 return false;
85 if (getClass() != obj.getClass())
86 return false;
87 LinkEvent other = (LinkEvent) obj;
88 if (dst == null) {
89 if (other.dst != null)
90 return false;
91 } else if (!dst.equals(other.dst))
92 return false;
93 if (src == null) {
94 if (other.src != null)
95 return false;
96 } else if (!src.equals(other.src))
97 return false;
98 return true;
99 }
100}