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; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 4 | import java.util.Objects; |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 5 | |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.topology.web.serializers.LinkEventSerializer; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.util.Dpid; |
| 8 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 10 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 11 | import org.apache.commons.lang.Validate; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 12 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 13 | |
| 14 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 15 | * Self-contained Link event Object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 16 | * <p/> |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 17 | * TODO: Rename to match what it is. (Switch/Port/Link/Device)Snapshot? |
| 18 | * FIXME: Current implementation directly use this object as |
| 19 | * Replication message, but should be sending update operation info. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 20 | */ |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 21 | |
| 22 | @JsonSerialize(using = LinkEventSerializer.class) |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 23 | public class LinkEvent extends TopologyElement<LinkEvent> { |
| 24 | |
| 25 | private final SwitchPort src; |
| 26 | private final SwitchPort dst; |
| 27 | // TODO add LastSeenTime, Capacity if appropriate |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 28 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 29 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 30 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 31 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 32 | @Deprecated |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 33 | protected LinkEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | src = null; |
| 35 | dst = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Creates the Link object. |
| 40 | * |
| 41 | * @param src source SwitchPort |
| 42 | * @param dst destination SwitchPort |
| 43 | */ |
| 44 | public LinkEvent(SwitchPort src, SwitchPort dst) { |
| 45 | Validate.notNull(src); |
| 46 | Validate.notNull(dst); |
| 47 | |
| 48 | this.src = src; |
| 49 | this.dst = dst; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Create an unfrozen copy of given Object. |
| 54 | * |
| 55 | * @param original to make copy of. |
| 56 | */ |
| 57 | public LinkEvent(LinkEvent original) { |
| 58 | super(original); |
| 59 | this.src = original.src; |
| 60 | this.dst = original.dst; |
| 61 | } |
| 62 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 63 | // TODO probably want to remove this |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 64 | public LinkEvent(Link link) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | src = new SwitchPort(link.getSrcSwitch().getDpid(), |
| 66 | link.getSrcPort().getNumber()); |
| 67 | dst = new SwitchPort(link.getDstSwitch().getDpid(), |
| 68 | link.getDstPort().getNumber()); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 69 | // FIXME losing attributes here |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Creates the Link object. |
| 74 | * |
| 75 | * @param srcDpid source switch DPID |
| 76 | * @param srcPortNo source port number |
| 77 | * @param dstDpid destination switch DPID |
| 78 | * @param dstPortNo destination port number |
| 79 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 80 | public LinkEvent(Dpid srcDpid, PortNumber srcPortNo, |
| 81 | Dpid dstDpid, PortNumber dstPortNo) { |
| 82 | src = new SwitchPort(srcDpid, srcPortNo); |
| 83 | dst = new SwitchPort(dstDpid, dstPortNo); |
| 84 | } |
| 85 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Gets the source SwitchPort. |
| 88 | * |
| 89 | * @return source SwitchPort. |
| 90 | */ |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 91 | public SwitchPort getSrc() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | return src; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Gets the destination SwitchPort. |
| 97 | * |
| 98 | * @return destination SwitchPort. |
| 99 | */ |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 100 | public SwitchPort getDst() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | return dst; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | return "[LinkEvent " + src + "->" + dst + "]"; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 109 | public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2; |
| 110 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 111 | public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo, |
| 112 | Dpid dstDpid, PortNumber dstPortNo) { |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 113 | return getLinkID(srcDpid.value(), (long) srcPortNo.value(), |
| 114 | dstDpid.value(), (long) dstPortNo.value()); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 117 | public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo, |
| 118 | Long dstDpid, Long dstPortNo) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 119 | return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES) |
| 120 | .putChar('L') |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 121 | .put(PortEvent.getPortID(srcDpid, srcPortNo)) |
| 122 | .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 125 | public byte[] getID() { |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 126 | return getIDasByteBuffer().array(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public ByteBuffer getIDasByteBuffer() { |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 130 | return getLinkID(src.getDpid(), src.getPortNumber(), |
| 131 | dst.getDpid(), dst.getPortNumber()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 132 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 133 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 134 | @Override |
| 135 | public int hashCode() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | final int prime = 31; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 137 | int result = super.hashCode(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | result = prime * result + ((dst == null) ? 0 : dst.hashCode()); |
| 139 | result = prime * result + ((src == null) ? 0 : src.hashCode()); |
| 140 | return result; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 145 | if (this == obj) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 147 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 148 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 149 | if (obj == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 151 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 152 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 153 | if (getClass() != obj.getClass()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 155 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | LinkEvent other = (LinkEvent) obj; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 157 | |
| 158 | // compare attributes |
| 159 | if (!super.equals(obj)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 160 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 161 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 162 | |
| 163 | return Objects.equals(this.src, other.src) && |
| 164 | Objects.equals(this.dst, other.dst); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 165 | } |
Ray Milkey | 0f913a0 | 2014-04-07 20:58:17 -0700 | [diff] [blame] | 166 | } |