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