blob: 4218d4737ee5286b9ae5698e9cff759c4934670c [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;
tom41a2c5f2014-09-19 09:20:35 -070049import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070050
51import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070052import java.util.Collection;
tome5ec3fd2014-09-04 15:18:06 -070053import java.util.Collections;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070054import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070055import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070056import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070057import java.util.List;
58import java.util.Map;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070059import java.util.Map.Entry;
tome5ec3fd2014-09-04 15:18:06 -070060import java.util.Objects;
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -070061import java.util.Optional;
tome5ec3fd2014-09-04 15:18:06 -070062import java.util.Set;
63import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070064import java.util.concurrent.ConcurrentMap;
Dusan Pajin11ff4a82015-08-20 18:03:05 +020065import java.util.concurrent.TimeUnit;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070066import java.util.concurrent.atomic.AtomicReference;
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -070067import java.util.stream.Stream;
tome5ec3fd2014-09-04 15:18:06 -070068
69import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070070import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070071import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070072import static com.google.common.base.Verify.verify;
Brian O'Connorabafb502014-12-02 22:26:20 -080073import static org.onosproject.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070074import static org.slf4j.LoggerFactory.getLogger;
Brian O'Connorabafb502014-12-02 22:26:20 -080075import static org.onosproject.net.DefaultAnnotations.union;
76import static org.onosproject.net.DefaultAnnotations.merge;
tome5ec3fd2014-09-04 15:18:06 -070077
78/**
tome4729872014-09-23 00:37:37 -070079 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070080 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070081 */
tom41a2c5f2014-09-19 09:20:35 -070082@Component(immediate = true)
83@Service
tomf80c9722014-09-24 14:49:18 -070084public class SimpleDeviceStore
85 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
86 implements DeviceStore {
tom41a2c5f2014-09-19 09:20:35 -070087
88 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070089
tom29df6f42014-09-05 08:14:14 -070090 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
91
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070092 // Collection of Description given from various providers
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070093 private final ConcurrentMap<DeviceId, Map<ProviderId, DeviceDescriptions>>
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070094 deviceDescs = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070095
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070096 // Cache of Device and Ports generated by compositing descriptions from providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070097 private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap();
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070098 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>>
99 devicePorts = Maps.newConcurrentMap();
sangho538108b2015-04-08 14:29:20 -0700100 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, PortStatistics>>
101 devicePortStats = Maps.newConcurrentMap();
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200102 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, PortStatistics>>
103 devicePortDeltaStats = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700104
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700105 // Available (=UP) devices
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700106 private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700107
tome5ec3fd2014-09-04 15:18:06 -0700108
tom41a2c5f2014-09-19 09:20:35 -0700109 @Activate
110 public void activate() {
111 log.info("Started");
112 }
113
114 @Deactivate
115 public void deactivate() {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700116 deviceDescs.clear();
117 devices.clear();
118 devicePorts.clear();
119 availableDevices.clear();
tom41a2c5f2014-09-19 09:20:35 -0700120 log.info("Stopped");
121 }
tom5bcc9462014-09-19 10:11:31 -0700122
tom41a2c5f2014-09-19 09:20:35 -0700123 @Override
124 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -0700125 return devices.size();
126 }
127
tom41a2c5f2014-09-19 09:20:35 -0700128 @Override
129 public Iterable<Device> getDevices() {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700130 return Collections.unmodifiableCollection(devices.values());
tome5ec3fd2014-09-04 15:18:06 -0700131 }
132
tom41a2c5f2014-09-19 09:20:35 -0700133 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800134 public Iterable<Device> getAvailableDevices() {
135 return FluentIterable.from(getDevices())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700136 .filter(input -> isAvailable(input.id()));
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800137 }
138
139 @Override
tom41a2c5f2014-09-19 09:20:35 -0700140 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700141 return devices.get(deviceId);
142 }
143
tom41a2c5f2014-09-19 09:20:35 -0700144 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700145 public DeviceEvent createOrUpdateDevice(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700146 DeviceId deviceId,
147 DeviceDescription deviceDescription) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700148 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700149 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700150
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700151 synchronized (providerDescs) {
152 // locking per device
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700153 DeviceDescriptions descs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700154 = getOrCreateProviderDeviceDescriptions(providerDescs,
155 providerId,
156 deviceDescription);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700157
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700158 Device oldDevice = devices.get(deviceId);
159 // update description
160 descs.putDeviceDesc(deviceDescription);
161 Device newDevice = composeDevice(deviceId, providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700162
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700163 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 }
tome5ec3fd2014-09-04 15:18:06 -0700170 }
tome5ec3fd2014-09-04 15:18:06 -0700171 }
172
173 // Creates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700174 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700175 private DeviceEvent createDevice(ProviderId providerId, Device newDevice) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700176 // update composed device cache
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700177 Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice);
178 verify(oldDevice == null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700179 "Unexpected Device in cache. PID:%s [old=%s, new=%s]",
180 providerId, oldDevice, newDevice);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700181
182 if (!providerId.isAncillary()) {
183 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700184 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700185
186 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700187 }
188
189 // Updates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700190 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700191 private DeviceEvent updateDevice(ProviderId providerId, Device oldDevice, Device newDevice) {
tome5ec3fd2014-09-04 15:18:06 -0700192 // We allow only certain attributes to trigger update
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700193 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 HIGUCHI8e493792014-10-01 19:36:32 -0700203
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700204 boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice);
205 if (!replaced) {
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700206 // FIXME: Is the enclosing if required here?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700207 verify(replaced,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700208 "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]",
Jian Li68c4fc42016-01-11 16:07:03 -0800209 providerId, oldDevice, devices.get(newDevice.id()), newDevice);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700210 }
211 if (!providerId.isAncillary()) {
212 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700213 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700214 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700215 }
216
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700217 // Otherwise merely attempt to change availability if primary provider
218 if (!providerId.isAncillary()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700219 boolean added = availableDevices.add(newDevice.id());
tom29df6f42014-09-05 08:14:14 -0700220 return !added ? null :
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700221 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700222 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700223 return null;
tome5ec3fd2014-09-04 15:18:06 -0700224 }
225
tom41a2c5f2014-09-19 09:20:35 -0700226 @Override
227 public DeviceEvent markOffline(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700228 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700229 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700230
231 // locking device
232 synchronized (providerDescs) {
tome5ec3fd2014-09-04 15:18:06 -0700233 Device device = devices.get(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700234 if (device == null) {
235 return null;
236 }
237 boolean removed = availableDevices.remove(deviceId);
238 if (removed) {
239 // TODO: broadcast ... DOWN only?
240 return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
241 }
242 return null;
tome5ec3fd2014-09-04 15:18:06 -0700243 }
244 }
245
helenyrwufd296b62016-06-22 17:43:02 -0700246 // implement differently if desired
247 @Override
248 public boolean markOnline(DeviceId deviceId) {
249 log.warn("Mark online not supported");
250 return false;
251 }
252
tom41a2c5f2014-09-19 09:20:35 -0700253 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700254 public List<DeviceEvent> updatePorts(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700255 DeviceId deviceId,
256 List<PortDescription> portDescriptions) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700257 Device device = devices.get(deviceId);
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800258 if (device == null) {
259 log.debug("Device {} doesn't exist or hasn't been initialized yet", deviceId);
260 return Collections.emptyList();
261 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700262
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700263 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700264 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
265
tom29df6f42014-09-05 08:14:14 -0700266 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700267 synchronized (descsMap) {
268 DeviceDescriptions descs = descsMap.get(providerId);
269 // every provider must provide DeviceDescription.
270 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700271 "Device description for Device ID %s from Provider %s was not found",
272 deviceId, providerId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700273
274 Map<PortNumber, Port> ports = getPortMap(deviceId);
tom29df6f42014-09-05 08:14:14 -0700275
276 // Add new ports
277 Set<PortNumber> processed = new HashSet<>();
278 for (PortDescription portDescription : portDescriptions) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700279 final PortNumber number = portDescription.portNumber();
280 processed.add(portDescription.portNumber());
281
282 final Port oldPort = ports.get(number);
283 final Port newPort;
284
285// event suppression hook?
286
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700287 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700288 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700289 newPort = composePort(device, number, descsMap);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700290
291 events.add(oldPort == null ?
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700292 createPort(device, newPort, ports) :
293 updatePort(device, oldPort, newPort, ports));
tom29df6f42014-09-05 08:14:14 -0700294 }
295
296 events.addAll(pruneOldPorts(device, ports, processed));
297 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700298 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700299 }
300
301 // Creates a new port based on the port description adds it to the map and
302 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700303 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700304 private DeviceEvent createPort(Device device, Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700305 Map<PortNumber, Port> ports) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700306 ports.put(newPort.number(), newPort);
307 return new DeviceEvent(PORT_ADDED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700308 }
309
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700310 // Checks if the specified port requires update and if so, it replaces the
tom29df6f42014-09-05 08:14:14 -0700311 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700312 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700313 private DeviceEvent updatePort(Device device, Port oldPort,
314 Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700315 Map<PortNumber, Port> ports) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700316 if (oldPort.isEnabled() != newPort.isEnabled() ||
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700317 oldPort.type() != newPort.type() ||
318 oldPort.portSpeed() != newPort.portSpeed() ||
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700319 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700320 ports.put(oldPort.number(), newPort);
321 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700322 }
323 return null;
324 }
325
326 // Prunes the specified list of ports based on which ports are in the
327 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700328 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700329 private List<DeviceEvent> pruneOldPorts(Device device,
330 Map<PortNumber, Port> ports,
331 Set<PortNumber> processed) {
332 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700333 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700334 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700335 Entry<PortNumber, Port> e = iterator.next();
336 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700337 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700338 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700339 iterator.remove();
340 }
341 }
342 return events;
343 }
344
345 // Gets the map of ports for the specified device; if one does not already
346 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700347 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700348 return devicePorts.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
tome5ec3fd2014-09-04 15:18:06 -0700349 }
350
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700351 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700352 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700353 Map<ProviderId, DeviceDescriptions> r;
354 r = deviceDescs.get(deviceId);
355 if (r != null) {
356 return r;
357 }
358 r = new HashMap<>();
359 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
360 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
361 if (concurrentlyAdded != null) {
362 return concurrentlyAdded;
363 } else {
364 return r;
365 }
366 }
367
368 // Guarded by deviceDescs value (=Device lock)
369 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700370 Map<ProviderId, DeviceDescriptions> device,
371 ProviderId providerId, DeviceDescription deltaDesc) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700372 synchronized (device) {
373 DeviceDescriptions r = device.get(providerId);
374 if (r == null) {
375 r = new DeviceDescriptions(deltaDesc);
376 device.put(providerId, r);
377 }
378 return r;
379 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700380 }
381
tom41a2c5f2014-09-19 09:20:35 -0700382 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700383 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700384 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700385 Device device = devices.get(deviceId);
386 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
387
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700388 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700389 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
390
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700391 synchronized (descsMap) {
392 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700393 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700394 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700395 "Device description for Device ID %s from Provider %s was not found",
396 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700397
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700398 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
399 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700400 final Port oldPort = ports.get(number);
401 final Port newPort;
402
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700403 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700404 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700405 newPort = composePort(device, number, descsMap);
406
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700407 if (oldPort == null) {
408 return createPort(device, newPort, ports);
409 } else {
410 return updatePort(device, oldPort, newPort, ports);
411 }
tom46a220d2014-09-05 08:25:56 -0700412 }
tome5ec3fd2014-09-04 15:18:06 -0700413 }
414
tom41a2c5f2014-09-19 09:20:35 -0700415 @Override
416 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700417 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700418 if (ports == null) {
419 return Collections.emptyList();
420 }
421 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700422 }
423
tom41a2c5f2014-09-19 09:20:35 -0700424 @Override
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -0700425 public Stream<PortDescription> getPortDescriptions(ProviderId providerId,
426 DeviceId deviceId) {
427 return Optional.ofNullable(deviceDescs.get(deviceId))
428 .map(m -> m.get(providerId))
429 .map(descs -> descs.portDescs.values().stream())
430 .orElse(Stream.empty());
431 }
432
433 @Override
sangho538108b2015-04-08 14:29:20 -0700434 public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200435 Collection<PortStatistics> newStatsCollection) {
sangho538108b2015-04-08 14:29:20 -0700436
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200437 ConcurrentMap<PortNumber, PortStatistics> prvStatsMap = devicePortStats.get(deviceId);
438 ConcurrentMap<PortNumber, PortStatistics> newStatsMap = Maps.newConcurrentMap();
439 ConcurrentMap<PortNumber, PortStatistics> deltaStatsMap = Maps.newConcurrentMap();
440
441 if (prvStatsMap != null) {
442 for (PortStatistics newStats : newStatsCollection) {
443 PortNumber port = PortNumber.portNumber(newStats.port());
444 PortStatistics prvStats = prvStatsMap.get(port);
445 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
446 PortStatistics deltaStats = builder.build();
447 if (prvStats != null) {
448 deltaStats = calcDeltaStats(deviceId, prvStats, newStats);
449 }
450 deltaStatsMap.put(port, deltaStats);
451 newStatsMap.put(port, newStats);
452 }
453 } else {
454 for (PortStatistics newStats : newStatsCollection) {
455 PortNumber port = PortNumber.portNumber(newStats.port());
456 newStatsMap.put(port, newStats);
457 }
sangho538108b2015-04-08 14:29:20 -0700458 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200459 devicePortDeltaStats.put(deviceId, deltaStatsMap);
460 devicePortStats.put(deviceId, newStatsMap);
sangho538108b2015-04-08 14:29:20 -0700461 return new DeviceEvent(PORT_STATS_UPDATED, devices.get(deviceId), null);
462 }
463
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200464 public PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats) {
465 // calculate time difference
466 long deltaStatsSec, deltaStatsNano;
467 if (newStats.durationNano() < prvStats.durationNano()) {
468 deltaStatsNano = newStats.durationNano() - prvStats.durationNano() + TimeUnit.SECONDS.toNanos(1);
469 deltaStatsSec = newStats.durationSec() - prvStats.durationSec() - 1L;
470 } else {
471 deltaStatsNano = newStats.durationNano() - prvStats.durationNano();
472 deltaStatsSec = newStats.durationSec() - prvStats.durationSec();
473 }
474 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
475 DefaultPortStatistics deltaStats = builder.setDeviceId(deviceId)
476 .setPort(newStats.port())
477 .setPacketsReceived(newStats.packetsReceived() - prvStats.packetsReceived())
478 .setPacketsSent(newStats.packetsSent() - prvStats.packetsSent())
479 .setBytesReceived(newStats.bytesReceived() - prvStats.bytesReceived())
480 .setBytesSent(newStats.bytesSent() - prvStats.bytesSent())
481 .setPacketsRxDropped(newStats.packetsRxDropped() - prvStats.packetsRxDropped())
482 .setPacketsTxDropped(newStats.packetsTxDropped() - prvStats.packetsTxDropped())
483 .setPacketsRxErrors(newStats.packetsRxErrors() - prvStats.packetsRxErrors())
484 .setPacketsTxErrors(newStats.packetsTxErrors() - prvStats.packetsTxErrors())
485 .setDurationSec(deltaStatsSec)
486 .setDurationNano(deltaStatsNano)
487 .build();
488 return deltaStats;
489 }
490
sangho538108b2015-04-08 14:29:20 -0700491 @Override
tom41a2c5f2014-09-19 09:20:35 -0700492 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700493 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
494 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700495 }
496
tom41a2c5f2014-09-19 09:20:35 -0700497 @Override
Yuta HIGUCHI6eb00cc2016-06-10 11:55:12 -0700498 public PortDescription getPortDescription(ProviderId providerId,
499 DeviceId deviceId,
500 PortNumber portNumber) {
501 return Optional.ofNullable(deviceDescs.get(deviceId))
502 .map(m -> m.get(providerId))
503 .map(descs -> descs.getPortDesc(portNumber))
504 .orElse(null);
505 }
506
507 @Override
sangho538108b2015-04-08 14:29:20 -0700508 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
509 Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId);
510 if (portStats == null) {
511 return Collections.emptyList();
512 }
513 return ImmutableList.copyOf(portStats.values());
514 }
515
516 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200517 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
518 Map<PortNumber, PortStatistics> portStats = devicePortDeltaStats.get(deviceId);
519 if (portStats == null) {
520 return Collections.emptyList();
521 }
522 return ImmutableList.copyOf(portStats.values());
523 }
524
525 @Override
tom41a2c5f2014-09-19 09:20:35 -0700526 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700527 return availableDevices.contains(deviceId);
528 }
529
tom41a2c5f2014-09-19 09:20:35 -0700530 @Override
tom41a2c5f2014-09-19 09:20:35 -0700531 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700532 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700533 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700534 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700535 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700536 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700537 if (ports != null) {
538 ports.clear();
539 }
540 availableDevices.remove(deviceId);
541 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700542 return device == null ? null :
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700543 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700544 }
545 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700546
547 /**
548 * Returns a Device, merging description given from multiple Providers.
549 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700550 * @param deviceId device identifier
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700551 * @param providerDescs Collection of Descriptions from multiple providers
552 * @return Device instance
553 */
554 private Device composeDevice(DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700555 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700556
557 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
558
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800559 ProviderId primary = pickPrimaryPid(providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700560
561 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700562
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700563 final DeviceDescription base = desc.getDeviceDesc();
564 Type type = base.type();
565 String manufacturer = base.manufacturer();
566 String hwVersion = base.hwVersion();
567 String swVersion = base.swVersion();
568 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700569 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700570 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700571 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700572
573 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
574 if (e.getKey().equals(primary)) {
575 continue;
576 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700577 // TODO: should keep track of Description timestamp
578 // and only merge conflicting keys when timestamp is newer
579 // Currently assuming there will never be a key conflict between
580 // providers
581
582 // annotation merging. not so efficient, should revisit later
583 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700584 }
585
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700586 return new DefaultDevice(primary, deviceId, type, manufacturer,
587 hwVersion, swVersion, serialNumber,
588 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700589 }
590
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700591 /**
592 * Returns a Port, merging description given from multiple Providers.
593 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700594 * @param device device the port is on
595 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700596 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700597 * @return Port instance
598 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700599 private Port composePort(Device device, PortNumber number,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700600 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700601
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800602 ProviderId primary = pickPrimaryPid(descsMap);
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700603 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700604 // if no primary, assume not enabled
605 // TODO: revisit this default port enabled/disabled behavior
606 boolean isEnabled = false;
607 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
608
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700609 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700610 if (portDesc != null) {
611 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700612 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700613 }
614
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700615 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700616 if (e.getKey().equals(primary)) {
617 continue;
618 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700619 // TODO: should keep track of Description timestamp
620 // and only merge conflicting keys when timestamp is newer
621 // Currently assuming there will never be a key conflict between
622 // providers
623
624 // annotation merging. not so efficient, should revisit later
625 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
626 if (otherPortDesc != null) {
627 annotations = merge(annotations, otherPortDesc.annotations());
628 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700629 }
630
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700631 return portDesc == null ?
632 new DefaultPort(device, number, false, annotations) :
633 new DefaultPort(device, number, isEnabled, portDesc.type(),
634 portDesc.portSpeed(), annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700635 }
636
637 /**
638 * @return primary ProviderID, or randomly chosen one if none exists
639 */
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800640 private ProviderId pickPrimaryPid(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700641 ProviderId fallBackPrimary = null;
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().isAncillary()) {
644 return e.getKey();
645 } else if (fallBackPrimary == null) {
646 // pick randomly as a fallback in case there is no primary
647 fallBackPrimary = e.getKey();
648 }
649 }
650 return fallBackPrimary;
651 }
652
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700653 /**
654 * Collection of Description of a Device and it's Ports given from a Provider.
655 */
656 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700657
658 private final AtomicReference<DeviceDescription> deviceDesc;
659 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
660
661 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700662 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700663 this.portDescs = new ConcurrentHashMap<>();
664 }
665
666 public DeviceDescription getDeviceDesc() {
667 return deviceDesc.get();
668 }
669
670 public PortDescription getPortDesc(PortNumber number) {
671 return portDescs.get(number);
672 }
673
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700674 /**
675 * Puts DeviceDescription, merging annotations as necessary.
676 *
677 * @param newDesc new DeviceDescription
678 * @return previous DeviceDescription
679 */
680 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
681 DeviceDescription oldOne = deviceDesc.get();
682 DeviceDescription newOne = newDesc;
683 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700684 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700685 newDesc.annotations());
686 newOne = new DefaultDeviceDescription(newOne, merged);
687 }
688 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700689 }
690
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700691 /**
692 * Puts PortDescription, merging annotations as necessary.
693 *
694 * @param newDesc new PortDescription
695 * @return previous PortDescription
696 */
697 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
698 PortDescription oldOne = portDescs.get(newDesc.portNumber());
699 PortDescription newOne = newDesc;
700 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700701 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700702 newDesc.annotations());
703 newOne = new DefaultPortDescription(newOne, merged);
704 }
705 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700706 }
707 }
tome5ec3fd2014-09-04 15:18:06 -0700708}