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