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 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.topology.web.serializers.PortEventSerializer; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.util.Dpid; |
| 5 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 7 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 8 | import static com.google.common.base.Preconditions.checkNotNull; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 9 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 10 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 11 | import java.nio.ByteBuffer; |
Sho SHIMIZU | f34f150 | 2014-06-13 13:48:00 -0700 | [diff] [blame] | 12 | import java.util.Objects; |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 13 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 14 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 15 | * Self-contained Port event Object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 16 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 17 | * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot? |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 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 | */ |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 21 | @JsonSerialize(using = PortEventSerializer.class) |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 22 | public class PortEvent extends TopologyElement<PortEvent> { |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 23 | public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 24 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 25 | private final SwitchPort id; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 26 | // TODO Add Hardware Address |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 27 | |
| 28 | // TODO: Where should the attribute names be defined? |
| 29 | /** |
| 30 | * Attribute name for description. |
| 31 | */ |
| 32 | public static final String DESCRIPTION = "description"; |
| 33 | |
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 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 36 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 37 | @Deprecated |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 38 | protected PortEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | id = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 42 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 43 | * Constructor for given SwitchPort. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 44 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 45 | * @param switchPort the SwitchPort to identify the port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 46 | */ |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 47 | public PortEvent(SwitchPort switchPort) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 48 | this.id = checkNotNull(switchPort); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 49 | } |
| 50 | |
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 | * Constructor for given switch DPID and port number. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 53 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 54 | * @param dpid the DPID of the switch the port belongs to |
| 55 | * @param number the PortNumber to identify the port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 56 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 57 | public PortEvent(Dpid dpid, PortNumber number) { |
| 58 | this.id = new SwitchPort(dpid, number); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 61 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 62 | * Copy constructor. |
| 63 | * <p> |
| 64 | * Creates an unfrozen copy of the given PortEvent object. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 65 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 66 | * @param original the object to make copy of |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 67 | */ |
| 68 | public PortEvent(PortEvent original) { |
| 69 | super(original); |
| 70 | this.id = original.id; |
| 71 | } |
| 72 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Gets the SwitchPort identifying this port. |
| 75 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 76 | * @return the SwitchPort identifying this port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 77 | */ |
| 78 | public SwitchPort getSwitchPort() { |
| 79 | return id; |
| 80 | } |
| 81 | |
| 82 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 83 | * Gets the DPID of the switch this port belongs to. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 84 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 85 | * @return the DPID of the switch this port belongs to |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 86 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 87 | public Dpid getDpid() { |
| 88 | return id.getDpid(); |
| 89 | } |
| 90 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Gets the port number. |
| 93 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 94 | * @return the port number |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 95 | */ |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 96 | public PortNumber getPortNumber() { |
| 97 | return id.getPortNumber(); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Computes the port ID for a given switch DPID and a port number. |
| 102 | * |
| 103 | * @param dpid the switch DPID to use |
| 104 | * @param number the port number to use |
| 105 | * @return the port ID as a ByteBuffer |
| 106 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 107 | public static ByteBuffer getPortID(Dpid dpid, PortNumber number) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 108 | checkNotNull(dpid); |
| 109 | checkNotNull(number); |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 110 | return getPortID(dpid.value(), number.value()); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Computes the port ID for a given switch DPID and a port number. |
| 115 | * <p> |
| 116 | * TODO: This method should be removed and replaced with the corresponding |
| 117 | * getPortID(Dpid, PortNumber) method. |
| 118 | * |
| 119 | * @param dpid the switch DPID to use |
| 120 | * @param number the port number to use |
| 121 | * @return the port ID as a ByteBuffer |
| 122 | */ |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 123 | public static ByteBuffer getPortID(Long dpid, Long number) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | if (dpid == null) { |
| 125 | throw new IllegalArgumentException("dpid cannot be null"); |
| 126 | } |
| 127 | if (number == null) { |
| 128 | throw new IllegalArgumentException("number cannot be null"); |
| 129 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 130 | return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES) |
| 131 | .putChar('S').putLong(dpid) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | .putChar('P').putLong(number).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public Dpid getOriginDpid() { |
| 137 | return this.id.getDpid(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 140 | @Override |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 141 | public ByteBuffer getIDasByteBuffer() { |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 142 | return getPortID(getDpid(), getPortNumber()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 143 | } |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 144 | |
| 145 | @Override |
| 146 | public int hashCode() { |
| 147 | return 31 * super.hashCode() + Objects.hashCode(id); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean equals(Object o) { |
| 152 | if (this == o) { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | if (o == null || getClass() != o.getClass()) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | // Compare attributes |
| 161 | if (!super.equals(o)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | PortEvent other = (PortEvent) o; |
| 166 | return Objects.equals(this.id, other.id); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public String toString() { |
| 171 | return "[PortEvent " + getDpid() + "@" + getPortNumber() + "]"; |
| 172 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 173 | } |