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