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