blob: df20b2df18cb4af56d98435dfbb86065d609901a [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 HIGUCHIf5479702014-09-25 00:09:24 -07005
tom41a2c5f2014-09-19 09:20:35 -07006import org.apache.felix.scr.annotations.Activate;
7import org.apache.felix.scr.annotations.Component;
8import org.apache.felix.scr.annotations.Deactivate;
9import org.apache.felix.scr.annotations.Service;
tome5ec3fd2014-09-04 15:18:06 -070010import org.onlab.onos.net.DefaultDevice;
tom29df6f42014-09-05 08:14:14 -070011import org.onlab.onos.net.DefaultPort;
tome5ec3fd2014-09-04 15:18:06 -070012import org.onlab.onos.net.Device;
13import org.onlab.onos.net.DeviceId;
tome5ec3fd2014-09-04 15:18:06 -070014import org.onlab.onos.net.Port;
15import org.onlab.onos.net.PortNumber;
16import org.onlab.onos.net.device.DeviceDescription;
17import org.onlab.onos.net.device.DeviceEvent;
tom41a2c5f2014-09-19 09:20:35 -070018import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070019import org.onlab.onos.net.device.DeviceStoreDelegate;
tome5ec3fd2014-09-04 15:18:06 -070020import org.onlab.onos.net.device.PortDescription;
21import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070022import org.onlab.onos.store.AbstractStore;
tom41a2c5f2014-09-19 09:20:35 -070023import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070024
25import java.util.ArrayList;
26import java.util.Collections;
tom29df6f42014-09-05 08:14:14 -070027import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070028import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070029import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070030import java.util.List;
31import java.util.Map;
32import java.util.Objects;
33import java.util.Set;
34import java.util.concurrent.ConcurrentHashMap;
35
36import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070037import static com.google.common.base.Predicates.notNull;
tom29df6f42014-09-05 08:14:14 -070038import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070039import static org.slf4j.LoggerFactory.getLogger;
tome5ec3fd2014-09-04 15:18:06 -070040
41/**
tome4729872014-09-23 00:37:37 -070042 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070043 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070044 */
tom41a2c5f2014-09-19 09:20:35 -070045@Component(immediate = true)
46@Service
tomf80c9722014-09-24 14:49:18 -070047public class SimpleDeviceStore
48 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
49 implements DeviceStore {
tom41a2c5f2014-09-19 09:20:35 -070050
51 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070052
tom29df6f42014-09-05 08:14:14 -070053 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
54
tome5ec3fd2014-09-04 15:18:06 -070055 private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>();
tom249829a2014-09-04 15:28:04 -070056 private final Set<DeviceId> availableDevices = new HashSet<>();
tom29df6f42014-09-05 08:14:14 -070057 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
tome5ec3fd2014-09-04 15:18:06 -070058
tom41a2c5f2014-09-19 09:20:35 -070059 @Activate
60 public void activate() {
61 log.info("Started");
62 }
63
64 @Deactivate
65 public void deactivate() {
66 log.info("Stopped");
67 }
tom5bcc9462014-09-19 10:11:31 -070068
tom41a2c5f2014-09-19 09:20:35 -070069 @Override
70 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -070071 return devices.size();
72 }
73
tom41a2c5f2014-09-19 09:20:35 -070074 @Override
75 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -070076 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
77 }
78
tom41a2c5f2014-09-19 09:20:35 -070079 @Override
80 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -070081 return devices.get(deviceId);
82 }
83
tom41a2c5f2014-09-19 09:20:35 -070084 @Override
85 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -070086 DeviceDescription deviceDescription) {
87 DefaultDevice device = devices.get(deviceId);
88 if (device == null) {
89 return createDevice(providerId, deviceId, deviceDescription);
90 }
91 return updateDevice(providerId, device, deviceDescription);
92 }
93
94 // Creates the device and returns the appropriate event if necessary.
95 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
96 DeviceDescription desc) {
97 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
98 desc.manufacturer(),
99 desc.hwVersion(), desc.swVersion(),
100 desc.serialNumber());
101 synchronized (this) {
102 devices.put(deviceId, device);
tom249829a2014-09-04 15:28:04 -0700103 availableDevices.add(deviceId);
tome5ec3fd2014-09-04 15:18:06 -0700104 }
tom29df6f42014-09-05 08:14:14 -0700105 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700106 }
107
108 // Updates the device and returns the appropriate event if necessary.
109 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
110 DeviceDescription desc) {
111 // We allow only certain attributes to trigger update
112 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
113 !Objects.equals(device.swVersion(), desc.swVersion())) {
114 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
115 desc.type(),
116 desc.manufacturer(),
117 desc.hwVersion(),
118 desc.swVersion(),
119 desc.serialNumber());
120 synchronized (this) {
121 devices.put(device.id(), updated);
tom249829a2014-09-04 15:28:04 -0700122 availableDevices.add(device.id());
tome5ec3fd2014-09-04 15:18:06 -0700123 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700124 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, updated, null);
tome5ec3fd2014-09-04 15:18:06 -0700125 }
126
127 // Otherwise merely attempt to change availability
128 synchronized (this) {
tom249829a2014-09-04 15:28:04 -0700129 boolean added = availableDevices.add(device.id());
tom29df6f42014-09-05 08:14:14 -0700130 return !added ? null :
131 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700132 }
133 }
134
tom41a2c5f2014-09-19 09:20:35 -0700135 @Override
136 public DeviceEvent markOffline(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700137 synchronized (this) {
138 Device device = devices.get(deviceId);
tom89b63c52014-09-16 09:19:51 -0700139 boolean removed = device != null && availableDevices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700140 return !removed ? null :
141 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700142 }
143 }
144
tom41a2c5f2014-09-19 09:20:35 -0700145 @Override
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700146 public List<DeviceEvent> updatePorts(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700147 List<PortDescription> portDescriptions) {
tom29df6f42014-09-05 08:14:14 -0700148 List<DeviceEvent> events = new ArrayList<>();
149 synchronized (this) {
150 Device device = devices.get(deviceId);
151 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
152 Map<PortNumber, Port> ports = getPortMap(deviceId);
153
154 // Add new ports
155 Set<PortNumber> processed = new HashSet<>();
156 for (PortDescription portDescription : portDescriptions) {
157 Port port = ports.get(portDescription.portNumber());
tom24c55cd2014-09-06 10:47:25 -0700158 events.add(port == null ?
159 createPort(device, portDescription, ports) :
160 updatePort(device, port, portDescription, ports));
tom29df6f42014-09-05 08:14:14 -0700161 processed.add(portDescription.portNumber());
162 }
163
164 events.addAll(pruneOldPorts(device, ports, processed));
165 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700166 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700167 }
168
169 // Creates a new port based on the port description adds it to the map and
170 // Returns corresponding event.
171 private DeviceEvent createPort(Device device, PortDescription portDescription,
172 Map<PortNumber, Port> ports) {
173 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
174 portDescription.isEnabled());
175 ports.put(port.number(), port);
176 return new DeviceEvent(PORT_ADDED, device, port);
177 }
178
179 // CHecks if the specified port requires update and if so, it replaces the
180 // existing entry in the map and returns corresponding event.
181 private DeviceEvent updatePort(Device device, Port port,
182 PortDescription portDescription,
183 Map<PortNumber, Port> ports) {
184 if (port.isEnabled() != portDescription.isEnabled()) {
185 DefaultPort updatedPort =
186 new DefaultPort(device, portDescription.portNumber(),
187 portDescription.isEnabled());
188 ports.put(port.number(), updatedPort);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700189 return new DeviceEvent(PORT_UPDATED, device, updatedPort);
tom29df6f42014-09-05 08:14:14 -0700190 }
191 return null;
192 }
193
194 // Prunes the specified list of ports based on which ports are in the
195 // processed list and returns list of corresponding events.
196 private List<DeviceEvent> pruneOldPorts(Device device,
197 Map<PortNumber, Port> ports,
198 Set<PortNumber> processed) {
199 List<DeviceEvent> events = new ArrayList<>();
200 Iterator<PortNumber> iterator = ports.keySet().iterator();
201 while (iterator.hasNext()) {
202 PortNumber portNumber = iterator.next();
tom24c55cd2014-09-06 10:47:25 -0700203 if (!processed.contains(portNumber)) {
tom29df6f42014-09-05 08:14:14 -0700204 events.add(new DeviceEvent(PORT_REMOVED, device,
205 ports.get(portNumber)));
206 iterator.remove();
207 }
208 }
209 return events;
210 }
211
212 // Gets the map of ports for the specified device; if one does not already
213 // exist, it creates and registers a new one.
214 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
215 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
216 if (ports == null) {
217 ports = new HashMap<>();
218 devicePorts.put(deviceId, ports);
219 }
220 return ports;
tome5ec3fd2014-09-04 15:18:06 -0700221 }
222
tom41a2c5f2014-09-19 09:20:35 -0700223 @Override
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700224 public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700225 PortDescription portDescription) {
tom46a220d2014-09-05 08:25:56 -0700226 synchronized (this) {
227 Device device = devices.get(deviceId);
228 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
229 Map<PortNumber, Port> ports = getPortMap(deviceId);
230 Port port = ports.get(portDescription.portNumber());
231 return updatePort(device, port, portDescription, ports);
232 }
tome5ec3fd2014-09-04 15:18:06 -0700233 }
234
tom41a2c5f2014-09-19 09:20:35 -0700235 @Override
236 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700237 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
238 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700239 }
240
tom41a2c5f2014-09-19 09:20:35 -0700241 @Override
242 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700243 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
244 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700245 }
246
tom41a2c5f2014-09-19 09:20:35 -0700247 @Override
248 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700249 return availableDevices.contains(deviceId);
250 }
251
tom41a2c5f2014-09-19 09:20:35 -0700252 @Override
tom41a2c5f2014-09-19 09:20:35 -0700253 public DeviceEvent removeDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700254 synchronized (this) {
tome5ec3fd2014-09-04 15:18:06 -0700255 Device device = devices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700256 return device == null ? null :
257 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700258 }
259 }
tome5ec3fd2014-09-04 15:18:06 -0700260}