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