blob: 83af7863aa5806e9577f1314ef1c82503ed6b6b6 [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 {
14 private final SwitchPort src;
15 private final SwitchPort dst;
16
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) {
32 src = new SwitchPort(link.getSourceSwitch().getDpid(),
33 link.getSourcePort().getNumber());
34 dst = new SwitchPort(link.getDestinationSwitch().getDpid(),
35 link.getDestinationPort().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
53 public static byte[] getLinkID(Long src_dpid, Long src_port_no,
54 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))
57 .put(PortEvent.getPortID(dst_dpid, dst_port_no)).array();
58 }
59
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080060 public byte[] getID() {
61 return getLinkID(src.getDpid(), src.getNumber(),
Toshio Koide0c9106d2014-02-19 15:26:38 -080062 dst.getDpid(), dst.getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080063 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080064
65 public Link getLink(NetworkGraph graph) {
66 Port srcPort = graph.getPort(getSrc().getDpid(), getSrc().getNumber());
67 if (srcPort == null) return null;
68 Link link = srcPort.getOutgoingLink();
69 if (link == null) return null;
70 if (link.getDestinationSwitch().getDpid() != getDst().getDpid()) return null;
71 if (link.getDestinationPort().getNumber() != getDst().getNumber()) return null;
72 return link;
73 }
74
75 @Override
76 public int hashCode() {
77 final int prime = 31;
78 int result = 1;
79 result = prime * result + ((dst == null) ? 0 : dst.hashCode());
80 result = prime * result + ((src == null) ? 0 : src.hashCode());
81 return result;
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj)
87 return true;
88 if (obj == null)
89 return false;
90 if (getClass() != obj.getClass())
91 return false;
92 LinkEvent other = (LinkEvent) obj;
93 if (dst == null) {
94 if (other.dst != null)
95 return false;
96 } else if (!dst.equals(other.dst))
97 return false;
98 if (src == null) {
99 if (other.src != null)
100 return false;
101 } else if (!src.equals(other.src))
102 return false;
103 return true;
104 }
105}