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.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 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 14 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
| 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; |
| 25 | |
| 26 | /** |
| 27 | * Device object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 29 | * TODO switch to ProtoBuf, etc. |
| 30 | */ |
| 31 | public class KVDevice extends KVObject { |
| 32 | private static final Logger log = LoggerFactory.getLogger(KVDevice.class); |
| 33 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 34 | private static final ThreadLocal<Kryo> DEVICE_KRYO = 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; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 59 | // Assume there is only one ip on a device now. |
| 60 | private int ip; |
| 61 | private long lastSeenTime; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 62 | |
| 63 | // Assuming mac is unique cluster-wide |
| 64 | public static byte[] getDeviceID(final byte[] mac) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 65 | return DeviceEvent.getDeviceID(mac).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | public static byte[] getMacFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 69 | ByteBuffer keyBuf = ByteBuffer.wrap(key); |
| 70 | if (keyBuf.getChar() != 'D') { |
| 71 | throw new IllegalArgumentException("Invalid Device key"); |
| 72 | } |
| 73 | byte[] mac = new byte[keyBuf.remaining()]; |
| 74 | keyBuf.get(mac); |
| 75 | return mac; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public KVDevice(final byte[] mac) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 79 | super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME), getDeviceID(mac)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 80 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 81 | this.mac = mac; |
| 82 | this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 83 | this.isPortIdsModified = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get an instance from Key. |
| 88 | * |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 89 | * @param key |
| 90 | * @return |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | * @note You need to call `read()` to get the DB content. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 92 | */ |
| 93 | public static KVDevice createFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 94 | return new KVDevice(getMacFromKey(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | public static Iterable<KVDevice> getAllDevices() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 98 | return new DeviceEnumerator(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | public static class DeviceEnumerator implements Iterable<KVDevice> { |
| 102 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public Iterator<KVDevice> iterator() { |
| 105 | return new DeviceIterator(); |
| 106 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public static class DeviceIterator extends AbstractObjectIterator<KVDevice> { |
| 110 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 111 | public DeviceIterator() { |
| 112 | super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME)); |
| 113 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 114 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 115 | @Override |
| 116 | public KVDevice next() { |
| 117 | IKVEntry o = enumerator.next(); |
| 118 | KVDevice e = KVDevice.createFromKey(o.getKey()); |
| 119 | e.deserialize(o.getValue(), o.getVersion()); |
| 120 | return e; |
| 121 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public byte[] getMac() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 125 | // TODO may need to clone() to be sure this object will be immutable. |
| 126 | return mac; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 130 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | public void addPortId(final byte[] portId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 134 | // TODO: Should we copy portId, or reference is OK. |
| 135 | isPortIdsModified |= portIds.add(portId); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public void removePortId(final byte[] portId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 139 | isPortIdsModified |= portIds.remove(portId); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public void emptyPortIds() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 143 | portIds.clear(); |
| 144 | this.isPortIdsModified = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public void addAllToPortIds(final Collection<byte[]> portIds) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 148 | // TODO: Should we copy portId, or reference is OK. |
| 149 | isPortIdsModified |= this.portIds.addAll(portIds); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 153 | * @return Unmodifiable Set view of all the PortIds; |
| 154 | */ |
| 155 | public Set<byte[]> getAllPortIds() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 156 | return Collections.unmodifiableSet(portIds); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 161 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 162 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 163 | map.put(PROP_MAC, mac); |
| 164 | if (isPortIdsModified) { |
| 165 | byte[][] portIdArray = new byte[portIds.size()][]; |
| 166 | map.put(PROP_PORT_IDS, portIds.toArray(portIdArray)); |
| 167 | isPortIdsModified = false; |
| 168 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 169 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 170 | return serializePropertyMap(DEVICE_KRYO.get(), map); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| 174 | protected boolean deserialize(final byte[] bytes) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 175 | boolean success = deserializePropertyMap(DEVICE_KRYO.get(), bytes); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 176 | if (!success) { |
| 177 | log.error("Deserializing Link: " + this + " failed."); |
| 178 | return false; |
| 179 | } |
| 180 | Map<Object, Object> map = this.getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 181 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 182 | if (this.portIds == null) { |
| 183 | this.portIds = new TreeSet<>( |
| 184 | ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 185 | } |
| 186 | byte[][] portIdArray = (byte[][]) map.get(PROP_PORT_IDS); |
| 187 | if (portIdArray != null) { |
| 188 | this.portIds.clear(); |
| 189 | this.portIds.addAll(Arrays.asList(portIdArray)); |
| 190 | isPortIdsModified = false; |
| 191 | } else { |
| 192 | // trigger write on next serialize |
| 193 | isPortIdsModified = true; |
| 194 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 195 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 196 | return success; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 201 | // TODO output all properties? |
| 202 | return "[" + this.getClass().getSimpleName() |
| 203 | + " " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 204 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 205 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 206 | public int getIp() { |
| 207 | return ip; |
| 208 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 209 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 210 | public void setIp(int ip) { |
| 211 | this.ip = ip; |
| 212 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 213 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 214 | public long getLastSeenTime() { |
| 215 | return lastSeenTime; |
| 216 | } |
| 217 | |
| 218 | public void setLastSeenTime(long lastSeenTime) { |
| 219 | this.lastSeenTime = lastSeenTime; |
| 220 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 221 | } |