blob: 11a120403246e1572dce85b454c6c423baf3e508 [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]",
209 providerId, oldDevice, devices.get(newDevice.id())
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700210 , newDevice);
211 }
212 if (!providerId.isAncillary()) {
213 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700214 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700215 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700216 }
217
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700218 // Otherwise merely attempt to change availability if primary provider
219 if (!providerId.isAncillary()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700220 boolean added = availableDevices.add(newDevice.id());
tom29df6f42014-09-05 08:14:14 -0700221 return !added ? null :
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700222 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700223 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700224 return null;
tome5ec3fd2014-09-04 15:18:06 -0700225 }
226
tom41a2c5f2014-09-19 09:20:35 -0700227 @Override
228 public DeviceEvent markOffline(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700229 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700230 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700231
232 // locking device
233 synchronized (providerDescs) {
tome5ec3fd2014-09-04 15:18:06 -0700234 Device device = devices.get(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700235 if (device == null) {
236 return null;
237 }
238 boolean removed = availableDevices.remove(deviceId);
239 if (removed) {
240 // TODO: broadcast ... DOWN only?
241 return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
242 }
243 return null;
tome5ec3fd2014-09-04 15:18:06 -0700244 }
245 }
246
tom41a2c5f2014-09-19 09:20:35 -0700247 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700248 public List<DeviceEvent> updatePorts(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700249 DeviceId deviceId,
250 List<PortDescription> portDescriptions) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700251 Device device = devices.get(deviceId);
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800252 if (device == null) {
253 log.debug("Device {} doesn't exist or hasn't been initialized yet", deviceId);
254 return Collections.emptyList();
255 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700256
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700257 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700258 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
259
tom29df6f42014-09-05 08:14:14 -0700260 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700261 synchronized (descsMap) {
262 DeviceDescriptions descs = descsMap.get(providerId);
263 // every provider must provide DeviceDescription.
264 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700265 "Device description for Device ID %s from Provider %s was not found",
266 deviceId, providerId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700267
268 Map<PortNumber, Port> ports = getPortMap(deviceId);
tom29df6f42014-09-05 08:14:14 -0700269
270 // Add new ports
271 Set<PortNumber> processed = new HashSet<>();
272 for (PortDescription portDescription : portDescriptions) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700273 final PortNumber number = portDescription.portNumber();
274 processed.add(portDescription.portNumber());
275
276 final Port oldPort = ports.get(number);
277 final Port newPort;
278
279// event suppression hook?
280
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700281 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700282 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700283 newPort = composePort(device, number, descsMap);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700284
285 events.add(oldPort == null ?
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700286 createPort(device, newPort, ports) :
287 updatePort(device, oldPort, newPort, ports));
tom29df6f42014-09-05 08:14:14 -0700288 }
289
290 events.addAll(pruneOldPorts(device, ports, processed));
291 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700292 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700293 }
294
295 // Creates a new port based on the port description adds it to the map and
296 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700297 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700298 private DeviceEvent createPort(Device device, Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700299 Map<PortNumber, Port> ports) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700300 ports.put(newPort.number(), newPort);
301 return new DeviceEvent(PORT_ADDED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700302 }
303
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700304 // Checks if the specified port requires update and if so, it replaces the
tom29df6f42014-09-05 08:14:14 -0700305 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700306 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700307 private DeviceEvent updatePort(Device device, Port oldPort,
308 Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700309 Map<PortNumber, Port> ports) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700310 if (oldPort.isEnabled() != newPort.isEnabled() ||
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700311 oldPort.type() != newPort.type() ||
312 oldPort.portSpeed() != newPort.portSpeed() ||
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700313 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700314 ports.put(oldPort.number(), newPort);
315 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700316 }
317 return null;
318 }
319
320 // Prunes the specified list of ports based on which ports are in the
321 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700322 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700323 private List<DeviceEvent> pruneOldPorts(Device device,
324 Map<PortNumber, Port> ports,
325 Set<PortNumber> processed) {
326 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700327 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700328 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700329 Entry<PortNumber, Port> e = iterator.next();
330 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700331 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700332 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700333 iterator.remove();
334 }
335 }
336 return events;
337 }
338
339 // Gets the map of ports for the specified device; if one does not already
340 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700341 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
342 return createIfAbsentUnchecked(devicePorts, deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700343 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
tome5ec3fd2014-09-04 15:18:06 -0700344 }
345
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700346 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700347 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700348 Map<ProviderId, DeviceDescriptions> r;
349 r = deviceDescs.get(deviceId);
350 if (r != null) {
351 return r;
352 }
353 r = new HashMap<>();
354 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
355 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
356 if (concurrentlyAdded != null) {
357 return concurrentlyAdded;
358 } else {
359 return r;
360 }
361 }
362
363 // Guarded by deviceDescs value (=Device lock)
364 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700365 Map<ProviderId, DeviceDescriptions> device,
366 ProviderId providerId, DeviceDescription deltaDesc) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700367 synchronized (device) {
368 DeviceDescriptions r = device.get(providerId);
369 if (r == null) {
370 r = new DeviceDescriptions(deltaDesc);
371 device.put(providerId, r);
372 }
373 return r;
374 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700375 }
376
tom41a2c5f2014-09-19 09:20:35 -0700377 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700378 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700379 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700380 Device device = devices.get(deviceId);
381 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
382
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700383 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700384 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
385
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700386 synchronized (descsMap) {
387 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700388 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700389 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700390 "Device description for Device ID %s from Provider %s was not found",
391 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700392
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700393 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
394 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700395 final Port oldPort = ports.get(number);
396 final Port newPort;
397
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700398 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700399 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700400 newPort = composePort(device, number, descsMap);
401
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700402 if (oldPort == null) {
403 return createPort(device, newPort, ports);
404 } else {
405 return updatePort(device, oldPort, newPort, ports);
406 }
tom46a220d2014-09-05 08:25:56 -0700407 }
tome5ec3fd2014-09-04 15:18:06 -0700408 }
409
tom41a2c5f2014-09-19 09:20:35 -0700410 @Override
411 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700412 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700413 if (ports == null) {
414 return Collections.emptyList();
415 }
416 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700417 }
418
tom41a2c5f2014-09-19 09:20:35 -0700419 @Override
sangho538108b2015-04-08 14:29:20 -0700420 public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200421 Collection<PortStatistics> newStatsCollection) {
sangho538108b2015-04-08 14:29:20 -0700422
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200423 ConcurrentMap<PortNumber, PortStatistics> prvStatsMap = devicePortStats.get(deviceId);
424 ConcurrentMap<PortNumber, PortStatistics> newStatsMap = Maps.newConcurrentMap();
425 ConcurrentMap<PortNumber, PortStatistics> deltaStatsMap = Maps.newConcurrentMap();
426
427 if (prvStatsMap != null) {
428 for (PortStatistics newStats : newStatsCollection) {
429 PortNumber port = PortNumber.portNumber(newStats.port());
430 PortStatistics prvStats = prvStatsMap.get(port);
431 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
432 PortStatistics deltaStats = builder.build();
433 if (prvStats != null) {
434 deltaStats = calcDeltaStats(deviceId, prvStats, newStats);
435 }
436 deltaStatsMap.put(port, deltaStats);
437 newStatsMap.put(port, newStats);
438 }
439 } else {
440 for (PortStatistics newStats : newStatsCollection) {
441 PortNumber port = PortNumber.portNumber(newStats.port());
442 newStatsMap.put(port, newStats);
443 }
sangho538108b2015-04-08 14:29:20 -0700444 }
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200445 devicePortDeltaStats.put(deviceId, deltaStatsMap);
446 devicePortStats.put(deviceId, newStatsMap);
sangho538108b2015-04-08 14:29:20 -0700447 return new DeviceEvent(PORT_STATS_UPDATED, devices.get(deviceId), null);
448 }
449
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200450 public PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats) {
451 // calculate time difference
452 long deltaStatsSec, deltaStatsNano;
453 if (newStats.durationNano() < prvStats.durationNano()) {
454 deltaStatsNano = newStats.durationNano() - prvStats.durationNano() + TimeUnit.SECONDS.toNanos(1);
455 deltaStatsSec = newStats.durationSec() - prvStats.durationSec() - 1L;
456 } else {
457 deltaStatsNano = newStats.durationNano() - prvStats.durationNano();
458 deltaStatsSec = newStats.durationSec() - prvStats.durationSec();
459 }
460 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
461 DefaultPortStatistics deltaStats = builder.setDeviceId(deviceId)
462 .setPort(newStats.port())
463 .setPacketsReceived(newStats.packetsReceived() - prvStats.packetsReceived())
464 .setPacketsSent(newStats.packetsSent() - prvStats.packetsSent())
465 .setBytesReceived(newStats.bytesReceived() - prvStats.bytesReceived())
466 .setBytesSent(newStats.bytesSent() - prvStats.bytesSent())
467 .setPacketsRxDropped(newStats.packetsRxDropped() - prvStats.packetsRxDropped())
468 .setPacketsTxDropped(newStats.packetsTxDropped() - prvStats.packetsTxDropped())
469 .setPacketsRxErrors(newStats.packetsRxErrors() - prvStats.packetsRxErrors())
470 .setPacketsTxErrors(newStats.packetsTxErrors() - prvStats.packetsTxErrors())
471 .setDurationSec(deltaStatsSec)
472 .setDurationNano(deltaStatsNano)
473 .build();
474 return deltaStats;
475 }
476
sangho538108b2015-04-08 14:29:20 -0700477 @Override
tom41a2c5f2014-09-19 09:20:35 -0700478 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700479 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
480 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700481 }
482
tom41a2c5f2014-09-19 09:20:35 -0700483 @Override
sangho538108b2015-04-08 14:29:20 -0700484 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
485 Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId);
486 if (portStats == null) {
487 return Collections.emptyList();
488 }
489 return ImmutableList.copyOf(portStats.values());
490 }
491
492 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200493 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
494 Map<PortNumber, PortStatistics> portStats = devicePortDeltaStats.get(deviceId);
495 if (portStats == null) {
496 return Collections.emptyList();
497 }
498 return ImmutableList.copyOf(portStats.values());
499 }
500
501 @Override
tom41a2c5f2014-09-19 09:20:35 -0700502 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700503 return availableDevices.contains(deviceId);
504 }
505
tom41a2c5f2014-09-19 09:20:35 -0700506 @Override
tom41a2c5f2014-09-19 09:20:35 -0700507 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700508 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700509 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700510 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700511 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700512 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700513 if (ports != null) {
514 ports.clear();
515 }
516 availableDevices.remove(deviceId);
517 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700518 return device == null ? null :
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700519 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700520 }
521 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700522
523 /**
524 * Returns a Device, merging description given from multiple Providers.
525 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700526 * @param deviceId device identifier
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700527 * @param providerDescs Collection of Descriptions from multiple providers
528 * @return Device instance
529 */
530 private Device composeDevice(DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700531 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700532
533 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
534
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800535 ProviderId primary = pickPrimaryPid(providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700536
537 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700538
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700539 final DeviceDescription base = desc.getDeviceDesc();
540 Type type = base.type();
541 String manufacturer = base.manufacturer();
542 String hwVersion = base.hwVersion();
543 String swVersion = base.swVersion();
544 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700545 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700546 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700547 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700548
549 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
550 if (e.getKey().equals(primary)) {
551 continue;
552 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700553 // TODO: should keep track of Description timestamp
554 // and only merge conflicting keys when timestamp is newer
555 // Currently assuming there will never be a key conflict between
556 // providers
557
558 // annotation merging. not so efficient, should revisit later
559 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700560 }
561
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700562 return new DefaultDevice(primary, deviceId, type, manufacturer,
563 hwVersion, swVersion, serialNumber,
564 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700565 }
566
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700567 /**
568 * Returns a Port, merging description given from multiple Providers.
569 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700570 * @param device device the port is on
571 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700572 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700573 * @return Port instance
574 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700575 private Port composePort(Device device, PortNumber number,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700576 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700577
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800578 ProviderId primary = pickPrimaryPid(descsMap);
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700579 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700580 // if no primary, assume not enabled
581 // TODO: revisit this default port enabled/disabled behavior
582 boolean isEnabled = false;
583 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
584
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700585 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700586 if (portDesc != null) {
587 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700588 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700589 }
590
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700591 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700592 if (e.getKey().equals(primary)) {
593 continue;
594 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700595 // TODO: should keep track of Description timestamp
596 // and only merge conflicting keys when timestamp is newer
597 // Currently assuming there will never be a key conflict between
598 // providers
599
600 // annotation merging. not so efficient, should revisit later
601 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
602 if (otherPortDesc != null) {
603 annotations = merge(annotations, otherPortDesc.annotations());
604 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700605 }
606
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700607 return portDesc == null ?
608 new DefaultPort(device, number, false, annotations) :
609 new DefaultPort(device, number, isEnabled, portDesc.type(),
610 portDesc.portSpeed(), annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700611 }
612
613 /**
614 * @return primary ProviderID, or randomly chosen one if none exists
615 */
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800616 private ProviderId pickPrimaryPid(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700617 ProviderId fallBackPrimary = null;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700618 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700619 if (!e.getKey().isAncillary()) {
620 return e.getKey();
621 } else if (fallBackPrimary == null) {
622 // pick randomly as a fallback in case there is no primary
623 fallBackPrimary = e.getKey();
624 }
625 }
626 return fallBackPrimary;
627 }
628
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700629 /**
630 * Collection of Description of a Device and it's Ports given from a Provider.
631 */
632 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700633
634 private final AtomicReference<DeviceDescription> deviceDesc;
635 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
636
637 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700638 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700639 this.portDescs = new ConcurrentHashMap<>();
640 }
641
642 public DeviceDescription getDeviceDesc() {
643 return deviceDesc.get();
644 }
645
646 public PortDescription getPortDesc(PortNumber number) {
647 return portDescs.get(number);
648 }
649
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700650 /**
651 * Puts DeviceDescription, merging annotations as necessary.
652 *
653 * @param newDesc new DeviceDescription
654 * @return previous DeviceDescription
655 */
656 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
657 DeviceDescription oldOne = deviceDesc.get();
658 DeviceDescription newOne = newDesc;
659 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700660 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700661 newDesc.annotations());
662 newOne = new DefaultDeviceDescription(newOne, merged);
663 }
664 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700665 }
666
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700667 /**
668 * Puts PortDescription, merging annotations as necessary.
669 *
670 * @param newDesc new PortDescription
671 * @return previous PortDescription
672 */
673 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
674 PortDescription oldOne = portDescs.get(newDesc.portNumber());
675 PortDescription newOne = newDesc;
676 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700677 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700678 newDesc.annotations());
679 newOne = new DefaultPortDescription(newOne, merged);
680 }
681 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700682 }
683 }
tome5ec3fd2014-09-04 15:18:06 -0700684}