blob: 105f6f7b269c92d7a7f9ab37080e8e5a0f4bb7d7 [file] [log] [blame]
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07001package net.onrc.onos.datastore.topology;
2
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
13import net.onrc.onos.datastore.DataStoreClient;
14import net.onrc.onos.datastore.IKVTable.IKVEntry;
15import net.onrc.onos.datastore.topology.KVLink.STATUS;
16import net.onrc.onos.datastore.utils.ByteArrayComparator;
17import net.onrc.onos.datastore.utils.ByteArrayUtil;
18import net.onrc.onos.datastore.utils.KVObject;
19import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
20
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>() {
35 @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 }
47 };
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;
58
59 // Assuming mac is unique cluster-wide
60 public static byte[] getDeviceID(final byte[] mac) {
61 return DeviceEvent.getDeviceID(mac).array();
62 }
63
64 public static byte[] getMacFromKey(final byte[] key) {
65 ByteBuffer keyBuf = ByteBuffer.wrap(key);
66 if (keyBuf.getChar() != 'D') {
67 throw new IllegalArgumentException("Invalid Device key");
68 }
69 byte[] mac = new byte[keyBuf.remaining()];
70 keyBuf.get(mac);
71 return mac;
72 }
73
74 public KVDevice(final byte[] mac) {
75 super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME), getDeviceID(mac));
76
77 this.mac = mac;
78 this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
79 this.isPortIdsModified = true;
80 }
81
82 /**
83 * Get an instance from Key.
84 *
85 * @note You need to call `read()` to get the DB content.
86 * @param key
87 * @return
88 */
89 public static KVDevice createFromKey(final byte[] key) {
90 return new KVDevice(getMacFromKey(key));
91 }
92
93 public static Iterable<KVDevice> getAllDevices() {
94 return new DeviceEnumerator();
95 }
96
97 public static class DeviceEnumerator implements Iterable<KVDevice> {
98
99 @Override
100 public Iterator<KVDevice> iterator() {
101 return new DeviceIterator();
102 }
103 }
104
105 public static class DeviceIterator extends AbstractObjectIterator<KVDevice> {
106
107 public DeviceIterator() {
108 super(DataStoreClient.getClient().getTable(GLOBAL_DEVICE_TABLE_NAME));
109 }
110
111 @Override
112 public KVDevice next() {
113 IKVEntry o = enumerator.next();
114 KVDevice e = KVDevice.createFromKey(o.getKey());
115 e.deserialize(o.getValue(), o.getVersion());
116 return e;
117 }
118 }
119
120 public byte[] getMac() {
121 // TODO may need to clone() to be sure this object will be immutable.
122 return mac;
123 }
124
125 public byte[] getId() {
126 return getKey();
127 }
128
129 public void addPortId(final byte[] portId) {
130 // TODO: Should we copy portId, or reference is OK.
131 isPortIdsModified |= portIds.add(portId);
132 }
133
134 public void removePortId(final byte[] portId) {
135 isPortIdsModified |= portIds.remove(portId);
136 }
137
138 public void emptyPortIds() {
139 portIds.clear();
140 this.isPortIdsModified = true;
141 }
142
143 public void addAllToPortIds(final Collection<byte[]> portIds) {
144 // TODO: Should we copy portId, or reference is OK.
145 isPortIdsModified |= this.portIds.addAll(portIds);
146 }
147
148 /**
149 *
150 * @return Unmodifiable Set view of all the PortIds;
151 */
152 public Set<byte[]> getAllPortIds() {
153 return Collections.unmodifiableSet(portIds);
154 }
155
156 @Override
157 public byte[] serialize() {
158 Map<Object, Object> map = getPropertyMap();
159
160 map.put(PROP_MAC, mac);
161 if (isPortIdsModified) {
162 byte[][] portIdArray = new byte[portIds.size()][];
163 map.put(PROP_PORT_IDS, portIds.toArray(portIdArray));
164 isPortIdsModified = false;
165 }
166
167 return serializePropertyMap(deviceKryo.get(), map);
168 }
169
170 @Override
171 protected boolean deserialize(final byte[] bytes) {
172 boolean success = deserializePropertyMap(deviceKryo.get(), bytes);
173 if (!success) {
174 log.error("Deserializing Link: " + this + " failed.");
175 return false;
176 }
177 Map<Object, Object> map = this.getPropertyMap();
178
179 if (this.portIds == null) {
180 this.portIds = new TreeSet<>(
181 ByteArrayComparator.BYTEARRAY_COMPARATOR);
182 }
183 byte[][] portIdArray = (byte[][]) map.get(PROP_PORT_IDS);
184 if (portIdArray != null) {
185 this.portIds.clear();
186 this.portIds.addAll(Arrays.asList(portIdArray));
187 isPortIdsModified = false;
188 } else {
189 // trigger write on next serialize
190 isPortIdsModified = true;
191 }
192
193 return success;
194 }
195
196 @Override
197 public String toString() {
198 // TODO output all properties?
199 return "[" + this.getClass().getSimpleName()
200 + " " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]";
201 }
202}