blob: 8e42ffb52ef6084d09a63588025aebfd62916d0e [file] [log] [blame]
tom202175a2014-09-19 19:00:11 -07001package org.onlab.onos.net.trivial.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/**
tome4729872014-09-23 00:37:37 -070038 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070039 * 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 }
tom5bcc9462014-09-19 10:11:31 -070063
tom41a2c5f2014-09-19 09:20:35 -070064 @Override
65 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -070066 return devices.size();
67 }
68
tom41a2c5f2014-09-19 09:20:35 -070069 @Override
70 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -070071 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
72 }
73
tom41a2c5f2014-09-19 09:20:35 -070074 @Override
75 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -070076 return devices.get(deviceId);
77 }
78
tom41a2c5f2014-09-19 09:20:35 -070079 @Override
80 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -070081 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);
tom80c0e5e2014-09-08 18:08:58 -070099
100 // For now claim the device as a master automatically.
101 roles.put(deviceId, MastershipRole.MASTER);
tome5ec3fd2014-09-04 15:18:06 -0700102 }
tom29df6f42014-09-05 08:14:14 -0700103 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700104 }
105
106 // Updates the device and returns the appropriate event if necessary.
107 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
108 DeviceDescription desc) {
109 // We allow only certain attributes to trigger update
110 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
111 !Objects.equals(device.swVersion(), desc.swVersion())) {
112 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
113 desc.type(),
114 desc.manufacturer(),
115 desc.hwVersion(),
116 desc.swVersion(),
117 desc.serialNumber());
118 synchronized (this) {
119 devices.put(device.id(), updated);
tom249829a2014-09-04 15:28:04 -0700120 availableDevices.add(device.id());
tome5ec3fd2014-09-04 15:18:06 -0700121 }
tom29df6f42014-09-05 08:14:14 -0700122 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700123 }
124
125 // Otherwise merely attempt to change availability
126 synchronized (this) {
tom249829a2014-09-04 15:28:04 -0700127 boolean added = availableDevices.add(device.id());
tom29df6f42014-09-05 08:14:14 -0700128 return !added ? null :
129 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700130 }
131 }
132
tom41a2c5f2014-09-19 09:20:35 -0700133 @Override
134 public DeviceEvent markOffline(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700135 synchronized (this) {
136 Device device = devices.get(deviceId);
tom89b63c52014-09-16 09:19:51 -0700137 boolean removed = device != null && availableDevices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700138 return !removed ? null :
139 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700140 }
141 }
142
tom41a2c5f2014-09-19 09:20:35 -0700143 @Override
144 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700145 List<PortDescription> portDescriptions) {
tom29df6f42014-09-05 08:14:14 -0700146 List<DeviceEvent> events = new ArrayList<>();
147 synchronized (this) {
148 Device device = devices.get(deviceId);
149 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
150 Map<PortNumber, Port> ports = getPortMap(deviceId);
151
152 // Add new ports
153 Set<PortNumber> processed = new HashSet<>();
154 for (PortDescription portDescription : portDescriptions) {
155 Port port = ports.get(portDescription.portNumber());
tom24c55cd2014-09-06 10:47:25 -0700156 events.add(port == null ?
157 createPort(device, portDescription, ports) :
158 updatePort(device, port, portDescription, ports));
tom29df6f42014-09-05 08:14:14 -0700159 processed.add(portDescription.portNumber());
160 }
161
162 events.addAll(pruneOldPorts(device, ports, processed));
163 }
164 return events;
165 }
166
167 // Creates a new port based on the port description adds it to the map and
168 // Returns corresponding event.
169 private DeviceEvent createPort(Device device, PortDescription portDescription,
170 Map<PortNumber, Port> ports) {
171 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
172 portDescription.isEnabled());
173 ports.put(port.number(), port);
174 return new DeviceEvent(PORT_ADDED, device, port);
175 }
176
177 // CHecks if the specified port requires update and if so, it replaces the
178 // existing entry in the map and returns corresponding event.
179 private DeviceEvent updatePort(Device device, Port port,
180 PortDescription portDescription,
181 Map<PortNumber, Port> ports) {
182 if (port.isEnabled() != portDescription.isEnabled()) {
183 DefaultPort updatedPort =
184 new DefaultPort(device, portDescription.portNumber(),
185 portDescription.isEnabled());
186 ports.put(port.number(), updatedPort);
187 return new DeviceEvent(PORT_UPDATED, device, port);
188 }
189 return null;
190 }
191
192 // Prunes the specified list of ports based on which ports are in the
193 // processed list and returns list of corresponding events.
194 private List<DeviceEvent> pruneOldPorts(Device device,
195 Map<PortNumber, Port> ports,
196 Set<PortNumber> processed) {
197 List<DeviceEvent> events = new ArrayList<>();
198 Iterator<PortNumber> iterator = ports.keySet().iterator();
199 while (iterator.hasNext()) {
200 PortNumber portNumber = iterator.next();
tom24c55cd2014-09-06 10:47:25 -0700201 if (!processed.contains(portNumber)) {
tom29df6f42014-09-05 08:14:14 -0700202 events.add(new DeviceEvent(PORT_REMOVED, device,
203 ports.get(portNumber)));
204 iterator.remove();
205 }
206 }
207 return events;
208 }
209
210 // Gets the map of ports for the specified device; if one does not already
211 // exist, it creates and registers a new one.
212 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
213 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
214 if (ports == null) {
215 ports = new HashMap<>();
216 devicePorts.put(deviceId, ports);
217 }
218 return ports;
tome5ec3fd2014-09-04 15:18:06 -0700219 }
220
tom41a2c5f2014-09-19 09:20:35 -0700221 @Override
222 public DeviceEvent updatePortStatus(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700223 PortDescription portDescription) {
tom46a220d2014-09-05 08:25:56 -0700224 synchronized (this) {
225 Device device = devices.get(deviceId);
226 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
227 Map<PortNumber, Port> ports = getPortMap(deviceId);
228 Port port = ports.get(portDescription.portNumber());
229 return updatePort(device, port, portDescription, ports);
230 }
tome5ec3fd2014-09-04 15:18:06 -0700231 }
232
tom41a2c5f2014-09-19 09:20:35 -0700233 @Override
234 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700235 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
236 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700237 }
238
tom41a2c5f2014-09-19 09:20:35 -0700239 @Override
240 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700241 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
242 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700243 }
244
tom41a2c5f2014-09-19 09:20:35 -0700245 @Override
246 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700247 return availableDevices.contains(deviceId);
248 }
249
tom41a2c5f2014-09-19 09:20:35 -0700250 @Override
251 public MastershipRole getRole(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700252 MastershipRole role = roles.get(deviceId);
253 return role != null ? role : MastershipRole.NONE;
254 }
255
tom41a2c5f2014-09-19 09:20:35 -0700256 @Override
257 public DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
tom249829a2014-09-04 15:28:04 -0700258 synchronized (this) {
259 Device device = getDevice(deviceId);
tom29df6f42014-09-05 08:14:14 -0700260 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
tom249829a2014-09-04 15:28:04 -0700261 MastershipRole oldRole = roles.put(deviceId, role);
tom29df6f42014-09-05 08:14:14 -0700262 return oldRole == role ? null :
263 new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null);
tom249829a2014-09-04 15:28:04 -0700264 }
tome5ec3fd2014-09-04 15:18:06 -0700265 }
266
tom41a2c5f2014-09-19 09:20:35 -0700267 @Override
268 public DeviceEvent removeDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700269 synchronized (this) {
270 roles.remove(deviceId);
271 Device device = devices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700272 return device == null ? null :
273 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700274 }
275 }
tome5ec3fd2014-09-04 15:18:06 -0700276}