blob: 631c6aa4e83a53c7ab6110202843229399aab530 [file] [log] [blame]
tom8cc5aa72014-09-19 15:14:43 -07001package org.onlab.onos.store.device.impl;
2
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -07003import com.google.common.base.Optional;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -07004import com.google.common.cache.LoadingCache;
5import com.google.common.collect.ImmutableList;
6import com.google.common.collect.ImmutableSet;
7import com.google.common.collect.ImmutableSet.Builder;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -07008import com.hazelcast.core.IMap;
9import com.hazelcast.core.ISet;
tom0872a172014-09-23 11:24:26 -070010import org.apache.felix.scr.annotations.Activate;
11import org.apache.felix.scr.annotations.Component;
12import org.apache.felix.scr.annotations.Deactivate;
tom0872a172014-09-23 11:24:26 -070013import org.apache.felix.scr.annotations.Service;
14import org.onlab.onos.net.DefaultDevice;
15import org.onlab.onos.net.DefaultPort;
16import org.onlab.onos.net.Device;
17import org.onlab.onos.net.DeviceId;
18import org.onlab.onos.net.MastershipRole;
19import org.onlab.onos.net.Port;
20import org.onlab.onos.net.PortNumber;
21import org.onlab.onos.net.device.DeviceDescription;
22import org.onlab.onos.net.device.DeviceEvent;
23import org.onlab.onos.net.device.DeviceStore;
24import org.onlab.onos.net.device.PortDescription;
25import org.onlab.onos.net.provider.ProviderId;
tom0872a172014-09-23 11:24:26 -070026import org.onlab.onos.store.impl.AbsentInvalidatingLoadingCache;
tomb41d1ac2014-09-24 01:51:24 -070027import org.onlab.onos.store.impl.AbstractDistributedStore;
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070028import org.onlab.onos.store.impl.OptionalCacheLoader;
tom0872a172014-09-23 11:24:26 -070029import org.slf4j.Logger;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070030
tom0872a172014-09-23 11:24:26 -070031import java.util.ArrayList;
32import java.util.Collections;
33import java.util.HashMap;
34import java.util.HashSet;
35import java.util.Iterator;
36import java.util.List;
37import java.util.Map;
38import java.util.Objects;
39import java.util.Set;
40
41import static com.google.common.base.Preconditions.checkArgument;
tomb41d1ac2014-09-24 01:51:24 -070042import static com.google.common.cache.CacheBuilder.newBuilder;
tom0872a172014-09-23 11:24:26 -070043import static org.onlab.onos.net.device.DeviceEvent.Type.*;
44import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070045
tom8cc5aa72014-09-19 15:14:43 -070046/**
47 * Manages inventory of infrastructure devices using Hazelcast-backed map.
48 */
49@Component(immediate = true)
50@Service
tomb41d1ac2014-09-24 01:51:24 -070051public class DistributedDeviceStore extends AbstractDistributedStore
52 implements DeviceStore {
tom8cc5aa72014-09-19 15:14:43 -070053
54 private final Logger log = getLogger(getClass());
55
56 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
57
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070058 // private IMap<DeviceId, DefaultDevice> cache;
59 private IMap<byte[], byte[]> rawDevices;
60 private LoadingCache<DeviceId, Optional<DefaultDevice>> devices;
61
62 // private IMap<DeviceId, MastershipRole> roles;
63 private IMap<byte[], byte[]> rawRoles;
64 private LoadingCache<DeviceId, Optional<MastershipRole>> roles;
65
66 // private ISet<DeviceId> availableDevices;
67 private ISet<byte[]> availableDevices;
68
69 // TODO DevicePorts is very inefficient consider restructuring.
70 // private IMap<DeviceId, Map<PortNumber, Port>> devicePorts;
71 private IMap<byte[], byte[]> rawDevicePorts;
72 private LoadingCache<DeviceId, Optional<Map<PortNumber, Port>>> devicePorts;
73
tom8cc5aa72014-09-19 15:14:43 -070074 @Activate
75 public void activate() {
tomb41d1ac2014-09-24 01:51:24 -070076 super.activate();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070077
78 // IMap event handler needs value
79 final boolean includeValue = true;
80
81 // TODO decide on Map name scheme to avoid collision
82 rawDevices = theInstance.getMap("devices");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070083 final OptionalCacheLoader<DeviceId, DefaultDevice> deviceLoader
tomb41d1ac2014-09-24 01:51:24 -070084 = new OptionalCacheLoader<>(storeService, rawDevices);
85 devices = new AbsentInvalidatingLoadingCache<>(newBuilder().build(deviceLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070086 // refresh/populate cache based on notification from other instance
tomb41d1ac2014-09-24 01:51:24 -070087 rawDevices.addEntryListener(new RemoteEventHandler<>(devices), includeValue);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070088
89 rawRoles = theInstance.getMap("roles");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070090 final OptionalCacheLoader<DeviceId, MastershipRole> rolesLoader
tomb41d1ac2014-09-24 01:51:24 -070091 = new OptionalCacheLoader<>(storeService, rawRoles);
92 roles = new AbsentInvalidatingLoadingCache<>(newBuilder().build(rolesLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070093 // refresh/populate cache based on notification from other instance
tomb41d1ac2014-09-24 01:51:24 -070094 rawRoles.addEntryListener(new RemoteEventHandler<>(roles), includeValue);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070095
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070096 // TODO cache availableDevices
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070097 availableDevices = theInstance.getSet("availableDevices");
98
99 rawDevicePorts = theInstance.getMap("devicePorts");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700100 final OptionalCacheLoader<DeviceId, Map<PortNumber, Port>> devicePortLoader
tomb41d1ac2014-09-24 01:51:24 -0700101 = new OptionalCacheLoader<>(storeService, rawDevicePorts);
102 devicePorts = new AbsentInvalidatingLoadingCache<>(newBuilder().build(devicePortLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700103 // refresh/populate cache based on notification from other instance
tomb41d1ac2014-09-24 01:51:24 -0700104 rawDevicePorts.addEntryListener(new RemoteEventHandler<>(devicePorts), includeValue);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700105
tomb41d1ac2014-09-24 01:51:24 -0700106 log.info("Started");
tom8cc5aa72014-09-19 15:14:43 -0700107 }
108
109 @Deactivate
110 public void deactivate() {
111 log.info("Stopped");
112 }
113
114 @Override
115 public int getDeviceCount() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700116 // TODO IMap size or cache size?
117 return rawDevices.size();
tom8cc5aa72014-09-19 15:14:43 -0700118 }
119
120 @Override
121 public Iterable<Device> getDevices() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700122// TODO Revisit if we ever need to do this.
123// log.info("{}:{}", rawMap.size(), cache.size());
124// if (rawMap.size() != cache.size()) {
125// for (Entry<byte[], byte[]> e : rawMap.entrySet()) {
126// final DeviceId key = deserialize(e.getKey());
127// final DefaultDevice val = deserialize(e.getValue());
128// cache.put(key, val);
129// }
130// }
131
132 // TODO builder v.s. copyOf. Guava semms to be using copyOf?
tom0872a172014-09-23 11:24:26 -0700133 Builder<Device> builder = ImmutableSet.builder();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700134 for (Optional<DefaultDevice> e : devices.asMap().values()) {
135 if (e.isPresent()) {
136 builder.add(e.get());
137 }
138 }
139 return builder.build();
tom8cc5aa72014-09-19 15:14:43 -0700140 }
141
142 @Override
143 public Device getDevice(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700144 // TODO revisit if ignoring exception is safe.
145 return devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700146 }
147
148 @Override
149 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700150 DeviceDescription deviceDescription) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700151 DefaultDevice device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700152 if (device == null) {
153 return createDevice(providerId, deviceId, deviceDescription);
154 }
155 return updateDevice(providerId, device, deviceDescription);
156 }
157
158 // Creates the device and returns the appropriate event if necessary.
159 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
160 DeviceDescription desc) {
161 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
162 desc.manufacturer(),
163 desc.hwVersion(), desc.swVersion(),
164 desc.serialNumber());
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700165
tom8cc5aa72014-09-19 15:14:43 -0700166 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700167 final byte[] deviceIdBytes = serialize(deviceId);
168 rawDevices.put(deviceIdBytes, serialize(device));
169 devices.put(deviceId, Optional.of(device));
170
171 availableDevices.add(deviceIdBytes);
tom8cc5aa72014-09-19 15:14:43 -0700172
173 // For now claim the device as a master automatically.
Ayaka Koshibef6021f32014-09-24 09:40:47 -0700174 //rawRoles.put(deviceIdBytes, serialize(MastershipRole.MASTER));
175 //roles.put(deviceId, Optional.of(MastershipRole.MASTER));
tom8cc5aa72014-09-19 15:14:43 -0700176 }
177 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
178 }
179
180 // Updates the device and returns the appropriate event if necessary.
181 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
182 DeviceDescription desc) {
183 // We allow only certain attributes to trigger update
184 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
tom0872a172014-09-23 11:24:26 -0700185 !Objects.equals(device.swVersion(), desc.swVersion())) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700186
tom8cc5aa72014-09-19 15:14:43 -0700187 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
188 desc.type(),
189 desc.manufacturer(),
190 desc.hwVersion(),
191 desc.swVersion(),
192 desc.serialNumber());
193 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700194 devices.put(device.id(), Optional.of(updated));
195 availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700196 }
197 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
198 }
199
200 // Otherwise merely attempt to change availability
201 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700202 boolean added = availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700203 return !added ? null :
204 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
205 }
206 }
207
208 @Override
209 public DeviceEvent markOffline(DeviceId deviceId) {
210 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700211 Device device = devices.getUnchecked(deviceId).orNull();
212 boolean removed = device != null && availableDevices.remove(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700213 return !removed ? null :
214 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
215 }
216 }
217
218 @Override
219 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700220 List<PortDescription> portDescriptions) {
tom8cc5aa72014-09-19 15:14:43 -0700221 List<DeviceEvent> events = new ArrayList<>();
222 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700223 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700224 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
225 Map<PortNumber, Port> ports = getPortMap(deviceId);
226
227 // Add new ports
228 Set<PortNumber> processed = new HashSet<>();
229 for (PortDescription portDescription : portDescriptions) {
230 Port port = ports.get(portDescription.portNumber());
231 events.add(port == null ?
232 createPort(device, portDescription, ports) :
233 updatePort(device, port, portDescription, ports));
234 processed.add(portDescription.portNumber());
235 }
236
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700237 updatePortMap(deviceId, ports);
238
tom8cc5aa72014-09-19 15:14:43 -0700239 events.addAll(pruneOldPorts(device, ports, processed));
240 }
241 return events;
242 }
243
244 // Creates a new port based on the port description adds it to the map and
245 // Returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700246 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700247 private DeviceEvent createPort(Device device, PortDescription portDescription,
248 Map<PortNumber, Port> ports) {
249 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
250 portDescription.isEnabled());
251 ports.put(port.number(), port);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700252 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700253 return new DeviceEvent(PORT_ADDED, device, port);
254 }
255
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700256 // Checks if the specified port requires update and if so, it replaces the
tom8cc5aa72014-09-19 15:14:43 -0700257 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700258 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700259 private DeviceEvent updatePort(Device device, Port port,
260 PortDescription portDescription,
261 Map<PortNumber, Port> ports) {
262 if (port.isEnabled() != portDescription.isEnabled()) {
263 DefaultPort updatedPort =
264 new DefaultPort(device, portDescription.portNumber(),
265 portDescription.isEnabled());
266 ports.put(port.number(), updatedPort);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700267 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700268 return new DeviceEvent(PORT_UPDATED, device, port);
269 }
270 return null;
271 }
272
273 // Prunes the specified list of ports based on which ports are in the
274 // processed list and returns list of corresponding events.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700275 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700276 private List<DeviceEvent> pruneOldPorts(Device device,
277 Map<PortNumber, Port> ports,
278 Set<PortNumber> processed) {
279 List<DeviceEvent> events = new ArrayList<>();
280 Iterator<PortNumber> iterator = ports.keySet().iterator();
281 while (iterator.hasNext()) {
282 PortNumber portNumber = iterator.next();
283 if (!processed.contains(portNumber)) {
284 events.add(new DeviceEvent(PORT_REMOVED, device,
285 ports.get(portNumber)));
286 iterator.remove();
287 }
288 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700289 if (!events.isEmpty()) {
290 updatePortMap(device.id(), ports);
291 }
tom8cc5aa72014-09-19 15:14:43 -0700292 return events;
293 }
294
295 // Gets the map of ports for the specified device; if one does not already
296 // exist, it creates and registers a new one.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700297 // WARN: returned value is a copy, changes made to the Map
298 // needs to be written back using updatePortMap
299 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700300 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700301 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700302 if (ports == null) {
303 ports = new HashMap<>();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700304 // this probably is waste of time in most cases.
305 updatePortMap(deviceId, ports);
tom8cc5aa72014-09-19 15:14:43 -0700306 }
307 return ports;
308 }
309
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700310 //@GuardedBy("this")
311 private void updatePortMap(DeviceId deviceId, Map<PortNumber, Port> ports) {
312 rawDevicePorts.put(serialize(deviceId), serialize(ports));
313 devicePorts.put(deviceId, Optional.of(ports));
314 }
315
tom8cc5aa72014-09-19 15:14:43 -0700316 @Override
317 public DeviceEvent updatePortStatus(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700318 PortDescription portDescription) {
tom8cc5aa72014-09-19 15:14:43 -0700319 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700320 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700321 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
322 Map<PortNumber, Port> ports = getPortMap(deviceId);
323 Port port = ports.get(portDescription.portNumber());
324 return updatePort(device, port, portDescription, ports);
325 }
326 }
327
328 @Override
329 public List<Port> getPorts(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700330 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
331 return ports == null ? Collections.<Port>emptyList() : ImmutableList.copyOf(ports.values());
tom8cc5aa72014-09-19 15:14:43 -0700332 }
333
334 @Override
335 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700336 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700337 return ports == null ? null : ports.get(portNumber);
338 }
339
340 @Override
341 public boolean isAvailable(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700342 return availableDevices.contains(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700343 }
344
345 @Override
tom8cc5aa72014-09-19 15:14:43 -0700346 public DeviceEvent removeDevice(DeviceId deviceId) {
347 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700348 byte[] deviceIdBytes = serialize(deviceId);
349 rawRoles.remove(deviceIdBytes);
350 roles.invalidate(deviceId);
351
352 // TODO conditional remove?
353 Device device = deserialize(rawDevices.remove(deviceIdBytes));
354 devices.invalidate(deviceId);
tom8cc5aa72014-09-19 15:14:43 -0700355 return device == null ? null :
356 new DeviceEvent(DEVICE_REMOVED, device, null);
357 }
358 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700359
360 // TODO cache serialized DeviceID if we suffer from serialization cost
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700361
tom8cc5aa72014-09-19 15:14:43 -0700362}