Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
| 4 | |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.topology.PortEvent.SwitchPort; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 6 | |
| 7 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 8 | * Self-contained Link event Object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 9 | * <p/> |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 10 | * TODO: We probably want common base class/interface for Self-Contained Event Object. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 11 | */ |
| 12 | public class LinkEvent { |
Yuta HIGUCHI | f65bcd5 | 2014-02-23 15:09:57 -0800 | [diff] [blame] | 13 | protected final SwitchPort src; |
| 14 | protected final SwitchPort dst; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 15 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 16 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 17 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 18 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 19 | @Deprecated |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 20 | public LinkEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | src = null; |
| 22 | dst = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 23 | } |
| 24 | |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 25 | public LinkEvent(Long srcDpid, Long srcPortNo, Long dstDpid, |
| 26 | Long dstPortNo) { |
| 27 | src = new SwitchPort(srcDpid, srcPortNo); |
| 28 | dst = new SwitchPort(dstDpid, dstPortNo); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 29 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 30 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 31 | public LinkEvent(Link link) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | src = new SwitchPort(link.getSrcSwitch().getDpid(), |
| 33 | link.getSrcPort().getNumber()); |
| 34 | dst = new SwitchPort(link.getDstSwitch().getDpid(), |
| 35 | link.getDstPort().getNumber()); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public SwitchPort getSrc() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | return src; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | public SwitchPort getDst() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | return dst; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | return "[LinkEvent " + src + "->" + dst + "]"; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 51 | public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2; |
| 52 | |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 53 | public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo, |
| 54 | Long dstDpid, Long dstPortNo) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES).putChar('L') |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 56 | .put(PortEvent.getPortID(srcDpid, srcPortNo)) |
| 57 | .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 60 | public byte[] getID() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | return getLinkID(src.getDpid(), src.getNumber(), |
| 62 | dst.getDpid(), dst.getNumber()).array(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public ByteBuffer getIDasByteBuffer() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | return getLinkID(src.getDpid(), src.getNumber(), |
| 67 | dst.getDpid(), dst.getNumber()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 68 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 69 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 70 | @Override |
| 71 | public int hashCode() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 81 | if (this == obj) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 83 | } |
| 84 | if (obj == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 86 | } |
| 87 | if (getClass() != obj.getClass()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 89 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | LinkEvent other = (LinkEvent) obj; |
| 91 | if (dst == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 92 | if (other.dst != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 94 | } |
| 95 | } else if (!dst.equals(other.dst)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 97 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | if (src == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 99 | if (other.src != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 101 | } |
| 102 | } else if (!src.equals(other.src)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 104 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | return true; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 106 | } |
Ray Milkey | 0f913a0 | 2014-04-07 20:58:17 -0700 | [diff] [blame] | 107 | } |