Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 1 | package net.onrc.onos.datastore.topology; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.util.Arrays; |
| 5 | import java.util.Collection; |
| 6 | import java.util.Collections; |
| 7 | import java.util.HashMap; |
| 8 | import java.util.Iterator; |
| 9 | import java.util.Map; |
| 10 | import java.util.Set; |
| 11 | import java.util.TreeSet; |
| 12 | |
| 13 | import net.onrc.onos.datastore.DataStoreClient; |
| 14 | import net.onrc.onos.datastore.IKVTable.IKVEntry; |
| 15 | import net.onrc.onos.datastore.topology.KVLink.STATUS; |
| 16 | import net.onrc.onos.datastore.utils.ByteArrayComparator; |
| 17 | import net.onrc.onos.datastore.utils.ByteArrayUtil; |
| 18 | import net.onrc.onos.datastore.utils.KVObject; |
| 19 | import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent; |
| 20 | |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import com.esotericsoftware.kryo.Kryo; |
| 25 | |
| 26 | /** |
| 27 | * Device object. |
| 28 | * |
| 29 | * TODO switch to ProtoBuf, etc. |
| 30 | */ |
| 31 | public class KVDevice extends KVObject { |
| 32 | private static final Logger log = LoggerFactory.getLogger(KVDevice.class); |
| 33 | |
| 34 | private static final ThreadLocal<Kryo> deviceKryo = 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 | |
| 49 | public static final String GLOBAL_DEVICE_TABLE_NAME = "G:Device"; |
| 50 | |
| 51 | // FIXME these should be Enum or some number, not String |
| 52 | private static final String PROP_MAC = "mac"; |
| 53 | private static final String PROP_PORT_IDS = "port-ids"; |
| 54 | |
| 55 | private final byte[] mac; |
| 56 | private TreeSet<byte[]> portIds; |
| 57 | private transient boolean isPortIdsModified; |
| 58 | |
| 59 | // Assuming mac is unique cluster-wide |
| 60 | public static byte[] getDeviceID(final byte[] mac) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 61 | return DeviceEvent.getDeviceID(mac).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public static byte[] getMacFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 65 | ByteBuffer keyBuf = ByteBuffer.wrap(key); |
| 66 | if (keyBuf.getChar() != 'D') { |
| 67 | throw new IllegalArgumentException("Invalid Device key"); |
| 68 | } |
| 69 | byte[] mac = new byte[keyBuf.remaining()]; |
| 70 | keyBuf.get(mac); |
| 71 | return mac; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public KVDevice(final byte[] mac) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 75 | super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME), getDeviceID(mac)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 76 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 77 | this.mac = mac; |
| 78 | this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 79 | this.isPortIdsModified = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get an instance from Key. |
| 84 | * |
| 85 | * @note You need to call `read()` to get the DB content. |
| 86 | * @param key |
| 87 | * @return |
| 88 | */ |
| 89 | public static KVDevice createFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 90 | return new KVDevice(getMacFromKey(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public static Iterable<KVDevice> getAllDevices() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 94 | return new DeviceEnumerator(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | public static class DeviceEnumerator implements Iterable<KVDevice> { |
| 98 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 99 | @Override |
| 100 | public Iterator<KVDevice> iterator() { |
| 101 | return new DeviceIterator(); |
| 102 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public static class DeviceIterator extends AbstractObjectIterator<KVDevice> { |
| 106 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 107 | public DeviceIterator() { |
| 108 | super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME)); |
| 109 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 110 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 111 | @Override |
| 112 | public KVDevice next() { |
| 113 | IKVEntry o = enumerator.next(); |
| 114 | KVDevice e = KVDevice.createFromKey(o.getKey()); |
| 115 | e.deserialize(o.getValue(), o.getVersion()); |
| 116 | return e; |
| 117 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public byte[] getMac() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 121 | // TODO may need to clone() to be sure this object will be immutable. |
| 122 | return mac; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 126 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public void addPortId(final byte[] portId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 130 | // TODO: Should we copy portId, or reference is OK. |
| 131 | isPortIdsModified |= portIds.add(portId); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | public void removePortId(final byte[] portId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 135 | isPortIdsModified |= portIds.remove(portId); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public void emptyPortIds() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 139 | portIds.clear(); |
| 140 | this.isPortIdsModified = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | public void addAllToPortIds(final Collection<byte[]> portIds) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 144 | // TODO: Should we copy portId, or reference is OK. |
| 145 | isPortIdsModified |= this.portIds.addAll(portIds); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * |
| 150 | * @return Unmodifiable Set view of all the PortIds; |
| 151 | */ |
| 152 | public Set<byte[]> getAllPortIds() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 153 | return Collections.unmodifiableSet(portIds); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 158 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 159 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 160 | map.put(PROP_MAC, mac); |
| 161 | if (isPortIdsModified) { |
| 162 | byte[][] portIdArray = new byte[portIds.size()][]; |
| 163 | map.put(PROP_PORT_IDS, portIds.toArray(portIdArray)); |
| 164 | isPortIdsModified = false; |
| 165 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 166 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 167 | return serializePropertyMap(deviceKryo.get(), map); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | protected boolean deserialize(final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 172 | boolean success = deserializePropertyMap(deviceKryo.get(), bytes); |
| 173 | if (!success) { |
| 174 | log.error("Deserializing Link: " + this + " failed."); |
| 175 | return false; |
| 176 | } |
| 177 | Map<Object, Object> map = this.getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 178 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 179 | if (this.portIds == null) { |
| 180 | this.portIds = new TreeSet<>( |
| 181 | ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 182 | } |
| 183 | byte[][] portIdArray = (byte[][]) map.get(PROP_PORT_IDS); |
| 184 | if (portIdArray != null) { |
| 185 | this.portIds.clear(); |
| 186 | this.portIds.addAll(Arrays.asList(portIdArray)); |
| 187 | isPortIdsModified = false; |
| 188 | } else { |
| 189 | // trigger write on next serialize |
| 190 | isPortIdsModified = true; |
| 191 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 192 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 193 | return success; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 198 | // TODO output all properties? |
| 199 | return "[" + this.getClass().getSimpleName() |
| 200 | + " " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 201 | } |
| 202 | } |