blob: 0c7fb0c1564928e68f2b58ecb174f5e0862bcd2a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
tomea961ff2014-10-01 12:45:15 -070016package org.onlab.onos.store.trivial.impl;
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;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070027import org.onlab.onos.net.AnnotationsUtil;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070028import org.onlab.onos.net.DefaultAnnotations;
tome5ec3fd2014-09-04 15:18:06 -070029import org.onlab.onos.net.DefaultDevice;
tom29df6f42014-09-05 08:14:14 -070030import org.onlab.onos.net.DefaultPort;
tome5ec3fd2014-09-04 15:18:06 -070031import org.onlab.onos.net.Device;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070032import org.onlab.onos.net.Device.Type;
tome5ec3fd2014-09-04 15:18:06 -070033import org.onlab.onos.net.DeviceId;
tome5ec3fd2014-09-04 15:18:06 -070034import org.onlab.onos.net.Port;
35import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070036import org.onlab.onos.net.SparseAnnotations;
37import org.onlab.onos.net.device.DefaultDeviceDescription;
38import org.onlab.onos.net.device.DefaultPortDescription;
tome5ec3fd2014-09-04 15:18:06 -070039import org.onlab.onos.net.device.DeviceDescription;
40import org.onlab.onos.net.device.DeviceEvent;
tom41a2c5f2014-09-19 09:20:35 -070041import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070042import org.onlab.onos.net.device.DeviceStoreDelegate;
tome5ec3fd2014-09-04 15:18:06 -070043import org.onlab.onos.net.device.PortDescription;
44import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070045import org.onlab.onos.store.AbstractStore;
alshabib7911a052014-10-16 17:49:37 -070046import org.onlab.packet.ChassisId;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070047import org.onlab.util.NewConcurrentHashMap;
tom41a2c5f2014-09-19 09:20:35 -070048import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070049
50import java.util.ArrayList;
51import java.util.Collections;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070052import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070053import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070054import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070055import java.util.List;
56import java.util.Map;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070057import java.util.Map.Entry;
tome5ec3fd2014-09-04 15:18:06 -070058import java.util.Objects;
59import java.util.Set;
60import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070061import java.util.concurrent.ConcurrentMap;
62import java.util.concurrent.atomic.AtomicReference;
tome5ec3fd2014-09-04 15:18:06 -070063
64import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070065import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070066import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070067import static com.google.common.base.Verify.verify;
tom29df6f42014-09-05 08:14:14 -070068import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070069import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070070import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -070071import static org.onlab.onos.net.DefaultAnnotations.union;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070072import static org.onlab.onos.net.DefaultAnnotations.merge;
tome5ec3fd2014-09-04 15:18:06 -070073
74/**
tome4729872014-09-23 00:37:37 -070075 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070076 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070077 */
tom41a2c5f2014-09-19 09:20:35 -070078@Component(immediate = true)
79@Service
tomf80c9722014-09-24 14:49:18 -070080public class SimpleDeviceStore
81 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
82 implements DeviceStore {
tom41a2c5f2014-09-19 09:20:35 -070083
84 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070085
tom29df6f42014-09-05 08:14:14 -070086 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
87
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070088 // Collection of Description given from various providers
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070089 private final ConcurrentMap<DeviceId, Map<ProviderId, DeviceDescriptions>>
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070090 deviceDescs = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070091
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070092 // Cache of Device and Ports generated by compositing descriptions from providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070093 private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap();
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070094 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>>
95 devicePorts = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070096
Thomas Vachuska56dbeb12014-10-22 16:40:44 -070097 // Available (=UP) devices
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070098 private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070099
tome5ec3fd2014-09-04 15:18:06 -0700100
tom41a2c5f2014-09-19 09:20:35 -0700101 @Activate
102 public void activate() {
103 log.info("Started");
104 }
105
106 @Deactivate
107 public void deactivate() {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700108 deviceDescs.clear();
109 devices.clear();
110 devicePorts.clear();
111 availableDevices.clear();
tom41a2c5f2014-09-19 09:20:35 -0700112 log.info("Stopped");
113 }
tom5bcc9462014-09-19 10:11:31 -0700114
tom41a2c5f2014-09-19 09:20:35 -0700115 @Override
116 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -0700117 return devices.size();
118 }
119
tom41a2c5f2014-09-19 09:20:35 -0700120 @Override
121 public Iterable<Device> getDevices() {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700122 return Collections.unmodifiableCollection(devices.values());
tome5ec3fd2014-09-04 15:18:06 -0700123 }
124
tom41a2c5f2014-09-19 09:20:35 -0700125 @Override
126 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700127 return devices.get(deviceId);
128 }
129
tom41a2c5f2014-09-19 09:20:35 -0700130 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700131 public DeviceEvent createOrUpdateDevice(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700132 DeviceId deviceId,
133 DeviceDescription deviceDescription) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700134 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700135 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700136
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700137 synchronized (providerDescs) {
138 // locking per device
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700139 DeviceDescriptions descs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700140 = getOrCreateProviderDeviceDescriptions(providerDescs,
141 providerId,
142 deviceDescription);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700143
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700144 Device oldDevice = devices.get(deviceId);
145 // update description
146 descs.putDeviceDesc(deviceDescription);
147 Device newDevice = composeDevice(deviceId, providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700148
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700149 if (oldDevice == null) {
150 // ADD
151 return createDevice(providerId, newDevice);
152 } else {
153 // UPDATE or ignore (no change or stale)
154 return updateDevice(providerId, oldDevice, newDevice);
155 }
tome5ec3fd2014-09-04 15:18:06 -0700156 }
tome5ec3fd2014-09-04 15:18:06 -0700157 }
158
159 // Creates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700160 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700161 private DeviceEvent createDevice(ProviderId providerId, Device newDevice) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700162 // update composed device cache
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700163 Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice);
164 verify(oldDevice == null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700165 "Unexpected Device in cache. PID:%s [old=%s, new=%s]",
166 providerId, oldDevice, newDevice);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700167
168 if (!providerId.isAncillary()) {
169 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700170 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700171
172 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700173 }
174
175 // Updates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700176 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700177 private DeviceEvent updateDevice(ProviderId providerId, Device oldDevice, Device newDevice) {
tome5ec3fd2014-09-04 15:18:06 -0700178 // We allow only certain attributes to trigger update
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700179 boolean propertiesChanged =
180 !Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) ||
181 !Objects.equals(oldDevice.swVersion(), newDevice.swVersion());
182 boolean annotationsChanged =
183 !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations());
184
185 // Primary providers can respond to all changes, but ancillary ones
186 // should respond only to annotation changes.
187 if ((providerId.isAncillary() && annotationsChanged) ||
188 (!providerId.isAncillary() && (propertiesChanged || annotationsChanged))) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700189
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700190 boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice);
191 if (!replaced) {
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700192 // FIXME: Is the enclosing if required here?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700193 verify(replaced,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700194 "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]",
195 providerId, oldDevice, devices.get(newDevice.id())
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700196 , newDevice);
197 }
198 if (!providerId.isAncillary()) {
199 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700200 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700201 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700202 }
203
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700204 // Otherwise merely attempt to change availability if primary provider
205 if (!providerId.isAncillary()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700206 boolean added = availableDevices.add(newDevice.id());
tom29df6f42014-09-05 08:14:14 -0700207 return !added ? null :
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700208 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700209 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700210 return null;
tome5ec3fd2014-09-04 15:18:06 -0700211 }
212
tom41a2c5f2014-09-19 09:20:35 -0700213 @Override
214 public DeviceEvent markOffline(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700215 Map<ProviderId, DeviceDescriptions> providerDescs
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700216 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700217
218 // locking device
219 synchronized (providerDescs) {
tome5ec3fd2014-09-04 15:18:06 -0700220 Device device = devices.get(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700221 if (device == null) {
222 return null;
223 }
224 boolean removed = availableDevices.remove(deviceId);
225 if (removed) {
226 // TODO: broadcast ... DOWN only?
227 return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
228 }
229 return null;
tome5ec3fd2014-09-04 15:18:06 -0700230 }
231 }
232
tom41a2c5f2014-09-19 09:20:35 -0700233 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700234 public List<DeviceEvent> updatePorts(ProviderId providerId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700235 DeviceId deviceId,
236 List<PortDescription> portDescriptions) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700237 Device device = devices.get(deviceId);
238 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
239
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700240 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700241 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
242
tom29df6f42014-09-05 08:14:14 -0700243 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700244 synchronized (descsMap) {
245 DeviceDescriptions descs = descsMap.get(providerId);
246 // every provider must provide DeviceDescription.
247 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700248 "Device description for Device ID %s from Provider %s was not found",
249 deviceId, providerId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700250
251 Map<PortNumber, Port> ports = getPortMap(deviceId);
tom29df6f42014-09-05 08:14:14 -0700252
253 // Add new ports
254 Set<PortNumber> processed = new HashSet<>();
255 for (PortDescription portDescription : portDescriptions) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700256 final PortNumber number = portDescription.portNumber();
257 processed.add(portDescription.portNumber());
258
259 final Port oldPort = ports.get(number);
260 final Port newPort;
261
262// event suppression hook?
263
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700264 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700265 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700266 newPort = composePort(device, number, descsMap);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700267
268 events.add(oldPort == null ?
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700269 createPort(device, newPort, ports) :
270 updatePort(device, oldPort, newPort, ports));
tom29df6f42014-09-05 08:14:14 -0700271 }
272
273 events.addAll(pruneOldPorts(device, ports, processed));
274 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700275 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700276 }
277
278 // Creates a new port based on the port description adds it to the map and
279 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700280 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700281 private DeviceEvent createPort(Device device, Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700282 Map<PortNumber, Port> ports) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700283 ports.put(newPort.number(), newPort);
284 return new DeviceEvent(PORT_ADDED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700285 }
286
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700287 // Checks if the specified port requires update and if so, it replaces the
tom29df6f42014-09-05 08:14:14 -0700288 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700289 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700290 private DeviceEvent updatePort(Device device, Port oldPort,
291 Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700292 Map<PortNumber, Port> ports) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700293 if (oldPort.isEnabled() != newPort.isEnabled() ||
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700294 oldPort.type() != newPort.type() ||
295 oldPort.portSpeed() != newPort.portSpeed() ||
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700296 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700297 ports.put(oldPort.number(), newPort);
298 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700299 }
300 return null;
301 }
302
303 // Prunes the specified list of ports based on which ports are in the
304 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700305 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700306 private List<DeviceEvent> pruneOldPorts(Device device,
307 Map<PortNumber, Port> ports,
308 Set<PortNumber> processed) {
309 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700310 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700311 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700312 Entry<PortNumber, Port> e = iterator.next();
313 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700314 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700315 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700316 iterator.remove();
317 }
318 }
319 return events;
320 }
321
322 // Gets the map of ports for the specified device; if one does not already
323 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700324 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
325 return createIfAbsentUnchecked(devicePorts, deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700326 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
tome5ec3fd2014-09-04 15:18:06 -0700327 }
328
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700329 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700330 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700331 Map<ProviderId, DeviceDescriptions> r;
332 r = deviceDescs.get(deviceId);
333 if (r != null) {
334 return r;
335 }
336 r = new HashMap<>();
337 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
338 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
339 if (concurrentlyAdded != null) {
340 return concurrentlyAdded;
341 } else {
342 return r;
343 }
344 }
345
346 // Guarded by deviceDescs value (=Device lock)
347 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700348 Map<ProviderId, DeviceDescriptions> device,
349 ProviderId providerId, DeviceDescription deltaDesc) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700350 synchronized (device) {
351 DeviceDescriptions r = device.get(providerId);
352 if (r == null) {
353 r = new DeviceDescriptions(deltaDesc);
354 device.put(providerId, r);
355 }
356 return r;
357 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700358 }
359
tom41a2c5f2014-09-19 09:20:35 -0700360 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700361 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700362 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700363 Device device = devices.get(deviceId);
364 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
365
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700366 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700367 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
368
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700369 synchronized (descsMap) {
370 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700371 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700372 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700373 "Device description for Device ID %s from Provider %s was not found",
374 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700375
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700376 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
377 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700378 final Port oldPort = ports.get(number);
379 final Port newPort;
380
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700381 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700382 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700383 newPort = composePort(device, number, descsMap);
384
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700385 if (oldPort == null) {
386 return createPort(device, newPort, ports);
387 } else {
388 return updatePort(device, oldPort, newPort, ports);
389 }
tom46a220d2014-09-05 08:25:56 -0700390 }
tome5ec3fd2014-09-04 15:18:06 -0700391 }
392
tom41a2c5f2014-09-19 09:20:35 -0700393 @Override
394 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700395 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700396 if (ports == null) {
397 return Collections.emptyList();
398 }
399 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700400 }
401
tom41a2c5f2014-09-19 09:20:35 -0700402 @Override
403 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700404 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
405 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700406 }
407
tom41a2c5f2014-09-19 09:20:35 -0700408 @Override
409 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700410 return availableDevices.contains(deviceId);
411 }
412
tom41a2c5f2014-09-19 09:20:35 -0700413 @Override
tom41a2c5f2014-09-19 09:20:35 -0700414 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700415 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700416 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700417 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700418 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700419 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700420 if (ports != null) {
421 ports.clear();
422 }
423 availableDevices.remove(deviceId);
424 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700425 return device == null ? null :
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700426 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700427 }
428 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700429
430 /**
431 * Returns a Device, merging description given from multiple Providers.
432 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700433 * @param deviceId device identifier
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700434 * @param providerDescs Collection of Descriptions from multiple providers
435 * @return Device instance
436 */
437 private Device composeDevice(DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700438 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700439
440 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
441
442 ProviderId primary = pickPrimaryPID(providerDescs);
443
444 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700445
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700446 final DeviceDescription base = desc.getDeviceDesc();
447 Type type = base.type();
448 String manufacturer = base.manufacturer();
449 String hwVersion = base.hwVersion();
450 String swVersion = base.swVersion();
451 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700452 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700453 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700454 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700455
456 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
457 if (e.getKey().equals(primary)) {
458 continue;
459 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700460 // TODO: should keep track of Description timestamp
461 // and only merge conflicting keys when timestamp is newer
462 // Currently assuming there will never be a key conflict between
463 // providers
464
465 // annotation merging. not so efficient, should revisit later
466 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700467 }
468
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700469 return new DefaultDevice(primary, deviceId, type, manufacturer,
470 hwVersion, swVersion, serialNumber,
471 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700472 }
473
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700474 /**
475 * Returns a Port, merging description given from multiple Providers.
476 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700477 * @param device device the port is on
478 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700479 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700480 * @return Port instance
481 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700482 private Port composePort(Device device, PortNumber number,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700483 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700484
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700485 ProviderId primary = pickPrimaryPID(descsMap);
486 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700487 // if no primary, assume not enabled
488 // TODO: revisit this default port enabled/disabled behavior
489 boolean isEnabled = false;
490 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
491
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700492 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700493 if (portDesc != null) {
494 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700495 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700496 }
497
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700498 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700499 if (e.getKey().equals(primary)) {
500 continue;
501 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700502 // TODO: should keep track of Description timestamp
503 // and only merge conflicting keys when timestamp is newer
504 // Currently assuming there will never be a key conflict between
505 // providers
506
507 // annotation merging. not so efficient, should revisit later
508 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
509 if (otherPortDesc != null) {
510 annotations = merge(annotations, otherPortDesc.annotations());
511 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700512 }
513
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700514 return portDesc == null ?
515 new DefaultPort(device, number, false, annotations) :
516 new DefaultPort(device, number, isEnabled, portDesc.type(),
517 portDesc.portSpeed(), annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700518 }
519
520 /**
521 * @return primary ProviderID, or randomly chosen one if none exists
522 */
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700523 private ProviderId pickPrimaryPID(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700524 ProviderId fallBackPrimary = null;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700525 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700526 if (!e.getKey().isAncillary()) {
527 return e.getKey();
528 } else if (fallBackPrimary == null) {
529 // pick randomly as a fallback in case there is no primary
530 fallBackPrimary = e.getKey();
531 }
532 }
533 return fallBackPrimary;
534 }
535
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700536 /**
537 * Collection of Description of a Device and it's Ports given from a Provider.
538 */
539 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700540
541 private final AtomicReference<DeviceDescription> deviceDesc;
542 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
543
544 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700545 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700546 this.portDescs = new ConcurrentHashMap<>();
547 }
548
549 public DeviceDescription getDeviceDesc() {
550 return deviceDesc.get();
551 }
552
553 public PortDescription getPortDesc(PortNumber number) {
554 return portDescs.get(number);
555 }
556
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700557 /**
558 * Puts DeviceDescription, merging annotations as necessary.
559 *
560 * @param newDesc new DeviceDescription
561 * @return previous DeviceDescription
562 */
563 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
564 DeviceDescription oldOne = deviceDesc.get();
565 DeviceDescription newOne = newDesc;
566 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700567 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700568 newDesc.annotations());
569 newOne = new DefaultDeviceDescription(newOne, merged);
570 }
571 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700572 }
573
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700574 /**
575 * Puts PortDescription, merging annotations as necessary.
576 *
577 * @param newDesc new PortDescription
578 * @return previous PortDescription
579 */
580 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
581 PortDescription oldOne = portDescs.get(newDesc.portNumber());
582 PortDescription newOne = newDesc;
583 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700584 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700585 newDesc.annotations());
586 newOne = new DefaultPortDescription(newOne, merged);
587 }
588 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700589 }
590 }
tome5ec3fd2014-09-04 15:18:06 -0700591}