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