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 | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame^] | 54 | import static org.onlab.onos.net.DefaultAnnotations.union; |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 55 | import static org.onlab.onos.net.DefaultAnnotations.merge; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 58 | * Manages inventory of infrastructure devices using trivial in-memory |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 59 | * structures implementation. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 60 | */ |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 61 | @Component(immediate = true) |
| 62 | @Service |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 63 | public class SimpleDeviceStore |
| 64 | extends AbstractStore<DeviceEvent, DeviceStoreDelegate> |
| 65 | implements DeviceStore { |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 68 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 69 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 70 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 71 | // collection of Description given from various providers |
| 72 | private final ConcurrentMap<DeviceId, |
| 73 | ConcurrentMap<ProviderId, DeviceDescriptions>> |
| 74 | deviceDescs = new ConcurrentHashMap<>(); |
| 75 | |
| 76 | // cache of Device and Ports generated by compositing descriptions from providers |
| 77 | private final ConcurrentMap<DeviceId, Device> devices = new ConcurrentHashMap<>(); |
| 78 | private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = new ConcurrentHashMap<>(); |
| 79 | |
| 80 | // available(=UP) devices |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 81 | private final Set<DeviceId> availableDevices = new HashSet<>(); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 82 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 83 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 84 | @Activate |
| 85 | public void activate() { |
| 86 | log.info("Started"); |
| 87 | } |
| 88 | |
| 89 | @Deactivate |
| 90 | public void deactivate() { |
| 91 | log.info("Stopped"); |
| 92 | } |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 93 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public int getDeviceCount() { |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 96 | return devices.size(); |
| 97 | } |
| 98 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public Iterable<Device> getDevices() { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 101 | return Collections.unmodifiableCollection(devices.values()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 102 | } |
| 103 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public Device getDevice(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 106 | return devices.get(deviceId); |
| 107 | } |
| 108 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 109 | @Override |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 110 | public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 111 | DeviceDescription deviceDescription) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 112 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 113 | = getDeviceDescriptions(deviceId); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 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()) || |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 154 | !AnnotationsUtil.isEqual(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() || |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 241 | !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) { |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 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 | |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 267 | private ConcurrentMap<ProviderId, DeviceDescriptions> getDeviceDescriptions( |
| 268 | DeviceId deviceId) { |
| 269 | return createIfAbsentUnchecked(deviceDescs, deviceId, |
| 270 | NewConcurrentHashMap.<ProviderId, DeviceDescriptions>ifNeeded()); |
| 271 | } |
| 272 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 273 | // Gets the map of ports for the specified device; if one does not already |
| 274 | // exist, it creates and registers a new one. |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 275 | private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) { |
| 276 | return createIfAbsentUnchecked(devicePorts, deviceId, |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 277 | NewConcurrentHashMap.<PortNumber, Port>ifNeeded()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 278 | } |
| 279 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 280 | @Override |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 281 | public synchronized DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 282 | PortDescription portDescription) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 283 | Device device = devices.get(deviceId); |
| 284 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 285 | |
| 286 | ConcurrentMap<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
| 287 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 288 | |
| 289 | DeviceDescriptions descs = descsMap.get(providerId); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 290 | // assuming all providers must to give DeviceDescription |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 291 | checkArgument(descs != null, |
| 292 | "Device description for Device ID %s from Provider %s was not found", |
| 293 | deviceId, providerId); |
| 294 | |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 295 | synchronized (this) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 296 | ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId); |
| 297 | final PortNumber number = portDescription.portNumber(); |
| 298 | Port oldPort = ports.get(number); |
| 299 | // update description |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 300 | descs.putPortDesc(portDescription); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 301 | Port newPort = composePort(device, number, descsMap); |
| 302 | if (oldPort == null) { |
| 303 | return createPort(device, newPort, ports); |
| 304 | } else { |
| 305 | return updatePort(device, oldPort, newPort, ports); |
| 306 | } |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 307 | } |
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 List<Port> getPorts(DeviceId deviceId) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 312 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 313 | if (ports == null) { |
| 314 | return Collections.emptyList(); |
| 315 | } |
| 316 | return ImmutableList.copyOf(ports.values()); |
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 Port getPort(DeviceId deviceId, PortNumber portNumber) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 321 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 322 | return ports == null ? null : ports.get(portNumber); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 323 | } |
| 324 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public boolean isAvailable(DeviceId deviceId) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 327 | return availableDevices.contains(deviceId); |
| 328 | } |
| 329 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 330 | @Override |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 331 | public DeviceEvent removeDevice(DeviceId deviceId) { |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 332 | ConcurrentMap<ProviderId, DeviceDescriptions> descs = getDeviceDescriptions(deviceId); |
| 333 | synchronized (descs) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 334 | Device device = devices.remove(deviceId); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 335 | // should DEVICE_REMOVED carry removed ports? |
| 336 | ConcurrentMap<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 337 | if (ports != null) { |
| 338 | ports.clear(); |
| 339 | } |
| 340 | availableDevices.remove(deviceId); |
| 341 | descs.clear(); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 342 | return device == null ? null : |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 343 | new DeviceEvent(DEVICE_REMOVED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 346 | |
| 347 | /** |
| 348 | * Returns a Device, merging description given from multiple Providers. |
| 349 | * |
| 350 | * @param deviceId device identifier |
| 351 | * @param providerDescs Collection of Descriptions from multiple providers |
| 352 | * @return Device instance |
| 353 | */ |
| 354 | private Device composeDevice(DeviceId deviceId, |
| 355 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 356 | |
| 357 | checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied"); |
| 358 | |
| 359 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 360 | |
| 361 | DeviceDescriptions desc = providerDescs.get(primary); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 362 | |
| 363 | // base |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 364 | Type type = desc.getDeviceDesc().type(); |
| 365 | String manufacturer = desc.getDeviceDesc().manufacturer(); |
| 366 | String hwVersion = desc.getDeviceDesc().hwVersion(); |
| 367 | String swVersion = desc.getDeviceDesc().swVersion(); |
| 368 | String serialNumber = desc.getDeviceDesc().serialNumber(); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 369 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 370 | annotations = merge(annotations, desc.getDeviceDesc().annotations()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 371 | |
| 372 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 373 | if (e.getKey().equals(primary)) { |
| 374 | continue; |
| 375 | } |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 376 | // TODO: should keep track of Description timestamp |
| 377 | // and only merge conflicting keys when timestamp is newer |
| 378 | // Currently assuming there will never be a key conflict between |
| 379 | // providers |
| 380 | |
| 381 | // annotation merging. not so efficient, should revisit later |
| 382 | annotations = merge(annotations, e.getValue().getDeviceDesc().annotations()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 385 | return new DefaultDevice(primary, deviceId , type, manufacturer, |
| 386 | hwVersion, swVersion, serialNumber, annotations); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 389 | // probably want composePort"s" also |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 390 | private Port composePort(Device device, PortNumber number, |
| 391 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 392 | |
| 393 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 394 | DeviceDescriptions primDescs = providerDescs.get(primary); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 395 | // if no primary, assume not enabled |
| 396 | // TODO: revisit this default port enabled/disabled behavior |
| 397 | boolean isEnabled = false; |
| 398 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 399 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 400 | final PortDescription portDesc = primDescs.getPortDesc(number); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 401 | if (portDesc != null) { |
| 402 | isEnabled = portDesc.isEnabled(); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 403 | annotations = merge(annotations, portDesc.annotations()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 407 | if (e.getKey().equals(primary)) { |
| 408 | continue; |
| 409 | } |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 410 | // TODO: should keep track of Description timestamp |
| 411 | // and only merge conflicting keys when timestamp is newer |
| 412 | // Currently assuming there will never be a key conflict between |
| 413 | // providers |
| 414 | |
| 415 | // annotation merging. not so efficient, should revisit later |
| 416 | final PortDescription otherPortDesc = e.getValue().getPortDesc(number); |
| 417 | if (otherPortDesc != null) { |
| 418 | annotations = merge(annotations, otherPortDesc.annotations()); |
| 419 | } |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 422 | return new DefaultPort(device, number, isEnabled, annotations); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * @return primary ProviderID, or randomly chosen one if none exists |
| 427 | */ |
| 428 | private ProviderId pickPrimaryPID( |
| 429 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 430 | ProviderId fallBackPrimary = null; |
| 431 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 432 | if (!e.getKey().isAncillary()) { |
| 433 | return e.getKey(); |
| 434 | } else if (fallBackPrimary == null) { |
| 435 | // pick randomly as a fallback in case there is no primary |
| 436 | fallBackPrimary = e.getKey(); |
| 437 | } |
| 438 | } |
| 439 | return fallBackPrimary; |
| 440 | } |
| 441 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 442 | public static final class InitDeviceDescs |
| 443 | implements ConcurrentInitializer<DeviceDescriptions> { |
| 444 | private final DeviceDescription deviceDesc; |
| 445 | public InitDeviceDescs(DeviceDescription deviceDesc) { |
| 446 | this.deviceDesc = checkNotNull(deviceDesc); |
| 447 | } |
| 448 | @Override |
| 449 | public DeviceDescriptions get() throws ConcurrentException { |
| 450 | return new DeviceDescriptions(deviceDesc); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | |
| 455 | /** |
| 456 | * Collection of Description of a Device and it's Ports given from a Provider. |
| 457 | */ |
| 458 | private static class DeviceDescriptions { |
| 459 | // private final DeviceId id; |
| 460 | // private final ProviderId pid; |
| 461 | |
| 462 | private final AtomicReference<DeviceDescription> deviceDesc; |
| 463 | private final ConcurrentMap<PortNumber, PortDescription> portDescs; |
| 464 | |
| 465 | public DeviceDescriptions(DeviceDescription desc) { |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 466 | this.deviceDesc = new AtomicReference<>(checkNotNull(desc)); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 467 | this.portDescs = new ConcurrentHashMap<>(); |
| 468 | } |
| 469 | |
| 470 | public DeviceDescription getDeviceDesc() { |
| 471 | return deviceDesc.get(); |
| 472 | } |
| 473 | |
| 474 | public PortDescription getPortDesc(PortNumber number) { |
| 475 | return portDescs.get(number); |
| 476 | } |
| 477 | |
| 478 | public Collection<PortDescription> getPortDescs() { |
| 479 | return Collections.unmodifiableCollection(portDescs.values()); |
| 480 | } |
| 481 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Puts DeviceDescription, merging annotations as necessary. |
| 484 | * |
| 485 | * @param newDesc new DeviceDescription |
| 486 | * @return previous DeviceDescription |
| 487 | */ |
| 488 | public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) { |
| 489 | DeviceDescription oldOne = deviceDesc.get(); |
| 490 | DeviceDescription newOne = newDesc; |
| 491 | if (oldOne != null) { |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame^] | 492 | SparseAnnotations merged = union(oldOne.annotations(), |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 493 | newDesc.annotations()); |
| 494 | newOne = new DefaultDeviceDescription(newOne, merged); |
| 495 | } |
| 496 | return deviceDesc.getAndSet(newOne); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 499 | /** |
| 500 | * Puts PortDescription, merging annotations as necessary. |
| 501 | * |
| 502 | * @param newDesc new PortDescription |
| 503 | * @return previous PortDescription |
| 504 | */ |
| 505 | public synchronized PortDescription putPortDesc(PortDescription newDesc) { |
| 506 | PortDescription oldOne = portDescs.get(newDesc.portNumber()); |
| 507 | PortDescription newOne = newDesc; |
| 508 | if (oldOne != null) { |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame^] | 509 | SparseAnnotations merged = union(oldOne.annotations(), |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 510 | newDesc.annotations()); |
| 511 | newOne = new DefaultPortDescription(newOne, merged); |
| 512 | } |
| 513 | return portDescs.put(newOne.portNumber(), newOne); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 516 | } |