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; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Iterator; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
| 10 | import java.util.TreeSet; |
| 11 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 13 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.datastore.serializers.Device.DeviceProperty; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.datastore.topology.KVLink.STATUS; |
| 16 | import net.onrc.onos.core.datastore.utils.ByteArrayComparator; |
| 17 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
| 18 | import net.onrc.onos.core.datastore.utils.KVObject; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.topology.DeviceEvent; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 20 | |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import com.esotericsoftware.kryo.Kryo; |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 25 | import com.google.protobuf.ByteString; |
| 26 | import com.google.protobuf.InvalidProtocolBufferException; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Device object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 31 | */ |
| 32 | public class KVDevice extends KVObject { |
| 33 | private static final Logger log = LoggerFactory.getLogger(KVDevice.class); |
| 34 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 35 | private static final ThreadLocal<Kryo> DEVICE_KRYO = new ThreadLocal<Kryo>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 36 | @Override |
| 37 | protected Kryo initialValue() { |
| 38 | Kryo kryo = new Kryo(); |
| 39 | kryo.setRegistrationRequired(true); |
| 40 | kryo.setReferences(false); |
| 41 | kryo.register(byte[].class); |
| 42 | kryo.register(byte[][].class); |
| 43 | kryo.register(HashMap.class); |
| 44 | // TODO check if we should explicitly specify EnumSerializer |
| 45 | kryo.register(STATUS.class); |
| 46 | return kryo; |
| 47 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 50 | static final String DEVICE_TABLE_SUFFIX = ":Device"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 51 | |
| 52 | private final byte[] mac; |
| 53 | private TreeSet<byte[]> portIds; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 55 | /** |
| 56 | * Generate a DeviceID from MAC address. |
| 57 | * <p/> |
| 58 | * We're assuming MAC address can be an unique identifier for Device. |
| 59 | * |
| 60 | * @param mac MAC address |
| 61 | * @return DeviceID |
| 62 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 63 | public static byte[] getDeviceID(final byte[] mac) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 64 | return DeviceEvent.getDeviceID(mac).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 67 | /** |
| 68 | * Gets the MAC address from DeviceID. |
| 69 | * |
| 70 | * @param key DeviceID |
| 71 | * @return MAC address |
| 72 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 73 | public static byte[] getMacFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 74 | ByteBuffer keyBuf = ByteBuffer.wrap(key); |
| 75 | if (keyBuf.getChar() != 'D') { |
| 76 | throw new IllegalArgumentException("Invalid Device key"); |
| 77 | } |
| 78 | byte[] mac = new byte[keyBuf.remaining()]; |
| 79 | keyBuf.get(mac); |
| 80 | return mac; |
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 | * KVDevice constructor for default namespace. |
| 85 | * |
| 86 | * @param mac MAC address |
| 87 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 88 | public KVDevice(final byte[] mac) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 89 | this(mac, DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 93 | * KVDevice constructor for specified namespace. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 94 | * |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 95 | * @param mac MAC address |
| 96 | * @param namespace namespace to create this object |
| 97 | */ |
| 98 | public KVDevice(final byte[] mac, final String namespace) { |
| 99 | super(DataStoreClient.getClient() |
| 100 | .getTable(namespace + DEVICE_TABLE_SUFFIX), |
| 101 | getDeviceID(mac), namespace); |
| 102 | |
| 103 | this.mac = mac.clone(); |
| 104 | this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Gets an instance from DeviceID in default namespace. |
| 109 | * |
| 110 | * @param key DeviceID |
| 111 | * @return KVDevice instance |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | * @note You need to call `read()` to get the DB content. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 113 | */ |
| 114 | public static KVDevice createFromKey(final byte[] key) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 115 | return createFromKey(key, DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 118 | /** |
| 119 | * Gets an instance from DeviceID in specified namespace. |
| 120 | * |
| 121 | * @param key DeviceID |
| 122 | * @param namespace namespace to create this object in |
| 123 | * @return KVDevice instance |
| 124 | * @note You need to call `read()` to get the DB content. |
| 125 | */ |
| 126 | public static KVDevice createFromKey(final byte[] key, final String namespace) { |
| 127 | return new KVDevice(getMacFromKey(key), namespace); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Gets all the Devices in default namespace. |
| 132 | * |
| 133 | * @return Devices |
| 134 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 135 | public static Iterable<KVDevice> getAllDevices() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 136 | return new DeviceEnumerator(DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 139 | /** |
| 140 | * Gets all the Devices in specified namespace. |
| 141 | * |
| 142 | * @param namespace namespace to iterate over |
| 143 | * @return Devices |
| 144 | */ |
| 145 | public static Iterable<KVDevice> getAllDevices(final String namespace) { |
| 146 | return new DeviceEnumerator(namespace); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Utility class to provide Iterable interface. |
| 151 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 152 | public static class DeviceEnumerator implements Iterable<KVDevice> { |
| 153 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 154 | private final String namespace; |
| 155 | |
| 156 | /** |
| 157 | * Constructor to iterate Links in specified namespace. |
| 158 | * |
| 159 | * @param namespace namespace to iterate through |
| 160 | */ |
| 161 | public DeviceEnumerator(final String namespace) { |
| 162 | this.namespace = namespace; |
| 163 | } |
| 164 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public Iterator<KVDevice> iterator() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 167 | return new DeviceIterator(namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 168 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 171 | /** |
| 172 | * Utility class to provide Iterator over all the Device objects. |
| 173 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 174 | public static class DeviceIterator extends AbstractObjectIterator<KVDevice> { |
| 175 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 176 | /** |
| 177 | * Constructor to create an iterator to iterate all the Devices |
| 178 | * in specified namespace. |
| 179 | * |
| 180 | * @param namespace namespace to iterate through |
| 181 | */ |
| 182 | public DeviceIterator(final String namespace) { |
| 183 | super(DataStoreClient.getClient() |
| 184 | .getTable(namespace + DEVICE_TABLE_SUFFIX), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 185 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 186 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 187 | @Override |
| 188 | public KVDevice next() { |
| 189 | IKVEntry o = enumerator.next(); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 190 | KVDevice e = KVDevice.createFromKey(o.getKey(), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 191 | e.deserialize(o.getValue(), o.getVersion()); |
| 192 | return e; |
| 193 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 196 | /** |
| 197 | * Gets the MAC address. |
| 198 | * |
| 199 | * @return MAC address |
| 200 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 201 | public byte[] getMac() { |
Yuta HIGUCHI | ce7e7f8 | 2014-04-15 21:37:38 -0700 | [diff] [blame] | 202 | return mac.clone(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 205 | /** |
| 206 | * Gets the DeviceID. |
| 207 | * |
| 208 | * @return DeviceID |
| 209 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 210 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 211 | return getKey(); |
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 | * Add a port to this Device's attachment points. |
| 216 | * |
| 217 | * @param portId PortID of the port which this Device is attached |
| 218 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 219 | public void addPortId(final byte[] portId) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 220 | portIds.add(portId.clone()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 224 | * Remove a port from this Device's attachment points. |
| 225 | * |
| 226 | * @param portId PortID to remove |
| 227 | */ |
| 228 | public void removePortId(final byte[] portId) { |
| 229 | portIds.remove(portId); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Empty this Device's attachment points. |
| 234 | */ |
| 235 | public void emptyPortIds() { |
| 236 | portIds.clear(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Add ports to this Device's attachment points. |
| 241 | * |
| 242 | * @param newPortIds PortIDs which this Device is attached |
| 243 | */ |
| 244 | public void addAllToPortIds(final Collection<byte[]> newPortIds) { |
| 245 | // TODO: Should we copy each portId, or reference is OK. |
| 246 | portIds.addAll(newPortIds); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Gets all the PortIDs which this Device is attached. |
| 251 | * |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 252 | * @return Unmodifiable Set view of all the PortIds; |
| 253 | */ |
| 254 | public Set<byte[]> getAllPortIds() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 255 | return Collections.unmodifiableSet(portIds); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 260 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 261 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 262 | DeviceProperty.Builder dev = DeviceProperty.newBuilder(); |
| 263 | |
| 264 | dev.setMac(ByteString.copyFrom(mac)); |
| 265 | for (byte[] port : portIds) { |
| 266 | dev.addPortIds(ByteString.copyFrom(port)); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 267 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 268 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 269 | if (!map.isEmpty()) { |
| 270 | byte[] propMaps = serializePropertyMap(DEVICE_KRYO.get(), map); |
| 271 | dev.setValue(ByteString.copyFrom(propMaps)); |
| 272 | } |
| 273 | |
| 274 | return dev.build().toByteArray(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override |
| 278 | protected boolean deserialize(final byte[] bytes) { |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 279 | |
| 280 | try { |
| 281 | boolean success = true; |
| 282 | |
| 283 | DeviceProperty dev = DeviceProperty.parseFrom(bytes); |
| 284 | for (ByteString portId : dev.getPortIdsList()) { |
| 285 | this.addPortId(portId.toByteArray()); |
| 286 | } |
| 287 | byte[] props = dev.getValue().toByteArray(); |
| 288 | success &= deserializePropertyMap(DEVICE_KRYO.get(), props); |
| 289 | return success; |
| 290 | } catch (InvalidProtocolBufferException e) { |
| 291 | log.error("Deserializing Device: " + this + " failed.", e); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 292 | return false; |
| 293 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 298 | // TODO output all properties? |
| 299 | return "[" + this.getClass().getSimpleName() |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 300 | + " " + ByteArrayUtil.toHexStringBuilder(mac, ":") + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 301 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 302 | } |