blob: 3c56d2c8cf54af8deea2c8ee924cbd537451df01 [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
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070013import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14
Jonathan Hart6df90172014-04-03 10:13:11 -070015import net.onrc.onos.core.datastore.DataStoreClient;
16import net.onrc.onos.core.datastore.IKVTable.IKVEntry;
17import net.onrc.onos.core.datastore.topology.KVLink.STATUS;
18import net.onrc.onos.core.datastore.utils.ByteArrayComparator;
19import net.onrc.onos.core.datastore.utils.ByteArrayUtil;
20import net.onrc.onos.core.datastore.utils.KVObject;
Jonathan Hart472062d2014-04-03 10:56:48 -070021import net.onrc.onos.core.topology.DeviceEvent;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070022
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.esotericsoftware.kryo.Kryo;
27
28/**
29 * Device object.
Ray Milkey269ffb92014-04-03 14:43:30 -070030 * <p/>
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070031 * TODO switch to ProtoBuf, etc.
32 */
33public class KVDevice extends KVObject {
34 private static final Logger log = LoggerFactory.getLogger(KVDevice.class);
35
Ray Milkey5c9f2db2014-04-09 10:31:21 -070036 private static final ThreadLocal<Kryo> DEVICE_KRYO = new ThreadLocal<Kryo>() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070037 @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 HIGUCHI66ca1bf2014-03-12 18:34:09 -070049 };
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 Milkey269ffb92014-04-03 14:43:30 -070060
TeruUd1c5b652014-03-24 13:58:46 -070061 // Assume there is only one ip on a device now.
62 private int ip;
63 private long lastSeenTime;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070064
65 // Assuming mac is unique cluster-wide
66 public static byte[] getDeviceID(final byte[] mac) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070067 return DeviceEvent.getDeviceID(mac).array();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070068 }
69
70 public static byte[] getMacFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070071 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 HIGUCHI66ca1bf2014-03-12 18:34:09 -070078 }
79
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070080 @SuppressFBWarnings(value = "EI_EXPOSE_REP2",
81 justification = "TODO: Store a copy of the object?")
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070082 public KVDevice(final byte[] mac) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070083 super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME), getDeviceID(mac));
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070084
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070085 this.mac = mac;
86 this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
87 this.isPortIdsModified = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070088 }
89
90 /**
91 * Get an instance from Key.
92 *
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070093 * @param key
94 * @return
Ray Milkey269ffb92014-04-03 14:43:30 -070095 * @note You need to call `read()` to get the DB content.
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070096 */
97 public static KVDevice createFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070098 return new KVDevice(getMacFromKey(key));
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070099 }
100
101 public static Iterable<KVDevice> getAllDevices() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700102 return new DeviceEnumerator();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700103 }
104
105 public static class DeviceEnumerator implements Iterable<KVDevice> {
106
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700107 @Override
108 public Iterator<KVDevice> iterator() {
109 return new DeviceIterator();
110 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700111 }
112
113 public static class DeviceIterator extends AbstractObjectIterator<KVDevice> {
114
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700115 public DeviceIterator() {
116 super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME));
117 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700118
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700119 @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 HIGUCHI66ca1bf2014-03-12 18:34:09 -0700126 }
127
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -0700128 @SuppressFBWarnings(value = "EI_EXPOSE_REP",
129 justification = "TODO: Return a copy of the object?")
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700130 public byte[] getMac() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700131 // TODO may need to clone() to be sure this object will be immutable.
132 return mac;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700133 }
134
135 public byte[] getId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700136 return getKey();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700137 }
138
139 public void addPortId(final byte[] portId) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700140 // TODO: Should we copy portId, or reference is OK.
141 isPortIdsModified |= portIds.add(portId);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700142 }
143
144 public void removePortId(final byte[] portId) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700145 isPortIdsModified |= portIds.remove(portId);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700146 }
147
148 public void emptyPortIds() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700149 portIds.clear();
150 this.isPortIdsModified = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700151 }
152
153 public void addAllToPortIds(final Collection<byte[]> portIds) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700154 // TODO: Should we copy portId, or reference is OK.
155 isPortIdsModified |= this.portIds.addAll(portIds);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700156 }
157
158 /**
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700159 * @return Unmodifiable Set view of all the PortIds;
160 */
161 public Set<byte[]> getAllPortIds() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700162 return Collections.unmodifiableSet(portIds);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700163 }
164
165 @Override
166 public byte[] serialize() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700167 Map<Object, Object> map = getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700168
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700169 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 HIGUCHI66ca1bf2014-03-12 18:34:09 -0700175
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700176 return serializePropertyMap(DEVICE_KRYO.get(), map);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700177 }
178
179 @Override
180 protected boolean deserialize(final byte[] bytes) {
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700181 boolean success = deserializePropertyMap(DEVICE_KRYO.get(), bytes);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700182 if (!success) {
183 log.error("Deserializing Link: " + this + " failed.");
184 return false;
185 }
186 Map<Object, Object> map = this.getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700187
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700188 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 HIGUCHI66ca1bf2014-03-12 18:34:09 -0700201
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700202 return success;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700203 }
204
205 @Override
206 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700207 // TODO output all properties?
208 return "[" + this.getClass().getSimpleName()
209 + " " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]";
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700210 }
TeruUd1c5b652014-03-24 13:58:46 -0700211
Ray Milkey269ffb92014-04-03 14:43:30 -0700212 public int getIp() {
213 return ip;
214 }
TeruUd1c5b652014-03-24 13:58:46 -0700215
Ray Milkey269ffb92014-04-03 14:43:30 -0700216 public void setIp(int ip) {
217 this.ip = ip;
218 }
TeruUd1c5b652014-03-24 13:58:46 -0700219
Ray Milkey269ffb92014-04-03 14:43:30 -0700220 public long getLastSeenTime() {
221 return lastSeenTime;
222 }
223
224 public void setLastSeenTime(long lastSeenTime) {
225 this.lastSeenTime = lastSeenTime;
226 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700227}