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