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