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