blob: 31b4292cd614d10b629ad5d8a01b1064d1523805 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070022
tom41a2c5f2014-09-19 09:20:35 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.AnnotationsUtil;
28import org.onosproject.net.DefaultAnnotations;
29import org.onosproject.net.DefaultDevice;
30import org.onosproject.net.DefaultPort;
31import org.onosproject.net.Device;
32import org.onosproject.net.Device.Type;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.SparseAnnotations;
37import org.onosproject.net.device.DefaultDeviceDescription;
38import org.onosproject.net.device.DefaultPortDescription;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020039import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.device.DeviceDescription;
41import org.onosproject.net.device.DeviceEvent;
42import org.onosproject.net.device.DeviceStore;
43import org.onosproject.net.device.DeviceStoreDelegate;
44import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070045import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.provider.ProviderId;
47import org.onosproject.store.AbstractStore;
alshabib7911a052014-10-16 17:49:37 -070048import org.onlab.packet.ChassisId;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070049import org.onlab.util.NewConcurrentHashMap;
tom41a2c5f2014-09-19 09:20:35 -070050import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070051
52import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070053import java.util.Collection;
tome5ec3fd2014-09-04 15:18:06 -070054import java.util.Collections;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070055import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070056import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070057import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070058import java.util.List;
59import java.util.Map;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070060import java.util.Map.Entry;
tome5ec3fd2014-09-04 15:18:06 -070061import java.util.Objects;
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -070062import java.util.Optional;
tome5ec3fd2014-09-04 15:18:06 -070063import java.util.Set;
64import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070065import java.util.concurrent.ConcurrentMap;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020066import java.util.concurrent.TimeUnit;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070067import java.util.concurrent.atomic.AtomicReference;
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -070068import java.util.stream.Stream;
tome5ec3fd2014-09-04 15:18:06 -070069
70import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070071import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070072import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070073import static com.google.common.base.Verify.verify;
Brian O'Connorabafb502014-12-02 22:26:20 -080074import static org.onosproject.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070075import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070076import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Brian O'Connorabafb502014-12-02 22:26:20 -080077import static org.onosproject.net.DefaultAnnotations.union;
78import static org.onosproject.net.DefaultAnnotations.merge;
tome5ec3fd2014-09-04 15:18:06 -070079
80/**
tome4729872014-09-23 00:37:37 -070081 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070082 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070083 */
tom41a2c5f2014-09-19 09:20:35 -070084@Component(immediate = true)
85@Service
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
131 public Iterable<Device> getDevices() {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700132 return Collections.unmodifiableCollection(devices.values());
tome5ec3fd2014-09-04 15:18:06 -0700133 }
134
tom41a2c5f2014-09-19 09:20:35 -0700135 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800136 public Iterable<Device> getAvailableDevices() {
137 return FluentIterable.from(getDevices())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700138 .filter(input -> isAvailable(input.id()));
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800139 }
140
141 @Override
tom41a2c5f2014-09-19 09:20:35 -0700142 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700143 return devices.get(deviceId);
144 }
145
tom41a2c5f2014-09-19 09:20:35 -0700146 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700147 public DeviceEvent createOrUpdateDevice(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700148 DeviceId deviceId,
149 DeviceDescription deviceDescription) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700150 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700151 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700152
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700153 synchronized (providerDescs) {
154 // locking per device
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700155 DeviceDescriptions descs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700156 = getOrCreateProviderDeviceDescriptions(providerDescs,
157 providerId,
158 deviceDescription);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700159
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700160 Device oldDevice = devices.get(deviceId);
161 // update description
162 descs.putDeviceDesc(deviceDescription);
163 Device newDevice = composeDevice(deviceId, providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700164
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700165 if (oldDevice == null) {
166 // ADD
167 return createDevice(providerId, newDevice);
168 } else {
169 // UPDATE or ignore (no change or stale)
170 return updateDevice(providerId, oldDevice, newDevice);
171 }
tome5ec3fd2014-09-04 15:18:06 -0700172 }
tome5ec3fd2014-09-04 15:18:06 -0700173 }
174
175 // Creates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700176 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700177 private DeviceEvent createDevice(ProviderId providerId, Device newDevice) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700178 // update composed device cache
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700179 Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice);
180 verify(oldDevice == null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700181 "Unexpected Device in cache. PID:%s [old=%s, new=%s]",
182 providerId, oldDevice, newDevice);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700183
184 if (!providerId.isAncillary()) {
185 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700186 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700187
188 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700189 }
190
191 // Updates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700192 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700193 private DeviceEvent updateDevice(ProviderId providerId, Device oldDevice, Device newDevice) {
tome5ec3fd2014-09-04 15:18:06 -0700194 // We allow only certain attributes to trigger update
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700195 boolean propertiesChanged =
196 !Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) ||
197 !Objects.equals(oldDevice.swVersion(), newDevice.swVersion());
198 boolean annotationsChanged =
199 !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations());
200
201 // Primary providers can respond to all changes, but ancillary ones
202 // should respond only to annotation changes.
203 if ((providerId.isAncillary() && annotationsChanged) ||
204 (!providerId.isAncillary() && (propertiesChanged || annotationsChanged))) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700205
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700206 boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice);
207 if (!replaced) {
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700208 // FIXME: Is the enclosing if required here?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700209 verify(replaced,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700210 "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]",
Jian Li68c4fc42016-01-11 16:07:03 -0800211 providerId, oldDevice, devices.get(newDevice.id()), newDevice);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700212 }
213 if (!providerId.isAncillary()) {
214 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700215 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700216 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700217 }
218
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700219 // Otherwise merely attempt to change availability if primary provider
220 if (!providerId.isAncillary()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700221 boolean added = availableDevices.add(newDevice.id());
tom29df6f42014-09-05 08:14:14 -0700222 return !added ? null :
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700223 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700224 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700225 return null;
tome5ec3fd2014-09-04 15:18:06 -0700226 }
227
tom41a2c5f2014-09-19 09:20:35 -0700228 @Override
229 public DeviceEvent markOffline(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700230 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700231 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700232
233 // locking device
234 synchronized (providerDescs) {
tome5ec3fd2014-09-04 15:18:06 -0700235 Device device = devices.get(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700236 if (device == null) {
237 return null;
238 }
239 boolean removed = availableDevices.remove(deviceId);
240 if (removed) {
241 // TODO: broadcast ... DOWN only?
242 return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
243 }
244 return null;
tome5ec3fd2014-09-04 15:18:06 -0700245 }
246 }
247
tom41a2c5f2014-09-19 09:20:35 -0700248 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700249 public List<DeviceEvent> updatePorts(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700250 DeviceId deviceId,
251 List<PortDescription> portDescriptions) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700252 Device device = devices.get(deviceId);
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800253 if (device == null) {
254 log.debug("Device {} doesn't exist or hasn't been initialized yet", deviceId);
255 return Collections.emptyList();
256 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700257
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700258 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700259 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
260
tom29df6f42014-09-05 08:14:14 -0700261 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700262 synchronized (descsMap) {
263 DeviceDescriptions descs = descsMap.get(providerId);
264 // every provider must provide DeviceDescription.
265 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700266 "Device description for Device ID %s from Provider %s was not found",
267 deviceId, providerId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700268
269 Map<PortNumber, Port> ports = getPortMap(deviceId);
tom29df6f42014-09-05 08:14:14 -0700270
271 // Add new ports
272 Set<PortNumber> processed = new HashSet<>();
273 for (PortDescription portDescription : portDescriptions) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700274 final PortNumber number = portDescription.portNumber();
275 processed.add(portDescription.portNumber());
276
277 final Port oldPort = ports.get(number);
278 final Port newPort;
279
280// event suppression hook?
281
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700282 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700283 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700284 newPort = composePort(device, number, descsMap);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700285
286 events.add(oldPort == null ?
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700287 createPort(device, newPort, ports) :
288 updatePort(device, oldPort, newPort, ports));
tom29df6f42014-09-05 08:14:14 -0700289 }
290
291 events.addAll(pruneOldPorts(device, ports, processed));
292 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700293 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700294 }
295
296 // Creates a new port based on the port description adds it to the map and
297 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700298 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700299 private DeviceEvent createPort(Device device, Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700300 Map<PortNumber, Port> ports) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700301 ports.put(newPort.number(), newPort);
302 return new DeviceEvent(PORT_ADDED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700303 }
304
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700305 // Checks if the specified port requires update and if so, it replaces the
tom29df6f42014-09-05 08:14:14 -0700306 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700307 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700308 private DeviceEvent updatePort(Device device, Port oldPort,
309 Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700310 Map<PortNumber, Port> ports) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700311 if (oldPort.isEnabled() != newPort.isEnabled() ||
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700312 oldPort.type() != newPort.type() ||
313 oldPort.portSpeed() != newPort.portSpeed() ||
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700314 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700315 ports.put(oldPort.number(), newPort);
316 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700317 }
318 return null;
319 }
320
321 // Prunes the specified list of ports based on which ports are in the
322 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700323 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700324 private List<DeviceEvent> pruneOldPorts(Device device,
325 Map<PortNumber, Port> ports,
326 Set<PortNumber> processed) {
327 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700328 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700329 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700330 Entry<PortNumber, Port> e = iterator.next();
331 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700332 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700333 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700334 iterator.remove();
335 }
336 }
337 return events;
338 }
339
340 // Gets the map of ports for the specified device; if one does not already
341 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700342 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
343 return createIfAbsentUnchecked(devicePorts, deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700344 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
tome5ec3fd2014-09-04 15:18:06 -0700345 }
346
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700347 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700348 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700349 Map<ProviderId, DeviceDescriptions> r;
350 r = deviceDescs.get(deviceId);
351 if (r != null) {
352 return r;
353 }
354 r = new HashMap<>();
355 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
356 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
357 if (concurrentlyAdded != null) {
358 return concurrentlyAdded;
359 } else {
360 return r;
361 }
362 }
363
364 // Guarded by deviceDescs value (=Device lock)
365 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700366 Map<ProviderId, DeviceDescriptions> device,
367 ProviderId providerId, DeviceDescription deltaDesc) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700368 synchronized (device) {
369 DeviceDescriptions r = device.get(providerId);
370 if (r == null) {
371 r = new DeviceDescriptions(deltaDesc);
372 device.put(providerId, r);
373 }
374 return r;
375 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700376 }
377
tom41a2c5f2014-09-19 09:20:35 -0700378 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700379 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700380 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700381 Device device = devices.get(deviceId);
382 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
383
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700384 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700385 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
386
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700387 synchronized (descsMap) {
388 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700389 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700390 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700391 "Device description for Device ID %s from Provider %s was not found",
392 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700393
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700394 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
395 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700396 final Port oldPort = ports.get(number);
397 final Port newPort;
398
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700399 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700400 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700401 newPort = composePort(device, number, descsMap);
402
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700403 if (oldPort == null) {
404 return createPort(device, newPort, ports);
405 } else {
406 return updatePort(device, oldPort, newPort, ports);
407 }
tom46a220d2014-09-05 08:25:56 -0700408 }
tome5ec3fd2014-09-04 15:18:06 -0700409 }
410
tom41a2c5f2014-09-19 09:20:35 -0700411 @Override
412 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700413 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700414 if (ports == null) {
415 return Collections.emptyList();
416 }
417 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700418 }
419
tom41a2c5f2014-09-19 09:20:35 -0700420 @Override
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -0700421 public Stream<PortDescription> getPortDescriptions(ProviderId providerId,
422 DeviceId deviceId) {
423 return Optional.ofNullable(deviceDescs.get(deviceId))
424 .map(m -> m.get(providerId))
425 .map(descs -> descs.portDescs.values().stream())
426 .orElse(Stream.empty());
427 }
428
429 @Override
sangho538108b2015-04-08 14:29:20 -0700430 public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200431 Collection<PortStatistics> newStatsCollection) {
sangho538108b2015-04-08 14:29:20 -0700432
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200433 ConcurrentMap<PortNumber, PortStatistics> prvStatsMap = devicePortStats.get(deviceId);
434 ConcurrentMap<PortNumber, PortStatistics> newStatsMap = Maps.newConcurrentMap();
435 ConcurrentMap<PortNumber, PortStatistics> deltaStatsMap = Maps.newConcurrentMap();
436
437 if (prvStatsMap != null) {
438 for (PortStatistics newStats : newStatsCollection) {
439 PortNumber port = PortNumber.portNumber(newStats.port());
440 PortStatistics prvStats = prvStatsMap.get(port);
441 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
442 PortStatistics deltaStats = builder.build();
443 if (prvStats != null) {
444 deltaStats = calcDeltaStats(deviceId, prvStats, newStats);
445 }
446 deltaStatsMap.put(port, deltaStats);
447 newStatsMap.put(port, newStats);
448 }
449 } else {
450 for (PortStatistics newStats : newStatsCollection) {
451 PortNumber port = PortNumber.portNumber(newStats.port());
452 newStatsMap.put(port, newStats);
453 }
sangho538108b2015-04-08 14:29:20 -0700454 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200455 devicePortDeltaStats.put(deviceId, deltaStatsMap);
456 devicePortStats.put(deviceId, newStatsMap);
sangho538108b2015-04-08 14:29:20 -0700457 return new DeviceEvent(PORT_STATS_UPDATED, devices.get(deviceId), null);
458 }
459
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200460 public PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats) {
461 // calculate time difference
462 long deltaStatsSec, deltaStatsNano;
463 if (newStats.durationNano() < prvStats.durationNano()) {
464 deltaStatsNano = newStats.durationNano() - prvStats.durationNano() + TimeUnit.SECONDS.toNanos(1);
465 deltaStatsSec = newStats.durationSec() - prvStats.durationSec() - 1L;
466 } else {
467 deltaStatsNano = newStats.durationNano() - prvStats.durationNano();
468 deltaStatsSec = newStats.durationSec() - prvStats.durationSec();
469 }
470 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
471 DefaultPortStatistics deltaStats = builder.setDeviceId(deviceId)
472 .setPort(newStats.port())
473 .setPacketsReceived(newStats.packetsReceived() - prvStats.packetsReceived())
474 .setPacketsSent(newStats.packetsSent() - prvStats.packetsSent())
475 .setBytesReceived(newStats.bytesReceived() - prvStats.bytesReceived())
476 .setBytesSent(newStats.bytesSent() - prvStats.bytesSent())
477 .setPacketsRxDropped(newStats.packetsRxDropped() - prvStats.packetsRxDropped())
478 .setPacketsTxDropped(newStats.packetsTxDropped() - prvStats.packetsTxDropped())
479 .setPacketsRxErrors(newStats.packetsRxErrors() - prvStats.packetsRxErrors())
480 .setPacketsTxErrors(newStats.packetsTxErrors() - prvStats.packetsTxErrors())
481 .setDurationSec(deltaStatsSec)
482 .setDurationNano(deltaStatsNano)
483 .build();
484 return deltaStats;
485 }
486
sangho538108b2015-04-08 14:29:20 -0700487 @Override
tom41a2c5f2014-09-19 09:20:35 -0700488 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700489 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
490 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700491 }
492
tom41a2c5f2014-09-19 09:20:35 -0700493 @Override
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -0700494 public PortDescription getPortDescription(ProviderId providerId,
495 DeviceId deviceId,
496 PortNumber portNumber) {
497 return Optional.ofNullable(deviceDescs.get(deviceId))
498 .map(m -> m.get(providerId))
499 .map(descs -> descs.getPortDesc(portNumber))
500 .orElse(null);
501 }
502
503 @Override
sangho538108b2015-04-08 14:29:20 -0700504 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
505 Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId);
506 if (portStats == null) {
507 return Collections.emptyList();
508 }
509 return ImmutableList.copyOf(portStats.values());
510 }
511
512 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200513 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
514 Map<PortNumber, PortStatistics> portStats = devicePortDeltaStats.get(deviceId);
515 if (portStats == null) {
516 return Collections.emptyList();
517 }
518 return ImmutableList.copyOf(portStats.values());
519 }
520
521 @Override
tom41a2c5f2014-09-19 09:20:35 -0700522 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700523 return availableDevices.contains(deviceId);
524 }
525
tom41a2c5f2014-09-19 09:20:35 -0700526 @Override
tom41a2c5f2014-09-19 09:20:35 -0700527 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700528 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700529 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700530 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700531 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700532 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700533 if (ports != null) {
534 ports.clear();
535 }
536 availableDevices.remove(deviceId);
537 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700538 return device == null ? null :
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700539 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700540 }
541 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700542
543 /**
544 * Returns a Device, merging description given from multiple Providers.
545 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700546 * @param deviceId device identifier
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700547 * @param providerDescs Collection of Descriptions from multiple providers
548 * @return Device instance
549 */
550 private Device composeDevice(DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700551 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700552
553 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
554
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800555 ProviderId primary = pickPrimaryPid(providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700556
557 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700558
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700559 final DeviceDescription base = desc.getDeviceDesc();
560 Type type = base.type();
561 String manufacturer = base.manufacturer();
562 String hwVersion = base.hwVersion();
563 String swVersion = base.swVersion();
564 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700565 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700566 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700567 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700568
569 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
570 if (e.getKey().equals(primary)) {
571 continue;
572 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700573 // TODO: should keep track of Description timestamp
574 // and only merge conflicting keys when timestamp is newer
575 // Currently assuming there will never be a key conflict between
576 // providers
577
578 // annotation merging. not so efficient, should revisit later
579 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700580 }
581
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700582 return new DefaultDevice(primary, deviceId, type, manufacturer,
583 hwVersion, swVersion, serialNumber,
584 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700585 }
586
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700587 /**
588 * Returns a Port, merging description given from multiple Providers.
589 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700590 * @param device device the port is on
591 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700592 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700593 * @return Port instance
594 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700595 private Port composePort(Device device, PortNumber number,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700596 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700597
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800598 ProviderId primary = pickPrimaryPid(descsMap);
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700599 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700600 // if no primary, assume not enabled
601 // TODO: revisit this default port enabled/disabled behavior
602 boolean isEnabled = false;
603 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
604
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700605 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700606 if (portDesc != null) {
607 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700608 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700609 }
610
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700611 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700612 if (e.getKey().equals(primary)) {
613 continue;
614 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700615 // TODO: should keep track of Description timestamp
616 // and only merge conflicting keys when timestamp is newer
617 // Currently assuming there will never be a key conflict between
618 // providers
619
620 // annotation merging. not so efficient, should revisit later
621 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
622 if (otherPortDesc != null) {
623 annotations = merge(annotations, otherPortDesc.annotations());
624 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700625 }
626
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700627 return portDesc == null ?
628 new DefaultPort(device, number, false, annotations) :
629 new DefaultPort(device, number, isEnabled, portDesc.type(),
630 portDesc.portSpeed(), annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700631 }
632
633 /**
634 * @return primary ProviderID, or randomly chosen one if none exists
635 */
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800636 private ProviderId pickPrimaryPid(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700637 ProviderId fallBackPrimary = null;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700638 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700639 if (!e.getKey().isAncillary()) {
640 return e.getKey();
641 } else if (fallBackPrimary == null) {
642 // pick randomly as a fallback in case there is no primary
643 fallBackPrimary = e.getKey();
644 }
645 }
646 return fallBackPrimary;
647 }
648
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700649 /**
650 * Collection of Description of a Device and it's Ports given from a Provider.
651 */
652 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700653
654 private final AtomicReference<DeviceDescription> deviceDesc;
655 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
656
657 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700658 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700659 this.portDescs = new ConcurrentHashMap<>();
660 }
661
662 public DeviceDescription getDeviceDesc() {
663 return deviceDesc.get();
664 }
665
666 public PortDescription getPortDesc(PortNumber number) {
667 return portDescs.get(number);
668 }
669
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700670 /**
671 * Puts DeviceDescription, merging annotations as necessary.
672 *
673 * @param newDesc new DeviceDescription
674 * @return previous DeviceDescription
675 */
676 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
677 DeviceDescription oldOne = deviceDesc.get();
678 DeviceDescription newOne = newDesc;
679 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700680 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700681 newDesc.annotations());
682 newOne = new DefaultDeviceDescription(newOne, merged);
683 }
684 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700685 }
686
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700687 /**
688 * Puts PortDescription, merging annotations as necessary.
689 *
690 * @param newDesc new PortDescription
691 * @return previous PortDescription
692 */
693 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
694 PortDescription oldOne = portDescs.get(newDesc.portNumber());
695 PortDescription newOne = newDesc;
696 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700697 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700698 newDesc.annotations());
699 newOne = new DefaultPortDescription(newOne, merged);
700 }
701 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700702 }
703 }
tome5ec3fd2014-09-04 15:18:06 -0700704}