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