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