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