blob: 75a9f1bfd5170274732bd387c4decddf307f998f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
62import 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;
tome5ec3fd2014-09-04 15:18:06 -070067
68import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070069import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070070import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070071import static com.google.common.base.Verify.verify;
Brian O'Connorabafb502014-12-02 22:26:20 -080072import static org.onosproject.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070073import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070074import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
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
tom41a2c5f2014-09-19 09:20:35 -0700246 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700247 public List<DeviceEvent> updatePorts(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700248 DeviceId deviceId,
249 List<PortDescription> portDescriptions) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700250 Device device = devices.get(deviceId);
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800251 if (device == null) {
252 log.debug("Device {} doesn't exist or hasn't been initialized yet", deviceId);
253 return Collections.emptyList();
254 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700255
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700256 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700257 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
258
tom29df6f42014-09-05 08:14:14 -0700259 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700260 synchronized (descsMap) {
261 DeviceDescriptions descs = descsMap.get(providerId);
262 // every provider must provide DeviceDescription.
263 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700264 "Device description for Device ID %s from Provider %s was not found",
265 deviceId, providerId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700266
267 Map<PortNumber, Port> ports = getPortMap(deviceId);
tom29df6f42014-09-05 08:14:14 -0700268
269 // Add new ports
270 Set<PortNumber> processed = new HashSet<>();
271 for (PortDescription portDescription : portDescriptions) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700272 final PortNumber number = portDescription.portNumber();
273 processed.add(portDescription.portNumber());
274
275 final Port oldPort = ports.get(number);
276 final Port newPort;
277
278// event suppression hook?
279
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700280 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700281 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700282 newPort = composePort(device, number, descsMap);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700283
284 events.add(oldPort == null ?
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700285 createPort(device, newPort, ports) :
286 updatePort(device, oldPort, newPort, ports));
tom29df6f42014-09-05 08:14:14 -0700287 }
288
289 events.addAll(pruneOldPorts(device, ports, processed));
290 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700291 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700292 }
293
294 // Creates a new port based on the port description adds it to the map and
295 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700296 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700297 private DeviceEvent createPort(Device device, Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700298 Map<PortNumber, Port> ports) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700299 ports.put(newPort.number(), newPort);
300 return new DeviceEvent(PORT_ADDED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700301 }
302
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700303 // Checks if the specified port requires update and if so, it replaces the
tom29df6f42014-09-05 08:14:14 -0700304 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700305 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700306 private DeviceEvent updatePort(Device device, Port oldPort,
307 Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700308 Map<PortNumber, Port> ports) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700309 if (oldPort.isEnabled() != newPort.isEnabled() ||
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700310 oldPort.type() != newPort.type() ||
311 oldPort.portSpeed() != newPort.portSpeed() ||
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700312 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700313 ports.put(oldPort.number(), newPort);
314 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700315 }
316 return null;
317 }
318
319 // Prunes the specified list of ports based on which ports are in the
320 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700321 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700322 private List<DeviceEvent> pruneOldPorts(Device device,
323 Map<PortNumber, Port> ports,
324 Set<PortNumber> processed) {
325 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700326 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700327 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700328 Entry<PortNumber, Port> e = iterator.next();
329 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700330 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700331 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700332 iterator.remove();
333 }
334 }
335 return events;
336 }
337
338 // Gets the map of ports for the specified device; if one does not already
339 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700340 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
341 return createIfAbsentUnchecked(devicePorts, deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700342 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
tome5ec3fd2014-09-04 15:18:06 -0700343 }
344
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700345 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700346 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700347 Map<ProviderId, DeviceDescriptions> r;
348 r = deviceDescs.get(deviceId);
349 if (r != null) {
350 return r;
351 }
352 r = new HashMap<>();
353 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
354 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
355 if (concurrentlyAdded != null) {
356 return concurrentlyAdded;
357 } else {
358 return r;
359 }
360 }
361
362 // Guarded by deviceDescs value (=Device lock)
363 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700364 Map<ProviderId, DeviceDescriptions> device,
365 ProviderId providerId, DeviceDescription deltaDesc) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700366 synchronized (device) {
367 DeviceDescriptions r = device.get(providerId);
368 if (r == null) {
369 r = new DeviceDescriptions(deltaDesc);
370 device.put(providerId, r);
371 }
372 return r;
373 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700374 }
375
tom41a2c5f2014-09-19 09:20:35 -0700376 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700377 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700378 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700379 Device device = devices.get(deviceId);
380 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
381
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700382 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700383 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
384
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700385 synchronized (descsMap) {
386 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700387 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700388 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700389 "Device description for Device ID %s from Provider %s was not found",
390 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700391
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700392 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
393 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700394 final Port oldPort = ports.get(number);
395 final Port newPort;
396
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700397 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700398 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700399 newPort = composePort(device, number, descsMap);
400
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700401 if (oldPort == null) {
402 return createPort(device, newPort, ports);
403 } else {
404 return updatePort(device, oldPort, newPort, ports);
405 }
tom46a220d2014-09-05 08:25:56 -0700406 }
tome5ec3fd2014-09-04 15:18:06 -0700407 }
408
tom41a2c5f2014-09-19 09:20:35 -0700409 @Override
410 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700411 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700412 if (ports == null) {
413 return Collections.emptyList();
414 }
415 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700416 }
417
tom41a2c5f2014-09-19 09:20:35 -0700418 @Override
sangho538108b2015-04-08 14:29:20 -0700419 public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200420 Collection<PortStatistics> newStatsCollection) {
sangho538108b2015-04-08 14:29:20 -0700421
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200422 ConcurrentMap<PortNumber, PortStatistics> prvStatsMap = devicePortStats.get(deviceId);
423 ConcurrentMap<PortNumber, PortStatistics> newStatsMap = Maps.newConcurrentMap();
424 ConcurrentMap<PortNumber, PortStatistics> deltaStatsMap = Maps.newConcurrentMap();
425
426 if (prvStatsMap != null) {
427 for (PortStatistics newStats : newStatsCollection) {
428 PortNumber port = PortNumber.portNumber(newStats.port());
429 PortStatistics prvStats = prvStatsMap.get(port);
430 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
431 PortStatistics deltaStats = builder.build();
432 if (prvStats != null) {
433 deltaStats = calcDeltaStats(deviceId, prvStats, newStats);
434 }
435 deltaStatsMap.put(port, deltaStats);
436 newStatsMap.put(port, newStats);
437 }
438 } else {
439 for (PortStatistics newStats : newStatsCollection) {
440 PortNumber port = PortNumber.portNumber(newStats.port());
441 newStatsMap.put(port, newStats);
442 }
sangho538108b2015-04-08 14:29:20 -0700443 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200444 devicePortDeltaStats.put(deviceId, deltaStatsMap);
445 devicePortStats.put(deviceId, newStatsMap);
sangho538108b2015-04-08 14:29:20 -0700446 return new DeviceEvent(PORT_STATS_UPDATED, devices.get(deviceId), null);
447 }
448
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200449 public PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats) {
450 // calculate time difference
451 long deltaStatsSec, deltaStatsNano;
452 if (newStats.durationNano() < prvStats.durationNano()) {
453 deltaStatsNano = newStats.durationNano() - prvStats.durationNano() + TimeUnit.SECONDS.toNanos(1);
454 deltaStatsSec = newStats.durationSec() - prvStats.durationSec() - 1L;
455 } else {
456 deltaStatsNano = newStats.durationNano() - prvStats.durationNano();
457 deltaStatsSec = newStats.durationSec() - prvStats.durationSec();
458 }
459 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
460 DefaultPortStatistics deltaStats = builder.setDeviceId(deviceId)
461 .setPort(newStats.port())
462 .setPacketsReceived(newStats.packetsReceived() - prvStats.packetsReceived())
463 .setPacketsSent(newStats.packetsSent() - prvStats.packetsSent())
464 .setBytesReceived(newStats.bytesReceived() - prvStats.bytesReceived())
465 .setBytesSent(newStats.bytesSent() - prvStats.bytesSent())
466 .setPacketsRxDropped(newStats.packetsRxDropped() - prvStats.packetsRxDropped())
467 .setPacketsTxDropped(newStats.packetsTxDropped() - prvStats.packetsTxDropped())
468 .setPacketsRxErrors(newStats.packetsRxErrors() - prvStats.packetsRxErrors())
469 .setPacketsTxErrors(newStats.packetsTxErrors() - prvStats.packetsTxErrors())
470 .setDurationSec(deltaStatsSec)
471 .setDurationNano(deltaStatsNano)
472 .build();
473 return deltaStats;
474 }
475
sangho538108b2015-04-08 14:29:20 -0700476 @Override
tom41a2c5f2014-09-19 09:20:35 -0700477 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700478 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
479 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700480 }
481
tom41a2c5f2014-09-19 09:20:35 -0700482 @Override
sangho538108b2015-04-08 14:29:20 -0700483 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
484 Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId);
485 if (portStats == null) {
486 return Collections.emptyList();
487 }
488 return ImmutableList.copyOf(portStats.values());
489 }
490
491 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200492 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
493 Map<PortNumber, PortStatistics> portStats = devicePortDeltaStats.get(deviceId);
494 if (portStats == null) {
495 return Collections.emptyList();
496 }
497 return ImmutableList.copyOf(portStats.values());
498 }
499
500 @Override
tom41a2c5f2014-09-19 09:20:35 -0700501 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700502 return availableDevices.contains(deviceId);
503 }
504
tom41a2c5f2014-09-19 09:20:35 -0700505 @Override
tom41a2c5f2014-09-19 09:20:35 -0700506 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700507 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700508 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700509 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700510 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700511 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700512 if (ports != null) {
513 ports.clear();
514 }
515 availableDevices.remove(deviceId);
516 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700517 return device == null ? null :
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700518 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700519 }
520 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700521
522 /**
523 * Returns a Device, merging description given from multiple Providers.
524 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700525 * @param deviceId device identifier
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700526 * @param providerDescs Collection of Descriptions from multiple providers
527 * @return Device instance
528 */
529 private Device composeDevice(DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700530 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700531
532 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
533
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800534 ProviderId primary = pickPrimaryPid(providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700535
536 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700537
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700538 final DeviceDescription base = desc.getDeviceDesc();
539 Type type = base.type();
540 String manufacturer = base.manufacturer();
541 String hwVersion = base.hwVersion();
542 String swVersion = base.swVersion();
543 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700544 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700545 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700546 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700547
548 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
549 if (e.getKey().equals(primary)) {
550 continue;
551 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700552 // TODO: should keep track of Description timestamp
553 // and only merge conflicting keys when timestamp is newer
554 // Currently assuming there will never be a key conflict between
555 // providers
556
557 // annotation merging. not so efficient, should revisit later
558 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700559 }
560
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700561 return new DefaultDevice(primary, deviceId, type, manufacturer,
562 hwVersion, swVersion, serialNumber,
563 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700564 }
565
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700566 /**
567 * Returns a Port, merging description given from multiple Providers.
568 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700569 * @param device device the port is on
570 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700571 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700572 * @return Port instance
573 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700574 private Port composePort(Device device, PortNumber number,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700575 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700576
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800577 ProviderId primary = pickPrimaryPid(descsMap);
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700578 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700579 // if no primary, assume not enabled
580 // TODO: revisit this default port enabled/disabled behavior
581 boolean isEnabled = false;
582 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
583
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700584 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700585 if (portDesc != null) {
586 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700587 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700588 }
589
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700590 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700591 if (e.getKey().equals(primary)) {
592 continue;
593 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700594 // TODO: should keep track of Description timestamp
595 // and only merge conflicting keys when timestamp is newer
596 // Currently assuming there will never be a key conflict between
597 // providers
598
599 // annotation merging. not so efficient, should revisit later
600 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
601 if (otherPortDesc != null) {
602 annotations = merge(annotations, otherPortDesc.annotations());
603 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700604 }
605
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700606 return portDesc == null ?
607 new DefaultPort(device, number, false, annotations) :
608 new DefaultPort(device, number, isEnabled, portDesc.type(),
609 portDesc.portSpeed(), annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700610 }
611
612 /**
613 * @return primary ProviderID, or randomly chosen one if none exists
614 */
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800615 private ProviderId pickPrimaryPid(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700616 ProviderId fallBackPrimary = null;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700617 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700618 if (!e.getKey().isAncillary()) {
619 return e.getKey();
620 } else if (fallBackPrimary == null) {
621 // pick randomly as a fallback in case there is no primary
622 fallBackPrimary = e.getKey();
623 }
624 }
625 return fallBackPrimary;
626 }
627
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700628 /**
629 * Collection of Description of a Device and it's Ports given from a Provider.
630 */
631 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700632
633 private final AtomicReference<DeviceDescription> deviceDesc;
634 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
635
636 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700637 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700638 this.portDescs = new ConcurrentHashMap<>();
639 }
640
641 public DeviceDescription getDeviceDesc() {
642 return deviceDesc.get();
643 }
644
645 public PortDescription getPortDesc(PortNumber number) {
646 return portDescs.get(number);
647 }
648
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700649 /**
650 * Puts DeviceDescription, merging annotations as necessary.
651 *
652 * @param newDesc new DeviceDescription
653 * @return previous DeviceDescription
654 */
655 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
656 DeviceDescription oldOne = deviceDesc.get();
657 DeviceDescription newOne = newDesc;
658 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700659 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700660 newDesc.annotations());
661 newOne = new DefaultDeviceDescription(newOne, merged);
662 }
663 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700664 }
665
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700666 /**
667 * Puts PortDescription, merging annotations as necessary.
668 *
669 * @param newDesc new PortDescription
670 * @return previous PortDescription
671 */
672 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
673 PortDescription oldOne = portDescs.get(newDesc.portNumber());
674 PortDescription newOne = newDesc;
675 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700676 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700677 newDesc.annotations());
678 newOne = new DefaultPortDescription(newOne, merged);
679 }
680 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700681 }
682 }
tome5ec3fd2014-09-04 15:18:06 -0700683}