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. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 109 | * <p/> |
| 110 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 111 | * |
| 112 | * @param key DeviceID |
| 113 | * @return KVDevice instance |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 114 | */ |
| 115 | public static KVDevice createFromKey(final byte[] key) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 116 | return createFromKey(key, DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Gets an instance from DeviceID in specified namespace. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 121 | * <p/> |
| 122 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 123 | * |
| 124 | * @param key DeviceID |
| 125 | * @param namespace namespace to create this object in |
| 126 | * @return KVDevice instance |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 127 | */ |
| 128 | public static KVDevice createFromKey(final byte[] key, final String namespace) { |
| 129 | return new KVDevice(getMacFromKey(key), namespace); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets all the Devices in default namespace. |
| 134 | * |
| 135 | * @return Devices |
| 136 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 137 | public static Iterable<KVDevice> getAllDevices() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 138 | return new DeviceEnumerator(DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Gets all the Devices in specified namespace. |
| 143 | * |
| 144 | * @param namespace namespace to iterate over |
| 145 | * @return Devices |
| 146 | */ |
| 147 | public static Iterable<KVDevice> getAllDevices(final String namespace) { |
| 148 | return new DeviceEnumerator(namespace); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Utility class to provide Iterable interface. |
| 153 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 154 | public static class DeviceEnumerator implements Iterable<KVDevice> { |
| 155 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 156 | private final String namespace; |
| 157 | |
| 158 | /** |
| 159 | * Constructor to iterate Links in specified namespace. |
| 160 | * |
| 161 | * @param namespace namespace to iterate through |
| 162 | */ |
| 163 | public DeviceEnumerator(final String namespace) { |
| 164 | this.namespace = namespace; |
| 165 | } |
| 166 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 167 | @Override |
| 168 | public Iterator<KVDevice> iterator() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 169 | return new DeviceIterator(namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 170 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Utility class to provide Iterator over all the Device objects. |
| 175 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 176 | public static class DeviceIterator extends AbstractObjectIterator<KVDevice> { |
| 177 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Constructor to create an iterator to iterate all the Devices |
| 180 | * in specified namespace. |
| 181 | * |
| 182 | * @param namespace namespace to iterate through |
| 183 | */ |
| 184 | public DeviceIterator(final String namespace) { |
| 185 | super(DataStoreClient.getClient() |
| 186 | .getTable(namespace + DEVICE_TABLE_SUFFIX), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 187 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 188 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public KVDevice next() { |
| 191 | IKVEntry o = enumerator.next(); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 192 | KVDevice e = KVDevice.createFromKey(o.getKey(), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 193 | e.deserialize(o.getValue(), o.getVersion()); |
| 194 | return e; |
| 195 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Gets the MAC address. |
| 200 | * |
| 201 | * @return MAC address |
| 202 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 203 | public byte[] getMac() { |
Yuta HIGUCHI | ce7e7f8 | 2014-04-15 21:37:38 -0700 | [diff] [blame] | 204 | return mac.clone(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 207 | /** |
| 208 | * Gets the DeviceID. |
| 209 | * |
| 210 | * @return DeviceID |
| 211 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 212 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 213 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 216 | /** |
| 217 | * Add a port to this Device's attachment points. |
| 218 | * |
| 219 | * @param portId PortID of the port which this Device is attached |
| 220 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 221 | public void addPortId(final byte[] portId) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 222 | portIds.add(portId.clone()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 226 | * Remove a port from this Device's attachment points. |
| 227 | * |
| 228 | * @param portId PortID to remove |
| 229 | */ |
| 230 | public void removePortId(final byte[] portId) { |
| 231 | portIds.remove(portId); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Empty this Device's attachment points. |
| 236 | */ |
| 237 | public void emptyPortIds() { |
| 238 | portIds.clear(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Add ports to this Device's attachment points. |
| 243 | * |
| 244 | * @param newPortIds PortIDs which this Device is attached |
| 245 | */ |
| 246 | public void addAllToPortIds(final Collection<byte[]> newPortIds) { |
| 247 | // TODO: Should we copy each portId, or reference is OK. |
| 248 | portIds.addAll(newPortIds); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Gets all the PortIDs which this Device is attached. |
| 253 | * |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 254 | * @return Unmodifiable Set view of all the PortIds; |
| 255 | */ |
| 256 | public Set<byte[]> getAllPortIds() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 257 | return Collections.unmodifiableSet(portIds); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 262 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 263 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 264 | DeviceProperty.Builder dev = DeviceProperty.newBuilder(); |
| 265 | |
| 266 | dev.setMac(ByteString.copyFrom(mac)); |
| 267 | for (byte[] port : portIds) { |
| 268 | dev.addPortIds(ByteString.copyFrom(port)); |
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 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 271 | if (!map.isEmpty()) { |
| 272 | byte[] propMaps = serializePropertyMap(DEVICE_KRYO.get(), map); |
| 273 | dev.setValue(ByteString.copyFrom(propMaps)); |
| 274 | } |
| 275 | |
| 276 | return dev.build().toByteArray(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | protected boolean deserialize(final byte[] bytes) { |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 281 | |
| 282 | try { |
| 283 | boolean success = true; |
| 284 | |
| 285 | DeviceProperty dev = DeviceProperty.parseFrom(bytes); |
| 286 | for (ByteString portId : dev.getPortIdsList()) { |
| 287 | this.addPortId(portId.toByteArray()); |
| 288 | } |
| 289 | byte[] props = dev.getValue().toByteArray(); |
| 290 | success &= deserializePropertyMap(DEVICE_KRYO.get(), props); |
| 291 | return success; |
| 292 | } catch (InvalidProtocolBufferException e) { |
| 293 | log.error("Deserializing Device: " + this + " failed.", e); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 294 | return false; |
| 295 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 300 | // TODO output all properties? |
| 301 | return "[" + this.getClass().getSimpleName() |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 302 | + " " + ByteArrayUtil.toHexStringBuilder(mac, ":") + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 303 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 304 | } |