blob: fbfaf9db1fecb725f9d714034c18e09695e9d92b [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
tome5ec3fd2014-09-04 15:18:06 -07002
Yuta HIGUCHIf5479702014-09-25 00:09:24 -07003import com.google.common.collect.FluentIterable;
tom46a220d2014-09-05 08:25:56 -07004import com.google.common.collect.ImmutableList;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -07005import com.google.common.collect.Maps;
6import com.google.common.collect.Sets;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -07007
tom41a2c5f2014-09-19 09:20:35 -07008import org.apache.felix.scr.annotations.Activate;
9import org.apache.felix.scr.annotations.Component;
10import org.apache.felix.scr.annotations.Deactivate;
11import org.apache.felix.scr.annotations.Service;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070012import org.onlab.onos.net.AnnotationsUtil;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070013import org.onlab.onos.net.DefaultAnnotations;
tome5ec3fd2014-09-04 15:18:06 -070014import org.onlab.onos.net.DefaultDevice;
tom29df6f42014-09-05 08:14:14 -070015import org.onlab.onos.net.DefaultPort;
tome5ec3fd2014-09-04 15:18:06 -070016import org.onlab.onos.net.Device;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070017import org.onlab.onos.net.Device.Type;
tome5ec3fd2014-09-04 15:18:06 -070018import org.onlab.onos.net.DeviceId;
tome5ec3fd2014-09-04 15:18:06 -070019import org.onlab.onos.net.Port;
20import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070021import org.onlab.onos.net.SparseAnnotations;
22import org.onlab.onos.net.device.DefaultDeviceDescription;
23import org.onlab.onos.net.device.DefaultPortDescription;
tome5ec3fd2014-09-04 15:18:06 -070024import org.onlab.onos.net.device.DeviceDescription;
25import org.onlab.onos.net.device.DeviceEvent;
tom41a2c5f2014-09-19 09:20:35 -070026import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070027import org.onlab.onos.net.device.DeviceStoreDelegate;
tome5ec3fd2014-09-04 15:18:06 -070028import org.onlab.onos.net.device.PortDescription;
29import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070030import org.onlab.onos.store.AbstractStore;
alshabib7911a052014-10-16 17:49:37 -070031import org.onlab.packet.ChassisId;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070032import org.onlab.util.NewConcurrentHashMap;
tom41a2c5f2014-09-19 09:20:35 -070033import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070034
35import java.util.ArrayList;
36import java.util.Collections;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070037import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070038import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070039import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070040import java.util.List;
41import java.util.Map;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070042import java.util.Map.Entry;
tome5ec3fd2014-09-04 15:18:06 -070043import java.util.Objects;
44import java.util.Set;
45import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070046import java.util.concurrent.ConcurrentMap;
47import java.util.concurrent.atomic.AtomicReference;
tome5ec3fd2014-09-04 15:18:06 -070048
49import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070050import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070051import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070052import static com.google.common.base.Verify.verify;
tom29df6f42014-09-05 08:14:14 -070053import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070054import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070055import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -070056import static org.onlab.onos.net.DefaultAnnotations.union;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070057import static org.onlab.onos.net.DefaultAnnotations.merge;
tome5ec3fd2014-09-04 15:18:06 -070058
59/**
tome4729872014-09-23 00:37:37 -070060 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070061 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070062 */
tom41a2c5f2014-09-19 09:20:35 -070063@Component(immediate = true)
64@Service
tomf80c9722014-09-24 14:49:18 -070065public class SimpleDeviceStore
66 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
67 implements DeviceStore {
tom41a2c5f2014-09-19 09:20:35 -070068
69 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070070
tom29df6f42014-09-05 08:14:14 -070071 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
72
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070073 // collection of Description given from various providers
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -070074 private final ConcurrentMap<DeviceId, Map<ProviderId, DeviceDescriptions>>
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070075 deviceDescs = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070076
77 // cache of Device and Ports generated by compositing descriptions from providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070078 private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap();
79 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = Maps.newConcurrentMap();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070080
81 // available(=UP) devices
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070082 private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet();
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070083
tome5ec3fd2014-09-04 15:18:06 -070084
tom41a2c5f2014-09-19 09:20:35 -070085 @Activate
86 public void activate() {
87 log.info("Started");
88 }
89
90 @Deactivate
91 public void deactivate() {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070092 deviceDescs.clear();
93 devices.clear();
94 devicePorts.clear();
95 availableDevices.clear();
tom41a2c5f2014-09-19 09:20:35 -070096 log.info("Stopped");
97 }
tom5bcc9462014-09-19 10:11:31 -070098
tom41a2c5f2014-09-19 09:20:35 -070099 @Override
100 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -0700101 return devices.size();
102 }
103
tom41a2c5f2014-09-19 09:20:35 -0700104 @Override
105 public Iterable<Device> getDevices() {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700106 return Collections.unmodifiableCollection(devices.values());
tome5ec3fd2014-09-04 15:18:06 -0700107 }
108
tom41a2c5f2014-09-19 09:20:35 -0700109 @Override
110 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700111 return devices.get(deviceId);
112 }
113
tom41a2c5f2014-09-19 09:20:35 -0700114 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700115 public DeviceEvent createOrUpdateDevice(ProviderId providerId,
116 DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700117 DeviceDescription deviceDescription) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700118
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700119 Map<ProviderId, DeviceDescriptions> providerDescs
120 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700121
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700122 synchronized (providerDescs) {
123 // locking per device
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700124
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700125 DeviceDescriptions descs
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700126 = getOrCreateProviderDeviceDescriptions(providerDescs,
127 providerId,
128 deviceDescription);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700129
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700130 Device oldDevice = devices.get(deviceId);
131 // update description
132 descs.putDeviceDesc(deviceDescription);
133 Device newDevice = composeDevice(deviceId, providerDescs);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700134
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700135 if (oldDevice == null) {
136 // ADD
137 return createDevice(providerId, newDevice);
138 } else {
139 // UPDATE or ignore (no change or stale)
140 return updateDevice(providerId, oldDevice, newDevice);
141 }
tome5ec3fd2014-09-04 15:18:06 -0700142 }
tome5ec3fd2014-09-04 15:18:06 -0700143 }
144
145 // Creates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700146 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700147 private DeviceEvent createDevice(ProviderId providerId, Device newDevice) {
148
149 // update composed device cache
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700150 Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice);
151 verify(oldDevice == null,
152 "Unexpected Device in cache. PID:%s [old=%s, new=%s]",
153 providerId, oldDevice, newDevice);
154
155 if (!providerId.isAncillary()) {
156 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700157 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700158
159 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700160 }
161
162 // Updates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700163 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700164 private DeviceEvent updateDevice(ProviderId providerId, Device oldDevice, Device newDevice) {
165
tome5ec3fd2014-09-04 15:18:06 -0700166 // We allow only certain attributes to trigger update
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700167 if (!Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) ||
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700168 !Objects.equals(oldDevice.swVersion(), newDevice.swVersion()) ||
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700169 !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations())) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700170
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700171 boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice);
172 if (!replaced) {
173 verify(replaced,
174 "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]",
175 providerId, oldDevice, devices.get(newDevice.id())
176 , newDevice);
177 }
178 if (!providerId.isAncillary()) {
179 availableDevices.add(newDevice.id());
tome5ec3fd2014-09-04 15:18:06 -0700180 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700181 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700182 }
183
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700184 // Otherwise merely attempt to change availability if primary provider
185 if (!providerId.isAncillary()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700186 boolean added = availableDevices.add(newDevice.id());
tom29df6f42014-09-05 08:14:14 -0700187 return !added ? null :
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700188 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null);
tome5ec3fd2014-09-04 15:18:06 -0700189 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700190 return null;
tome5ec3fd2014-09-04 15:18:06 -0700191 }
192
tom41a2c5f2014-09-19 09:20:35 -0700193 @Override
194 public DeviceEvent markOffline(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700195 Map<ProviderId, DeviceDescriptions> providerDescs
196 = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700197
198 // locking device
199 synchronized (providerDescs) {
tome5ec3fd2014-09-04 15:18:06 -0700200 Device device = devices.get(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700201 if (device == null) {
202 return null;
203 }
204 boolean removed = availableDevices.remove(deviceId);
205 if (removed) {
206 // TODO: broadcast ... DOWN only?
207 return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
208 }
209 return null;
tome5ec3fd2014-09-04 15:18:06 -0700210 }
211 }
212
tom41a2c5f2014-09-19 09:20:35 -0700213 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700214 public List<DeviceEvent> updatePorts(ProviderId providerId,
215 DeviceId deviceId,
216 List<PortDescription> portDescriptions) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700217
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700218 Device device = devices.get(deviceId);
219 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
220
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700221 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700222 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
223
tom29df6f42014-09-05 08:14:14 -0700224 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700225 synchronized (descsMap) {
226 DeviceDescriptions descs = descsMap.get(providerId);
227 // every provider must provide DeviceDescription.
228 checkArgument(descs != null,
229 "Device description for Device ID %s from Provider %s was not found",
230 deviceId, providerId);
231
232 Map<PortNumber, Port> ports = getPortMap(deviceId);
tom29df6f42014-09-05 08:14:14 -0700233
234 // Add new ports
235 Set<PortNumber> processed = new HashSet<>();
236 for (PortDescription portDescription : portDescriptions) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700237 final PortNumber number = portDescription.portNumber();
238 processed.add(portDescription.portNumber());
239
240 final Port oldPort = ports.get(number);
241 final Port newPort;
242
243// event suppression hook?
244
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700245 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700246 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700247 newPort = composePort(device, number, descsMap);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700248
249 events.add(oldPort == null ?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700250 createPort(device, newPort, ports) :
251 updatePort(device, oldPort, newPort, ports));
tom29df6f42014-09-05 08:14:14 -0700252 }
253
254 events.addAll(pruneOldPorts(device, ports, processed));
255 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700256 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700257 }
258
259 // Creates a new port based on the port description adds it to the map and
260 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700261 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700262 private DeviceEvent createPort(Device device, Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700263 Map<PortNumber, Port> ports) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700264 ports.put(newPort.number(), newPort);
265 return new DeviceEvent(PORT_ADDED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700266 }
267
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700268 // Checks if the specified port requires update and if so, it replaces the
tom29df6f42014-09-05 08:14:14 -0700269 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700270 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700271 private DeviceEvent updatePort(Device device, Port oldPort,
272 Port newPort,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700273 Map<PortNumber, Port> ports) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700274 if (oldPort.isEnabled() != newPort.isEnabled() ||
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700275 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700276
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700277 ports.put(oldPort.number(), newPort);
278 return new DeviceEvent(PORT_UPDATED, device, newPort);
tom29df6f42014-09-05 08:14:14 -0700279 }
280 return null;
281 }
282
283 // Prunes the specified list of ports based on which ports are in the
284 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700285 // Guarded by deviceDescs value (=Device lock)
tom29df6f42014-09-05 08:14:14 -0700286 private List<DeviceEvent> pruneOldPorts(Device device,
287 Map<PortNumber, Port> ports,
288 Set<PortNumber> processed) {
289 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700290 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
tom29df6f42014-09-05 08:14:14 -0700291 while (iterator.hasNext()) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700292 Entry<PortNumber, Port> e = iterator.next();
293 PortNumber portNumber = e.getKey();
tom24c55cd2014-09-06 10:47:25 -0700294 if (!processed.contains(portNumber)) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700295 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
tom29df6f42014-09-05 08:14:14 -0700296 iterator.remove();
297 }
298 }
299 return events;
300 }
301
302 // Gets the map of ports for the specified device; if one does not already
303 // exist, it creates and registers a new one.
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700304 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
305 return createIfAbsentUnchecked(devicePorts, deviceId,
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700306 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
tome5ec3fd2014-09-04 15:18:06 -0700307 }
308
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700309 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptions(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700310 DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700311 Map<ProviderId, DeviceDescriptions> r;
312 r = deviceDescs.get(deviceId);
313 if (r != null) {
314 return r;
315 }
316 r = new HashMap<>();
317 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
318 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
319 if (concurrentlyAdded != null) {
320 return concurrentlyAdded;
321 } else {
322 return r;
323 }
324 }
325
326 // Guarded by deviceDescs value (=Device lock)
327 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
328 Map<ProviderId, DeviceDescriptions> device,
329 ProviderId providerId, DeviceDescription deltaDesc) {
330
331 synchronized (device) {
332 DeviceDescriptions r = device.get(providerId);
333 if (r == null) {
334 r = new DeviceDescriptions(deltaDesc);
335 device.put(providerId, r);
336 }
337 return r;
338 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700339 }
340
tom41a2c5f2014-09-19 09:20:35 -0700341 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700342 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700343 PortDescription portDescription) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700344 Device device = devices.get(deviceId);
345 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
346
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700347 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700348 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
349
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700350 synchronized (descsMap) {
351 DeviceDescriptions descs = descsMap.get(providerId);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700352 // assuming all providers must give DeviceDescription first
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700353 checkArgument(descs != null,
354 "Device description for Device ID %s from Provider %s was not found",
355 deviceId, providerId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700356
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700357 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
358 final PortNumber number = portDescription.portNumber();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700359 final Port oldPort = ports.get(number);
360 final Port newPort;
361
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700362 // update description
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700363 descs.putPortDesc(portDescription);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700364 newPort = composePort(device, number, descsMap);
365
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700366 if (oldPort == null) {
367 return createPort(device, newPort, ports);
368 } else {
369 return updatePort(device, oldPort, newPort, ports);
370 }
tom46a220d2014-09-05 08:25:56 -0700371 }
tome5ec3fd2014-09-04 15:18:06 -0700372 }
373
tom41a2c5f2014-09-19 09:20:35 -0700374 @Override
375 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700376 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700377 if (ports == null) {
378 return Collections.emptyList();
379 }
380 return ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700381 }
382
tom41a2c5f2014-09-19 09:20:35 -0700383 @Override
384 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700385 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
386 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700387 }
388
tom41a2c5f2014-09-19 09:20:35 -0700389 @Override
390 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700391 return availableDevices.contains(deviceId);
392 }
393
tom41a2c5f2014-09-19 09:20:35 -0700394 @Override
tom41a2c5f2014-09-19 09:20:35 -0700395 public DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700396 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700397 synchronized (descs) {
tome5ec3fd2014-09-04 15:18:06 -0700398 Device device = devices.remove(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700399 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700400 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700401 if (ports != null) {
402 ports.clear();
403 }
404 availableDevices.remove(deviceId);
405 descs.clear();
tom29df6f42014-09-05 08:14:14 -0700406 return device == null ? null :
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700407 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700408 }
409 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700410
411 /**
412 * Returns a Device, merging description given from multiple Providers.
413 *
414 * @param deviceId device identifier
415 * @param providerDescs Collection of Descriptions from multiple providers
416 * @return Device instance
417 */
418 private Device composeDevice(DeviceId deviceId,
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700419 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700420
421 checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied");
422
423 ProviderId primary = pickPrimaryPID(providerDescs);
424
425 DeviceDescriptions desc = providerDescs.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700426
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700427 final DeviceDescription base = desc.getDeviceDesc();
428 Type type = base.type();
429 String manufacturer = base.manufacturer();
430 String hwVersion = base.hwVersion();
431 String swVersion = base.swVersion();
432 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700433 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700434 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700435 annotations = merge(annotations, base.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700436
437 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
438 if (e.getKey().equals(primary)) {
439 continue;
440 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700441 // TODO: should keep track of Description timestamp
442 // and only merge conflicting keys when timestamp is newer
443 // Currently assuming there will never be a key conflict between
444 // providers
445
446 // annotation merging. not so efficient, should revisit later
447 annotations = merge(annotations, e.getValue().getDeviceDesc().annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700448 }
449
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700450 return new DefaultDevice(primary, deviceId , type, manufacturer,
alshabib7911a052014-10-16 17:49:37 -0700451 hwVersion, swVersion, serialNumber,
452 chassisId, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700453 }
454
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700455 /**
456 * Returns a Port, merging description given from multiple Providers.
457 *
458 * @param device device the port is on
459 * @param number port number
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700460 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700461 * @return Port instance
462 */
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700463 private Port composePort(Device device, PortNumber number,
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700464 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700465
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700466 ProviderId primary = pickPrimaryPID(descsMap);
467 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700468 // if no primary, assume not enabled
469 // TODO: revisit this default port enabled/disabled behavior
470 boolean isEnabled = false;
471 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
472
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700473 final PortDescription portDesc = primDescs.getPortDesc(number);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700474 if (portDesc != null) {
475 isEnabled = portDesc.isEnabled();
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700476 annotations = merge(annotations, portDesc.annotations());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700477 }
478
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700479 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700480 if (e.getKey().equals(primary)) {
481 continue;
482 }
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700483 // TODO: should keep track of Description timestamp
484 // and only merge conflicting keys when timestamp is newer
485 // Currently assuming there will never be a key conflict between
486 // providers
487
488 // annotation merging. not so efficient, should revisit later
489 final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
490 if (otherPortDesc != null) {
491 annotations = merge(annotations, otherPortDesc.annotations());
492 }
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700493 }
494
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700495 return new DefaultPort(device, number, isEnabled, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700496 }
497
498 /**
499 * @return primary ProviderID, or randomly chosen one if none exists
500 */
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700501 private ProviderId pickPrimaryPID(Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700502 ProviderId fallBackPrimary = null;
Yuta HIGUCHI2c546a12014-10-16 11:02:00 -0700503 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700504 if (!e.getKey().isAncillary()) {
505 return e.getKey();
506 } else if (fallBackPrimary == null) {
507 // pick randomly as a fallback in case there is no primary
508 fallBackPrimary = e.getKey();
509 }
510 }
511 return fallBackPrimary;
512 }
513
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700514 /**
515 * Collection of Description of a Device and it's Ports given from a Provider.
516 */
517 private static class DeviceDescriptions {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700518
519 private final AtomicReference<DeviceDescription> deviceDesc;
520 private final ConcurrentMap<PortNumber, PortDescription> portDescs;
521
522 public DeviceDescriptions(DeviceDescription desc) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700523 this.deviceDesc = new AtomicReference<>(checkNotNull(desc));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700524 this.portDescs = new ConcurrentHashMap<>();
525 }
526
527 public DeviceDescription getDeviceDesc() {
528 return deviceDesc.get();
529 }
530
531 public PortDescription getPortDesc(PortNumber number) {
532 return portDescs.get(number);
533 }
534
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700535 /**
536 * Puts DeviceDescription, merging annotations as necessary.
537 *
538 * @param newDesc new DeviceDescription
539 * @return previous DeviceDescription
540 */
541 public synchronized DeviceDescription putDeviceDesc(DeviceDescription newDesc) {
542 DeviceDescription oldOne = deviceDesc.get();
543 DeviceDescription newOne = newDesc;
544 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700545 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700546 newDesc.annotations());
547 newOne = new DefaultDeviceDescription(newOne, merged);
548 }
549 return deviceDesc.getAndSet(newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700550 }
551
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700552 /**
553 * Puts PortDescription, merging annotations as necessary.
554 *
555 * @param newDesc new PortDescription
556 * @return previous PortDescription
557 */
558 public synchronized PortDescription putPortDesc(PortDescription newDesc) {
559 PortDescription oldOne = portDescs.get(newDesc.portNumber());
560 PortDescription newOne = newDesc;
561 if (oldOne != null) {
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700562 SparseAnnotations merged = union(oldOne.annotations(),
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700563 newDesc.annotations());
564 newOne = new DefaultPortDescription(newOne, merged);
565 }
566 return portDescs.put(newOne.portNumber(), newOne);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700567 }
568 }
tome5ec3fd2014-09-04 15:18:06 -0700569}