blob: 3fa9dc20f07447ad17fab0149b4b63a9833db83e [file] [log] [blame]
tom8bf2e6b2014-09-10 20:53:54 -07001package org.onlab.onos.net.trivial.device.impl;
tome5ec3fd2014-09-04 15:18:06 -07002
tom46a220d2014-09-05 08:25:56 -07003import com.google.common.collect.ImmutableList;
tom41a2c5f2014-09-19 09:20:35 -07004import org.apache.felix.scr.annotations.Activate;
5import org.apache.felix.scr.annotations.Component;
6import org.apache.felix.scr.annotations.Deactivate;
7import org.apache.felix.scr.annotations.Service;
tome5ec3fd2014-09-04 15:18:06 -07008import org.onlab.onos.net.DefaultDevice;
tom29df6f42014-09-05 08:14:14 -07009import org.onlab.onos.net.DefaultPort;
tome5ec3fd2014-09-04 15:18:06 -070010import org.onlab.onos.net.Device;
11import org.onlab.onos.net.DeviceId;
12import org.onlab.onos.net.MastershipRole;
13import org.onlab.onos.net.Port;
14import org.onlab.onos.net.PortNumber;
15import org.onlab.onos.net.device.DeviceDescription;
16import org.onlab.onos.net.device.DeviceEvent;
tom41a2c5f2014-09-19 09:20:35 -070017import org.onlab.onos.net.device.DeviceStore;
tome5ec3fd2014-09-04 15:18:06 -070018import org.onlab.onos.net.device.PortDescription;
19import org.onlab.onos.net.provider.ProviderId;
tom41a2c5f2014-09-19 09:20:35 -070020import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070021
22import java.util.ArrayList;
23import java.util.Collections;
tom29df6f42014-09-05 08:14:14 -070024import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070025import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070026import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070027import java.util.List;
28import java.util.Map;
29import java.util.Objects;
30import java.util.Set;
31import java.util.concurrent.ConcurrentHashMap;
32
33import static com.google.common.base.Preconditions.checkArgument;
tom29df6f42014-09-05 08:14:14 -070034import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070035import static org.slf4j.LoggerFactory.getLogger;
tome5ec3fd2014-09-04 15:18:06 -070036
37/**
tomcbff9392014-09-10 00:45:23 -070038 * Manages inventory of infrastructure DEVICES using trivial in-memory
39 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070040 */
tom41a2c5f2014-09-19 09:20:35 -070041@Component(immediate = true)
42@Service
43public class SimpleDeviceStore implements DeviceStore {
44
45 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070046
tom29df6f42014-09-05 08:14:14 -070047 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
48
tome5ec3fd2014-09-04 15:18:06 -070049 private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>();
tom249829a2014-09-04 15:28:04 -070050 private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>();
51 private final Set<DeviceId> availableDevices = new HashSet<>();
tom29df6f42014-09-05 08:14:14 -070052 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
tome5ec3fd2014-09-04 15:18:06 -070053
tom41a2c5f2014-09-19 09:20:35 -070054 @Activate
55 public void activate() {
56 log.info("Started");
57 }
58
59 @Deactivate
60 public void deactivate() {
61 log.info("Stopped");
62 }
63 @Override
64 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -070065 return devices.size();
66 }
67
tom41a2c5f2014-09-19 09:20:35 -070068 @Override
69 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -070070 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
71 }
72
tom41a2c5f2014-09-19 09:20:35 -070073 @Override
74 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -070075 return devices.get(deviceId);
76 }
77
tom41a2c5f2014-09-19 09:20:35 -070078 @Override
79 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -070080 DeviceDescription deviceDescription) {
81 DefaultDevice device = devices.get(deviceId);
82 if (device == null) {
83 return createDevice(providerId, deviceId, deviceDescription);
84 }
85 return updateDevice(providerId, device, deviceDescription);
86 }
87
88 // Creates the device and returns the appropriate event if necessary.
89 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
90 DeviceDescription desc) {
91 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
92 desc.manufacturer(),
93 desc.hwVersion(), desc.swVersion(),
94 desc.serialNumber());
95 synchronized (this) {
96 devices.put(deviceId, device);
tom249829a2014-09-04 15:28:04 -070097 availableDevices.add(deviceId);
tom80c0e5e2014-09-08 18:08:58 -070098
99 // For now claim the device as a master automatically.
100 roles.put(deviceId, MastershipRole.MASTER);
tome5ec3fd2014-09-04 15:18:06 -0700101 }
tom29df6f42014-09-05 08:14:14 -0700102 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700103 }
104
105 // Updates the device and returns the appropriate event if necessary.
106 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
107 DeviceDescription desc) {
108 // We allow only certain attributes to trigger update
109 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
110 !Objects.equals(device.swVersion(), desc.swVersion())) {
111 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
112 desc.type(),
113 desc.manufacturer(),
114 desc.hwVersion(),
115 desc.swVersion(),
116 desc.serialNumber());
117 synchronized (this) {
118 devices.put(device.id(), updated);
tom249829a2014-09-04 15:28:04 -0700119 availableDevices.add(device.id());
tome5ec3fd2014-09-04 15:18:06 -0700120 }
tom29df6f42014-09-05 08:14:14 -0700121 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700122 }
123
124 // Otherwise merely attempt to change availability
125 synchronized (this) {
tom249829a2014-09-04 15:28:04 -0700126 boolean added = availableDevices.add(device.id());
tom29df6f42014-09-05 08:14:14 -0700127 return !added ? null :
128 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700129 }
130 }
131
tom41a2c5f2014-09-19 09:20:35 -0700132 @Override
133 public DeviceEvent markOffline(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700134 synchronized (this) {
135 Device device = devices.get(deviceId);
tom89b63c52014-09-16 09:19:51 -0700136 boolean removed = device != null && availableDevices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700137 return !removed ? null :
138 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700139 }
140 }
141
tom41a2c5f2014-09-19 09:20:35 -0700142 @Override
143 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700144 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());
tom24c55cd2014-09-06 10:47:25 -0700155 events.add(port == null ?
156 createPort(device, portDescription, ports) :
157 updatePort(device, port, portDescription, ports));
tom29df6f42014-09-05 08:14:14 -0700158 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();
tom24c55cd2014-09-06 10:47:25 -0700200 if (!processed.contains(portNumber)) {
tom29df6f42014-09-05 08:14:14 -0700201 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
tom41a2c5f2014-09-19 09:20:35 -0700220 @Override
221 public DeviceEvent updatePortStatus(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700222 PortDescription portDescription) {
tom46a220d2014-09-05 08:25:56 -0700223 synchronized (this) {
224 Device device = devices.get(deviceId);
225 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
226 Map<PortNumber, Port> ports = getPortMap(deviceId);
227 Port port = ports.get(portDescription.portNumber());
228 return updatePort(device, port, portDescription, ports);
229 }
tome5ec3fd2014-09-04 15:18:06 -0700230 }
231
tom41a2c5f2014-09-19 09:20:35 -0700232 @Override
233 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700234 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
235 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700236 }
237
tom41a2c5f2014-09-19 09:20:35 -0700238 @Override
239 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700240 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
241 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700242 }
243
tom41a2c5f2014-09-19 09:20:35 -0700244 @Override
245 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700246 return availableDevices.contains(deviceId);
247 }
248
tom41a2c5f2014-09-19 09:20:35 -0700249 @Override
250 public MastershipRole getRole(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700251 MastershipRole role = roles.get(deviceId);
252 return role != null ? role : MastershipRole.NONE;
253 }
254
tom41a2c5f2014-09-19 09:20:35 -0700255 @Override
256 public DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
tom249829a2014-09-04 15:28:04 -0700257 synchronized (this) {
258 Device device = getDevice(deviceId);
tom29df6f42014-09-05 08:14:14 -0700259 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
tom249829a2014-09-04 15:28:04 -0700260 MastershipRole oldRole = roles.put(deviceId, role);
tom29df6f42014-09-05 08:14:14 -0700261 return oldRole == role ? null :
262 new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null);
tom249829a2014-09-04 15:28:04 -0700263 }
tome5ec3fd2014-09-04 15:18:06 -0700264 }
265
tom41a2c5f2014-09-19 09:20:35 -0700266 @Override
267 public DeviceEvent removeDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700268 synchronized (this) {
269 roles.remove(deviceId);
270 Device device = devices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700271 return device == null ? null :
272 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700273 }
274 }
tome5ec3fd2014-09-04 15:18:06 -0700275}