blob: 8b1d8af69bcd25287611228005cce57dde6940f1 [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datastore.topology;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07002
3import java.nio.ByteBuffer;
4import java.util.Arrays;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.HashMap;
8import java.util.Iterator;
9import java.util.Map;
10import java.util.Set;
11import java.util.TreeSet;
12
Jonathan Hart6df90172014-04-03 10:13:11 -070013import net.onrc.onos.core.datastore.DataStoreClient;
14import net.onrc.onos.core.datastore.IKVTable.IKVEntry;
15import net.onrc.onos.core.datastore.topology.KVLink.STATUS;
16import net.onrc.onos.core.datastore.utils.ByteArrayComparator;
17import net.onrc.onos.core.datastore.utils.ByteArrayUtil;
18import net.onrc.onos.core.datastore.utils.KVObject;
Jonathan Hart472062d2014-04-03 10:56:48 -070019import net.onrc.onos.core.topology.DeviceEvent;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070020
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import com.esotericsoftware.kryo.Kryo;
25
26/**
27 * Device object.
Ray Milkey269ffb92014-04-03 14:43:30 -070028 * <p/>
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070029 * TODO switch to ProtoBuf, etc.
30 */
31public 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 HIGUCHI826b4a42014-03-24 13:10:33 -070035 @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 HIGUCHI66ca1bf2014-03-12 18:34:09 -070047 };
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 Milkey269ffb92014-04-03 14:43:30 -070058
TeruUd1c5b652014-03-24 13:58:46 -070059 // Assume there is only one ip on a device now.
60 private int ip;
61 private long lastSeenTime;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070062
63 // Assuming mac is unique cluster-wide
64 public static byte[] getDeviceID(final byte[] mac) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070065 return DeviceEvent.getDeviceID(mac).array();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070066 }
67
68 public static byte[] getMacFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070069 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 HIGUCHI66ca1bf2014-03-12 18:34:09 -070076 }
77
78 public KVDevice(final byte[] mac) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070079 super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME), getDeviceID(mac));
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070080
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070081 this.mac = mac;
82 this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
83 this.isPortIdsModified = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070084 }
85
86 /**
87 * Get an instance from Key.
88 *
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070089 * @param key
90 * @return
Ray Milkey269ffb92014-04-03 14:43:30 -070091 * @note You need to call `read()` to get the DB content.
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070092 */
93 public static KVDevice createFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070094 return new KVDevice(getMacFromKey(key));
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070095 }
96
97 public static Iterable<KVDevice> getAllDevices() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070098 return new DeviceEnumerator();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070099 }
100
101 public static class DeviceEnumerator implements Iterable<KVDevice> {
102
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700103 @Override
104 public Iterator<KVDevice> iterator() {
105 return new DeviceIterator();
106 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700107 }
108
109 public static class DeviceIterator extends AbstractObjectIterator<KVDevice> {
110
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700111 public DeviceIterator() {
112 super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME));
113 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700114
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700115 @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 HIGUCHI66ca1bf2014-03-12 18:34:09 -0700122 }
123
124 public byte[] getMac() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700125 // TODO may need to clone() to be sure this object will be immutable.
126 return mac;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700127 }
128
129 public byte[] getId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700130 return getKey();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700131 }
132
133 public void addPortId(final byte[] portId) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700134 // TODO: Should we copy portId, or reference is OK.
135 isPortIdsModified |= portIds.add(portId);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700136 }
137
138 public void removePortId(final byte[] portId) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700139 isPortIdsModified |= portIds.remove(portId);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700140 }
141
142 public void emptyPortIds() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700143 portIds.clear();
144 this.isPortIdsModified = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700145 }
146
147 public void addAllToPortIds(final Collection<byte[]> portIds) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700148 // TODO: Should we copy portId, or reference is OK.
149 isPortIdsModified |= this.portIds.addAll(portIds);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700150 }
151
152 /**
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700153 * @return Unmodifiable Set view of all the PortIds;
154 */
155 public Set<byte[]> getAllPortIds() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700156 return Collections.unmodifiableSet(portIds);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700157 }
158
159 @Override
160 public byte[] serialize() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700161 Map<Object, Object> map = getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700162
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700163 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 HIGUCHI66ca1bf2014-03-12 18:34:09 -0700169
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700170 return serializePropertyMap(deviceKryo.get(), map);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700171 }
172
173 @Override
174 protected boolean deserialize(final byte[] bytes) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700175 boolean success = deserializePropertyMap(deviceKryo.get(), bytes);
176 if (!success) {
177 log.error("Deserializing Link: " + this + " failed.");
178 return false;
179 }
180 Map<Object, Object> map = this.getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700181
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700182 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 HIGUCHI66ca1bf2014-03-12 18:34:09 -0700195
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700196 return success;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700197 }
198
199 @Override
200 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700201 // TODO output all properties?
202 return "[" + this.getClass().getSimpleName()
203 + " " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]";
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700204 }
TeruUd1c5b652014-03-24 13:58:46 -0700205
Ray Milkey269ffb92014-04-03 14:43:30 -0700206 public int getIp() {
207 return ip;
208 }
TeruUd1c5b652014-03-24 13:58:46 -0700209
Ray Milkey269ffb92014-04-03 14:43:30 -0700210 public void setIp(int ip) {
211 this.ip = ip;
212 }
TeruUd1c5b652014-03-24 13:58:46 -0700213
Ray Milkey269ffb92014-04-03 14:43:30 -0700214 public long getLastSeenTime() {
215 return lastSeenTime;
216 }
217
218 public void setLastSeenTime(long lastSeenTime) {
219 this.lastSeenTime = lastSeenTime;
220 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700221}