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