blob: fdffad25aa946eaab2a9ebece3e13a7f2a97ac52 [file] [log] [blame]
tome5ec3fd2014-09-04 15:18:06 -07001package org.onlab.onos.net.trivial.impl;
2
tom46a220d2014-09-05 08:25:56 -07003import com.google.common.collect.ImmutableList;
tome5ec3fd2014-09-04 15:18:06 -07004import org.onlab.onos.net.DefaultDevice;
tom29df6f42014-09-05 08:14:14 -07005import org.onlab.onos.net.DefaultPort;
tome5ec3fd2014-09-04 15:18:06 -07006import org.onlab.onos.net.Device;
7import org.onlab.onos.net.DeviceId;
8import org.onlab.onos.net.MastershipRole;
9import org.onlab.onos.net.Port;
10import org.onlab.onos.net.PortNumber;
11import org.onlab.onos.net.device.DeviceDescription;
12import org.onlab.onos.net.device.DeviceEvent;
13import org.onlab.onos.net.device.PortDescription;
14import org.onlab.onos.net.provider.ProviderId;
15
16import java.util.ArrayList;
17import java.util.Collections;
tom29df6f42014-09-05 08:14:14 -070018import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070019import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070020import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070021import java.util.List;
22import java.util.Map;
23import java.util.Objects;
24import java.util.Set;
25import java.util.concurrent.ConcurrentHashMap;
26
27import static com.google.common.base.Preconditions.checkArgument;
tom29df6f42014-09-05 08:14:14 -070028import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tome5ec3fd2014-09-04 15:18:06 -070029
30/**
tomad2d2092014-09-06 23:24:20 -070031 * Manages inventory of infrastructure devices using trivial in-memory
32 * implementation.
tome5ec3fd2014-09-04 15:18:06 -070033 */
34class SimpleDeviceStore {
35
tom29df6f42014-09-05 08:14:14 -070036 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
37
tome5ec3fd2014-09-04 15:18:06 -070038 private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>();
tom249829a2014-09-04 15:28:04 -070039 private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>();
40 private final Set<DeviceId> availableDevices = new HashSet<>();
tom29df6f42014-09-05 08:14:14 -070041 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
tome5ec3fd2014-09-04 15:18:06 -070042
43 /**
tomad2d2092014-09-06 23:24:20 -070044 * Returns the number of devices known to the system.
45 *
46 * @return number of devices
47 */
tomeadbb462014-09-07 16:10:19 -070048 int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -070049 return devices.size();
50 }
51
52 /**
tome5ec3fd2014-09-04 15:18:06 -070053 * Returns an iterable collection of all devices known to the system.
54 *
55 * @return device collection
56 */
57 Iterable<Device> getDevices() {
58 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
59 }
60
61 /**
62 * Returns the device with the specified identifier.
63 *
64 * @param deviceId device identifier
65 * @return device
66 */
67 Device getDevice(DeviceId deviceId) {
68 return devices.get(deviceId);
69 }
70
71 /**
72 * Creates a new infrastructure device, or updates an existing one using
73 * the supplied device description.
74 *
75 * @param providerId provider identifier
76 * @param deviceId device identifier
77 * @param deviceDescription device description
78 * @return ready to send event describing what occurred; null if no change
79 */
80 DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
81 DeviceDescription deviceDescription) {
82 DefaultDevice device = devices.get(deviceId);
83 if (device == null) {
84 return createDevice(providerId, deviceId, deviceDescription);
85 }
86 return updateDevice(providerId, device, deviceDescription);
87 }
88
89 // Creates the device and returns the appropriate event if necessary.
90 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
91 DeviceDescription desc) {
92 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
93 desc.manufacturer(),
94 desc.hwVersion(), desc.swVersion(),
95 desc.serialNumber());
96 synchronized (this) {
97 devices.put(deviceId, device);
tom249829a2014-09-04 15:28:04 -070098 availableDevices.add(deviceId);
tome5ec3fd2014-09-04 15:18:06 -070099 }
tom29df6f42014-09-05 08:14:14 -0700100 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700101 }
102
103 // Updates the device and returns the appropriate event if necessary.
104 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
105 DeviceDescription desc) {
106 // We allow only certain attributes to trigger update
107 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
108 !Objects.equals(device.swVersion(), desc.swVersion())) {
109 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
110 desc.type(),
111 desc.manufacturer(),
112 desc.hwVersion(),
113 desc.swVersion(),
114 desc.serialNumber());
115 synchronized (this) {
116 devices.put(device.id(), updated);
tom249829a2014-09-04 15:28:04 -0700117 availableDevices.add(device.id());
tome5ec3fd2014-09-04 15:18:06 -0700118 }
tom29df6f42014-09-05 08:14:14 -0700119 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700120 }
121
122 // Otherwise merely attempt to change availability
123 synchronized (this) {
tom249829a2014-09-04 15:28:04 -0700124 boolean added = availableDevices.add(device.id());
tom29df6f42014-09-05 08:14:14 -0700125 return !added ? null :
126 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700127 }
128 }
129
130 /**
131 * Removes the specified infrastructure device.
132 *
133 * @param deviceId device identifier
134 * @return ready to send event describing what occurred; null if no change
135 */
136 DeviceEvent markOffline(DeviceId deviceId) {
137 synchronized (this) {
138 Device device = devices.get(deviceId);
tom29df6f42014-09-05 08:14:14 -0700139 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
tom249829a2014-09-04 15:28:04 -0700140 boolean removed = availableDevices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700141 return !removed ? null :
142 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700143 }
144 }
145
146 /**
147 * Updates the ports of the specified infrastructure device using the given
148 * list of port descriptions. The list is assumed to be comprehensive.
149 *
150 * @param deviceId device identifier
151 * @param portDescriptions list of port descriptions
152 * @return ready to send events describing what occurred; empty list if no change
153 */
154 List<DeviceEvent> updatePorts(DeviceId deviceId,
155 List<PortDescription> portDescriptions) {
tom29df6f42014-09-05 08:14:14 -0700156 List<DeviceEvent> events = new ArrayList<>();
157 synchronized (this) {
158 Device device = devices.get(deviceId);
159 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
160 Map<PortNumber, Port> ports = getPortMap(deviceId);
161
162 // Add new ports
163 Set<PortNumber> processed = new HashSet<>();
164 for (PortDescription portDescription : portDescriptions) {
165 Port port = ports.get(portDescription.portNumber());
tom24c55cd2014-09-06 10:47:25 -0700166 events.add(port == null ?
167 createPort(device, portDescription, ports) :
168 updatePort(device, port, portDescription, ports));
tom29df6f42014-09-05 08:14:14 -0700169 processed.add(portDescription.portNumber());
170 }
171
172 events.addAll(pruneOldPorts(device, ports, processed));
173 }
174 return events;
175 }
176
177 // Creates a new port based on the port description adds it to the map and
178 // Returns corresponding event.
179 private DeviceEvent createPort(Device device, PortDescription portDescription,
180 Map<PortNumber, Port> ports) {
181 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
182 portDescription.isEnabled());
183 ports.put(port.number(), port);
184 return new DeviceEvent(PORT_ADDED, device, port);
185 }
186
187 // CHecks if the specified port requires update and if so, it replaces the
188 // existing entry in the map and returns corresponding event.
189 private DeviceEvent updatePort(Device device, Port port,
190 PortDescription portDescription,
191 Map<PortNumber, Port> ports) {
192 if (port.isEnabled() != portDescription.isEnabled()) {
193 DefaultPort updatedPort =
194 new DefaultPort(device, portDescription.portNumber(),
195 portDescription.isEnabled());
196 ports.put(port.number(), updatedPort);
197 return new DeviceEvent(PORT_UPDATED, device, port);
198 }
199 return null;
200 }
201
202 // Prunes the specified list of ports based on which ports are in the
203 // processed list and returns list of corresponding events.
204 private List<DeviceEvent> pruneOldPorts(Device device,
205 Map<PortNumber, Port> ports,
206 Set<PortNumber> processed) {
207 List<DeviceEvent> events = new ArrayList<>();
208 Iterator<PortNumber> iterator = ports.keySet().iterator();
209 while (iterator.hasNext()) {
210 PortNumber portNumber = iterator.next();
tom24c55cd2014-09-06 10:47:25 -0700211 if (!processed.contains(portNumber)) {
tom29df6f42014-09-05 08:14:14 -0700212 events.add(new DeviceEvent(PORT_REMOVED, device,
213 ports.get(portNumber)));
214 iterator.remove();
215 }
216 }
217 return events;
218 }
219
220 // Gets the map of ports for the specified device; if one does not already
221 // exist, it creates and registers a new one.
222 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
223 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
224 if (ports == null) {
225 ports = new HashMap<>();
226 devicePorts.put(deviceId, ports);
227 }
228 return ports;
tome5ec3fd2014-09-04 15:18:06 -0700229 }
230
231 /**
232 * Updates the port status of the specified infrastructure device using the
233 * given port description.
234 *
235 * @param deviceId device identifier
236 * @param portDescription port description
237 * @return ready to send event describing what occurred; null if no change
238 */
239 DeviceEvent updatePortStatus(DeviceId deviceId,
240 PortDescription portDescription) {
tom46a220d2014-09-05 08:25:56 -0700241 synchronized (this) {
242 Device device = devices.get(deviceId);
243 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
244 Map<PortNumber, Port> ports = getPortMap(deviceId);
245 Port port = ports.get(portDescription.portNumber());
246 return updatePort(device, port, portDescription, ports);
247 }
tome5ec3fd2014-09-04 15:18:06 -0700248 }
249
250 /**
251 * Returns the list of ports that belong to the specified device.
252 *
253 * @param deviceId device identifier
254 * @return list of device ports
255 */
256 List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700257 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
258 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700259 }
260
261 /**
262 * Returns the specified device port.
263 *
264 * @param deviceId device identifier
265 * @param portNumber port number
266 * @return device port
267 */
268 Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700269 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
270 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700271 }
272
273 /**
tomff7eb7c2014-09-08 12:49:03 -0700274 * Indicates whether the specified device is available/online.
275 *
276 * @param deviceId device identifier
277 * @return true if device is available
278 */
279 boolean isAvailable(DeviceId deviceId) {
280 return availableDevices.contains(deviceId);
281 }
282
283 /**
tome5ec3fd2014-09-04 15:18:06 -0700284 * Returns the mastership role determined for this device.
285 *
286 * @param deviceId device identifier
287 * @return mastership role
288 */
289 MastershipRole getRole(DeviceId deviceId) {
290 MastershipRole role = roles.get(deviceId);
291 return role != null ? role : MastershipRole.NONE;
292 }
293
294 /**
295 * Administratively sets the role of the specified device.
296 *
297 * @param deviceId device identifier
298 * @param role mastership role to apply
299 * @return mastership role change event or null if no change
300 */
301 DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
tom249829a2014-09-04 15:28:04 -0700302 synchronized (this) {
303 Device device = getDevice(deviceId);
tom29df6f42014-09-05 08:14:14 -0700304 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
tom249829a2014-09-04 15:28:04 -0700305 MastershipRole oldRole = roles.put(deviceId, role);
tom29df6f42014-09-05 08:14:14 -0700306 return oldRole == role ? null :
307 new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null);
tom249829a2014-09-04 15:28:04 -0700308 }
tome5ec3fd2014-09-04 15:18:06 -0700309 }
310
311 /**
312 * Administratively removes the specified device from the store.
313 *
314 * @param deviceId device to be removed
315 */
316 DeviceEvent removeDevice(DeviceId deviceId) {
317 synchronized (this) {
318 roles.remove(deviceId);
319 Device device = devices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700320 return device == null ? null :
321 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700322 }
323 }
tome5ec3fd2014-09-04 15:18:06 -0700324}