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