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