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