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