blob: d73dd9e6158f75f60bc28636a0349da97c84472f [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;
tom0755a362014-09-24 11:54:43 -070024import org.onlab.onos.net.device.DeviceStoreDelegate;
tom0872a172014-09-23 11:24:26 -070025import org.onlab.onos.net.device.PortDescription;
26import org.onlab.onos.net.provider.ProviderId;
tom0872a172014-09-23 11:24:26 -070027import org.onlab.onos.store.impl.AbsentInvalidatingLoadingCache;
tomb41d1ac2014-09-24 01:51:24 -070028import org.onlab.onos.store.impl.AbstractDistributedStore;
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070029import org.onlab.onos.store.impl.OptionalCacheLoader;
tom0872a172014-09-23 11:24:26 -070030import org.slf4j.Logger;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070031
tom0872a172014-09-23 11:24:26 -070032import java.util.ArrayList;
33import java.util.Collections;
34import java.util.HashMap;
35import java.util.HashSet;
36import java.util.Iterator;
37import java.util.List;
38import java.util.Map;
39import java.util.Objects;
40import java.util.Set;
41
42import static com.google.common.base.Preconditions.checkArgument;
tomb41d1ac2014-09-24 01:51:24 -070043import static com.google.common.cache.CacheBuilder.newBuilder;
tom0872a172014-09-23 11:24:26 -070044import static org.onlab.onos.net.device.DeviceEvent.Type.*;
45import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070046
tom8cc5aa72014-09-19 15:14:43 -070047/**
48 * Manages inventory of infrastructure devices using Hazelcast-backed map.
49 */
50@Component(immediate = true)
51@Service
tom0755a362014-09-24 11:54:43 -070052public class DistributedDeviceStore
53 extends AbstractDistributedStore<DeviceEvent, DeviceStoreDelegate>
tomb41d1ac2014-09-24 01:51:24 -070054 implements DeviceStore {
tom8cc5aa72014-09-19 15:14:43 -070055
56 private final Logger log = getLogger(getClass());
57
58 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
59
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070060 // private IMap<DeviceId, DefaultDevice> cache;
61 private IMap<byte[], byte[]> rawDevices;
62 private LoadingCache<DeviceId, Optional<DefaultDevice>> devices;
63
64 // private IMap<DeviceId, MastershipRole> roles;
65 private IMap<byte[], byte[]> rawRoles;
66 private LoadingCache<DeviceId, Optional<MastershipRole>> roles;
67
68 // private ISet<DeviceId> availableDevices;
69 private ISet<byte[]> availableDevices;
70
71 // TODO DevicePorts is very inefficient consider restructuring.
72 // private IMap<DeviceId, Map<PortNumber, Port>> devicePorts;
73 private IMap<byte[], byte[]> rawDevicePorts;
74 private LoadingCache<DeviceId, Optional<Map<PortNumber, Port>>> devicePorts;
75
tom8cc5aa72014-09-19 15:14:43 -070076 @Activate
77 public void activate() {
tomb41d1ac2014-09-24 01:51:24 -070078 super.activate();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070079
80 // IMap event handler needs value
81 final boolean includeValue = true;
82
83 // TODO decide on Map name scheme to avoid collision
84 rawDevices = theInstance.getMap("devices");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070085 final OptionalCacheLoader<DeviceId, DefaultDevice> deviceLoader
tomb41d1ac2014-09-24 01:51:24 -070086 = new OptionalCacheLoader<>(storeService, rawDevices);
87 devices = new AbsentInvalidatingLoadingCache<>(newBuilder().build(deviceLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070088 // refresh/populate cache based on notification from other instance
tomb41d1ac2014-09-24 01:51:24 -070089 rawDevices.addEntryListener(new RemoteEventHandler<>(devices), includeValue);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070090
91 rawRoles = theInstance.getMap("roles");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070092 final OptionalCacheLoader<DeviceId, MastershipRole> rolesLoader
tomb41d1ac2014-09-24 01:51:24 -070093 = new OptionalCacheLoader<>(storeService, rawRoles);
94 roles = new AbsentInvalidatingLoadingCache<>(newBuilder().build(rolesLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070095 // refresh/populate cache based on notification from other instance
tomb41d1ac2014-09-24 01:51:24 -070096 rawRoles.addEntryListener(new RemoteEventHandler<>(roles), includeValue);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070097
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070098 // TODO cache availableDevices
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070099 availableDevices = theInstance.getSet("availableDevices");
100
101 rawDevicePorts = theInstance.getMap("devicePorts");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700102 final OptionalCacheLoader<DeviceId, Map<PortNumber, Port>> devicePortLoader
tomb41d1ac2014-09-24 01:51:24 -0700103 = new OptionalCacheLoader<>(storeService, rawDevicePorts);
104 devicePorts = new AbsentInvalidatingLoadingCache<>(newBuilder().build(devicePortLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700105 // refresh/populate cache based on notification from other instance
tomb41d1ac2014-09-24 01:51:24 -0700106 rawDevicePorts.addEntryListener(new RemoteEventHandler<>(devicePorts), includeValue);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700107
tomb41d1ac2014-09-24 01:51:24 -0700108 log.info("Started");
tom8cc5aa72014-09-19 15:14:43 -0700109 }
110
111 @Deactivate
112 public void deactivate() {
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -0700113
tom8cc5aa72014-09-19 15:14:43 -0700114 log.info("Stopped");
115 }
116
117 @Override
118 public int getDeviceCount() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700119 // TODO IMap size or cache size?
120 return rawDevices.size();
tom8cc5aa72014-09-19 15:14:43 -0700121 }
122
123 @Override
124 public Iterable<Device> getDevices() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700125// TODO Revisit if we ever need to do this.
126// log.info("{}:{}", rawMap.size(), cache.size());
127// if (rawMap.size() != cache.size()) {
128// for (Entry<byte[], byte[]> e : rawMap.entrySet()) {
129// final DeviceId key = deserialize(e.getKey());
130// final DefaultDevice val = deserialize(e.getValue());
131// cache.put(key, val);
132// }
133// }
134
135 // TODO builder v.s. copyOf. Guava semms to be using copyOf?
tom0872a172014-09-23 11:24:26 -0700136 Builder<Device> builder = ImmutableSet.builder();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700137 for (Optional<DefaultDevice> e : devices.asMap().values()) {
138 if (e.isPresent()) {
139 builder.add(e.get());
140 }
141 }
142 return builder.build();
tom8cc5aa72014-09-19 15:14:43 -0700143 }
144
145 @Override
146 public Device getDevice(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700147 // TODO revisit if ignoring exception is safe.
148 return devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700149 }
150
151 @Override
152 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700153 DeviceDescription deviceDescription) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700154 DefaultDevice device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700155 if (device == null) {
156 return createDevice(providerId, deviceId, deviceDescription);
157 }
158 return updateDevice(providerId, device, deviceDescription);
159 }
160
161 // Creates the device and returns the appropriate event if necessary.
162 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
163 DeviceDescription desc) {
164 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
165 desc.manufacturer(),
166 desc.hwVersion(), desc.swVersion(),
167 desc.serialNumber());
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700168
tom8cc5aa72014-09-19 15:14:43 -0700169 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700170 final byte[] deviceIdBytes = serialize(deviceId);
171 rawDevices.put(deviceIdBytes, serialize(device));
172 devices.put(deviceId, Optional.of(device));
173
174 availableDevices.add(deviceIdBytes);
tom8cc5aa72014-09-19 15:14:43 -0700175
176 // For now claim the device as a master automatically.
Ayaka Koshibef6021f32014-09-24 09:40:47 -0700177 //rawRoles.put(deviceIdBytes, serialize(MastershipRole.MASTER));
178 //roles.put(deviceId, Optional.of(MastershipRole.MASTER));
tom8cc5aa72014-09-19 15:14:43 -0700179 }
180 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
181 }
182
183 // Updates the device and returns the appropriate event if necessary.
184 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
185 DeviceDescription desc) {
186 // We allow only certain attributes to trigger update
187 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
tom0872a172014-09-23 11:24:26 -0700188 !Objects.equals(device.swVersion(), desc.swVersion())) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700189
tom8cc5aa72014-09-19 15:14:43 -0700190 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
191 desc.type(),
192 desc.manufacturer(),
193 desc.hwVersion(),
194 desc.swVersion(),
195 desc.serialNumber());
196 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700197 devices.put(device.id(), Optional.of(updated));
198 availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700199 }
200 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
201 }
202
203 // Otherwise merely attempt to change availability
204 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700205 boolean added = availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700206 return !added ? null :
207 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
208 }
209 }
210
211 @Override
212 public DeviceEvent markOffline(DeviceId deviceId) {
213 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700214 Device device = devices.getUnchecked(deviceId).orNull();
215 boolean removed = device != null && availableDevices.remove(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700216 return !removed ? null :
217 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
218 }
219 }
220
221 @Override
222 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700223 List<PortDescription> portDescriptions) {
tom8cc5aa72014-09-19 15:14:43 -0700224 List<DeviceEvent> events = new ArrayList<>();
225 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700226 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700227 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
228 Map<PortNumber, Port> ports = getPortMap(deviceId);
229
230 // Add new ports
231 Set<PortNumber> processed = new HashSet<>();
232 for (PortDescription portDescription : portDescriptions) {
233 Port port = ports.get(portDescription.portNumber());
234 events.add(port == null ?
235 createPort(device, portDescription, ports) :
236 updatePort(device, port, portDescription, ports));
237 processed.add(portDescription.portNumber());
238 }
239
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700240 updatePortMap(deviceId, ports);
241
tom8cc5aa72014-09-19 15:14:43 -0700242 events.addAll(pruneOldPorts(device, ports, processed));
243 }
244 return events;
245 }
246
247 // Creates a new port based on the port description adds it to the map and
248 // Returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700249 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700250 private DeviceEvent createPort(Device device, PortDescription portDescription,
251 Map<PortNumber, Port> ports) {
252 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
253 portDescription.isEnabled());
254 ports.put(port.number(), port);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700255 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700256 return new DeviceEvent(PORT_ADDED, device, port);
257 }
258
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700259 // Checks if the specified port requires update and if so, it replaces the
tom8cc5aa72014-09-19 15:14:43 -0700260 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700261 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700262 private DeviceEvent updatePort(Device device, Port port,
263 PortDescription portDescription,
264 Map<PortNumber, Port> ports) {
265 if (port.isEnabled() != portDescription.isEnabled()) {
266 DefaultPort updatedPort =
267 new DefaultPort(device, portDescription.portNumber(),
268 portDescription.isEnabled());
269 ports.put(port.number(), updatedPort);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700270 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700271 return new DeviceEvent(PORT_UPDATED, device, port);
272 }
273 return null;
274 }
275
276 // Prunes the specified list of ports based on which ports are in the
277 // processed list and returns list of corresponding events.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700278 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700279 private List<DeviceEvent> pruneOldPorts(Device device,
280 Map<PortNumber, Port> ports,
281 Set<PortNumber> processed) {
282 List<DeviceEvent> events = new ArrayList<>();
283 Iterator<PortNumber> iterator = ports.keySet().iterator();
284 while (iterator.hasNext()) {
285 PortNumber portNumber = iterator.next();
286 if (!processed.contains(portNumber)) {
287 events.add(new DeviceEvent(PORT_REMOVED, device,
288 ports.get(portNumber)));
289 iterator.remove();
290 }
291 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700292 if (!events.isEmpty()) {
293 updatePortMap(device.id(), ports);
294 }
tom8cc5aa72014-09-19 15:14:43 -0700295 return events;
296 }
297
298 // Gets the map of ports for the specified device; if one does not already
299 // exist, it creates and registers a new one.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700300 // WARN: returned value is a copy, changes made to the Map
301 // needs to be written back using updatePortMap
302 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700303 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700304 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700305 if (ports == null) {
306 ports = new HashMap<>();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700307 // this probably is waste of time in most cases.
308 updatePortMap(deviceId, ports);
tom8cc5aa72014-09-19 15:14:43 -0700309 }
310 return ports;
311 }
312
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700313 //@GuardedBy("this")
314 private void updatePortMap(DeviceId deviceId, Map<PortNumber, Port> ports) {
315 rawDevicePorts.put(serialize(deviceId), serialize(ports));
316 devicePorts.put(deviceId, Optional.of(ports));
317 }
318
tom8cc5aa72014-09-19 15:14:43 -0700319 @Override
320 public DeviceEvent updatePortStatus(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700321 PortDescription portDescription) {
tom8cc5aa72014-09-19 15:14:43 -0700322 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700323 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700324 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
325 Map<PortNumber, Port> ports = getPortMap(deviceId);
326 Port port = ports.get(portDescription.portNumber());
327 return updatePort(device, port, portDescription, ports);
328 }
329 }
330
331 @Override
332 public List<Port> getPorts(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700333 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
334 return ports == null ? Collections.<Port>emptyList() : ImmutableList.copyOf(ports.values());
tom8cc5aa72014-09-19 15:14:43 -0700335 }
336
337 @Override
338 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700339 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700340 return ports == null ? null : ports.get(portNumber);
341 }
342
343 @Override
344 public boolean isAvailable(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700345 return availableDevices.contains(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700346 }
347
348 @Override
tom8cc5aa72014-09-19 15:14:43 -0700349 public DeviceEvent removeDevice(DeviceId deviceId) {
350 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700351 byte[] deviceIdBytes = serialize(deviceId);
352 rawRoles.remove(deviceIdBytes);
353 roles.invalidate(deviceId);
354
355 // TODO conditional remove?
356 Device device = deserialize(rawDevices.remove(deviceIdBytes));
357 devices.invalidate(deviceId);
tom8cc5aa72014-09-19 15:14:43 -0700358 return device == null ? null :
359 new DeviceEvent(DEVICE_REMOVED, device, null);
360 }
361 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700362
363 // TODO cache serialized DeviceID if we suffer from serialization cost
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700364
tom8cc5aa72014-09-19 15:14:43 -0700365}