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() { |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame^] | 113 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public int getDeviceCount() { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 119 | // TODO IMap size or cache size? |
| 120 | return rawDevices.size(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Iterable<Device> getDevices() { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 125 | // TODO Revisit if we ever need to do this. |
| 126 | // log.info("{}:{}", rawMap.size(), cache.size()); |
| 127 | // if (rawMap.size() != cache.size()) { |
| 128 | // for (Entry<byte[], byte[]> e : rawMap.entrySet()) { |
| 129 | // final DeviceId key = deserialize(e.getKey()); |
| 130 | // final DefaultDevice val = deserialize(e.getValue()); |
| 131 | // cache.put(key, val); |
| 132 | // } |
| 133 | // } |
| 134 | |
| 135 | // TODO builder v.s. copyOf. Guava semms to be using copyOf? |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 136 | Builder<Device> builder = ImmutableSet.builder(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 137 | for (Optional<DefaultDevice> e : devices.asMap().values()) { |
| 138 | if (e.isPresent()) { |
| 139 | builder.add(e.get()); |
| 140 | } |
| 141 | } |
| 142 | return builder.build(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Device getDevice(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 147 | // TODO revisit if ignoring exception is safe. |
| 148 | return devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 153 | DeviceDescription deviceDescription) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 154 | DefaultDevice device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 155 | if (device == null) { |
| 156 | return createDevice(providerId, deviceId, deviceDescription); |
| 157 | } |
| 158 | return updateDevice(providerId, device, deviceDescription); |
| 159 | } |
| 160 | |
| 161 | // Creates the device and returns the appropriate event if necessary. |
| 162 | private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId, |
| 163 | DeviceDescription desc) { |
| 164 | DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(), |
| 165 | desc.manufacturer(), |
| 166 | desc.hwVersion(), desc.swVersion(), |
| 167 | desc.serialNumber()); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 168 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 169 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 170 | final byte[] deviceIdBytes = serialize(deviceId); |
| 171 | rawDevices.put(deviceIdBytes, serialize(device)); |
| 172 | devices.put(deviceId, Optional.of(device)); |
| 173 | |
| 174 | availableDevices.add(deviceIdBytes); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 175 | |
| 176 | // For now claim the device as a master automatically. |
Ayaka Koshibe | f6021f3 | 2014-09-24 09:40:47 -0700 | [diff] [blame] | 177 | //rawRoles.put(deviceIdBytes, serialize(MastershipRole.MASTER)); |
| 178 | //roles.put(deviceId, Optional.of(MastershipRole.MASTER)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 179 | } |
| 180 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null); |
| 181 | } |
| 182 | |
| 183 | // Updates the device and returns the appropriate event if necessary. |
| 184 | private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device, |
| 185 | DeviceDescription desc) { |
| 186 | // We allow only certain attributes to trigger update |
| 187 | if (!Objects.equals(device.hwVersion(), desc.hwVersion()) || |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 188 | !Objects.equals(device.swVersion(), desc.swVersion())) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 189 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 190 | DefaultDevice updated = new DefaultDevice(providerId, device.id(), |
| 191 | desc.type(), |
| 192 | desc.manufacturer(), |
| 193 | desc.hwVersion(), |
| 194 | desc.swVersion(), |
| 195 | desc.serialNumber()); |
| 196 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 197 | devices.put(device.id(), Optional.of(updated)); |
| 198 | availableDevices.add(serialize(device.id())); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 199 | } |
| 200 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null); |
| 201 | } |
| 202 | |
| 203 | // Otherwise merely attempt to change availability |
| 204 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 205 | boolean added = availableDevices.add(serialize(device.id())); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 206 | return !added ? null : |
| 207 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public DeviceEvent markOffline(DeviceId deviceId) { |
| 213 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 214 | Device device = devices.getUnchecked(deviceId).orNull(); |
| 215 | boolean removed = device != null && availableDevices.remove(serialize(deviceId)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 216 | return !removed ? null : |
| 217 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public List<DeviceEvent> updatePorts(DeviceId deviceId, |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 223 | List<PortDescription> portDescriptions) { |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 224 | List<DeviceEvent> events = new ArrayList<>(); |
| 225 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 226 | Device device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 227 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 228 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 229 | |
| 230 | // Add new ports |
| 231 | Set<PortNumber> processed = new HashSet<>(); |
| 232 | for (PortDescription portDescription : portDescriptions) { |
| 233 | Port port = ports.get(portDescription.portNumber()); |
| 234 | events.add(port == null ? |
| 235 | createPort(device, portDescription, ports) : |
| 236 | updatePort(device, port, portDescription, ports)); |
| 237 | processed.add(portDescription.portNumber()); |
| 238 | } |
| 239 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 240 | updatePortMap(deviceId, ports); |
| 241 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 242 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 243 | } |
| 244 | return events; |
| 245 | } |
| 246 | |
| 247 | // Creates a new port based on the port description adds it to the map and |
| 248 | // Returns corresponding event. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 249 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 250 | private DeviceEvent createPort(Device device, PortDescription portDescription, |
| 251 | Map<PortNumber, Port> ports) { |
| 252 | DefaultPort port = new DefaultPort(device, portDescription.portNumber(), |
| 253 | portDescription.isEnabled()); |
| 254 | ports.put(port.number(), port); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 255 | updatePortMap(device.id(), ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 256 | return new DeviceEvent(PORT_ADDED, device, port); |
| 257 | } |
| 258 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 259 | // Checks if the specified port requires update and if so, it replaces the |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 260 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 261 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 262 | private DeviceEvent updatePort(Device device, Port port, |
| 263 | PortDescription portDescription, |
| 264 | Map<PortNumber, Port> ports) { |
| 265 | if (port.isEnabled() != portDescription.isEnabled()) { |
| 266 | DefaultPort updatedPort = |
| 267 | new DefaultPort(device, portDescription.portNumber(), |
| 268 | portDescription.isEnabled()); |
| 269 | ports.put(port.number(), updatedPort); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 270 | updatePortMap(device.id(), ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 271 | return new DeviceEvent(PORT_UPDATED, device, port); |
| 272 | } |
| 273 | return null; |
| 274 | } |
| 275 | |
| 276 | // Prunes the specified list of ports based on which ports are in the |
| 277 | // processed list and returns list of corresponding events. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 278 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 279 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 280 | Map<PortNumber, Port> ports, |
| 281 | Set<PortNumber> processed) { |
| 282 | List<DeviceEvent> events = new ArrayList<>(); |
| 283 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 284 | while (iterator.hasNext()) { |
| 285 | PortNumber portNumber = iterator.next(); |
| 286 | if (!processed.contains(portNumber)) { |
| 287 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 288 | ports.get(portNumber))); |
| 289 | iterator.remove(); |
| 290 | } |
| 291 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 292 | if (!events.isEmpty()) { |
| 293 | updatePortMap(device.id(), ports); |
| 294 | } |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 295 | return events; |
| 296 | } |
| 297 | |
| 298 | // Gets the map of ports for the specified device; if one does not already |
| 299 | // exist, it creates and registers a new one. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 300 | // WARN: returned value is a copy, changes made to the Map |
| 301 | // needs to be written back using updatePortMap |
| 302 | //@GuardedBy("this") |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 303 | private Map<PortNumber, Port> getPortMap(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 304 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 305 | if (ports == null) { |
| 306 | ports = new HashMap<>(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 307 | // this probably is waste of time in most cases. |
| 308 | updatePortMap(deviceId, ports); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 309 | } |
| 310 | return ports; |
| 311 | } |
| 312 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 313 | //@GuardedBy("this") |
| 314 | private void updatePortMap(DeviceId deviceId, Map<PortNumber, Port> ports) { |
| 315 | rawDevicePorts.put(serialize(deviceId), serialize(ports)); |
| 316 | devicePorts.put(deviceId, Optional.of(ports)); |
| 317 | } |
| 318 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 319 | @Override |
| 320 | public DeviceEvent updatePortStatus(DeviceId deviceId, |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 321 | PortDescription portDescription) { |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 322 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 323 | Device device = devices.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 324 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 325 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 326 | Port port = ports.get(portDescription.portNumber()); |
| 327 | return updatePort(device, port, portDescription, ports); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public List<Port> getPorts(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 333 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
| 334 | return ports == null ? Collections.<Port>emptyList() : ImmutableList.copyOf(ports.values()); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 339 | Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull(); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 340 | return ports == null ? null : ports.get(portNumber); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean isAvailable(DeviceId deviceId) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 345 | return availableDevices.contains(serialize(deviceId)); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 349 | public DeviceEvent removeDevice(DeviceId deviceId) { |
| 350 | synchronized (this) { |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 351 | byte[] deviceIdBytes = serialize(deviceId); |
| 352 | rawRoles.remove(deviceIdBytes); |
| 353 | roles.invalidate(deviceId); |
| 354 | |
| 355 | // TODO conditional remove? |
| 356 | Device device = deserialize(rawDevices.remove(deviceIdBytes)); |
| 357 | devices.invalidate(deviceId); |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 358 | return device == null ? null : |
| 359 | new DeviceEvent(DEVICE_REMOVED, device, null); |
| 360 | } |
| 361 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 362 | |
| 363 | // TODO cache serialized DeviceID if we suffer from serialization cost |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 364 | |
tom | 8cc5aa7 | 2014-09-19 15:14:43 -0700 | [diff] [blame] | 365 | } |