blob: 14268b560e90585f6cb54231338f43c1233338ec [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.
28 *
29 * 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;
TeruUd1c5b652014-03-24 13:58:46 -070058
59 // 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 *
89 * @note You need to call `read()` to get the DB content.
90 * @param key
91 * @return
92 */
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 /**
153 *
154 * @return Unmodifiable Set view of all the PortIds;
155 */
156 public Set<byte[]> getAllPortIds() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700157 return Collections.unmodifiableSet(portIds);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700158 }
159
160 @Override
161 public byte[] serialize() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700162 Map<Object, Object> map = getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700163
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700164 map.put(PROP_MAC, mac);
165 if (isPortIdsModified) {
166 byte[][] portIdArray = new byte[portIds.size()][];
167 map.put(PROP_PORT_IDS, portIds.toArray(portIdArray));
168 isPortIdsModified = false;
169 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700170
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700171 return serializePropertyMap(deviceKryo.get(), map);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700172 }
173
174 @Override
175 protected boolean deserialize(final byte[] bytes) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700176 boolean success = deserializePropertyMap(deviceKryo.get(), bytes);
177 if (!success) {
178 log.error("Deserializing Link: " + this + " failed.");
179 return false;
180 }
181 Map<Object, Object> map = this.getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700182
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700183 if (this.portIds == null) {
184 this.portIds = new TreeSet<>(
185 ByteArrayComparator.BYTEARRAY_COMPARATOR);
186 }
187 byte[][] portIdArray = (byte[][]) map.get(PROP_PORT_IDS);
188 if (portIdArray != null) {
189 this.portIds.clear();
190 this.portIds.addAll(Arrays.asList(portIdArray));
191 isPortIdsModified = false;
192 } else {
193 // trigger write on next serialize
194 isPortIdsModified = true;
195 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700196
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700197 return success;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700198 }
199
200 @Override
201 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700202 // TODO output all properties?
203 return "[" + this.getClass().getSimpleName()
204 + " " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]";
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700205 }
TeruUd1c5b652014-03-24 13:58:46 -0700206
207 public int getIp() {
208 return ip;
209 }
210
211 public void setIp(int ip) {
212 this.ip = ip;
213 }
214
215 public long getLastSeenTime() {
216 return lastSeenTime;
217 }
218
219 public void setLastSeenTime(long lastSeenTime) {
220 this.lastSeenTime = lastSeenTime;
221 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700222}