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.LinkProperty; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.datastore.utils.KVObject; |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.topology.LinkData; |
| 13 | import net.onrc.onos.core.topology.PortData; |
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 | * Link object in data store. |
| 26 | */ |
| 27 | public class KVLink extends KVObject { |
| 28 | private static final Logger log = LoggerFactory.getLogger(KVLink.class); |
| 29 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 30 | private static final ThreadLocal<Kryo> LINK_KRYO = new ThreadLocal<Kryo>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 31 | @Override |
| 32 | protected Kryo initialValue() { |
| 33 | Kryo kryo = new Kryo(); |
| 34 | kryo.setRegistrationRequired(true); |
| 35 | kryo.setReferences(false); |
| 36 | kryo.register(byte[].class); |
| 37 | kryo.register(byte[][].class); |
| 38 | kryo.register(HashMap.class); |
| 39 | // TODO check if we should explicitly specify EnumSerializer |
| 40 | kryo.register(STATUS.class); |
| 41 | return kryo; |
| 42 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 45 | // TODO eliminate this class and util SwitchPort. |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Internal data structure to represent a port on a switch. |
| 48 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 49 | public static class SwitchPort { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 50 | public final Long dpid; |
| 51 | public final Long number; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 52 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Constructor. |
| 55 | * |
| 56 | * @param dpid datapath ID of this switch port |
| 57 | * @param number port number of this port on switch({@code dpid}) |
| 58 | */ |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 59 | public SwitchPort(final Long dpid, final Long number) { |
| 60 | this.dpid = dpid; |
| 61 | this.number = number; |
| 62 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 63 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 64 | /** |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 65 | * Constructor. |
| 66 | * |
Praseed Balakrishnan | ccb3583 | 2014-07-09 11:04:29 -0700 | [diff] [blame] | 67 | * @param srcDpid datapath ID of this switch port |
| 68 | * @param srcPortNo port number of this port on switch({@code dpid}) |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 69 | */ |
| 70 | public SwitchPort(final Dpid srcDpid, final PortNumber srcPortNo) { |
| 71 | this.dpid = srcDpid.value(); |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 72 | this.number = srcPortNo.value(); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 76 | * Gets the PortID of a port this object represent. |
| 77 | * |
| 78 | * @return PortID |
| 79 | */ |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 80 | public byte[] getPortID() { |
| 81 | return KVPort.getPortID(dpid, number); |
| 82 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 83 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Gets the SwitchID of a switch this object represent. |
| 86 | * |
| 87 | * @return SwitchID |
| 88 | */ |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 89 | public byte[] getSwitchID() { |
| 90 | return KVSwitch.getSwitchID(dpid); |
| 91 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 92 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public String toString() { |
| 95 | return "(" + Long.toHexString(dpid) + "@" + number + ")"; |
| 96 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 97 | |
| 98 | } |
| 99 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 100 | static final String LINK_TABLE_SUFFIX = ":Link"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 101 | |
| 102 | // must not re-order enum members, ordinal will be sent over wire |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Status. |
| 105 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 106 | public enum STATUS { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 107 | INACTIVE, ACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private final SwitchPort src; |
| 111 | private final SwitchPort dst; |
| 112 | private STATUS status; |
| 113 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Generate a LinkID from Link 4-tuples. |
| 116 | * |
| 117 | * @param srcDpid source DPID |
| 118 | * @param srcPortNo source port number |
| 119 | * @param dstDpid destination DPID |
| 120 | * @param dstPortNo destination port number |
| 121 | * @return LinkID |
| 122 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 123 | public static byte[] getLinkID(final Dpid srcDpid, final PortNumber srcPortNo, |
| 124 | final Dpid dstDpid, final PortNumber dstPortNo) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 125 | return LinkData.getLinkID(srcDpid, srcPortNo, |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 126 | dstDpid, dstPortNo).array(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Generate a LinkID from Link 4-tuples. |
| 131 | * |
| 132 | * @param srcDpid source DPID |
| 133 | * @param srcPortNo source port number |
| 134 | * @param dstDpid destination DPID |
| 135 | * @param dstPortNo destination port number |
| 136 | * @return LinkID |
| 137 | */ |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 138 | public static byte[] getLinkID(final Long srcDpid, final Long srcPortNo, |
| 139 | final Long dstDpid, final Long dstPortNo) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 140 | return LinkData.getLinkID(srcDpid, srcPortNo, dstDpid, |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 141 | dstPortNo).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Gets Link 4-tuples from LinkID. |
| 146 | * |
| 147 | * @param key LinkID |
| 148 | * @return Link 4-tuple: [src DPID, src PortNo, dst DPID, dst PortNo] |
| 149 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 150 | public static long[] getLinkTupleFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 151 | return getLinkTupleFromKey(ByteBuffer.wrap(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Gets Link 4-tuples from LinkID. |
| 156 | * |
| 157 | * @param keyBuf LinkID |
| 158 | * @return Link 4-tuple: [src DPID, src PortNo, dst DPID, dst PortNo] |
| 159 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 160 | public static long[] getLinkTupleFromKey(final ByteBuffer keyBuf) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 161 | if (keyBuf.getChar() != 'L') { |
| 162 | throw new IllegalArgumentException("Invalid Link key"); |
| 163 | } |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 164 | final long[] srcPortPair = KVPort.getPortPairFromKey(keyBuf.slice()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 165 | keyBuf.position(2 + PortData.PORTID_BYTES); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 166 | final long[] dstPortPair = KVPort.getPortPairFromKey(keyBuf.slice()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 167 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 168 | long[] tuple = new long[4]; |
| 169 | tuple[0] = srcPortPair[0]; |
| 170 | tuple[1] = srcPortPair[1]; |
| 171 | tuple[2] = dstPortPair[0]; |
| 172 | tuple[3] = dstPortPair[1]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 173 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 174 | return tuple; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * KVLink constructor for default namespace. |
| 180 | * |
| 181 | * @param srcDpid source DPID |
| 182 | * @param srcPortNo source port number |
| 183 | * @param dstDpid destination DPID |
| 184 | * @param dstPortNo destination port number |
| 185 | */ |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 186 | public KVLink(final Long srcDpid, final Long srcPortNo, |
| 187 | final Long dstDpid, final Long dstPortNo) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 188 | this(srcDpid, srcPortNo, dstDpid, dstPortNo, DEFAULT_NAMESPACE); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * KVLink constructor for specified namespace. |
| 193 | * |
| 194 | * @param srcDpid source DPID |
| 195 | * @param srcPortNo source port number |
| 196 | * @param dstDpid destination DPID |
| 197 | * @param dstPortNo destination port number |
| 198 | * @param namespace namespace to create this object |
| 199 | */ |
| 200 | public KVLink(final Long srcDpid, final Long srcPortNo, |
| 201 | final Long dstDpid, final Long dstPortNo, |
| 202 | final String namespace) { |
| 203 | super(DataStoreClient.getClient() |
| 204 | .getTable(namespace + LINK_TABLE_SUFFIX), |
| 205 | getLinkID(srcDpid, srcPortNo, dstDpid, dstPortNo), |
| 206 | namespace); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 207 | |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 208 | src = new SwitchPort(srcDpid, srcPortNo); |
| 209 | dst = new SwitchPort(dstDpid, dstPortNo); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 210 | status = STATUS.INACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 214 | * KVLink constructor for default namespace. |
| 215 | * |
| 216 | * @param srcDpid source DPID |
| 217 | * @param srcPortNo source port number |
| 218 | * @param dstDpid destination DPID |
| 219 | * @param dstPortNo destination port number |
| 220 | */ |
| 221 | public KVLink(final Dpid srcDpid, final PortNumber srcPortNo, |
| 222 | final Dpid dstDpid, final PortNumber dstPortNo) { |
| 223 | this(srcDpid, srcPortNo, dstDpid, dstPortNo, DEFAULT_NAMESPACE); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * KVLink constructor for specified namespace. |
| 228 | * |
| 229 | * @param srcDpid source DPID |
| 230 | * @param srcPortNo source port number |
| 231 | * @param dstDpid destination DPID |
| 232 | * @param dstPortNo destination port number |
| 233 | * @param namespace namespace to create this object |
| 234 | */ |
| 235 | public KVLink(final Dpid srcDpid, final PortNumber srcPortNo, |
| 236 | final Dpid dstDpid, final PortNumber dstPortNo, |
| 237 | final String namespace) { |
| 238 | super(DataStoreClient.getClient() |
| 239 | .getTable(namespace + LINK_TABLE_SUFFIX), |
| 240 | getLinkID(srcDpid, srcPortNo, dstDpid, dstPortNo), |
| 241 | namespace); |
| 242 | |
| 243 | src = new SwitchPort(srcDpid, srcPortNo); |
| 244 | dst = new SwitchPort(dstDpid, dstPortNo); |
| 245 | status = STATUS.INACTIVE; |
| 246 | } |
| 247 | |
| 248 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 249 | * Gets an instance from LinkID in default namespace. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 250 | * <p/> |
| 251 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 252 | * |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 253 | * @param key LinkID |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 254 | * @return KVLink instance |
| 255 | */ |
| 256 | public static KVLink createFromKey(final byte[] key) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 257 | return createFromKey(key, DEFAULT_NAMESPACE); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Gets an instance from LinkID in specified namespace. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 262 | * <p/> |
| 263 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 264 | * |
| 265 | * @param key LinkID |
| 266 | * @param namespace namespace to create this object in |
| 267 | * @return KVLink instance |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 268 | */ |
| 269 | public static KVLink createFromKey(final byte[] key, final String namespace) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 270 | long[] linkTuple = getLinkTupleFromKey(key); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 271 | return new KVLink(linkTuple[0], linkTuple[1], |
| 272 | linkTuple[2], linkTuple[3], |
| 273 | namespace); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 276 | /** |
| 277 | * Gets all the Links in default namespace. |
| 278 | * |
| 279 | * @return Links |
| 280 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 281 | public static Iterable<KVLink> getAllLinks() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 282 | return getAllLinks(DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 285 | /** |
| 286 | * Gets all the Links in specified namespace. |
| 287 | * |
| 288 | * @param namespace namespace to iterate over |
| 289 | * @return Links |
| 290 | */ |
| 291 | public static Iterable<KVLink> getAllLinks(final String namespace) { |
| 292 | return new LinkEnumerator(namespace); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Utility class to provide Iterable interface. |
| 297 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 298 | public static class LinkEnumerator implements Iterable<KVLink> { |
| 299 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 300 | private final String namespace; |
| 301 | |
| 302 | /** |
| 303 | * Constructor to iterate Links in specified namespace. |
| 304 | * |
| 305 | * @param namespace namespace to iterate through |
| 306 | */ |
| 307 | public LinkEnumerator(final String namespace) { |
| 308 | this.namespace = namespace; |
| 309 | } |
| 310 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public Iterator<KVLink> iterator() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 313 | return new LinkIterator(namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 314 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Utility class to provide Iterator over all the Link objects. |
| 319 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 320 | public static class LinkIterator extends AbstractObjectIterator<KVLink> { |
| 321 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 322 | /** |
| 323 | * Constructor to create an iterator to iterate all the Links |
| 324 | * in specified namespace. |
| 325 | * |
| 326 | * @param namespace namespace to iterate through |
| 327 | */ |
| 328 | public LinkIterator(final String namespace) { |
| 329 | super(DataStoreClient.getClient() |
| 330 | .getTable(namespace + LINK_TABLE_SUFFIX), |
| 331 | namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 332 | } |
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 | @Override |
| 335 | public KVLink next() { |
| 336 | IKVEntry o = enumerator.next(); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 337 | KVLink e = KVLink.createFromKey(o.getKey(), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 338 | e.deserialize(o.getValue(), o.getVersion()); |
| 339 | return e; |
| 340 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 343 | /** |
| 344 | * Gets the status. |
| 345 | * |
| 346 | * @return status |
| 347 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 348 | public STATUS getStatus() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 349 | return status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Sets the status. |
| 354 | * |
| 355 | * @param status new status |
| 356 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 357 | public void setStatus(final STATUS status) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 358 | this.status = status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 361 | /** |
| 362 | * Gets the source SwitchPort object. |
| 363 | * |
| 364 | * @return source SwitchPort object |
| 365 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 366 | public SwitchPort getSrc() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 367 | return src; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 370 | /** |
| 371 | * Gets the destination SwitchPort object. |
| 372 | * |
| 373 | * @return destination SwitchPort object |
| 374 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 375 | public SwitchPort getDst() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 376 | return dst; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 379 | /** |
| 380 | * Gets the LinkID of this object. |
| 381 | * |
| 382 | * @return LinkID |
| 383 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 384 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 385 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 390 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 391 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 392 | LinkProperty.Builder link = LinkProperty.newBuilder(); |
| 393 | link.setSrcSwId(ByteString.copyFrom(src.getSwitchID())); |
| 394 | link.setSrcPortId(ByteString.copyFrom(src.getPortID())); |
| 395 | link.setDstSwId(ByteString.copyFrom(dst.getSwitchID())); |
| 396 | link.setDstPortId(ByteString.copyFrom(dst.getPortID())); |
| 397 | link.setStatus(status.ordinal()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 398 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 399 | if (!map.isEmpty()) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 400 | byte[] propMaps = serializePropertyMap(LINK_KRYO.get(), map); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 401 | link.setValue(ByteString.copyFrom(propMaps)); |
| 402 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 403 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 404 | return link.build().toByteArray(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | protected boolean deserialize(final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 409 | try { |
| 410 | boolean success = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 411 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 412 | LinkProperty link = LinkProperty.parseFrom(bytes); |
| 413 | byte[] props = link.getValue().toByteArray(); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 414 | success &= deserializePropertyMap(LINK_KRYO.get(), props); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 415 | this.status = STATUS.values()[link.getStatus()]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 416 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 417 | return success; |
| 418 | } catch (InvalidProtocolBufferException e) { |
| 419 | log.error("Deserializing Link: " + this + " failed.", e); |
| 420 | return false; |
| 421 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 426 | // TODO output all properties? |
| 427 | return "[" + this.getClass().getSimpleName() |
| 428 | + " " + src + "->" + dst + " STATUS:" + status + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 429 | } |
| 430 | } |