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