blob: 9a6d55018a271f9cb02d53a9e1557a170e96be81 [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
Ray Milkey5c9f2db2014-04-09 10:31:21 -070034 private static final ThreadLocal<Kryo> DEVICE_KRYO = 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 HIGUCHIce7e7f82014-04-15 21:37:38 -070081 this.mac = mac.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070082 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 HIGUCHIce7e7f82014-04-15 21:37:38 -0700125 return mac.clone();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700126 }
127
128 public byte[] getId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700129 return getKey();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700130 }
131
132 public void addPortId(final byte[] portId) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700133 // TODO: Should we copy portId, or reference is OK.
134 isPortIdsModified |= portIds.add(portId);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700135 }
136
137 public void removePortId(final byte[] portId) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700138 isPortIdsModified |= portIds.remove(portId);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700139 }
140
141 public void emptyPortIds() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700142 portIds.clear();
143 this.isPortIdsModified = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700144 }
145
146 public void addAllToPortIds(final Collection<byte[]> portIds) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700147 // TODO: Should we copy portId, or reference is OK.
148 isPortIdsModified |= this.portIds.addAll(portIds);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700149 }
150
151 /**
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700152 * @return Unmodifiable Set view of all the PortIds;
153 */
154 public Set<byte[]> getAllPortIds() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700155 return Collections.unmodifiableSet(portIds);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700156 }
157
158 @Override
159 public byte[] serialize() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700160 Map<Object, Object> map = getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700161
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700162 map.put(PROP_MAC, mac);
163 if (isPortIdsModified) {
164 byte[][] portIdArray = new byte[portIds.size()][];
165 map.put(PROP_PORT_IDS, portIds.toArray(portIdArray));
166 isPortIdsModified = false;
167 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700168
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700169 return serializePropertyMap(DEVICE_KRYO.get(), map);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700170 }
171
172 @Override
173 protected boolean deserialize(final byte[] bytes) {
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700174 boolean success = deserializePropertyMap(DEVICE_KRYO.get(), bytes);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700175 if (!success) {
176 log.error("Deserializing Link: " + this + " failed.");
177 return false;
178 }
179 Map<Object, Object> map = this.getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700180
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700181 if (this.portIds == null) {
182 this.portIds = new TreeSet<>(
183 ByteArrayComparator.BYTEARRAY_COMPARATOR);
184 }
185 byte[][] portIdArray = (byte[][]) map.get(PROP_PORT_IDS);
186 if (portIdArray != null) {
187 this.portIds.clear();
188 this.portIds.addAll(Arrays.asList(portIdArray));
189 isPortIdsModified = false;
190 } else {
191 // trigger write on next serialize
192 isPortIdsModified = true;
193 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700194
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700195 return success;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700196 }
197
198 @Override
199 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700200 // TODO output all properties?
201 return "[" + this.getClass().getSimpleName()
202 + " " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]";
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700203 }
TeruUd1c5b652014-03-24 13:58:46 -0700204
Ray Milkey269ffb92014-04-03 14:43:30 -0700205 public int getIp() {
206 return ip;
207 }
TeruUd1c5b652014-03-24 13:58:46 -0700208
Ray Milkey269ffb92014-04-03 14:43:30 -0700209 public void setIp(int ip) {
210 this.ip = ip;
211 }
TeruUd1c5b652014-03-24 13:58:46 -0700212
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 public long getLastSeenTime() {
214 return lastSeenTime;
215 }
216
217 public void setLastSeenTime(long lastSeenTime) {
218 this.lastSeenTime = lastSeenTime;
219 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700220}