Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [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; |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame] | 8 | import java.util.Iterator; |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 9 | import java.util.Map; |
| 10 | import java.util.Set; |
| 11 | import java.util.TreeSet; |
| 12 | |
| 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
| 15 | |
| 16 | import com.esotericsoftware.kryo.Kryo; |
| 17 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame] | 18 | import edu.stanford.ramcloud.JRamCloud; |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 19 | import net.onrc.onos.datastore.RCObject; |
| 20 | import net.onrc.onos.datastore.RCTable; |
| 21 | import net.onrc.onos.datastore.topology.RCLink.STATUS; |
| 22 | import net.onrc.onos.datastore.utils.ByteArrayComparator; |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 23 | import net.onrc.onos.datastore.utils.ByteArrayUtil; |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 24 | |
| 25 | public class RCDevice extends RCObject { |
| 26 | @SuppressWarnings("unused") |
| 27 | private static final Logger log = LoggerFactory.getLogger(RCDevice.class); |
| 28 | |
| 29 | private static final ThreadLocal<Kryo> deviceKryo = new ThreadLocal<Kryo>() { |
| 30 | @Override |
| 31 | protected Kryo initialValue() { |
| 32 | Kryo kryo = new Kryo(); |
| 33 | kryo.setRegistrationRequired(true); |
| 34 | kryo.setReferences(false); |
| 35 | kryo.register(byte[].class); |
| 36 | kryo.register(byte[][].class); |
| 37 | kryo.register(HashMap.class); |
| 38 | // TODO check if we should explicitly specify EnumSerializer |
| 39 | kryo.register(STATUS.class); |
| 40 | return kryo; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | public static final String GLOBAL_DEVICE_TABLE_NAME = "G:Device"; |
| 45 | |
| 46 | // FIXME these should be Enum or some number, not String |
| 47 | private static final String PROP_MAC = "mac"; |
| 48 | private static final String PROP_PORT_IDS = "port-ids"; |
| 49 | |
| 50 | private final byte[] mac; |
| 51 | private TreeSet<byte[]> portIds; |
| 52 | transient private boolean isPortIdsModified; |
| 53 | |
| 54 | // Assuming mac is unique cluster-wide |
| 55 | public static byte[] getDeviceID(final byte[] mac) { |
| 56 | return ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac) |
| 57 | .array(); |
| 58 | } |
| 59 | |
Yuta HIGUCHI | 1ca1afa | 2014-02-04 19:33:57 -0800 | [diff] [blame^] | 60 | public static StringBuilder keysToSB(Collection<byte[]> keys) { |
| 61 | StringBuilder sb = new StringBuilder(); |
| 62 | sb.append("["); |
| 63 | boolean hasWritten = false; |
| 64 | for (byte[] key : keys) { |
| 65 | if (hasWritten) { |
| 66 | sb.append(", "); |
| 67 | } |
| 68 | sb.append(keyToString(key)); |
| 69 | hasWritten = true; |
| 70 | } |
| 71 | sb.append("]"); |
| 72 | return sb; |
| 73 | } |
| 74 | |
| 75 | public static String keyToString(byte[] key) { |
| 76 | // For debug log |
| 77 | byte[] mac = getMacFromKey(key); |
| 78 | return "D" + ByteArrayUtil.toHexStringBuffer(mac, ":"); |
| 79 | } |
| 80 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 81 | public static byte[] getMacFromKey(byte[] key) { |
| 82 | ByteBuffer keyBuf = ByteBuffer.wrap(key); |
| 83 | if (keyBuf.getChar() != 'D') { |
| 84 | throw new IllegalArgumentException("Invalid Device key"); |
| 85 | } |
| 86 | byte[] mac = new byte[keyBuf.remaining()]; |
| 87 | keyBuf.get(mac); |
| 88 | return mac; |
| 89 | } |
| 90 | |
| 91 | public RCDevice(byte[] mac) { |
| 92 | super(RCTable.getTable(GLOBAL_DEVICE_TABLE_NAME), getDeviceID(mac)); |
| 93 | |
| 94 | this.mac = mac; |
| 95 | this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 96 | this.isPortIdsModified = true; |
| 97 | } |
| 98 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Get an instance from Key. |
| 101 | * |
| 102 | * @note You need to call `read()` to get the DB content. |
| 103 | * @param key |
| 104 | * @return |
| 105 | */ |
| 106 | public static <D extends RCObject> D createFromKey(byte[] key) { |
| 107 | @SuppressWarnings("unchecked") |
| 108 | D d = (D) new RCDevice(getMacFromKey(key)); |
| 109 | return d; |
| 110 | } |
| 111 | |
| 112 | public static Iterable<RCDevice> getAllDevices() { |
| 113 | return new DeviceEnumerator(); |
| 114 | } |
| 115 | |
| 116 | public static class DeviceEnumerator implements Iterable<RCDevice> { |
| 117 | |
| 118 | @Override |
| 119 | public Iterator<RCDevice> iterator() { |
| 120 | return new DeviceIterator(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public static class DeviceIterator extends ObjectIterator<RCDevice> { |
| 125 | |
| 126 | public DeviceIterator() { |
| 127 | super(RCTable.getTable(GLOBAL_DEVICE_TABLE_NAME)); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public RCDevice next() { |
| 132 | JRamCloud.Object o = enumerator.next(); |
| 133 | RCDevice e = RCDevice.createFromKey(o.key); |
| 134 | e.setValueAndDeserialize(o.value, o.version); |
| 135 | return e; |
| 136 | } |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public byte[] getMac() { |
| 140 | // TODO may need to clone() to be sure this object will be immutable. |
| 141 | return mac; |
| 142 | } |
| 143 | |
| 144 | public byte[] getId() { |
| 145 | return getKey(); |
| 146 | } |
| 147 | |
| 148 | public void addPortId(byte[] portId) { |
| 149 | // TODO: Should we copy portId, or reference is OK. |
| 150 | isPortIdsModified |= portIds.add(portId); |
| 151 | } |
| 152 | |
| 153 | public void removePortId(byte[] portId) { |
| 154 | isPortIdsModified |= portIds.remove(portId); |
| 155 | } |
| 156 | |
| 157 | public void emptyPortIds() { |
| 158 | portIds.clear(); |
| 159 | this.isPortIdsModified = true; |
| 160 | } |
| 161 | |
| 162 | public void addAllToPortIds(Collection<byte[]> portIds) { |
| 163 | // TODO: Should we copy portId, or reference is OK. |
| 164 | isPortIdsModified |= this.portIds.addAll(portIds); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * |
| 169 | * @return Unmodifiable Set view of all the PortIds; |
| 170 | */ |
| 171 | public Set<byte[]> getAllPortIds() { |
| 172 | return Collections.unmodifiableSet(portIds); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void serializeAndSetValue() { |
| 177 | Map<Object, Object> map = getObjectMap(); |
| 178 | |
| 179 | map.put(PROP_MAC, mac); |
| 180 | if (isPortIdsModified) { |
| 181 | byte[] portIdArray[] = new byte[portIds.size()][]; |
| 182 | map.put(PROP_PORT_IDS, portIds.toArray(portIdArray)); |
| 183 | isPortIdsModified = false; |
| 184 | } |
| 185 | |
| 186 | serializeAndSetValue(deviceKryo.get(), map); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public Map<Object, Object> deserializeObjectFromValue() { |
| 191 | Map<Object, Object> map = deserializeObjectFromValue(deviceKryo.get()); |
| 192 | |
| 193 | if (this.portIds == null) { |
| 194 | this.portIds = new TreeSet<>( |
| 195 | ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 196 | } |
| 197 | byte[] portIdArray[] = (byte[][]) map.get(PROP_PORT_IDS); |
| 198 | if (portIdArray != null) { |
| 199 | this.portIds.clear(); |
| 200 | this.portIds.addAll(Arrays.asList(portIdArray)); |
| 201 | isPortIdsModified = false; |
| 202 | } else { |
| 203 | // trigger write on next serialize |
| 204 | isPortIdsModified = true; |
| 205 | } |
| 206 | |
| 207 | return map; |
| 208 | } |
| 209 | |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 210 | @Override |
| 211 | public String toString() { |
| 212 | // TODO OUTPUT ALL? |
| 213 | return "[RCDevice " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]"; |
| 214 | } |
| 215 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 216 | public static void main(String[] args) { |
| 217 | // TODO Auto-generated method stub |
| 218 | |
| 219 | } |
| 220 | |
| 221 | } |