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 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 12 | import static com.google.common.base.Preconditions.checkNotNull; |
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> { |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 25 | public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 26 | |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 27 | private final LinkTuple id; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 28 | // TODO add LastSeenTime, Capacity if appropriate |
Yuta HIGUCHI | cb8455e | 2014-07-15 18:42:10 -0700 | [diff] [blame] | 29 | protected static final Double DEFAULT_CAPACITY = Double.POSITIVE_INFINITY; |
| 30 | private Double capacity = DEFAULT_CAPACITY; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 31 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 32 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 33 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 34 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 35 | @Deprecated |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 36 | protected LinkEvent() { |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 37 | id = null; |
| 38 | } |
| 39 | |
| 40 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 41 | * Constructor for given LinkTuple. |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 42 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 43 | * @param id the link tuple to identify the link |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 44 | */ |
| 45 | public LinkEvent(LinkTuple id) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 46 | this.id = checkNotNull(id); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 49 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 50 | * Constructor for given source and destination switch ports. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 51 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 52 | * @param src the source SwitchPort to use |
| 53 | * @param dst the destination SwitchPort to use |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 54 | */ |
| 55 | public LinkEvent(SwitchPort src, SwitchPort dst) { |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 56 | this(new LinkTuple(src, dst)); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 60 | * Constructor for a given Link object. |
| 61 | * <p> |
| 62 | * TODO: This constructor should probably be removed. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 63 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 64 | * @param link the Link object to use. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 65 | */ |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 66 | public LinkEvent(Link link) { |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 67 | this(link.getLinkTuple()); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 68 | // FIXME losing attributes here |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 71 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 72 | * Copy constructor. |
| 73 | * <p> |
| 74 | * Creates an unfrozen copy of the given LinkEvent object. |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 75 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 76 | * @param original the object ot make copy of |
| 77 | */ |
| 78 | public LinkEvent(LinkEvent original) { |
| 79 | super(original); |
| 80 | this.id = original.id; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets the LinkTuple that identifies this link. |
| 85 | * |
| 86 | * @return the LinkTuple identifying this link |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 87 | */ |
| 88 | public LinkTuple getLinkTuple() { |
| 89 | return id; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 92 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 93 | * Gets the link source SwitchPort. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 94 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 95 | * @return the link source SwitchPort |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 96 | */ |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 97 | public SwitchPort getSrc() { |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 98 | return getLinkTuple().getSrc(); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 101 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 102 | * Gets the link destination SwitchPort. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 103 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 104 | * @return the link destination SwitchPort |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 105 | */ |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 106 | public SwitchPort getDst() { |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 107 | return getLinkTuple().getDst(); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Yuta HIGUCHI | cb8455e | 2014-07-15 18:42:10 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Gets the link capacity. |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 112 | * <p> |
Yuta HIGUCHI | cb8455e | 2014-07-15 18:42:10 -0700 | [diff] [blame] | 113 | * TODO: What is the unit? |
| 114 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 115 | * @return the link capacity |
Yuta HIGUCHI | cb8455e | 2014-07-15 18:42:10 -0700 | [diff] [blame] | 116 | */ |
| 117 | public Double getCapacity() { |
| 118 | return capacity; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets the link capacity. |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 123 | * <p> |
Yuta HIGUCHI | cb8455e | 2014-07-15 18:42:10 -0700 | [diff] [blame] | 124 | * TODO: What is the unit? |
| 125 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 126 | * @param capacity the link capacity to set |
Yuta HIGUCHI | cb8455e | 2014-07-15 18:42:10 -0700 | [diff] [blame] | 127 | */ |
| 128 | void setCapacity(Double capacity) { |
| 129 | if (isFrozen()) { |
| 130 | throw new IllegalStateException("Tried to modify frozen instance: " + this); |
| 131 | } |
| 132 | |
| 133 | this.capacity = capacity; |
| 134 | } |
| 135 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Computes the link ID for given source and destination switch DPID and |
| 138 | * port numbers. |
| 139 | * |
| 140 | * @param srcDpid the source switch DPID to use |
| 141 | * @param srcPortNo the source port number to use |
| 142 | * @param dstDpid the destination switch DPID to use |
| 143 | * @param dstPortNo the destination port number to use |
| 144 | * @return the link ID as a ByteBuffer |
| 145 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 146 | public static ByteBuffer getLinkID(Dpid srcDpid, PortNumber srcPortNo, |
| 147 | Dpid dstDpid, PortNumber dstPortNo) { |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 148 | return getLinkID(srcDpid.value(), srcPortNo.value(), |
| 149 | dstDpid.value(), dstPortNo.value()); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Computes the link ID for given source and destination switch DPID and |
| 154 | * port numbers. |
| 155 | * <p> |
| 156 | * TODO: This method should be removed and replaced with the corresponding |
| 157 | * getLinkID(Dpid, PortNumber, Dpid, PortNumber) method. |
| 158 | * |
| 159 | * @param srcDpid the source switch DPID to use |
| 160 | * @param srcPortNo the source port number to use |
| 161 | * @param dstDpid the destination switch DPID to use |
| 162 | * @param dstPortNo the destination port number to use |
| 163 | * @return the link ID as a ByteBuffer |
| 164 | */ |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 165 | public static ByteBuffer getLinkID(Long srcDpid, Long srcPortNo, |
| 166 | Long dstDpid, Long dstPortNo) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 167 | return (ByteBuffer) ByteBuffer.allocate(LinkEvent.LINKID_BYTES) |
| 168 | .putChar('L') |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 169 | .put(PortEvent.getPortID(srcDpid, srcPortNo)) |
| 170 | .put(PortEvent.getPortID(dstDpid, dstPortNo)).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 173 | @Override |
| 174 | public Dpid getOriginDpid() { |
| 175 | return this.id.getDst().getDpid(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 178 | @Override |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 179 | public ByteBuffer getIDasByteBuffer() { |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 180 | return getLinkID(getSrc().getDpid(), getSrc().getPortNumber(), |
| 181 | getDst().getDpid(), getDst().getPortNumber()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 182 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 183 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 184 | @Override |
| 185 | public int hashCode() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 186 | return 31 * super.hashCode() + Objects.hashCode(id); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 190 | public boolean equals(Object o) { |
| 191 | if (this == o) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 193 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 194 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 195 | if (o == null || getClass() != o.getClass()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 196 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 197 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 198 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 199 | // Compare attributes |
| 200 | if (!super.equals(o)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 201 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 202 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 203 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 204 | LinkEvent other = (LinkEvent) o; |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 205 | return Objects.equals(this.id, other.id); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 206 | } |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 207 | |
| 208 | @Override |
| 209 | public String toString() { |
| 210 | return "[LinkEvent " + getSrc() + "->" + getDst() + "]"; |
| 211 | } |
Ray Milkey | 0f913a0 | 2014-04-07 20:58:17 -0700 | [diff] [blame] | 212 | } |