Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.topology; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.Map; |
| 7 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 9 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
Yuta HIGUCHI | 1cd9029 | 2014-04-03 14:31:10 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.datastore.serializers.Topology.PortProperty; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
| 12 | import net.onrc.onos.core.datastore.utils.KVObject; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.topology.PortEvent; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.util.Dpid; |
| 15 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 16 | |
| 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
| 20 | import com.esotericsoftware.kryo.Kryo; |
| 21 | import com.google.protobuf.ByteString; |
| 22 | import com.google.protobuf.InvalidProtocolBufferException; |
| 23 | |
| 24 | /** |
| 25 | * Port object in data store. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 27 | * Note: This class will not maintain invariants. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | * e.g., It will NOT automatically remove Links or Devices on Port, |
| 29 | * when deleting a Port. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 30 | */ |
| 31 | public class KVPort extends KVObject { |
| 32 | private static final Logger log = LoggerFactory.getLogger(KVPort.class); |
| 33 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 34 | private static final ThreadLocal<Kryo> PORT_KRYO = new ThreadLocal<Kryo>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 35 | @Override |
| 36 | protected Kryo initialValue() { |
| 37 | Kryo kryo = new Kryo(); |
| 38 | kryo.setRegistrationRequired(true); |
| 39 | kryo.setReferences(false); |
| 40 | kryo.register(byte[].class); |
| 41 | kryo.register(byte[][].class); |
| 42 | kryo.register(HashMap.class); |
| 43 | // TODO check if we should explicitly specify EnumSerializer |
| 44 | kryo.register(STATUS.class); |
| 45 | return kryo; |
| 46 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 49 | static final String PORT_TABLE_SUFFIX = ":Port"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 50 | |
| 51 | // must not re-order enum members, ordinal will be sent over wire |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Status. |
| 54 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 55 | public enum STATUS { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 56 | INACTIVE, ACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private final Long dpid; |
| 60 | private final Long number; |
| 61 | |
| 62 | private STATUS status; |
| 63 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Generate a PortID from port pair (dpid, number). |
| 66 | * |
| 67 | * @param dpid DPID of a switch it reside on. |
| 68 | * @param number port number of this |
| 69 | * @return PortID |
| 70 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 71 | public static byte[] getPortID(final Dpid dpid, final PortNumber number) { |
| 72 | return PortEvent.getPortID(dpid, number).array(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Generate a PortID from port pair (dpid, number). |
| 77 | * |
| 78 | * @param dpid DPID of a switch it reside on. |
| 79 | * @param number port number of this |
| 80 | * @return PortID |
| 81 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 82 | public static byte[] getPortID(final Long dpid, final Long number) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 83 | return PortEvent.getPortID(dpid, number).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Gets the port pair from PortID. |
| 88 | * |
| 89 | * @param key PortID |
| 90 | * @return port pair (dpid, number) |
| 91 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 92 | public static long[] getPortPairFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 93 | return getPortPairFromKey(ByteBuffer.wrap(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Gets the port pair from PortID. |
| 98 | * |
| 99 | * @param keyBuf PortID |
| 100 | * @return port pair (dpid, number) |
| 101 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 102 | public static long[] getPortPairFromKey(final ByteBuffer keyBuf) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 103 | if (keyBuf.getChar() != 'S') { |
| 104 | throw new IllegalArgumentException("Invalid Port key:" + keyBuf |
| 105 | + " " |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 106 | + ByteArrayUtil.toHexStringBuilder(keyBuf.array(), ":")); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 107 | } |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 108 | long[] pair = new long[2]; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 109 | pair[0] = keyBuf.getLong(); |
| 110 | if (keyBuf.getChar() != 'P') { |
| 111 | throw new IllegalArgumentException("Invalid Port key:" + keyBuf |
| 112 | + " " |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 113 | + ByteArrayUtil.toHexStringBuilder(keyBuf.array(), ":")); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 114 | } |
| 115 | pair[1] = keyBuf.getLong(); |
| 116 | return pair; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 117 | |
| 118 | } |
| 119 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Gets the port number from PortID. |
| 122 | * |
| 123 | * @param key PortID |
| 124 | * @return port number |
| 125 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 126 | public static long getDpidFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 127 | return getPortPairFromKey(key)[0]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Gets the dpid of an switch from PortID. |
| 132 | * |
| 133 | * @param key PortID |
| 134 | * @return dpid |
| 135 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 136 | public static long getNumberFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 137 | return getPortPairFromKey(key)[1]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 140 | /** |
| 141 | * KVPort constructor for default namespace. |
| 142 | * |
| 143 | * @param dpid DPID of the switch this port is on |
| 144 | * @param number port number of this port |
| 145 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 146 | public KVPort(final Long dpid, final Long number) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 147 | this(dpid, number, DEFAULT_NAMESPACE); |
| 148 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 149 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 150 | /** |
| 151 | * KVPort constructor for specified namespace. |
| 152 | * |
| 153 | * @param dpid DPID of the switch this port is on |
| 154 | * @param number port number of this port |
| 155 | * @param namespace namespace to create this object |
| 156 | */ |
| 157 | public KVPort(final Long dpid, final Long number, final String namespace) { |
| 158 | super(DataStoreClient.getClient() |
| 159 | .getTable(namespace + PORT_TABLE_SUFFIX), |
| 160 | getPortID(dpid, number), namespace); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 161 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 162 | this.dpid = dpid; |
| 163 | this.number = number; |
| 164 | this.status = STATUS.INACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 168 | * KVPort constructor for default namespace. |
| 169 | * |
| 170 | * @param dpid DPID of the switch this port is on |
| 171 | * @param number port number of this port |
| 172 | */ |
| 173 | public KVPort(final Dpid dpid, final PortNumber number) { |
| 174 | this(dpid, number, DEFAULT_NAMESPACE); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * KVPort constructor for specified namespace. |
| 179 | * |
| 180 | * @param dpid DPID of the switch this port is on |
| 181 | * @param number port number of this port |
| 182 | * @param namespace namespace to create this object |
| 183 | */ |
| 184 | public KVPort(final Dpid dpid, final PortNumber number, final String namespace) { |
| 185 | this(dpid.value(), (long) number.value(), namespace); |
| 186 | } |
| 187 | |
| 188 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 189 | * Gets an instance from PortID in default namespace. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 190 | * <p/> |
| 191 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 192 | * |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 193 | * @param key PortID |
| 194 | * @return {@link KVPort} instance |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 195 | */ |
| 196 | public static KVPort createFromKey(final byte[] key) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 197 | return createFromKey(key, DEFAULT_NAMESPACE); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Gets an instance from PortID in specified namespace. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 202 | * <p/> |
| 203 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 204 | * |
| 205 | * @param key PortID |
| 206 | * @param namespace namespace to create this object. |
| 207 | * @return {@link KVPort} instance |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 208 | */ |
| 209 | public static KVPort createFromKey(final byte[] key, final String namespace) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 210 | long[] pair = getPortPairFromKey(key); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 211 | return new KVPort(pair[0], pair[1], namespace); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Gets all the Ports in default namespace. |
| 216 | * |
| 217 | * @return Ports |
| 218 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 219 | public static Iterable<KVPort> getAllPorts() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 220 | return getAllPorts(DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Gets all the Ports in specified namespace. |
| 225 | * |
| 226 | * @param namespace namespace to iterate over |
| 227 | * @return Ports |
| 228 | */ |
| 229 | public static Iterable<KVPort> getAllPorts(final String namespace) { |
| 230 | return new PortEnumerator(namespace); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Utility class to provide Iterable interface. |
| 235 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 236 | public static class PortEnumerator implements Iterable<KVPort> { |
| 237 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 238 | private final String namespace; |
| 239 | |
| 240 | /** |
| 241 | * Constructor to iterate Ports in specified namespace. |
| 242 | * |
| 243 | * @param namespace namespace to iterate through |
| 244 | */ |
| 245 | public PortEnumerator(final String namespace) { |
| 246 | this.namespace = namespace; |
| 247 | } |
| 248 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 249 | @Override |
| 250 | public Iterator<KVPort> iterator() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 251 | return new PortIterator(namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 252 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Utility class to provide Iterator over all the Port objects. |
| 257 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 258 | public static class PortIterator extends AbstractObjectIterator<KVPort> { |
| 259 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Constructor to create an iterator to iterate all the Ports |
| 262 | * in specified namespace. |
| 263 | * |
| 264 | * @param namespace namespace to iterate through |
| 265 | */ |
| 266 | public PortIterator(final String namespace) { |
| 267 | super(DataStoreClient.getClient() |
| 268 | .getTable(namespace + PORT_TABLE_SUFFIX), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 269 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 270 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 271 | @Override |
| 272 | public KVPort next() { |
| 273 | IKVEntry o = enumerator.next(); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 274 | KVPort e = KVPort.createFromKey(o.getKey(), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 275 | e.deserialize(o.getValue(), o.getVersion()); |
| 276 | return e; |
| 277 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Gets the status. |
| 282 | * |
| 283 | * @return status |
| 284 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 285 | public STATUS getStatus() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 286 | return status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 289 | /** |
| 290 | * Sets the status. |
| 291 | * |
| 292 | * @param status new status |
| 293 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 294 | public void setStatus(final STATUS status) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 295 | this.status = status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Gets the DPID of the switch this port is on. |
| 300 | * |
| 301 | * @return DPID of the switch this port is on |
| 302 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 303 | public Long getDpid() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 304 | return dpid; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 307 | /** |
| 308 | * Gets the port number of this port. |
| 309 | * |
| 310 | * @return port number of this port |
| 311 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 312 | public Long getNumber() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 313 | return number; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Gets the PortID. |
| 318 | * |
| 319 | * @return PortID |
| 320 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 321 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 322 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 327 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 328 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 329 | PortProperty.Builder port = PortProperty.newBuilder(); |
| 330 | port.setDpid(dpid); |
| 331 | port.setNumber(number); |
| 332 | port.setStatus(status.ordinal()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 333 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 334 | if (!map.isEmpty()) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 335 | byte[] propMaps = serializePropertyMap(PORT_KRYO.get(), map); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 336 | port.setValue(ByteString.copyFrom(propMaps)); |
| 337 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 338 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 339 | return port.build().toByteArray(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Override |
| 343 | protected boolean deserialize(final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 344 | try { |
| 345 | boolean success = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 346 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 347 | PortProperty port = PortProperty.parseFrom(bytes); |
| 348 | byte[] props = port.getValue().toByteArray(); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 349 | success &= deserializePropertyMap(PORT_KRYO.get(), props); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 350 | this.status = STATUS.values()[port.getStatus()]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 351 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 352 | return success; |
| 353 | } catch (InvalidProtocolBufferException e) { |
| 354 | log.error("Deserializing Port: " + this + " failed.", e); |
| 355 | return false; |
| 356 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 361 | // TODO output all properties? |
| 362 | return "[" + this.getClass().getSimpleName() |
| 363 | + " 0x" + Long.toHexString(dpid) + "@" + number |
| 364 | + " STATUS:" + status + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 365 | } |
| 366 | } |