blob: 08fe458e824eb10689a5fc0329d3e5250b396a83 [file] [log] [blame]
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -08001package 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;
Yuta HIGUCHI10eebea2014-02-03 10:41:41 -08008import java.util.Iterator;
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -08009import java.util.Map;
10import java.util.Set;
11import java.util.TreeSet;
12
13import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
16import com.esotericsoftware.kryo.Kryo;
17
Yuta HIGUCHI10eebea2014-02-03 10:41:41 -080018import edu.stanford.ramcloud.JRamCloud;
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080019import net.onrc.onos.datastore.RCObject;
20import net.onrc.onos.datastore.RCTable;
21import net.onrc.onos.datastore.topology.RCLink.STATUS;
22import net.onrc.onos.datastore.utils.ByteArrayComparator;
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -080023import net.onrc.onos.datastore.utils.ByteArrayUtil;
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080024
25public class RCDevice extends RCObject {
26 @SuppressWarnings("unused")
27 private static final Logger log = LoggerFactory.getLogger(RCDevice.class);
28
29 private static final ThreadLocal<Kryo> deviceKryo = new ThreadLocal<Kryo>() {
30 @Override
31 protected Kryo initialValue() {
32 Kryo kryo = new Kryo();
33 kryo.setRegistrationRequired(true);
34 kryo.setReferences(false);
35 kryo.register(byte[].class);
36 kryo.register(byte[][].class);
37 kryo.register(HashMap.class);
38 // TODO check if we should explicitly specify EnumSerializer
39 kryo.register(STATUS.class);
40 return kryo;
41 }
42 };
43
44 public static final String GLOBAL_DEVICE_TABLE_NAME = "G:Device";
45
46 // FIXME these should be Enum or some number, not String
47 private static final String PROP_MAC = "mac";
48 private static final String PROP_PORT_IDS = "port-ids";
49
50 private final byte[] mac;
51 private TreeSet<byte[]> portIds;
52 transient private boolean isPortIdsModified;
53
54 // Assuming mac is unique cluster-wide
55 public static byte[] getDeviceID(final byte[] mac) {
56 return ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac)
57 .array();
58 }
59
60 public static byte[] getMacFromKey(byte[] key) {
61 ByteBuffer keyBuf = ByteBuffer.wrap(key);
62 if (keyBuf.getChar() != 'D') {
63 throw new IllegalArgumentException("Invalid Device key");
64 }
65 byte[] mac = new byte[keyBuf.remaining()];
66 keyBuf.get(mac);
67 return mac;
68 }
69
70 public RCDevice(byte[] mac) {
71 super(RCTable.getTable(GLOBAL_DEVICE_TABLE_NAME), getDeviceID(mac));
72
73 this.mac = mac;
74 this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
75 this.isPortIdsModified = true;
76 }
77
Yuta HIGUCHI10eebea2014-02-03 10:41:41 -080078 /**
79 * Get an instance from Key.
80 *
81 * @note You need to call `read()` to get the DB content.
82 * @param key
83 * @return
84 */
85 public static <D extends RCObject> D createFromKey(byte[] key) {
86 @SuppressWarnings("unchecked")
87 D d = (D) new RCDevice(getMacFromKey(key));
88 return d;
89 }
90
91 public static Iterable<RCDevice> getAllDevices() {
92 return new DeviceEnumerator();
93 }
94
95 public static class DeviceEnumerator implements Iterable<RCDevice> {
96
97 @Override
98 public Iterator<RCDevice> iterator() {
99 return new DeviceIterator();
100 }
101 }
102
103 public static class DeviceIterator extends ObjectIterator<RCDevice> {
104
105 public DeviceIterator() {
106 super(RCTable.getTable(GLOBAL_DEVICE_TABLE_NAME));
107 }
108
109 @Override
110 public RCDevice next() {
111 JRamCloud.Object o = enumerator.next();
112 RCDevice e = RCDevice.createFromKey(o.key);
113 e.setValueAndDeserialize(o.value, o.version);
114 return e;
115 }
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800116 }
117
118 public byte[] getMac() {
119 // TODO may need to clone() to be sure this object will be immutable.
120 return mac;
121 }
122
123 public byte[] getId() {
124 return getKey();
125 }
126
127 public void addPortId(byte[] portId) {
128 // TODO: Should we copy portId, or reference is OK.
129 isPortIdsModified |= portIds.add(portId);
130 }
131
132 public void removePortId(byte[] portId) {
133 isPortIdsModified |= portIds.remove(portId);
134 }
135
136 public void emptyPortIds() {
137 portIds.clear();
138 this.isPortIdsModified = true;
139 }
140
141 public void addAllToPortIds(Collection<byte[]> portIds) {
142 // TODO: Should we copy portId, or reference is OK.
143 isPortIdsModified |= this.portIds.addAll(portIds);
144 }
145
146 /**
147 *
148 * @return Unmodifiable Set view of all the PortIds;
149 */
150 public Set<byte[]> getAllPortIds() {
151 return Collections.unmodifiableSet(portIds);
152 }
153
154 @Override
155 public void serializeAndSetValue() {
156 Map<Object, Object> map = getObjectMap();
157
158 map.put(PROP_MAC, mac);
159 if (isPortIdsModified) {
160 byte[] portIdArray[] = new byte[portIds.size()][];
161 map.put(PROP_PORT_IDS, portIds.toArray(portIdArray));
162 isPortIdsModified = false;
163 }
164
165 serializeAndSetValue(deviceKryo.get(), map);
166 }
167
168 @Override
169 public Map<Object, Object> deserializeObjectFromValue() {
170 Map<Object, Object> map = deserializeObjectFromValue(deviceKryo.get());
171
172 if (this.portIds == null) {
173 this.portIds = new TreeSet<>(
174 ByteArrayComparator.BYTEARRAY_COMPARATOR);
175 }
176 byte[] portIdArray[] = (byte[][]) map.get(PROP_PORT_IDS);
177 if (portIdArray != null) {
178 this.portIds.clear();
179 this.portIds.addAll(Arrays.asList(portIdArray));
180 isPortIdsModified = false;
181 } else {
182 // trigger write on next serialize
183 isPortIdsModified = true;
184 }
185
186 return map;
187 }
188
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800189 @Override
190 public String toString() {
191 // TODO OUTPUT ALL?
192 return "[RCDevice " + ByteArrayUtil.toHexStringBuffer(mac, ":") + "]";
193 }
194
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800195 public static void main(String[] args) {
196 // TODO Auto-generated method stub
197
198 }
199
200}