tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 2 | |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Predicates.notNull; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 4 | import com.google.common.base.Optional; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 5 | import com.google.common.cache.LoadingCache; |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 6 | import com.google.common.collect.FluentIterable; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 7 | import com.google.common.collect.ImmutableList; |
| 8 | import com.google.common.collect.ImmutableSet; |
| 9 | import com.google.common.collect.ImmutableSet.Builder; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 10 | import com.hazelcast.core.IMap; |
| 11 | import com.hazelcast.core.ISet; |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 12 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 13 | import org.apache.felix.scr.annotations.Activate; |
| 14 | import org.apache.felix.scr.annotations.Component; |
| 15 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 16 | import org.apache.felix.scr.annotations.Service; |
| 17 | import org.onlab.onos.net.DefaultDevice; |
| 18 | import org.onlab.onos.net.DefaultPort; |
| 19 | import org.onlab.onos.net.Device; |
| 20 | import org.onlab.onos.net.DeviceId; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.Port; |
| 22 | import org.onlab.onos.net.PortNumber; |
| 23 | import org.onlab.onos.net.device.DeviceDescription; |
| 24 | import org.onlab.onos.net.device.DeviceEvent; |
| 25 | import org.onlab.onos.net.device.DeviceStore; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.device.PortDescription; |
| 28 | import org.onlab.onos.net.provider.ProviderId; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 29 | import org.onlab.onos.store.impl.AbsentInvalidatingLoadingCache; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 30 | import org.onlab.onos.store.impl.AbstractDistributedStore; |
Yuta HIGUCHI | 951e790 | 2014-09-23 14:45:11 -0700 | [diff] [blame] | 31 | import org.onlab.onos.store.impl.OptionalCacheLoader; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 33 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.Collections; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.HashSet; |
| 38 | import java.util.Iterator; |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
| 41 | import java.util.Objects; |
| 42 | import java.util.Set; |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 43 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 44 | import static com.google.common.base.Preconditions.checkArgument; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 45 | import static com.google.common.cache.CacheBuilder.newBuilder; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 46 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 48 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Manages inventory of infrastructure devices using Hazelcast-backed map. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 54 | public class DistributedDeviceStore |
| 55 | extends AbstractDistributedStore<DeviceEvent, DeviceStoreDelegate> |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 56 | implements DeviceStore { |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 61 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 62 | // private IMap<DeviceId, DefaultDevice> cache; |
| 63 | private IMap<byte[], byte[]> rawDevices; |
| 64 | private LoadingCache<DeviceId, Optional<DefaultDevice>> devices; |
| 65 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 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 | |
Yuta HIGUCHI | bb1fc72 | 2014-09-24 00:00:13 -0700 | [diff] [blame] | 74 | @Override |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 75 | @Activate |
| 76 | public void activate() { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 77 | super.activate(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 78 | |
| 79 | // IMap event handler needs value |
| 80 | final boolean includeValue = true; |
| 81 | |
| 82 | // TODO decide on Map name scheme to avoid collision |
| 83 | rawDevices = theInstance.getMap("devices"); |
Yuta HIGUCHI | 951e790 | 2014-09-23 14:45:11 -0700 | [diff] [blame] | 84 | final OptionalCacheLoader<DeviceId, DefaultDevice> deviceLoader |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 85 | = new OptionalCacheLoader<>(storeService, rawDevices); |
| 86 | devices = new AbsentInvalidatingLoadingCache<>(newBuilder().build(deviceLoader)); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 87 | // refresh/populate cache based on notification from other instance |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 88 | rawDevices.addEntryListener(new RemoteDeviceEventHandler(devices), includeValue); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 89 | |
Yuta HIGUCHI | 951e790 | 2014-09-23 14:45:11 -0700 | [diff] [blame] | 90 | // TODO cache availableDevices |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 91 | availableDevices = theInstance.getSet("availableDevices"); |
| 92 | |
| 93 | rawDevicePorts = theInstance.getMap("devicePorts"); |
Yuta HIGUCHI | 951e790 | 2014-09-23 14:45:11 -0700 | [diff] [blame] | 94 | final OptionalCacheLoader<DeviceId, Map<PortNumber, Port>> devicePortLoader |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 95 | = new OptionalCacheLoader<>(storeService, rawDevicePorts); |
| 96 | devicePorts = new AbsentInvalidatingLoadingCache<>(newBuilder().build(devicePortLoader)); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 97 | // refresh/populate cache based on notification from other instance |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 98 | rawDevicePorts.addEntryListener(new RemotePortEventHandler(devicePorts), includeValue); |
| 99 | |
| 100 | loadDeviceCache(); |
Yuta HIGUCHI | ce58fa7 | 2014-09-24 22:15:10 -0700 | [diff] [blame] | 101 | loadDevicePortsCache(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 102 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 103 | log.info("Started"); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Deactivate |
| 107 | public void deactivate() { |
| 108 | log.info("Stopped"); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int getDeviceCount() { |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 113 | return devices.asMap().size(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Iterable<Device> getDevices() { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 118 | // TODO builder v.s. copyOf. Guava semms to be using copyOf? |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 119 | Builder<Device> builder = ImmutableSet.builder(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 120 | for (Optional<DefaultDevice> e : devices.asMap().values()) { |
| 121 | if (e.isPresent()) { |
| 122 | builder.add(e.get()); |
| 123 | } |
| 124 | } |
| 125 | return builder.build(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 126 | } |
| 127 | |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 128 | private void loadDeviceCache() { |
Yuta HIGUCHI | ce58fa7 | 2014-09-24 22:15:10 -0700 | [diff] [blame] | 129 | for (byte[] keyBytes : rawDevices.keySet()) { |
| 130 | final DeviceId id = deserialize(keyBytes); |
| 131 | devices.refresh(id); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private void loadDevicePortsCache() { |
| 136 | for (byte[] keyBytes : rawDevicePorts.keySet()) { |
| 137 | final DeviceId id = deserialize(keyBytes); |
| 138 | devicePorts.refresh(id); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public Device getDevice(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 144 | // TODO revisit if ignoring exception is safe. |
| 145 | return devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 150 | DeviceDescription deviceDescription) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 151 | DefaultDevice device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 152 | 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 HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 165 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 166 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 167 | final byte[] deviceIdBytes = serialize(deviceId); |
| 168 | rawDevices.put(deviceIdBytes, serialize(device)); |
| 169 | devices.put(deviceId, Optional.of(device)); |
| 170 | |
| 171 | availableDevices.add(deviceIdBytes); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 172 | } |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 173 | return new DeviceEvent(DEVICE_ADDED, device, null); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Updates the device and returns the appropriate event if necessary. |
| 177 | private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device, |
| 178 | DeviceDescription desc) { |
| 179 | // We allow only certain attributes to trigger update |
| 180 | if (!Objects.equals(device.hwVersion(), desc.hwVersion()) || |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 181 | !Objects.equals(device.swVersion(), desc.swVersion())) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 182 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 183 | DefaultDevice updated = new DefaultDevice(providerId, device.id(), |
| 184 | desc.type(), |
| 185 | desc.manufacturer(), |
| 186 | desc.hwVersion(), |
| 187 | desc.swVersion(), |
| 188 | desc.serialNumber()); |
| 189 | synchronized (this) { |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 190 | final byte[] deviceIdBytes = serialize(device.id()); |
| 191 | rawDevices.put(deviceIdBytes, serialize(updated)); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 192 | devices.put(device.id(), Optional.of(updated)); |
| 193 | availableDevices.add(serialize(device.id())); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 194 | } |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 195 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, updated, null); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Otherwise merely attempt to change availability |
| 199 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 200 | boolean added = availableDevices.add(serialize(device.id())); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 201 | return !added ? null : |
| 202 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public DeviceEvent markOffline(DeviceId deviceId) { |
| 208 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 209 | Device device = devices.getUnchecked(deviceId).orNull(); |
| 210 | boolean removed = device != null && availableDevices.remove(serialize(deviceId)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 211 | return !removed ? null : |
| 212 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public List<DeviceEvent> updatePorts(DeviceId deviceId, |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 218 | List<PortDescription> portDescriptions) { |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 219 | List<DeviceEvent> events = new ArrayList<>(); |
| 220 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 221 | Device device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 222 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 223 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 224 | |
| 225 | // Add new ports |
| 226 | Set<PortNumber> processed = new HashSet<>(); |
| 227 | for (PortDescription portDescription : portDescriptions) { |
| 228 | Port port = ports.get(portDescription.portNumber()); |
| 229 | events.add(port == null ? |
| 230 | createPort(device, portDescription, ports) : |
| 231 | updatePort(device, port, portDescription, ports)); |
| 232 | processed.add(portDescription.portNumber()); |
| 233 | } |
| 234 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 235 | updatePortMap(deviceId, ports); |
| 236 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 237 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 238 | } |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 239 | return FluentIterable.from(events).filter(notNull()).toList(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Creates a new port based on the port description adds it to the map and |
| 243 | // Returns corresponding event. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 244 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 245 | private DeviceEvent createPort(Device device, PortDescription portDescription, |
| 246 | Map<PortNumber, Port> ports) { |
| 247 | DefaultPort port = new DefaultPort(device, portDescription.portNumber(), |
| 248 | portDescription.isEnabled()); |
| 249 | ports.put(port.number(), port); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 250 | updatePortMap(device.id(), ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 251 | return new DeviceEvent(PORT_ADDED, device, port); |
| 252 | } |
| 253 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 254 | // Checks if the specified port requires update and if so, it replaces the |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 255 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 256 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 257 | private DeviceEvent updatePort(Device device, Port port, |
| 258 | PortDescription portDescription, |
| 259 | Map<PortNumber, Port> ports) { |
| 260 | if (port.isEnabled() != portDescription.isEnabled()) { |
| 261 | DefaultPort updatedPort = |
| 262 | new DefaultPort(device, portDescription.portNumber(), |
| 263 | portDescription.isEnabled()); |
| 264 | ports.put(port.number(), updatedPort); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 265 | updatePortMap(device.id(), ports); |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 266 | return new DeviceEvent(PORT_UPDATED, device, updatedPort); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 267 | } |
| 268 | return null; |
| 269 | } |
| 270 | |
| 271 | // Prunes the specified list of ports based on which ports are in the |
| 272 | // processed list and returns list of corresponding events. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 273 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 274 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 275 | Map<PortNumber, Port> ports, |
| 276 | Set<PortNumber> processed) { |
| 277 | List<DeviceEvent> events = new ArrayList<>(); |
| 278 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 279 | while (iterator.hasNext()) { |
| 280 | PortNumber portNumber = iterator.next(); |
| 281 | if (!processed.contains(portNumber)) { |
| 282 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 283 | ports.get(portNumber))); |
| 284 | iterator.remove(); |
| 285 | } |
| 286 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 287 | if (!events.isEmpty()) { |
| 288 | updatePortMap(device.id(), ports); |
| 289 | } |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 290 | return events; |
| 291 | } |
| 292 | |
| 293 | // Gets the map of ports for the specified device; if one does not already |
| 294 | // exist, it creates and registers a new one. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 295 | // WARN: returned value is a copy, changes made to the Map |
| 296 | // needs to be written back using updatePortMap |
| 297 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 298 | private Map<PortNumber, Port> getPortMap(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 299 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 300 | if (ports == null) { |
| 301 | ports = new HashMap<>(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 302 | // this probably is waste of time in most cases. |
| 303 | updatePortMap(deviceId, ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 304 | } |
| 305 | return ports; |
| 306 | } |
| 307 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 308 | //@GuardedBy("this") |
| 309 | private void updatePortMap(DeviceId deviceId, Map<PortNumber, Port> ports) { |
| 310 | rawDevicePorts.put(serialize(deviceId), serialize(ports)); |
| 311 | devicePorts.put(deviceId, Optional.of(ports)); |
| 312 | } |
| 313 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 314 | @Override |
| 315 | public DeviceEvent updatePortStatus(DeviceId deviceId, |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 316 | PortDescription portDescription) { |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 317 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 318 | Device device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 319 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 320 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 321 | Port port = ports.get(portDescription.portNumber()); |
| 322 | return updatePort(device, port, portDescription, ports); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public List<Port> getPorts(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 328 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
| 329 | return ports == null ? Collections.<Port>emptyList() : ImmutableList.copyOf(ports.values()); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 334 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 335 | return ports == null ? null : ports.get(portNumber); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public boolean isAvailable(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 340 | return availableDevices.contains(serialize(deviceId)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 344 | public DeviceEvent removeDevice(DeviceId deviceId) { |
| 345 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 346 | byte[] deviceIdBytes = serialize(deviceId); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 347 | |
| 348 | // TODO conditional remove? |
| 349 | Device device = deserialize(rawDevices.remove(deviceIdBytes)); |
| 350 | devices.invalidate(deviceId); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 351 | return device == null ? null : |
| 352 | new DeviceEvent(DEVICE_REMOVED, device, null); |
| 353 | } |
| 354 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 355 | |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 356 | private class RemoteDeviceEventHandler extends RemoteEventHandler<DeviceId, DefaultDevice> { |
| 357 | public RemoteDeviceEventHandler(LoadingCache<DeviceId, Optional<DefaultDevice>> cache) { |
| 358 | super(cache); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | protected void onAdd(DeviceId deviceId, DefaultDevice device) { |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 363 | notifyDelegate(new DeviceEvent(DEVICE_ADDED, device)); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Override |
| 367 | protected void onRemove(DeviceId deviceId, DefaultDevice device) { |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 368 | notifyDelegate(new DeviceEvent(DEVICE_REMOVED, device)); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame^] | 372 | protected void onUpdate(DeviceId deviceId, DefaultDevice oldDevice, DefaultDevice device) { |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 373 | notifyDelegate(new DeviceEvent(DEVICE_UPDATED, device)); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | private class RemotePortEventHandler extends RemoteEventHandler<DeviceId, Map<PortNumber, Port>> { |
| 378 | public RemotePortEventHandler(LoadingCache<DeviceId, Optional<Map<PortNumber, Port>>> cache) { |
| 379 | super(cache); |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | protected void onAdd(DeviceId deviceId, Map<PortNumber, Port> ports) { |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 384 | // notifyDelegate(new DeviceEvent(PORT_ADDED, getDevice(deviceId))); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
| 388 | protected void onRemove(DeviceId deviceId, Map<PortNumber, Port> ports) { |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 389 | // notifyDelegate(new DeviceEvent(PORT_REMOVED, getDevice(deviceId))); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame^] | 393 | protected void onUpdate(DeviceId deviceId, Map<PortNumber, Port> oldPorts, Map<PortNumber, Port> ports) { |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 394 | // notifyDelegate(new DeviceEvent(PORT_UPDATED, getDevice(deviceId))); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 399 | // TODO cache serialized DeviceID if we suffer from serialization cost |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 400 | } |