blob: 9b6630fdb7dffb6c010a4cfed51437160e42da27 [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;
7import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08008
9/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070010 * Self-contained Link event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070011 * <p/>
Ray Milkeyb41100a2014-04-10 10:42:15 -070012 * TODO: We probably want common base class/interface for Self-Contained Event Object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080013 */
Ray Milkey2fa6ca42014-06-13 15:38:20 -070014
15@JsonSerialize(using = LinkEventSerializer.class)
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080016public class LinkEvent {
Yuta HIGUCHIf65bcd52014-02-23 15:09:57 -080017 protected final SwitchPort src;
18 protected final SwitchPort dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080019
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080020 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080021 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080022 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080023 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080024 public LinkEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070025 src = null;
26 dst = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080027 }
28
Ray Milkey9526d6f2014-04-10 14:54:15 -070029 public LinkEvent(Long srcDpid, Long srcPortNo, Long dstDpid,
30 Long dstPortNo) {
31 src = new SwitchPort(srcDpid, srcPortNo);
32 dst = new SwitchPort(dstDpid, dstPortNo);
Toshio Koide0c9106d2014-02-19 15:26:38 -080033 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080034
Toshio Koide0c9106d2014-02-19 15:26:38 -080035 public LinkEvent(Link link) {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 src = new SwitchPort(link.getSrcSwitch().getDpid(),
37 link.getSrcPort().getNumber());
38 dst = new SwitchPort(link.getDstSwitch().getDpid(),
39 link.getDstPort().getNumber());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080040 }
41
42 public SwitchPort getSrc() {
Ray Milkey269ffb92014-04-03 14:43:30 -070043 return src;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080044 }
45
46 public SwitchPort getDst() {
Ray Milkey269ffb92014-04-03 14:43:30 -070047 return dst;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080048 }
49
50 @Override
51 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070052 return "[LinkEvent " + src + "->" + dst + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080053 }
54
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080055 public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
56
Ray Milkey9526d6f2014-04-10 14:54:15 -070057 public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo,
58 Long dstDpid, Long dstPortNo) {
Ray Milkey269ffb92014-04-03 14:43:30 -070059 return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES).putChar('L')
Ray Milkey9526d6f2014-04-10 14:54:15 -070060 .put(PortEvent.getPortID(srcDpid, srcPortNo))
61 .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080062 }
63
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080064 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -070065 return getLinkID(src.getDpid(), src.getNumber(),
66 dst.getDpid(), dst.getNumber()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080067 }
68
69 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -070070 return getLinkID(src.getDpid(), src.getNumber(),
71 dst.getDpid(), dst.getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080072 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080073
Toshio Koide0c9106d2014-02-19 15:26:38 -080074 @Override
75 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -070076 final int prime = 31;
77 int result = 1;
78 result = prime * result + ((dst == null) ? 0 : dst.hashCode());
79 result = prime * result + ((src == null) ? 0 : src.hashCode());
80 return result;
Toshio Koide0c9106d2014-02-19 15:26:38 -080081 }
82
83 @Override
84 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070085 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -070086 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070087 }
88 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070089 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070090 }
91 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -070092 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070093 }
Ray Milkey269ffb92014-04-03 14:43:30 -070094 LinkEvent other = (LinkEvent) obj;
95 if (dst == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070096 if (other.dst != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070097 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070098 }
99 } else if (!dst.equals(other.dst)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 if (src == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700103 if (other.src != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700105 }
106 } else if (!src.equals(other.src)) {
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 return true;
Toshio Koide0c9106d2014-02-19 15:26:38 -0800110 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700111}