blob: a4a47e236147af52d35a0439b5a270b32cc5b06c [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 Vachuska56dbeb12014-10-22 16:40:44 -0700294 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700295
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700296 ports.put(oldPort.number(), newPort);
297 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700298 }
299 return null;
300 }
301
302 // Prunes the specified list of ports based on which ports are in the
303 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700304 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700305 private List<DeviceEvent> pruneOldPorts(Device device,
306 Map<PortNumber, Port> ports,
307 Set<PortNumber> processed) {
308 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700309 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700310 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700311 Entry<PortNumber, Port> e = iterator.next();
312 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700313 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700314 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700315 iterator.remove();
316 }
317 }
318 return events;
319 }
320
321 // Gets the map of ports for the specified device; if one does not already
322 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700323 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
324 return createIfAbsentUnchecked(devicePorts, deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700325 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
tome5ec3fd2014-09-04 15:18:06 -0700326 }
327
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700328 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700329 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700330 Map<ProviderId, DeviceDescriptions> r;
331 r = deviceDescs.get(deviceId);
332 if (r != null) {
333 return r;
334 }
335 r = new HashMap<>();
336 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
337 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
338 if (concurrentlyAdded != null) {
339 return concurrentlyAdded;
340 } else {
341 return r;
342 }
343 }
344
345 // Guarded by deviceDescs value (=Device lock)
346 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700347 Map<ProviderId, DeviceDescriptions> device,
348 ProviderId providerId, DeviceDescription deltaDesc) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700349 synchronized (device) {
350 DeviceDescriptions r = device.get(providerId);
351 if (r == null) {
352 r = new DeviceDescriptions(deltaDesc);
353 device.put(providerId, r);
354 }
355 return r;
356 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700357 }
358
tom41a2c5f2014-09-19 09:20:35 -0700359 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700360 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700361 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700362 Device device = devices.get(deviceId);
363 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
364
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700365 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700366 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
367
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700368 synchronized (descsMap) {
369 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700370 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700371 checkArgument(descs != null,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700372 "Device description for Device ID %s from Provider %s was not found",
373 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700374
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700375 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
376 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700377 final Port oldPort = ports.get(number);
378 final Port newPort;
379
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700380 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700381 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700382 newPort = composePort(device, number, descsMap);
383
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700384 if (oldPort == null) {
385 return createPort(device, newPort, ports);
386 } else {
387 return updatePort(device, oldPort, newPort, ports);
388 }
tom46a220d2014-09-05 08:25:56 -0700389 }
tome5ec3fd2014-09-04 15:18:06 -0700390 }
391
tom41a2c5f2014-09-19 09:20:35 -0700392 @Override
393 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700394 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700395 if (ports == null) {
396 return Collections.emptyList();
397 }
398 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700399 }
400
tom41a2c5f2014-09-19 09:20:35 -0700401 @Override
402 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700403 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
404 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700405 }
406
tom41a2c5f2014-09-19 09:20:35 -0700407 @Override
408 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700409 return availableDevices.contains(deviceId);
410 }
411
tom41a2c5f2014-09-19 09:20:35 -0700412 @Override
tom41a2c5f2014-09-19 09:20:35 -0700413 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700414 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700415 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700416 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700417 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700418 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700419 if (ports != null) {
420 ports.clear();
421 }
422 availableDevices.remove(deviceId);
423 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700424 return device == null ? null :
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700425 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700426 }
427 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700428
429 /**
430 * Returns a Device, merging description given from multiple Providers.
431 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700432 * @param deviceId device identifier
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700433 * @param providerDescs Collection of Descriptions from multiple providers
434 * @return Device instance
435 */
436 private Device composeDevice(DeviceId deviceId,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700437 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700438
439 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
440
441 ProviderId primary = pickPrimaryPID(providerDescs);
442
443 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700444
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700445 final DeviceDescription base = desc.getDeviceDesc();
446 Type type = base.type();
447 String manufacturer = base.manufacturer();
448 String hwVersion = base.hwVersion();
449 String swVersion = base.swVersion();
450 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700451 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700452 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700453 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700454
455 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
456 if (e.getKey().equals(primary)) {
457 continue;
458 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700459 // TODO: should keep track of Description timestamp
460 // and only merge conflicting keys when timestamp is newer
461 // Currently assuming there will never be a key conflict between
462 // providers
463
464 // annotation merging. not so efficient, should revisit later
465 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700466 }
467
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700468 return new DefaultDevice(primary, deviceId, type, manufacturer,
469 hwVersion, swVersion, serialNumber,
470 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700471 }
472
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700473 /**
474 * Returns a Port, merging description given from multiple Providers.
475 *
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700476 * @param device device the port is on
477 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700478 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700479 * @return Port instance
480 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700481 private Port composePort(Device device, PortNumber number,
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700482 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700483
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700484 ProviderId primary = pickPrimaryPID(descsMap);
485 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700486 // if no primary, assume not enabled
487 // TODO: revisit this default port enabled/disabled behavior
488 boolean isEnabled = false;
489 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
490
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700491 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700492 if (portDesc != null) {
493 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700494 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700495 }
496
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700497 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700498 if (e.getKey().equals(primary)) {
499 continue;
500 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700501 // TODO: should keep track of Description timestamp
502 // and only merge conflicting keys when timestamp is newer
503 // Currently assuming there will never be a key conflict between
504 // providers
505
506 // annotation merging. not so efficient, should revisit later
507 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
508 if (otherPortDesc != null) {
509 annotations = merge(annotations, otherPortDesc.annotations());
510 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700511 }
512
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700513 return new DefaultPort(device, number, isEnabled, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700514 }
515
516 /**
517 * @return primary ProviderID, or randomly chosen one if none exists
518 */
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700519 private ProviderId pickPrimaryPID(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700520 ProviderId fallBackPrimary = null;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700521 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700522 if (!e.getKey().isAncillary()) {
523 return e.getKey();
524 } else if (fallBackPrimary == null) {
525 // pick randomly as a fallback in case there is no primary
526 fallBackPrimary = e.getKey();
527 }
528 }
529 return fallBackPrimary;
530 }
531
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700532 /**
533 * Collection of Description of a Device and it's Ports given from a Provider.
534 */
535 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700536
537 private final AtomicReference<DeviceDescription> deviceDesc;
538 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
539
540 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700541 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700542 this.portDescs = new ConcurrentHashMap<>();
543 }
544
545 public DeviceDescription getDeviceDesc() {
546 return deviceDesc.get();
547 }
548
549 public PortDescription getPortDesc(PortNumber number) {
550 return portDescs.get(number);
551 }
552
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700553 /**
554 * Puts DeviceDescription, merging annotations as necessary.
555 *
556 * @param newDesc new DeviceDescription
557 * @return previous DeviceDescription
558 */
559 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
560 DeviceDescription oldOne = deviceDesc.get();
561 DeviceDescription newOne = newDesc;
562 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700563 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700564 newDesc.annotations());
565 newOne = new DefaultDeviceDescription(newOne, merged);
566 }
567 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700568 }
569
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700570 /**
571 * Puts PortDescription, merging annotations as necessary.
572 *
573 * @param newDesc new PortDescription
574 * @return previous PortDescription
575 */
576 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
577 PortDescription oldOne = portDescs.get(newDesc.portNumber());
578 PortDescription newOne = newDesc;
579 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700580 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700581 newDesc.annotations());
582 newOne = new DefaultPortDescription(newOne, merged);
583 }
584 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700585 }
586 }
tome5ec3fd2014-09-04 15:18:06 -0700587}