blob: ec4d916a18826dadee0f7406b9c1e622fd68ad50 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
tome5ec3fd2014-09-04 15:18:06 -070017
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070018import com.google.common.collect.FluentIterable;
tom46a220d2014-09-05 08:25:56 -070019import com.google.common.collect.ImmutableList;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070020import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.onlab.packet.ChassisId;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.AnnotationsUtil;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.DefaultDevice;
26import org.onosproject.net.DefaultPort;
27import org.onosproject.net.Device;
28import org.onosproject.net.Device.Type;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.SparseAnnotations;
33import org.onosproject.net.device.DefaultDeviceDescription;
34import org.onosproject.net.device.DefaultPortDescription;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020035import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.device.DeviceDescription;
37import org.onosproject.net.device.DeviceEvent;
38import org.onosproject.net.device.DeviceStore;
39import org.onosproject.net.device.DeviceStoreDelegate;
40import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070041import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.provider.ProviderId;
43import org.onosproject.store.AbstractStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044import org.osgi.service.component.annotations.Activate;
45import org.osgi.service.component.annotations.Component;
46import org.osgi.service.component.annotations.Deactivate;
tom41a2c5f2014-09-19 09:20:35 -070047import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070048
49import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070050import java.util.Collection;
tome5ec3fd2014-09-04 15:18:06 -070051import java.util.Collections;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070052import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070053import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070054import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070055import java.util.List;
56import java.util.Map;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070057import java.util.Map.Entry;
tome5ec3fd2014-09-04 15:18:06 -070058import java.util.Objects;
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -070059import java.util.Optional;
tome5ec3fd2014-09-04 15:18:06 -070060import java.util.Set;
61import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070062import java.util.concurrent.ConcurrentMap;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020063import java.util.concurrent.TimeUnit;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070064import java.util.concurrent.atomic.AtomicReference;
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -070065import java.util.stream.Stream;
tome5ec3fd2014-09-04 15:18:06 -070066
67import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070068import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070069import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070070import static com.google.common.base.Verify.verify;
Brian O'Connorabafb502014-12-02 22:26:20 -080071import static org.onosproject.net.DefaultAnnotations.merge;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072import static org.onosproject.net.DefaultAnnotations.union;
73import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
74import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
75import static org.onosproject.net.device.DeviceEvent.Type.PORT_ADDED;
76import static org.onosproject.net.device.DeviceEvent.Type.PORT_REMOVED;
77import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
78import static org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED;
79import static org.slf4j.LoggerFactory.getLogger;
tome5ec3fd2014-09-04 15:18:06 -070080
81/**
tome4729872014-09-23 00:37:37 -070082 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070083 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070084 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070085@Component(immediate = true, service = DeviceStore.class)
tomf80c9722014-09-24 14:49:18 -070086public class SimpleDeviceStore
87 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
88 implements DeviceStore {
tom41a2c5f2014-09-19 09:20:35 -070089
90 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070091
tom29df6f42014-09-05 08:14:14 -070092 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
93
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070094 // Collection of Description given from various providers
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070095 private final ConcurrentMap<DeviceId, Map<ProviderId, DeviceDescriptions>>
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070096 deviceDescs = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070097
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070098 // Cache of Device and Ports generated by compositing descriptions from providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070099 private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap();
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700100 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>>
101 devicePorts = Maps.newConcurrentMap();
sangho538108b2015-04-08 14:29:20 -0700102 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, PortStatistics>>
103 devicePortStats = Maps.newConcurrentMap();
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200104 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, PortStatistics>>
105 devicePortDeltaStats = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700106
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700107 // Available (=UP) devices
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700108 private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700109
tome5ec3fd2014-09-04 15:18:06 -0700110
tom41a2c5f2014-09-19 09:20:35 -0700111 @Activate
112 public void activate() {
113 log.info("Started");
114 }
115
116 @Deactivate
117 public void deactivate() {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700118 deviceDescs.clear();
119 devices.clear();
120 devicePorts.clear();
121 availableDevices.clear();
tom41a2c5f2014-09-19 09:20:35 -0700122 log.info("Stopped");
123 }
tom5bcc9462014-09-19 10:11:31 -0700124
tom41a2c5f2014-09-19 09:20:35 -0700125 @Override
126 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -0700127 return devices.size();
128 }
129
tom41a2c5f2014-09-19 09:20:35 -0700130 @Override
mskala0d0c6832017-07-12 11:21:23 +0200131 public int getAvailableDeviceCount() {
132 return availableDevices.size();
133 }
134
135 @Override
tom41a2c5f2014-09-19 09:20:35 -0700136 public Iterable<Device> getDevices() {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700137 return Collections.unmodifiableCollection(devices.values());
tome5ec3fd2014-09-04 15:18:06 -0700138 }
139
tom41a2c5f2014-09-19 09:20:35 -0700140 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800141 public Iterable<Device> getAvailableDevices() {
142 return FluentIterable.from(getDevices())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700143 .filter(input -> isAvailable(input.id()));
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800144 }
145
146 @Override
tom41a2c5f2014-09-19 09:20:35 -0700147 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700148 return devices.get(deviceId);
149 }
150
tom41a2c5f2014-09-19 09:20:35 -0700151 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700152 public DeviceEvent createOrUpdateDevice(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700153 DeviceId deviceId,
154 DeviceDescription deviceDescription) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700155 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700156 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700157
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700158 synchronized (providerDescs) {
159 // locking per device
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700160 DeviceDescriptions descs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700161 = getOrCreateProviderDeviceDescriptions(providerDescs,
162 providerId,
163 deviceDescription);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700164
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700165 Device oldDevice = devices.get(deviceId);
166 // update description
167 descs.putDeviceDesc(deviceDescription);
168 Device newDevice = composeDevice(deviceId, providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700169
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700170 if (oldDevice == null) {
171 // ADD
172 return createDevice(providerId, newDevice);
173 } else {
174 // UPDATE or ignore (no change or stale)
175 return updateDevice(providerId, oldDevice, newDevice);
176 }
tome5ec3fd2014-09-04 15:18:06 -0700177 }
tome5ec3fd2014-09-04 15:18:06 -0700178 }
179
180 // Creates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700181 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700182 private DeviceEvent createDevice(ProviderId providerId, Device newDevice) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700183 // update composed device cache
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700184 Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice);
185 verify(oldDevice == null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700186 "Unexpected Device in cache. PID:%s [old=%s, new=%s]",
187 providerId, oldDevice, newDevice);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700188
189 if (!providerId.isAncillary()) {
190 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700191 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700192
193 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700194 }
195
196 // Updates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700197 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700198 private DeviceEvent updateDevice(ProviderId providerId, Device oldDevice, Device newDevice) {
tome5ec3fd2014-09-04 15:18:06 -0700199 // We allow only certain attributes to trigger update
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700200 boolean propertiesChanged =
201 !Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) ||
202 !Objects.equals(oldDevice.swVersion(), newDevice.swVersion());
203 boolean annotationsChanged =
204 !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations());
205
206 // Primary providers can respond to all changes, but ancillary ones
207 // should respond only to annotation changes.
208 if ((providerId.isAncillary() && annotationsChanged) ||
209 (!providerId.isAncillary() && (propertiesChanged || annotationsChanged))) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700210
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700211 boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice);
212 if (!replaced) {
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700213 // FIXME: Is the enclosing if required here?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700214 verify(replaced,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700215 "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]",
Jian Li68c4fc42016-01-11 16:07:03 -0800216 providerId, oldDevice, devices.get(newDevice.id()), newDevice);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700217 }
218 if (!providerId.isAncillary()) {
219 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700220 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700221 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700222 }
223
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700224 // Otherwise merely attempt to change availability if primary provider
225 if (!providerId.isAncillary()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700226 boolean added = availableDevices.add(newDevice.id());
tom29df6f42014-09-05 08:14:14 -0700227 return !added ? null :
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700228 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700229 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700230 return null;
tome5ec3fd2014-09-04 15:18:06 -0700231 }
232
tom41a2c5f2014-09-19 09:20:35 -0700233 @Override
234 public DeviceEvent markOffline(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700235 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700236 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700237
238 // locking device
239 synchronized (providerDescs) {
tome5ec3fd2014-09-04 15:18:06 -0700240 Device device = devices.get(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700241 if (device == null) {
242 return null;
243 }
244 boolean removed = availableDevices.remove(deviceId);
245 if (removed) {
246 // TODO: broadcast ... DOWN only?
247 return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
248 }
249 return null;
tome5ec3fd2014-09-04 15:18:06 -0700250 }
251 }
252
helenyrwufd296b62016-06-22 17:43:02 -0700253 // implement differently if desired
254 @Override
Palash Kala6c526062018-04-03 18:25:11 +0900255 public DeviceEvent markOnline(DeviceId deviceId) {
helenyrwufd296b62016-06-22 17:43:02 -0700256 log.warn("Mark online not supported");
Palash Kala6c526062018-04-03 18:25:11 +0900257 return null;
helenyrwufd296b62016-06-22 17:43:02 -0700258 }
259
tom41a2c5f2014-09-19 09:20:35 -0700260 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700261 public List<DeviceEvent> updatePorts(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700262 DeviceId deviceId,
263 List<PortDescription> portDescriptions) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700264 Device device = devices.get(deviceId);
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800265 if (device == null) {
266 log.debug("Device {} doesn't exist or hasn't been initialized yet", deviceId);
267 return Collections.emptyList();
268 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700269
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700270 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700271 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
272
tom29df6f42014-09-05 08:14:14 -0700273 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700274 synchronized (descsMap) {
275 DeviceDescriptions descs = descsMap.get(providerId);
276 // every provider must provide DeviceDescription.
277 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700278 "Device description for Device ID %s from Provider %s was not found",
279 deviceId, providerId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700280
281 Map<PortNumber, Port> ports = getPortMap(deviceId);
tom29df6f42014-09-05 08:14:14 -0700282
283 // Add new ports
284 Set<PortNumber> processed = new HashSet<>();
285 for (PortDescription portDescription : portDescriptions) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700286 final PortNumber number = portDescription.portNumber();
287 processed.add(portDescription.portNumber());
288
289 final Port oldPort = ports.get(number);
290 final Port newPort;
291
292// event suppression hook?
293
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700294 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700295 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700296 newPort = composePort(device, number, descsMap);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700297
298 events.add(oldPort == null ?
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700299 createPort(device, newPort, ports) :
300 updatePort(device, oldPort, newPort, ports));
tom29df6f42014-09-05 08:14:14 -0700301 }
302
303 events.addAll(pruneOldPorts(device, ports, processed));
304 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700305 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700306 }
307
308 // Creates a new port based on the port description adds it to the map and
309 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700310 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700311 private DeviceEvent createPort(Device device, Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700312 Map<PortNumber, Port> ports) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700313 ports.put(newPort.number(), newPort);
314 return new DeviceEvent(PORT_ADDED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700315 }
316
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700317 // Checks if the specified port requires update and if so, it replaces the
tom29df6f42014-09-05 08:14:14 -0700318 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700319 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700320 private DeviceEvent updatePort(Device device, Port oldPort,
321 Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700322 Map<PortNumber, Port> ports) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700323 if (oldPort.isEnabled() != newPort.isEnabled() ||
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700324 oldPort.type() != newPort.type() ||
325 oldPort.portSpeed() != newPort.portSpeed() ||
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700326 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700327 ports.put(oldPort.number(), newPort);
328 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700329 }
330 return null;
331 }
332
333 // Prunes the specified list of ports based on which ports are in the
334 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700335 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700336 private List<DeviceEvent> pruneOldPorts(Device device,
337 Map<PortNumber, Port> ports,
338 Set<PortNumber> processed) {
339 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700340 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700341 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700342 Entry<PortNumber, Port> e = iterator.next();
343 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700344 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700345 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700346 iterator.remove();
347 }
348 }
349 return events;
350 }
351
352 // Gets the map of ports for the specified device; if one does not already
353 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700354 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700355 return devicePorts.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
tome5ec3fd2014-09-04 15:18:06 -0700356 }
357
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700358 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700359 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700360 Map<ProviderId, DeviceDescriptions> r;
361 r = deviceDescs.get(deviceId);
362 if (r != null) {
363 return r;
364 }
365 r = new HashMap<>();
366 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
367 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
368 if (concurrentlyAdded != null) {
369 return concurrentlyAdded;
370 } else {
371 return r;
372 }
373 }
374
375 // Guarded by deviceDescs value (=Device lock)
376 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700377 Map<ProviderId, DeviceDescriptions> device,
378 ProviderId providerId, DeviceDescription deltaDesc) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700379 synchronized (device) {
380 DeviceDescriptions r = device.get(providerId);
381 if (r == null) {
382 r = new DeviceDescriptions(deltaDesc);
383 device.put(providerId, r);
384 }
385 return r;
386 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700387 }
388
tom41a2c5f2014-09-19 09:20:35 -0700389 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700390 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700391 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700392 Device device = devices.get(deviceId);
393 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
394
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700395 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700396 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
397
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700398 synchronized (descsMap) {
399 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700400 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700401 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700402 "Device description for Device ID %s from Provider %s was not found",
403 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700404
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700405 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
406 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700407 final Port oldPort = ports.get(number);
408 final Port newPort;
409
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700410 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700411 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700412 newPort = composePort(device, number, descsMap);
413
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700414 if (oldPort == null) {
415 return createPort(device, newPort, ports);
416 } else {
417 return updatePort(device, oldPort, newPort, ports);
418 }
tom46a220d2014-09-05 08:25:56 -0700419 }
tome5ec3fd2014-09-04 15:18:06 -0700420 }
421
tom41a2c5f2014-09-19 09:20:35 -0700422 @Override
423 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700424 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700425 if (ports == null) {
426 return Collections.emptyList();
427 }
428 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700429 }
430
tom41a2c5f2014-09-19 09:20:35 -0700431 @Override
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -0700432 public Stream<PortDescription> getPortDescriptions(ProviderId providerId,
433 DeviceId deviceId) {
434 return Optional.ofNullable(deviceDescs.get(deviceId))
435 .map(m -> m.get(providerId))
436 .map(descs -> descs.portDescs.values().stream())
437 .orElse(Stream.empty());
438 }
439
440 @Override
sangho538108b2015-04-08 14:29:20 -0700441 public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200442 Collection<PortStatistics> newStatsCollection) {
sangho538108b2015-04-08 14:29:20 -0700443
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200444 ConcurrentMap<PortNumber, PortStatistics> prvStatsMap = devicePortStats.get(deviceId);
445 ConcurrentMap<PortNumber, PortStatistics> newStatsMap = Maps.newConcurrentMap();
446 ConcurrentMap<PortNumber, PortStatistics> deltaStatsMap = Maps.newConcurrentMap();
447
448 if (prvStatsMap != null) {
449 for (PortStatistics newStats : newStatsCollection) {
450 PortNumber port = PortNumber.portNumber(newStats.port());
451 PortStatistics prvStats = prvStatsMap.get(port);
452 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
453 PortStatistics deltaStats = builder.build();
454 if (prvStats != null) {
455 deltaStats = calcDeltaStats(deviceId, prvStats, newStats);
456 }
457 deltaStatsMap.put(port, deltaStats);
458 newStatsMap.put(port, newStats);
459 }
460 } else {
461 for (PortStatistics newStats : newStatsCollection) {
462 PortNumber port = PortNumber.portNumber(newStats.port());
463 newStatsMap.put(port, newStats);
464 }
sangho538108b2015-04-08 14:29:20 -0700465 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200466 devicePortDeltaStats.put(deviceId, deltaStatsMap);
467 devicePortStats.put(deviceId, newStatsMap);
sangho538108b2015-04-08 14:29:20 -0700468 return new DeviceEvent(PORT_STATS_UPDATED, devices.get(deviceId), null);
469 }
470
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200471 public PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats) {
472 // calculate time difference
473 long deltaStatsSec, deltaStatsNano;
474 if (newStats.durationNano() < prvStats.durationNano()) {
475 deltaStatsNano = newStats.durationNano() - prvStats.durationNano() + TimeUnit.SECONDS.toNanos(1);
476 deltaStatsSec = newStats.durationSec() - prvStats.durationSec() - 1L;
477 } else {
478 deltaStatsNano = newStats.durationNano() - prvStats.durationNano();
479 deltaStatsSec = newStats.durationSec() - prvStats.durationSec();
480 }
481 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
482 DefaultPortStatistics deltaStats = builder.setDeviceId(deviceId)
483 .setPort(newStats.port())
484 .setPacketsReceived(newStats.packetsReceived() - prvStats.packetsReceived())
485 .setPacketsSent(newStats.packetsSent() - prvStats.packetsSent())
486 .setBytesReceived(newStats.bytesReceived() - prvStats.bytesReceived())
487 .setBytesSent(newStats.bytesSent() - prvStats.bytesSent())
488 .setPacketsRxDropped(newStats.packetsRxDropped() - prvStats.packetsRxDropped())
489 .setPacketsTxDropped(newStats.packetsTxDropped() - prvStats.packetsTxDropped())
490 .setPacketsRxErrors(newStats.packetsRxErrors() - prvStats.packetsRxErrors())
491 .setPacketsTxErrors(newStats.packetsTxErrors() - prvStats.packetsTxErrors())
492 .setDurationSec(deltaStatsSec)
493 .setDurationNano(deltaStatsNano)
494 .build();
495 return deltaStats;
496 }
497
sangho538108b2015-04-08 14:29:20 -0700498 @Override
tom41a2c5f2014-09-19 09:20:35 -0700499 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700500 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
501 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700502 }
503
tom41a2c5f2014-09-19 09:20:35 -0700504 @Override
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -0700505 public PortDescription getPortDescription(ProviderId providerId,
506 DeviceId deviceId,
507 PortNumber portNumber) {
508 return Optional.ofNullable(deviceDescs.get(deviceId))
509 .map(m -> m.get(providerId))
510 .map(descs -> descs.getPortDesc(portNumber))
511 .orElse(null);
512 }
513
514 @Override
sangho538108b2015-04-08 14:29:20 -0700515 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
516 Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId);
517 if (portStats == null) {
518 return Collections.emptyList();
519 }
520 return ImmutableList.copyOf(portStats.values());
521 }
522
523 @Override
Viswanath KSP22774cd2016-08-20 20:06:30 +0530524 public PortStatistics getStatisticsForPort(DeviceId deviceId, PortNumber portNumber) {
525 Map<PortNumber, PortStatistics> portStatsMap = devicePortStats.get(deviceId);
526 if (portStatsMap == null) {
527 return null;
528 }
529 PortStatistics portStats = portStatsMap.get(portNumber);
530 return portStats;
531 }
532
533 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200534 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
535 Map<PortNumber, PortStatistics> portStats = devicePortDeltaStats.get(deviceId);
536 if (portStats == null) {
537 return Collections.emptyList();
538 }
539 return ImmutableList.copyOf(portStats.values());
540 }
541
542 @Override
Viswanath KSP22774cd2016-08-20 20:06:30 +0530543 public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, PortNumber portNumber) {
544 Map<PortNumber, PortStatistics> portStatsMap = devicePortDeltaStats.get(deviceId);
545 if (portStatsMap == null) {
546 return null;
547 }
548 PortStatistics portStats = portStatsMap.get(portNumber);
549 return portStats;
550 }
551
552 @Override
tom41a2c5f2014-09-19 09:20:35 -0700553 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700554 return availableDevices.contains(deviceId);
555 }
556
tom41a2c5f2014-09-19 09:20:35 -0700557 @Override
tom41a2c5f2014-09-19 09:20:35 -0700558 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700559 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700560 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700561 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700562 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700563 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700564 if (ports != null) {
565 ports.clear();
566 }
567 availableDevices.remove(deviceId);
568 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700569 return device == null ? null :
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700570 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700571 }
572 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700573
574 /**
575 * Returns a Device, merging description given from multiple Providers.
576 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700577 * @param deviceId device identifier
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700578 * @param providerDescs Collection of Descriptions from multiple providers
579 * @return Device instance
580 */
581 private Device composeDevice(DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700582 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700583
584 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
585
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800586 ProviderId primary = pickPrimaryPid(providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700587
588 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700589
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700590 final DeviceDescription base = desc.getDeviceDesc();
591 Type type = base.type();
592 String manufacturer = base.manufacturer();
593 String hwVersion = base.hwVersion();
594 String swVersion = base.swVersion();
595 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700596 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700597 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700598 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700599
600 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
601 if (e.getKey().equals(primary)) {
602 continue;
603 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700604 // TODO: should keep track of Description timestamp
605 // and only merge conflicting keys when timestamp is newer
606 // Currently assuming there will never be a key conflict between
607 // providers
608
609 // annotation merging. not so efficient, should revisit later
610 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700611 }
612
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700613 return new DefaultDevice(primary, deviceId, type, manufacturer,
614 hwVersion, swVersion, serialNumber,
615 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700616 }
617
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700618 /**
619 * Returns a Port, merging description given from multiple Providers.
620 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700621 * @param device device the port is on
622 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700623 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700624 * @return Port instance
625 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700626 private Port composePort(Device device, PortNumber number,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700627 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700628
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800629 ProviderId primary = pickPrimaryPid(descsMap);
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700630 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700631 // if no primary, assume not enabled
632 // TODO: revisit this default port enabled/disabled behavior
633 boolean isEnabled = false;
634 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
635
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700636 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700637 if (portDesc != null) {
638 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700639 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700640 }
641
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700642 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700643 if (e.getKey().equals(primary)) {
644 continue;
645 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700646 // TODO: should keep track of Description timestamp
647 // and only merge conflicting keys when timestamp is newer
648 // Currently assuming there will never be a key conflict between
649 // providers
650
651 // annotation merging. not so efficient, should revisit later
652 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
653 if (otherPortDesc != null) {
654 annotations = merge(annotations, otherPortDesc.annotations());
655 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700656 }
657
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700658 return portDesc == null ?
659 new DefaultPort(device, number, false, annotations) :
660 new DefaultPort(device, number, isEnabled, portDesc.type(),
661 portDesc.portSpeed(), annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700662 }
663
664 /**
665 * @return primary ProviderID, or randomly chosen one if none exists
666 */
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800667 private ProviderId pickPrimaryPid(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700668 ProviderId fallBackPrimary = null;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700669 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700670 if (!e.getKey().isAncillary()) {
671 return e.getKey();
672 } else if (fallBackPrimary == null) {
673 // pick randomly as a fallback in case there is no primary
674 fallBackPrimary = e.getKey();
675 }
676 }
677 return fallBackPrimary;
678 }
679
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700680 /**
681 * Collection of Description of a Device and it's Ports given from a Provider.
682 */
683 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700684
685 private final AtomicReference<DeviceDescription> deviceDesc;
686 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
687
688 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700689 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700690 this.portDescs = new ConcurrentHashMap<>();
691 }
692
693 public DeviceDescription getDeviceDesc() {
694 return deviceDesc.get();
695 }
696
697 public PortDescription getPortDesc(PortNumber number) {
698 return portDescs.get(number);
699 }
700
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700701 /**
702 * Puts DeviceDescription, merging annotations as necessary.
703 *
704 * @param newDesc new DeviceDescription
705 * @return previous DeviceDescription
706 */
707 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
708 DeviceDescription oldOne = deviceDesc.get();
709 DeviceDescription newOne = newDesc;
710 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700711 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700712 newDesc.annotations());
713 newOne = new DefaultDeviceDescription(newOne, merged);
714 }
715 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700716 }
717
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700718 /**
719 * Puts PortDescription, merging annotations as necessary.
720 *
721 * @param newDesc new PortDescription
722 * @return previous PortDescription
723 */
724 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
725 PortDescription oldOne = portDescs.get(newDesc.portNumber());
726 PortDescription newOne = newDesc;
727 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700728 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700729 newDesc.annotations());
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800730 newOne = DefaultPortDescription.builder(newOne)
731 .annotations(merged)
732 .build();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700733 }
734 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700735 }
736 }
tome5ec3fd2014-09-04 15:18:06 -0700737}