tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.trivial.impl; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 2 | |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 3 | import com.google.common.collect.FluentIterable; |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 4 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 5 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 6 | import org.apache.commons.lang3.concurrent.ConcurrentException; |
| 7 | import org.apache.commons.lang3.concurrent.ConcurrentInitializer; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 8 | import org.apache.felix.scr.annotations.Activate; |
| 9 | import org.apache.felix.scr.annotations.Component; |
| 10 | import org.apache.felix.scr.annotations.Deactivate; |
| 11 | import org.apache.felix.scr.annotations.Service; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.DefaultDevice; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.DefaultPort; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.Device; |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.Device.Type; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.DeviceId; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.Port; |
| 18 | import org.onlab.onos.net.PortNumber; |
| 19 | import org.onlab.onos.net.device.DeviceDescription; |
| 20 | import org.onlab.onos.net.device.DeviceEvent; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.device.DeviceStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.device.PortDescription; |
| 24 | import org.onlab.onos.net.provider.ProviderId; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 25 | import org.onlab.onos.store.AbstractStore; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 26 | import org.slf4j.Logger; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 30 | import java.util.Collections; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 31 | import java.util.HashSet; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 32 | import java.util.Iterator; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 33 | import java.util.List; |
| 34 | import java.util.Map; |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 35 | import java.util.Map.Entry; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 36 | import java.util.Objects; |
| 37 | import java.util.Set; |
| 38 | import java.util.concurrent.ConcurrentHashMap; |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 39 | import java.util.concurrent.ConcurrentMap; |
| 40 | import java.util.concurrent.atomic.AtomicReference; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 41 | |
| 42 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 43 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 44 | import static com.google.common.base.Predicates.notNull; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 45 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 46 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 47 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 48 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 49 | // TODO: synchronization should be done in more fine-grained manner. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 50 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 51 | * Manages inventory of infrastructure devices using trivial in-memory |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 52 | * structures implementation. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 53 | */ |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 54 | @Component(immediate = true) |
| 55 | @Service |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 56 | public class SimpleDeviceStore |
| 57 | extends AbstractStore<DeviceEvent, DeviceStoreDelegate> |
| 58 | implements DeviceStore { |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 61 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 62 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 63 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 64 | // collection of Description given from various providers |
| 65 | private final ConcurrentMap<DeviceId, |
| 66 | ConcurrentMap<ProviderId, DeviceDescriptions>> |
| 67 | deviceDescs = new ConcurrentHashMap<>(); |
| 68 | |
| 69 | // cache of Device and Ports generated by compositing descriptions from providers |
| 70 | private final ConcurrentMap<DeviceId, Device> devices = new ConcurrentHashMap<>(); |
| 71 | private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = new ConcurrentHashMap<>(); |
| 72 | |
| 73 | // available(=UP) devices |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 74 | private final Set<DeviceId> availableDevices = new HashSet<>(); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 75 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 76 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 77 | @Activate |
| 78 | public void activate() { |
| 79 | log.info("Started"); |
| 80 | } |
| 81 | |
| 82 | @Deactivate |
| 83 | public void deactivate() { |
| 84 | log.info("Stopped"); |
| 85 | } |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 86 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public int getDeviceCount() { |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 89 | return devices.size(); |
| 90 | } |
| 91 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public Iterable<Device> getDevices() { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 94 | return Collections.unmodifiableCollection(devices.values()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public Device getDevice(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 99 | return devices.get(deviceId); |
| 100 | } |
| 101 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 102 | @Override |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 103 | public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 104 | DeviceDescription deviceDescription) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 105 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs |
| 106 | = createIfAbsentUnchecked(deviceDescs, deviceId, |
| 107 | new InitConcurrentHashMap<ProviderId, DeviceDescriptions>()); |
| 108 | |
| 109 | Device oldDevice = devices.get(deviceId); |
| 110 | |
| 111 | DeviceDescriptions descs |
| 112 | = createIfAbsentUnchecked(providerDescs, providerId, |
| 113 | new InitDeviceDescs(deviceDescription)); |
| 114 | |
| 115 | descs.putDeviceDesc(deviceDescription); |
| 116 | |
| 117 | Device newDevice = composeDevice(deviceId, providerDescs); |
| 118 | |
| 119 | if (oldDevice == null) { |
| 120 | // ADD |
| 121 | return createDevice(providerId, newDevice); |
| 122 | } else { |
| 123 | // UPDATE or ignore (no change or stale) |
| 124 | return updateDevice(providerId, oldDevice, newDevice); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 125 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Creates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 129 | private DeviceEvent createDevice(ProviderId providerId, Device newDevice) { |
| 130 | |
| 131 | // update composed device cache |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 132 | synchronized (this) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 133 | devices.putIfAbsent(newDevice.id(), newDevice); |
| 134 | if (!providerId.isAncillary()) { |
| 135 | availableDevices.add(newDevice.id()); |
| 136 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 137 | } |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 138 | |
| 139 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Updates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 143 | private DeviceEvent updateDevice(ProviderId providerId, Device oldDevice, Device newDevice) { |
| 144 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 145 | // We allow only certain attributes to trigger update |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 146 | if (!Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) || |
| 147 | !Objects.equals(oldDevice.swVersion(), newDevice.swVersion())) { |
| 148 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 149 | synchronized (this) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 150 | devices.replace(newDevice.id(), oldDevice, newDevice); |
| 151 | if (!providerId.isAncillary()) { |
| 152 | availableDevices.add(newDevice.id()); |
| 153 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 154 | } |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 155 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 158 | // Otherwise merely attempt to change availability if primary provider |
| 159 | if (!providerId.isAncillary()) { |
| 160 | synchronized (this) { |
| 161 | boolean added = availableDevices.add(newDevice.id()); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 162 | return !added ? null : |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 163 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null); |
| 164 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 165 | } |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 166 | return null; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 167 | } |
| 168 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public DeviceEvent markOffline(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 171 | synchronized (this) { |
| 172 | Device device = devices.get(deviceId); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 173 | boolean removed = (device != null) && availableDevices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 174 | return !removed ? null : |
| 175 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 179 | @Override |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 180 | public synchronized List<DeviceEvent> updatePorts(ProviderId providerId, DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 181 | List<PortDescription> portDescriptions) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 182 | |
| 183 | // TODO: implement multi-provider |
| 184 | Device device = devices.get(deviceId); |
| 185 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 186 | |
| 187 | ConcurrentMap<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
| 188 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 189 | |
| 190 | DeviceDescriptions descs = descsMap.get(providerId); |
| 191 | checkArgument(descs != null, |
| 192 | "Device description for Device ID %s from Provider %s was not found", |
| 193 | deviceId, providerId); |
| 194 | |
| 195 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 196 | List<DeviceEvent> events = new ArrayList<>(); |
| 197 | synchronized (this) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 198 | ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 199 | |
| 200 | // Add new ports |
| 201 | Set<PortNumber> processed = new HashSet<>(); |
| 202 | for (PortDescription portDescription : portDescriptions) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 203 | PortNumber number = portDescription.portNumber(); |
| 204 | Port oldPort = ports.get(number); |
| 205 | // update description |
| 206 | descs.putPortDesc(number, portDescription); |
| 207 | Port newPort = composePort(device, number, descsMap); |
| 208 | |
| 209 | events.add(oldPort == null ? |
| 210 | createPort(device, newPort, ports) : |
| 211 | updatePort(device, oldPort, newPort, ports)); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 212 | processed.add(portDescription.portNumber()); |
| 213 | } |
| 214 | |
| 215 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 216 | } |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 217 | return FluentIterable.from(events).filter(notNull()).toList(); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Creates a new port based on the port description adds it to the map and |
| 221 | // Returns corresponding event. |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 222 | private DeviceEvent createPort(Device device, Port newPort, |
| 223 | ConcurrentMap<PortNumber, Port> ports) { |
| 224 | ports.put(newPort.number(), newPort); |
| 225 | return new DeviceEvent(PORT_ADDED, device, newPort); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // CHecks if the specified port requires update and if so, it replaces the |
| 229 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 230 | private DeviceEvent updatePort(Device device, Port oldPort, |
| 231 | Port newPort, |
| 232 | ConcurrentMap<PortNumber, Port> ports) { |
| 233 | if (oldPort.isEnabled() != newPort.isEnabled()) { |
| 234 | ports.put(oldPort.number(), newPort); |
| 235 | return new DeviceEvent(PORT_UPDATED, device, newPort); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 236 | } |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | // Prunes the specified list of ports based on which ports are in the |
| 241 | // processed list and returns list of corresponding events. |
| 242 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 243 | Map<PortNumber, Port> ports, |
| 244 | Set<PortNumber> processed) { |
| 245 | List<DeviceEvent> events = new ArrayList<>(); |
| 246 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 247 | while (iterator.hasNext()) { |
| 248 | PortNumber portNumber = iterator.next(); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 249 | if (!processed.contains(portNumber)) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 250 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 251 | ports.get(portNumber))); |
| 252 | iterator.remove(); |
| 253 | } |
| 254 | } |
| 255 | return events; |
| 256 | } |
| 257 | |
| 258 | // Gets the map of ports for the specified device; if one does not already |
| 259 | // exist, it creates and registers a new one. |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 260 | private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) { |
| 261 | return createIfAbsentUnchecked(devicePorts, deviceId, |
| 262 | new InitConcurrentHashMap<PortNumber, Port>()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 263 | } |
| 264 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 265 | @Override |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 266 | public synchronized DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 267 | PortDescription portDescription) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 268 | Device device = devices.get(deviceId); |
| 269 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 270 | |
| 271 | ConcurrentMap<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
| 272 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 273 | |
| 274 | DeviceDescriptions descs = descsMap.get(providerId); |
| 275 | checkArgument(descs != null, |
| 276 | "Device description for Device ID %s from Provider %s was not found", |
| 277 | deviceId, providerId); |
| 278 | |
| 279 | // TODO: implement multi-provider |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 280 | synchronized (this) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 281 | ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId); |
| 282 | final PortNumber number = portDescription.portNumber(); |
| 283 | Port oldPort = ports.get(number); |
| 284 | // update description |
| 285 | descs.putPortDesc(number, portDescription); |
| 286 | Port newPort = composePort(device, number, descsMap); |
| 287 | if (oldPort == null) { |
| 288 | return createPort(device, newPort, ports); |
| 289 | } else { |
| 290 | return updatePort(device, oldPort, newPort, ports); |
| 291 | } |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 292 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 293 | } |
| 294 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 295 | @Override |
| 296 | public List<Port> getPorts(DeviceId deviceId) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 297 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 298 | if (ports == null) { |
| 299 | return Collections.emptyList(); |
| 300 | } |
| 301 | return ImmutableList.copyOf(ports.values()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 302 | } |
| 303 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 304 | @Override |
| 305 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 306 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 307 | return ports == null ? null : ports.get(portNumber); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 308 | } |
| 309 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 310 | @Override |
| 311 | public boolean isAvailable(DeviceId deviceId) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 312 | return availableDevices.contains(deviceId); |
| 313 | } |
| 314 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 315 | @Override |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 316 | public DeviceEvent removeDevice(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 317 | synchronized (this) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 318 | Device device = devices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 319 | return device == null ? null : |
| 320 | new DeviceEvent(DEVICE_REMOVED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * Returns a Device, merging description given from multiple Providers. |
| 326 | * |
| 327 | * @param deviceId device identifier |
| 328 | * @param providerDescs Collection of Descriptions from multiple providers |
| 329 | * @return Device instance |
| 330 | */ |
| 331 | private Device composeDevice(DeviceId deviceId, |
| 332 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 333 | |
| 334 | checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied"); |
| 335 | |
| 336 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 337 | |
| 338 | DeviceDescriptions desc = providerDescs.get(primary); |
| 339 | Type type = desc.getDeviceDesc().type(); |
| 340 | String manufacturer = desc.getDeviceDesc().manufacturer(); |
| 341 | String hwVersion = desc.getDeviceDesc().hwVersion(); |
| 342 | String swVersion = desc.getDeviceDesc().swVersion(); |
| 343 | String serialNumber = desc.getDeviceDesc().serialNumber(); |
| 344 | |
| 345 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 346 | if (e.getKey().equals(primary)) { |
| 347 | continue; |
| 348 | } |
| 349 | // FIXME: implement attribute merging once we have K-V attributes |
| 350 | } |
| 351 | |
| 352 | return new DefaultDevice(primary, deviceId , type, manufacturer, hwVersion, swVersion, serialNumber); |
| 353 | } |
| 354 | |
| 355 | // probably want composePorts |
| 356 | private Port composePort(Device device, PortNumber number, |
| 357 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 358 | |
| 359 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 360 | DeviceDescriptions primDescs = providerDescs.get(primary); |
| 361 | final PortDescription portDesc = primDescs.getPortDesc(number); |
| 362 | boolean isEnabled; |
| 363 | if (portDesc != null) { |
| 364 | isEnabled = portDesc.isEnabled(); |
| 365 | } else { |
| 366 | // if no primary, assume not enabled |
| 367 | // TODO: revisit this port enabled/disabled behavior |
| 368 | isEnabled = false; |
| 369 | } |
| 370 | |
| 371 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 372 | if (e.getKey().equals(primary)) { |
| 373 | continue; |
| 374 | } |
| 375 | // FIXME: implement attribute merging once we have K-V attributes |
| 376 | } |
| 377 | |
| 378 | return new DefaultPort(device, number, isEnabled); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * @return primary ProviderID, or randomly chosen one if none exists |
| 383 | */ |
| 384 | private ProviderId pickPrimaryPID( |
| 385 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 386 | ProviderId fallBackPrimary = null; |
| 387 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 388 | if (!e.getKey().isAncillary()) { |
| 389 | return e.getKey(); |
| 390 | } else if (fallBackPrimary == null) { |
| 391 | // pick randomly as a fallback in case there is no primary |
| 392 | fallBackPrimary = e.getKey(); |
| 393 | } |
| 394 | } |
| 395 | return fallBackPrimary; |
| 396 | } |
| 397 | |
| 398 | // TODO: can be made generic |
| 399 | private static final class InitConcurrentHashMap<K, V> implements |
| 400 | ConcurrentInitializer<ConcurrentMap<K, V>> { |
| 401 | @Override |
| 402 | public ConcurrentMap<K, V> get() throws ConcurrentException { |
| 403 | return new ConcurrentHashMap<>(); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | public static final class InitDeviceDescs |
| 408 | implements ConcurrentInitializer<DeviceDescriptions> { |
| 409 | private final DeviceDescription deviceDesc; |
| 410 | public InitDeviceDescs(DeviceDescription deviceDesc) { |
| 411 | this.deviceDesc = checkNotNull(deviceDesc); |
| 412 | } |
| 413 | @Override |
| 414 | public DeviceDescriptions get() throws ConcurrentException { |
| 415 | return new DeviceDescriptions(deviceDesc); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | |
| 420 | /** |
| 421 | * Collection of Description of a Device and it's Ports given from a Provider. |
| 422 | */ |
| 423 | private static class DeviceDescriptions { |
| 424 | // private final DeviceId id; |
| 425 | // private final ProviderId pid; |
| 426 | |
| 427 | private final AtomicReference<DeviceDescription> deviceDesc; |
| 428 | private final ConcurrentMap<PortNumber, PortDescription> portDescs; |
| 429 | |
| 430 | public DeviceDescriptions(DeviceDescription desc) { |
| 431 | this.deviceDesc = new AtomicReference<>(desc); |
| 432 | this.portDescs = new ConcurrentHashMap<>(); |
| 433 | } |
| 434 | |
| 435 | public DeviceDescription getDeviceDesc() { |
| 436 | return deviceDesc.get(); |
| 437 | } |
| 438 | |
| 439 | public PortDescription getPortDesc(PortNumber number) { |
| 440 | return portDescs.get(number); |
| 441 | } |
| 442 | |
| 443 | public Collection<PortDescription> getPortDescs() { |
| 444 | return Collections.unmodifiableCollection(portDescs.values()); |
| 445 | } |
| 446 | |
| 447 | public DeviceDescription putDeviceDesc(DeviceDescription newDesc) { |
| 448 | return deviceDesc.getAndSet(newDesc); |
| 449 | } |
| 450 | |
| 451 | public PortDescription putPortDesc(PortNumber number, PortDescription newDesc) { |
| 452 | return portDescs.put(number, newDesc); |
| 453 | } |
| 454 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 455 | } |