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