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