tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 2 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkArgument; |
| 4 | import static com.google.common.base.Preconditions.checkNotNull; |
| 5 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 6 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED; |
| 7 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
| 8 | import static org.onlab.onos.net.device.DeviceEvent.Type.PORT_ADDED; |
| 9 | import static org.onlab.onos.net.device.DeviceEvent.Type.PORT_REMOVED; |
| 10 | import static org.onlab.onos.net.device.DeviceEvent.Type.PORT_UPDATED; |
| 11 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 12 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 13 | import java.net.URI; |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 14 | import java.util.ArrayList; |
| 15 | import java.util.Collections; |
| 16 | import java.util.HashMap; |
| 17 | import java.util.HashSet; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | import java.util.Objects; |
| 22 | import java.util.Set; |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 23 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onlab.onos.net.DefaultDevice; |
| 31 | import org.onlab.onos.net.DefaultPort; |
| 32 | import org.onlab.onos.net.Device; |
| 33 | import org.onlab.onos.net.DeviceId; |
| 34 | import org.onlab.onos.net.Element; |
| 35 | import org.onlab.onos.net.MastershipRole; |
| 36 | import org.onlab.onos.net.Port; |
| 37 | import org.onlab.onos.net.PortNumber; |
| 38 | import org.onlab.onos.net.device.DeviceDescription; |
| 39 | import org.onlab.onos.net.device.DeviceEvent; |
| 40 | import org.onlab.onos.net.device.DeviceStore; |
| 41 | import org.onlab.onos.net.device.PortDescription; |
| 42 | import org.onlab.onos.net.provider.ProviderId; |
| 43 | import org.onlab.util.KryoPool; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import com.google.common.base.Optional; |
| 47 | import com.google.common.cache.CacheBuilder; |
| 48 | import com.google.common.cache.CacheLoader; |
| 49 | import com.google.common.cache.LoadingCache; |
| 50 | import com.google.common.collect.ImmutableList; |
| 51 | import com.google.common.collect.ImmutableSet; |
| 52 | import com.google.common.collect.ImmutableSet.Builder; |
| 53 | import com.hazelcast.core.EntryAdapter; |
| 54 | import com.hazelcast.core.EntryEvent; |
| 55 | import com.hazelcast.core.HazelcastInstance; |
| 56 | import com.hazelcast.core.IMap; |
| 57 | import com.hazelcast.core.ISet; |
| 58 | import com.hazelcast.core.MapEvent; |
| 59 | |
| 60 | import de.javakaffee.kryoserializers.URISerializer; |
| 61 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Manages inventory of infrastructure devices using Hazelcast-backed map. |
| 65 | */ |
| 66 | @Component(immediate = true) |
| 67 | @Service |
| 68 | public class DistributedDeviceStore implements DeviceStore { |
| 69 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 70 | /** |
| 71 | * An IMap EntryListener, which reflects each remote event to cache. |
| 72 | * |
| 73 | * @param <K> IMap key type after deserialization |
| 74 | * @param <V> IMap value type after deserialization |
| 75 | */ |
| 76 | public static final class RemoteEventHandler<K, V> extends |
| 77 | EntryAdapter<byte[], byte[]> { |
| 78 | |
| 79 | private LoadingCache<K, Optional<V>> cache; |
| 80 | |
| 81 | /** |
| 82 | * Constructor. |
| 83 | * |
| 84 | * @param cache cache to update |
| 85 | */ |
| 86 | public RemoteEventHandler( |
| 87 | LoadingCache<K, Optional<V>> cache) { |
| 88 | this.cache = checkNotNull(cache); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void mapCleared(MapEvent event) { |
| 93 | cache.invalidateAll(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void entryUpdated(EntryEvent<byte[], byte[]> event) { |
| 98 | cache.put(POOL.<K>deserialize(event.getKey()), |
| 99 | Optional.of(POOL.<V>deserialize( |
| 100 | event.getValue()))); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void entryRemoved(EntryEvent<byte[], byte[]> event) { |
| 105 | cache.invalidate(POOL.<DeviceId>deserialize(event.getKey())); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void entryAdded(EntryEvent<byte[], byte[]> event) { |
| 110 | entryUpdated(event); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * CacheLoader to wrap Map value with Optional, |
| 116 | * to handle negative hit on underlying IMap. |
| 117 | * |
| 118 | * @param <K> IMap key type after deserialization |
| 119 | * @param <V> IMap value type after deserialization |
| 120 | */ |
| 121 | public static final class OptionalCacheLoader<K, V> extends |
| 122 | CacheLoader<K, Optional<V>> { |
| 123 | |
| 124 | private IMap<byte[], byte[]> rawMap; |
| 125 | |
| 126 | /** |
| 127 | * Constructor. |
| 128 | * |
| 129 | * @param rawMap underlying IMap |
| 130 | */ |
| 131 | public OptionalCacheLoader(IMap<byte[], byte[]> rawMap) { |
| 132 | this.rawMap = checkNotNull(rawMap); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Optional<V> load(K key) throws Exception { |
| 137 | byte[] keyBytes = serialize(key); |
| 138 | byte[] valBytes = rawMap.get(keyBytes); |
| 139 | if (valBytes == null) { |
| 140 | return Optional.absent(); |
| 141 | } |
| 142 | V dev = deserialize(valBytes); |
| 143 | return Optional.of(dev); |
| 144 | } |
| 145 | } |
| 146 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 147 | private final Logger log = getLogger(getClass()); |
| 148 | |
| 149 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 150 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 151 | // FIXME Slice out types used in common to separate pool/namespace. |
| 152 | private static final KryoPool POOL = KryoPool.newBuilder() |
| 153 | .register(URI.class, new URISerializer()) |
| 154 | .register( |
| 155 | ArrayList.class, |
| 156 | |
| 157 | ProviderId.class, |
| 158 | Device.Type.class, |
| 159 | |
| 160 | DeviceId.class, |
| 161 | DefaultDevice.class, |
| 162 | MastershipRole.class, |
| 163 | HashMap.class, |
| 164 | Port.class, |
| 165 | Element.class |
| 166 | ) |
| 167 | .register(PortNumber.class, new PortNumberSerializer()) |
| 168 | .register(DefaultPort.class, new DefaultPortSerializer()) |
| 169 | .build() |
| 170 | .populate(10); |
| 171 | |
| 172 | // private IMap<DeviceId, DefaultDevice> cache; |
| 173 | private IMap<byte[], byte[]> rawDevices; |
| 174 | private LoadingCache<DeviceId, Optional<DefaultDevice>> devices; |
| 175 | |
| 176 | // private IMap<DeviceId, MastershipRole> roles; |
| 177 | private IMap<byte[], byte[]> rawRoles; |
| 178 | private LoadingCache<DeviceId, Optional<MastershipRole>> roles; |
| 179 | |
| 180 | // private ISet<DeviceId> availableDevices; |
| 181 | private ISet<byte[]> availableDevices; |
| 182 | |
| 183 | // TODO DevicePorts is very inefficient consider restructuring. |
| 184 | // private IMap<DeviceId, Map<PortNumber, Port>> devicePorts; |
| 185 | private IMap<byte[], byte[]> rawDevicePorts; |
| 186 | private LoadingCache<DeviceId, Optional<Map<PortNumber, Port>>> devicePorts; |
| 187 | |
| 188 | // FIXME change to protected once we remove DistributedDeviceManagerTest. |
| 189 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 190 | /*protected*/public HazelcastInstance theInstance; |
| 191 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 192 | |
| 193 | @Activate |
| 194 | public void activate() { |
| 195 | log.info("Started"); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 196 | |
| 197 | // IMap event handler needs value |
| 198 | final boolean includeValue = true; |
| 199 | |
| 200 | // TODO decide on Map name scheme to avoid collision |
| 201 | rawDevices = theInstance.getMap("devices"); |
| 202 | devices = new AbsentInvalidatingLoadingCache<DeviceId, DefaultDevice>( |
| 203 | CacheBuilder.newBuilder() |
| 204 | .build(new OptionalCacheLoader<DeviceId, DefaultDevice>(rawDevices))); |
| 205 | // refresh/populate cache based on notification from other instance |
| 206 | rawDevices.addEntryListener( |
| 207 | new RemoteEventHandler<DeviceId, DefaultDevice>(devices), |
| 208 | includeValue); |
| 209 | |
| 210 | rawRoles = theInstance.getMap("roles"); |
| 211 | roles = new AbsentInvalidatingLoadingCache<DeviceId, MastershipRole>( |
| 212 | CacheBuilder.newBuilder() |
| 213 | .build(new OptionalCacheLoader<DeviceId, MastershipRole>(rawRoles))); |
| 214 | // refresh/populate cache based on notification from other instance |
| 215 | rawRoles.addEntryListener( |
| 216 | new RemoteEventHandler<DeviceId, MastershipRole>(roles), |
| 217 | includeValue); |
| 218 | |
| 219 | // TODO cache avai |
| 220 | availableDevices = theInstance.getSet("availableDevices"); |
| 221 | |
| 222 | rawDevicePorts = theInstance.getMap("devicePorts"); |
| 223 | devicePorts = new AbsentInvalidatingLoadingCache<DeviceId, Map<PortNumber, Port>>( |
| 224 | CacheBuilder.newBuilder() |
| 225 | .build(new OptionalCacheLoader<DeviceId, Map<PortNumber, Port>>(rawDevicePorts))); |
| 226 | // refresh/populate cache based on notification from other instance |
| 227 | rawDevicePorts.addEntryListener( |
| 228 | new RemoteEventHandler<DeviceId, Map<PortNumber, Port>>(devicePorts), |
| 229 | includeValue); |
| 230 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Deactivate |
| 234 | public void deactivate() { |
| 235 | log.info("Stopped"); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public int getDeviceCount() { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 240 | // TODO IMap size or cache size? |
| 241 | return rawDevices.size(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public Iterable<Device> getDevices() { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 246 | // TODO Revisit if we ever need to do this. |
| 247 | // log.info("{}:{}", rawMap.size(), cache.size()); |
| 248 | // if (rawMap.size() != cache.size()) { |
| 249 | // for (Entry<byte[], byte[]> e : rawMap.entrySet()) { |
| 250 | // final DeviceId key = deserialize(e.getKey()); |
| 251 | // final DefaultDevice val = deserialize(e.getValue()); |
| 252 | // cache.put(key, val); |
| 253 | // } |
| 254 | // } |
| 255 | |
| 256 | // TODO builder v.s. copyOf. Guava semms to be using copyOf? |
| 257 | Builder<Device> builder = ImmutableSet.<Device>builder(); |
| 258 | for (Optional<DefaultDevice> e : devices.asMap().values()) { |
| 259 | if (e.isPresent()) { |
| 260 | builder.add(e.get()); |
| 261 | } |
| 262 | } |
| 263 | return builder.build(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public Device getDevice(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 268 | // TODO revisit if ignoring exception is safe. |
| 269 | return devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 274 | DeviceDescription deviceDescription) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 275 | DefaultDevice device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 276 | if (device == null) { |
| 277 | return createDevice(providerId, deviceId, deviceDescription); |
| 278 | } |
| 279 | return updateDevice(providerId, device, deviceDescription); |
| 280 | } |
| 281 | |
| 282 | // Creates the device and returns the appropriate event if necessary. |
| 283 | private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId, |
| 284 | DeviceDescription desc) { |
| 285 | DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(), |
| 286 | desc.manufacturer(), |
| 287 | desc.hwVersion(), desc.swVersion(), |
| 288 | desc.serialNumber()); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 289 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 290 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 291 | final byte[] deviceIdBytes = serialize(deviceId); |
| 292 | rawDevices.put(deviceIdBytes, serialize(device)); |
| 293 | devices.put(deviceId, Optional.of(device)); |
| 294 | |
| 295 | availableDevices.add(deviceIdBytes); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 296 | |
| 297 | // For now claim the device as a master automatically. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 298 | rawRoles.put(deviceIdBytes, serialize(MastershipRole.MASTER)); |
| 299 | roles.put(deviceId, Optional.of(MastershipRole.MASTER)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 300 | } |
| 301 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null); |
| 302 | } |
| 303 | |
| 304 | // Updates the device and returns the appropriate event if necessary. |
| 305 | private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device, |
| 306 | DeviceDescription desc) { |
| 307 | // We allow only certain attributes to trigger update |
| 308 | if (!Objects.equals(device.hwVersion(), desc.hwVersion()) || |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 309 | !Objects.equals(device.swVersion(), desc.swVersion())) { |
| 310 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 311 | DefaultDevice updated = new DefaultDevice(providerId, device.id(), |
| 312 | desc.type(), |
| 313 | desc.manufacturer(), |
| 314 | desc.hwVersion(), |
| 315 | desc.swVersion(), |
| 316 | desc.serialNumber()); |
| 317 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 318 | devices.put(device.id(), Optional.of(updated)); |
| 319 | availableDevices.add(serialize(device.id())); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 320 | } |
| 321 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null); |
| 322 | } |
| 323 | |
| 324 | // Otherwise merely attempt to change availability |
| 325 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 326 | boolean added = availableDevices.add(serialize(device.id())); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 327 | return !added ? null : |
| 328 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public DeviceEvent markOffline(DeviceId deviceId) { |
| 334 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 335 | Device device = devices.getUnchecked(deviceId).orNull(); |
| 336 | boolean removed = device != null && availableDevices.remove(serialize(deviceId)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 337 | return !removed ? null : |
| 338 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public List<DeviceEvent> updatePorts(DeviceId deviceId, |
| 344 | List<PortDescription> portDescriptions) { |
| 345 | List<DeviceEvent> events = new ArrayList<>(); |
| 346 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 347 | Device device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 348 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 349 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 350 | |
| 351 | // Add new ports |
| 352 | Set<PortNumber> processed = new HashSet<>(); |
| 353 | for (PortDescription portDescription : portDescriptions) { |
| 354 | Port port = ports.get(portDescription.portNumber()); |
| 355 | events.add(port == null ? |
| 356 | createPort(device, portDescription, ports) : |
| 357 | updatePort(device, port, portDescription, ports)); |
| 358 | processed.add(portDescription.portNumber()); |
| 359 | } |
| 360 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 361 | updatePortMap(deviceId, ports); |
| 362 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 363 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 364 | } |
| 365 | return events; |
| 366 | } |
| 367 | |
| 368 | // Creates a new port based on the port description adds it to the map and |
| 369 | // Returns corresponding event. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 370 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 371 | private DeviceEvent createPort(Device device, PortDescription portDescription, |
| 372 | Map<PortNumber, Port> ports) { |
| 373 | DefaultPort port = new DefaultPort(device, portDescription.portNumber(), |
| 374 | portDescription.isEnabled()); |
| 375 | ports.put(port.number(), port); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 376 | updatePortMap(device.id(), ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 377 | return new DeviceEvent(PORT_ADDED, device, port); |
| 378 | } |
| 379 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 380 | // Checks if the specified port requires update and if so, it replaces the |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 381 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 382 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 383 | private DeviceEvent updatePort(Device device, Port port, |
| 384 | PortDescription portDescription, |
| 385 | Map<PortNumber, Port> ports) { |
| 386 | if (port.isEnabled() != portDescription.isEnabled()) { |
| 387 | DefaultPort updatedPort = |
| 388 | new DefaultPort(device, portDescription.portNumber(), |
| 389 | portDescription.isEnabled()); |
| 390 | ports.put(port.number(), updatedPort); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 391 | updatePortMap(device.id(), ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 392 | return new DeviceEvent(PORT_UPDATED, device, port); |
| 393 | } |
| 394 | return null; |
| 395 | } |
| 396 | |
| 397 | // Prunes the specified list of ports based on which ports are in the |
| 398 | // processed list and returns list of corresponding events. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 399 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 400 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 401 | Map<PortNumber, Port> ports, |
| 402 | Set<PortNumber> processed) { |
| 403 | List<DeviceEvent> events = new ArrayList<>(); |
| 404 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 405 | while (iterator.hasNext()) { |
| 406 | PortNumber portNumber = iterator.next(); |
| 407 | if (!processed.contains(portNumber)) { |
| 408 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 409 | ports.get(portNumber))); |
| 410 | iterator.remove(); |
| 411 | } |
| 412 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 413 | if (!events.isEmpty()) { |
| 414 | updatePortMap(device.id(), ports); |
| 415 | } |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 416 | return events; |
| 417 | } |
| 418 | |
| 419 | // Gets the map of ports for the specified device; if one does not already |
| 420 | // exist, it creates and registers a new one. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 421 | // WARN: returned value is a copy, changes made to the Map |
| 422 | // needs to be written back using updatePortMap |
| 423 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 424 | private Map<PortNumber, Port> getPortMap(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 425 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 426 | if (ports == null) { |
| 427 | ports = new HashMap<>(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 428 | // this probably is waste of time in most cases. |
| 429 | updatePortMap(deviceId, ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 430 | } |
| 431 | return ports; |
| 432 | } |
| 433 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 434 | //@GuardedBy("this") |
| 435 | private void updatePortMap(DeviceId deviceId, Map<PortNumber, Port> ports) { |
| 436 | rawDevicePorts.put(serialize(deviceId), serialize(ports)); |
| 437 | devicePorts.put(deviceId, Optional.of(ports)); |
| 438 | } |
| 439 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 440 | @Override |
| 441 | public DeviceEvent updatePortStatus(DeviceId deviceId, |
| 442 | PortDescription portDescription) { |
| 443 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 444 | Device device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 445 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 446 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 447 | Port port = ports.get(portDescription.portNumber()); |
| 448 | return updatePort(device, port, portDescription, ports); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public List<Port> getPorts(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 454 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
| 455 | return ports == null ? Collections.<Port>emptyList() : ImmutableList.copyOf(ports.values()); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 460 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 461 | return ports == null ? null : ports.get(portNumber); |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public boolean isAvailable(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 466 | return availableDevices.contains(serialize(deviceId)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public MastershipRole getRole(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 471 | MastershipRole role = roles.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 472 | return role != null ? role : MastershipRole.NONE; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public DeviceEvent setRole(DeviceId deviceId, MastershipRole role) { |
| 477 | synchronized (this) { |
| 478 | Device device = getDevice(deviceId); |
| 479 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 480 | MastershipRole oldRole = deserialize( |
| 481 | rawRoles.put(serialize(deviceId), serialize(role))); |
| 482 | roles.put(deviceId, Optional.of(role)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 483 | return oldRole == role ? null : |
| 484 | new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public DeviceEvent removeDevice(DeviceId deviceId) { |
| 490 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 491 | byte[] deviceIdBytes = serialize(deviceId); |
| 492 | rawRoles.remove(deviceIdBytes); |
| 493 | roles.invalidate(deviceId); |
| 494 | |
| 495 | // TODO conditional remove? |
| 496 | Device device = deserialize(rawDevices.remove(deviceIdBytes)); |
| 497 | devices.invalidate(deviceId); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 498 | return device == null ? null : |
| 499 | new DeviceEvent(DEVICE_REMOVED, device, null); |
| 500 | } |
| 501 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 502 | |
| 503 | // TODO cache serialized DeviceID if we suffer from serialization cost |
| 504 | |
| 505 | private static byte[] serialize(final Object obj) { |
| 506 | return POOL.serialize(obj); |
| 507 | } |
| 508 | |
| 509 | private static <T> T deserialize(final byte[] bytes) { |
| 510 | if (bytes == null) { |
| 511 | return null; |
| 512 | } |
| 513 | return POOL.deserialize(bytes); |
| 514 | } |
| 515 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 516 | } |