blob: 017950744c6fe46b14c68ee8bd0b5590b5838361 [file] [log] [blame]
tome5ec3fd2014-09-04 15:18:06 -07001package org.onlab.onos.net.trivial.impl;
2
3import org.onlab.onos.net.DefaultDevice;
tom29df6f42014-09-05 08:14:14 -07004import org.onlab.onos.net.DefaultPort;
tome5ec3fd2014-09-04 15:18:06 -07005import org.onlab.onos.net.Device;
6import org.onlab.onos.net.DeviceId;
7import org.onlab.onos.net.MastershipRole;
8import org.onlab.onos.net.Port;
9import org.onlab.onos.net.PortNumber;
10import org.onlab.onos.net.device.DeviceDescription;
11import org.onlab.onos.net.device.DeviceEvent;
12import org.onlab.onos.net.device.PortDescription;
13import org.onlab.onos.net.provider.ProviderId;
14
15import java.util.ArrayList;
16import java.util.Collections;
tom29df6f42014-09-05 08:14:14 -070017import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070018import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070019import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070020import java.util.List;
21import java.util.Map;
22import java.util.Objects;
23import java.util.Set;
24import java.util.concurrent.ConcurrentHashMap;
25
26import static com.google.common.base.Preconditions.checkArgument;
tom29df6f42014-09-05 08:14:14 -070027import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tome5ec3fd2014-09-04 15:18:06 -070028
29/**
30 * Manages inventory of infrastructure devices.
31 */
32class SimpleDeviceStore {
33
tom29df6f42014-09-05 08:14:14 -070034 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
35
tome5ec3fd2014-09-04 15:18:06 -070036 private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>();
tom249829a2014-09-04 15:28:04 -070037 private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>();
38 private final Set<DeviceId> availableDevices = new HashSet<>();
tom29df6f42014-09-05 08:14:14 -070039 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
tome5ec3fd2014-09-04 15:18:06 -070040
41 /**
42 * Returns an iterable collection of all devices known to the system.
43 *
44 * @return device collection
45 */
46 Iterable<Device> getDevices() {
47 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
48 }
49
50 /**
51 * Returns the device with the specified identifier.
52 *
53 * @param deviceId device identifier
54 * @return device
55 */
56 Device getDevice(DeviceId deviceId) {
57 return devices.get(deviceId);
58 }
59
60 /**
61 * Creates a new infrastructure device, or updates an existing one using
62 * the supplied device description.
63 *
64 * @param providerId provider identifier
65 * @param deviceId device identifier
66 * @param deviceDescription device description
67 * @return ready to send event describing what occurred; null if no change
68 */
69 DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
70 DeviceDescription deviceDescription) {
71 DefaultDevice device = devices.get(deviceId);
72 if (device == null) {
73 return createDevice(providerId, deviceId, deviceDescription);
74 }
75 return updateDevice(providerId, device, deviceDescription);
76 }
77
78 // Creates the device and returns the appropriate event if necessary.
79 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
80 DeviceDescription desc) {
81 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
82 desc.manufacturer(),
83 desc.hwVersion(), desc.swVersion(),
84 desc.serialNumber());
85 synchronized (this) {
86 devices.put(deviceId, device);
tom249829a2014-09-04 15:28:04 -070087 availableDevices.add(deviceId);
tome5ec3fd2014-09-04 15:18:06 -070088 }
tom29df6f42014-09-05 08:14:14 -070089 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
tome5ec3fd2014-09-04 15:18:06 -070090 }
91
92 // Updates the device and returns the appropriate event if necessary.
93 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
94 DeviceDescription desc) {
95 // We allow only certain attributes to trigger update
96 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
97 !Objects.equals(device.swVersion(), desc.swVersion())) {
98 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
99 desc.type(),
100 desc.manufacturer(),
101 desc.hwVersion(),
102 desc.swVersion(),
103 desc.serialNumber());
104 synchronized (this) {
105 devices.put(device.id(), updated);
tom249829a2014-09-04 15:28:04 -0700106 availableDevices.add(device.id());
tome5ec3fd2014-09-04 15:18:06 -0700107 }
tom29df6f42014-09-05 08:14:14 -0700108 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700109 }
110
111 // Otherwise merely attempt to change availability
112 synchronized (this) {
tom249829a2014-09-04 15:28:04 -0700113 boolean added = availableDevices.add(device.id());
tom29df6f42014-09-05 08:14:14 -0700114 return !added ? null :
115 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700116 }
117 }
118
119 /**
120 * Removes the specified infrastructure device.
121 *
122 * @param deviceId device identifier
123 * @return ready to send event describing what occurred; null if no change
124 */
125 DeviceEvent markOffline(DeviceId deviceId) {
126 synchronized (this) {
127 Device device = devices.get(deviceId);
tom29df6f42014-09-05 08:14:14 -0700128 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
tom249829a2014-09-04 15:28:04 -0700129 boolean removed = availableDevices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700130 return !removed ? null :
131 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700132 }
133 }
134
135 /**
136 * Updates the ports of the specified infrastructure device using the given
137 * list of port descriptions. The list is assumed to be comprehensive.
138 *
139 * @param deviceId device identifier
140 * @param portDescriptions list of port descriptions
141 * @return ready to send events describing what occurred; empty list if no change
142 */
143 List<DeviceEvent> updatePorts(DeviceId deviceId,
144 List<PortDescription> portDescriptions) {
tom29df6f42014-09-05 08:14:14 -0700145 List<DeviceEvent> events = new ArrayList<>();
146 synchronized (this) {
147 Device device = devices.get(deviceId);
148 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
149 Map<PortNumber, Port> ports = getPortMap(deviceId);
150
151 // Add new ports
152 Set<PortNumber> processed = new HashSet<>();
153 for (PortDescription portDescription : portDescriptions) {
154 Port port = ports.get(portDescription.portNumber());
155 DeviceEvent event = port == null ?
156 createPort(device, portDescription, ports) :
157 updatePort(device, port, portDescription, ports);
158 processed.add(portDescription.portNumber());
159 }
160
161 events.addAll(pruneOldPorts(device, ports, processed));
162 }
163 return events;
164 }
165
166 // Creates a new port based on the port description adds it to the map and
167 // Returns corresponding event.
168 private DeviceEvent createPort(Device device, PortDescription portDescription,
169 Map<PortNumber, Port> ports) {
170 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
171 portDescription.isEnabled());
172 ports.put(port.number(), port);
173 return new DeviceEvent(PORT_ADDED, device, port);
174 }
175
176 // CHecks if the specified port requires update and if so, it replaces the
177 // existing entry in the map and returns corresponding event.
178 private DeviceEvent updatePort(Device device, Port port,
179 PortDescription portDescription,
180 Map<PortNumber, Port> ports) {
181 if (port.isEnabled() != portDescription.isEnabled()) {
182 DefaultPort updatedPort =
183 new DefaultPort(device, portDescription.portNumber(),
184 portDescription.isEnabled());
185 ports.put(port.number(), updatedPort);
186 return new DeviceEvent(PORT_UPDATED, device, port);
187 }
188 return null;
189 }
190
191 // Prunes the specified list of ports based on which ports are in the
192 // processed list and returns list of corresponding events.
193 private List<DeviceEvent> pruneOldPorts(Device device,
194 Map<PortNumber, Port> ports,
195 Set<PortNumber> processed) {
196 List<DeviceEvent> events = new ArrayList<>();
197 Iterator<PortNumber> iterator = ports.keySet().iterator();
198 while (iterator.hasNext()) {
199 PortNumber portNumber = iterator.next();
200 if (processed.contains(portNumber)) {
201 events.add(new DeviceEvent(PORT_REMOVED, device,
202 ports.get(portNumber)));
203 iterator.remove();
204 }
205 }
206 return events;
207 }
208
209 // Gets the map of ports for the specified device; if one does not already
210 // exist, it creates and registers a new one.
211 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
212 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
213 if (ports == null) {
214 ports = new HashMap<>();
215 devicePorts.put(deviceId, ports);
216 }
217 return ports;
tome5ec3fd2014-09-04 15:18:06 -0700218 }
219
220 /**
221 * Updates the port status of the specified infrastructure device using the
222 * given port description.
223 *
224 * @param deviceId device identifier
225 * @param portDescription port description
226 * @return ready to send event describing what occurred; null if no change
227 */
228 DeviceEvent updatePortStatus(DeviceId deviceId,
229 PortDescription portDescription) {
230 return null;
231 }
232
233 /**
234 * Returns the list of ports that belong to the specified device.
235 *
236 * @param deviceId device identifier
237 * @return list of device ports
238 */
239 List<Port> getPorts(DeviceId deviceId) {
240 return null;
241 }
242
243 /**
244 * Returns the specified device port.
245 *
246 * @param deviceId device identifier
247 * @param portNumber port number
248 * @return device port
249 */
250 Port getPort(DeviceId deviceId, PortNumber portNumber) {
251 return null;
252 }
253
254 /**
255 * Returns the mastership role determined for this device.
256 *
257 * @param deviceId device identifier
258 * @return mastership role
259 */
260 MastershipRole getRole(DeviceId deviceId) {
261 MastershipRole role = roles.get(deviceId);
262 return role != null ? role : MastershipRole.NONE;
263 }
264
265 /**
266 * Administratively sets the role of the specified device.
267 *
268 * @param deviceId device identifier
269 * @param role mastership role to apply
270 * @return mastership role change event or null if no change
271 */
272 DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
tom249829a2014-09-04 15:28:04 -0700273 synchronized (this) {
274 Device device = getDevice(deviceId);
tom29df6f42014-09-05 08:14:14 -0700275 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
tom249829a2014-09-04 15:28:04 -0700276 MastershipRole oldRole = roles.put(deviceId, role);
tom29df6f42014-09-05 08:14:14 -0700277 return oldRole == role ? null :
278 new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null);
tom249829a2014-09-04 15:28:04 -0700279 }
tome5ec3fd2014-09-04 15:18:06 -0700280 }
281
282 /**
283 * Administratively removes the specified device from the store.
284 *
285 * @param deviceId device to be removed
286 */
287 DeviceEvent removeDevice(DeviceId deviceId) {
288 synchronized (this) {
289 roles.remove(deviceId);
290 Device device = devices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700291 return device == null ? null :
292 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700293 }
294 }
tom29df6f42014-09-05 08:14:14 -0700295
tome5ec3fd2014-09-04 15:18:06 -0700296}