blob: 52e8ed0f8986faeaaa2b799abc82629c0df2fbb5 [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() {
113 log.info("Stopped");
114 }
115
116 @Override
117 public int getDeviceCount() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700118 // TODO IMap size or cache size?
119 return rawDevices.size();
tom8cc5aa72014-09-19 15:14:43 -0700120 }
121
122 @Override
123 public Iterable<Device> getDevices() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700124// TODO Revisit if we ever need to do this.
125// log.info("{}:{}", rawMap.size(), cache.size());
126// if (rawMap.size() != cache.size()) {
127// for (Entry<byte[], byte[]> e : rawMap.entrySet()) {
128// final DeviceId key = deserialize(e.getKey());
129// final DefaultDevice val = deserialize(e.getValue());
130// cache.put(key, val);
131// }
132// }
133
134 // TODO builder v.s. copyOf. Guava semms to be using copyOf?
tom0872a172014-09-23 11:24:26 -0700135 Builder<Device> builder = ImmutableSet.builder();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700136 for (Optional<DefaultDevice> e : devices.asMap().values()) {
137 if (e.isPresent()) {
138 builder.add(e.get());
139 }
140 }
141 return builder.build();
tom8cc5aa72014-09-19 15:14:43 -0700142 }
143
144 @Override
145 public Device getDevice(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700146 // TODO revisit if ignoring exception is safe.
147 return devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700148 }
149
150 @Override
151 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700152 DeviceDescription deviceDescription) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700153 DefaultDevice device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700154 if (device == null) {
155 return createDevice(providerId, deviceId, deviceDescription);
156 }
157 return updateDevice(providerId, device, deviceDescription);
158 }
159
160 // Creates the device and returns the appropriate event if necessary.
161 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
162 DeviceDescription desc) {
163 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
164 desc.manufacturer(),
165 desc.hwVersion(), desc.swVersion(),
166 desc.serialNumber());
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700167
tom8cc5aa72014-09-19 15:14:43 -0700168 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700169 final byte[] deviceIdBytes = serialize(deviceId);
170 rawDevices.put(deviceIdBytes, serialize(device));
171 devices.put(deviceId, Optional.of(device));
172
173 availableDevices.add(deviceIdBytes);
tom8cc5aa72014-09-19 15:14:43 -0700174
175 // For now claim the device as a master automatically.
Ayaka Koshibef6021f32014-09-24 09:40:47 -0700176 //rawRoles.put(deviceIdBytes, serialize(MastershipRole.MASTER));
177 //roles.put(deviceId, Optional.of(MastershipRole.MASTER));
tom8cc5aa72014-09-19 15:14:43 -0700178 }
179 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
180 }
181
182 // Updates the device and returns the appropriate event if necessary.
183 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
184 DeviceDescription desc) {
185 // We allow only certain attributes to trigger update
186 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
tom0872a172014-09-23 11:24:26 -0700187 !Objects.equals(device.swVersion(), desc.swVersion())) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700188
tom8cc5aa72014-09-19 15:14:43 -0700189 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
190 desc.type(),
191 desc.manufacturer(),
192 desc.hwVersion(),
193 desc.swVersion(),
194 desc.serialNumber());
195 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700196 devices.put(device.id(), Optional.of(updated));
197 availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700198 }
199 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
200 }
201
202 // Otherwise merely attempt to change availability
203 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700204 boolean added = availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700205 return !added ? null :
206 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
207 }
208 }
209
210 @Override
211 public DeviceEvent markOffline(DeviceId deviceId) {
212 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700213 Device device = devices.getUnchecked(deviceId).orNull();
214 boolean removed = device != null && availableDevices.remove(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700215 return !removed ? null :
216 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
217 }
218 }
219
220 @Override
221 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700222 List<PortDescription> portDescriptions) {
tom8cc5aa72014-09-19 15:14:43 -0700223 List<DeviceEvent> events = new ArrayList<>();
224 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700225 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700226 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
227 Map<PortNumber, Port> ports = getPortMap(deviceId);
228
229 // Add new ports
230 Set<PortNumber> processed = new HashSet<>();
231 for (PortDescription portDescription : portDescriptions) {
232 Port port = ports.get(portDescription.portNumber());
233 events.add(port == null ?
234 createPort(device, portDescription, ports) :
235 updatePort(device, port, portDescription, ports));
236 processed.add(portDescription.portNumber());
237 }
238
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700239 updatePortMap(deviceId, ports);
240
tom8cc5aa72014-09-19 15:14:43 -0700241 events.addAll(pruneOldPorts(device, ports, processed));
242 }
243 return events;
244 }
245
246 // Creates a new port based on the port description adds it to the map and
247 // Returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700248 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700249 private DeviceEvent createPort(Device device, PortDescription portDescription,
250 Map<PortNumber, Port> ports) {
251 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
252 portDescription.isEnabled());
253 ports.put(port.number(), port);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700254 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700255 return new DeviceEvent(PORT_ADDED, device, port);
256 }
257
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700258 // Checks if the specified port requires update and if so, it replaces the
tom8cc5aa72014-09-19 15:14:43 -0700259 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700260 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700261 private DeviceEvent updatePort(Device device, Port port,
262 PortDescription portDescription,
263 Map<PortNumber, Port> ports) {
264 if (port.isEnabled() != portDescription.isEnabled()) {
265 DefaultPort updatedPort =
266 new DefaultPort(device, portDescription.portNumber(),
267 portDescription.isEnabled());
268 ports.put(port.number(), updatedPort);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700269 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700270 return new DeviceEvent(PORT_UPDATED, device, port);
271 }
272 return null;
273 }
274
275 // Prunes the specified list of ports based on which ports are in the
276 // processed list and returns list of corresponding events.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700277 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700278 private List<DeviceEvent> pruneOldPorts(Device device,
279 Map<PortNumber, Port> ports,
280 Set<PortNumber> processed) {
281 List<DeviceEvent> events = new ArrayList<>();
282 Iterator<PortNumber> iterator = ports.keySet().iterator();
283 while (iterator.hasNext()) {
284 PortNumber portNumber = iterator.next();
285 if (!processed.contains(portNumber)) {
286 events.add(new DeviceEvent(PORT_REMOVED, device,
287 ports.get(portNumber)));
288 iterator.remove();
289 }
290 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700291 if (!events.isEmpty()) {
292 updatePortMap(device.id(), ports);
293 }
tom8cc5aa72014-09-19 15:14:43 -0700294 return events;
295 }
296
297 // Gets the map of ports for the specified device; if one does not already
298 // exist, it creates and registers a new one.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700299 // WARN: returned value is a copy, changes made to the Map
300 // needs to be written back using updatePortMap
301 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700302 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700303 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700304 if (ports == null) {
305 ports = new HashMap<>();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700306 // this probably is waste of time in most cases.
307 updatePortMap(deviceId, ports);
tom8cc5aa72014-09-19 15:14:43 -0700308 }
309 return ports;
310 }
311
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700312 //@GuardedBy("this")
313 private void updatePortMap(DeviceId deviceId, Map<PortNumber, Port> ports) {
314 rawDevicePorts.put(serialize(deviceId), serialize(ports));
315 devicePorts.put(deviceId, Optional.of(ports));
316 }
317
tom8cc5aa72014-09-19 15:14:43 -0700318 @Override
319 public DeviceEvent updatePortStatus(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700320 PortDescription portDescription) {
tom8cc5aa72014-09-19 15:14:43 -0700321 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700322 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700323 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
324 Map<PortNumber, Port> ports = getPortMap(deviceId);
325 Port port = ports.get(portDescription.portNumber());
326 return updatePort(device, port, portDescription, ports);
327 }
328 }
329
330 @Override
331 public List<Port> getPorts(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700332 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
333 return ports == null ? Collections.<Port>emptyList() : ImmutableList.copyOf(ports.values());
tom8cc5aa72014-09-19 15:14:43 -0700334 }
335
336 @Override
337 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700338 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700339 return ports == null ? null : ports.get(portNumber);
340 }
341
342 @Override
343 public boolean isAvailable(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700344 return availableDevices.contains(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700345 }
346
347 @Override
tom8cc5aa72014-09-19 15:14:43 -0700348 public DeviceEvent removeDevice(DeviceId deviceId) {
349 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700350 byte[] deviceIdBytes = serialize(deviceId);
351 rawRoles.remove(deviceIdBytes);
352 roles.invalidate(deviceId);
353
354 // TODO conditional remove?
355 Device device = deserialize(rawDevices.remove(deviceIdBytes));
356 devices.invalidate(deviceId);
tom8cc5aa72014-09-19 15:14:43 -0700357 return device == null ? null :
358 new DeviceEvent(DEVICE_REMOVED, device, null);
359 }
360 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700361
362 // TODO cache serialized DeviceID if we suffer from serialization cost
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700363
tom8cc5aa72014-09-19 15:14:43 -0700364}