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