blob: 9b78798e7cef011aa0cfedf42d4f74bf89ff5c49 [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;
tomf80c9722014-09-24 14:49:18 -070018import org.onlab.onos.net.device.DeviceStoreDelegate;
tome5ec3fd2014-09-04 15:18:06 -070019import org.onlab.onos.net.device.PortDescription;
20import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070021import org.onlab.onos.store.AbstractStore;
tom41a2c5f2014-09-19 09:20:35 -070022import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070023
24import java.util.ArrayList;
25import java.util.Collections;
tom29df6f42014-09-05 08:14:14 -070026import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070027import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070028import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070029import java.util.List;
30import java.util.Map;
31import java.util.Objects;
32import java.util.Set;
33import java.util.concurrent.ConcurrentHashMap;
34
35import static com.google.common.base.Preconditions.checkArgument;
tom29df6f42014-09-05 08:14:14 -070036import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070037import static org.slf4j.LoggerFactory.getLogger;
tome5ec3fd2014-09-04 15:18:06 -070038
39/**
tome4729872014-09-23 00:37:37 -070040 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070041 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070042 */
tom41a2c5f2014-09-19 09:20:35 -070043@Component(immediate = true)
44@Service
tomf80c9722014-09-24 14:49:18 -070045public class SimpleDeviceStore
46 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
47 implements DeviceStore {
tom41a2c5f2014-09-19 09:20:35 -070048
49 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070050
tom29df6f42014-09-05 08:14:14 -070051 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
52
tome5ec3fd2014-09-04 15:18:06 -070053 private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>();
tom249829a2014-09-04 15:28:04 -070054 private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>();
55 private final Set<DeviceId> availableDevices = new HashSet<>();
tom29df6f42014-09-05 08:14:14 -070056 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
tome5ec3fd2014-09-04 15:18:06 -070057
tom41a2c5f2014-09-19 09:20:35 -070058 @Activate
59 public void activate() {
60 log.info("Started");
61 }
62
63 @Deactivate
64 public void deactivate() {
65 log.info("Stopped");
66 }
tom5bcc9462014-09-19 10:11:31 -070067
tom41a2c5f2014-09-19 09:20:35 -070068 @Override
69 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -070070 return devices.size();
71 }
72
tom41a2c5f2014-09-19 09:20:35 -070073 @Override
74 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -070075 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
76 }
77
tom41a2c5f2014-09-19 09:20:35 -070078 @Override
79 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -070080 return devices.get(deviceId);
81 }
82
tom41a2c5f2014-09-19 09:20:35 -070083 @Override
84 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -070085 DeviceDescription deviceDescription) {
86 DefaultDevice device = devices.get(deviceId);
87 if (device == null) {
88 return createDevice(providerId, deviceId, deviceDescription);
89 }
90 return updateDevice(providerId, device, deviceDescription);
91 }
92
93 // Creates the device and returns the appropriate event if necessary.
94 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
95 DeviceDescription desc) {
96 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
97 desc.manufacturer(),
98 desc.hwVersion(), desc.swVersion(),
99 desc.serialNumber());
100 synchronized (this) {
101 devices.put(deviceId, device);
tom249829a2014-09-04 15:28:04 -0700102 availableDevices.add(deviceId);
tom80c0e5e2014-09-08 18:08:58 -0700103
104 // For now claim the device as a master automatically.
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700105 // roles.put(deviceId, MastershipRole.MASTER);
tome5ec3fd2014-09-04 15:18:06 -0700106 }
tom29df6f42014-09-05 08:14:14 -0700107 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700108 }
109
110 // Updates the device and returns the appropriate event if necessary.
111 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
112 DeviceDescription desc) {
113 // We allow only certain attributes to trigger update
114 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
115 !Objects.equals(device.swVersion(), desc.swVersion())) {
116 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
117 desc.type(),
118 desc.manufacturer(),
119 desc.hwVersion(),
120 desc.swVersion(),
121 desc.serialNumber());
122 synchronized (this) {
123 devices.put(device.id(), updated);
tom249829a2014-09-04 15:28:04 -0700124 availableDevices.add(device.id());
tome5ec3fd2014-09-04 15:18:06 -0700125 }
tom29df6f42014-09-05 08:14:14 -0700126 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700127 }
128
129 // Otherwise merely attempt to change availability
130 synchronized (this) {
tom249829a2014-09-04 15:28:04 -0700131 boolean added = availableDevices.add(device.id());
tom29df6f42014-09-05 08:14:14 -0700132 return !added ? null :
133 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700134 }
135 }
136
tom41a2c5f2014-09-19 09:20:35 -0700137 @Override
138 public DeviceEvent markOffline(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700139 synchronized (this) {
140 Device device = devices.get(deviceId);
tom89b63c52014-09-16 09:19:51 -0700141 boolean removed = device != null && availableDevices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700142 return !removed ? null :
143 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700144 }
145 }
146
tom41a2c5f2014-09-19 09:20:35 -0700147 @Override
148 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700149 List<PortDescription> portDescriptions) {
tom29df6f42014-09-05 08:14:14 -0700150 List<DeviceEvent> events = new ArrayList<>();
151 synchronized (this) {
152 Device device = devices.get(deviceId);
153 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
154 Map<PortNumber, Port> ports = getPortMap(deviceId);
155
156 // Add new ports
157 Set<PortNumber> processed = new HashSet<>();
158 for (PortDescription portDescription : portDescriptions) {
159 Port port = ports.get(portDescription.portNumber());
tom24c55cd2014-09-06 10:47:25 -0700160 events.add(port == null ?
161 createPort(device, portDescription, ports) :
162 updatePort(device, port, portDescription, ports));
tom29df6f42014-09-05 08:14:14 -0700163 processed.add(portDescription.portNumber());
164 }
165
166 events.addAll(pruneOldPorts(device, ports, processed));
167 }
168 return events;
169 }
170
171 // Creates a new port based on the port description adds it to the map and
172 // Returns corresponding event.
173 private DeviceEvent createPort(Device device, PortDescription portDescription,
174 Map<PortNumber, Port> ports) {
175 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
176 portDescription.isEnabled());
177 ports.put(port.number(), port);
178 return new DeviceEvent(PORT_ADDED, device, port);
179 }
180
181 // CHecks if the specified port requires update and if so, it replaces the
182 // existing entry in the map and returns corresponding event.
183 private DeviceEvent updatePort(Device device, Port port,
184 PortDescription portDescription,
185 Map<PortNumber, Port> ports) {
186 if (port.isEnabled() != portDescription.isEnabled()) {
187 DefaultPort updatedPort =
188 new DefaultPort(device, portDescription.portNumber(),
189 portDescription.isEnabled());
190 ports.put(port.number(), updatedPort);
191 return new DeviceEvent(PORT_UPDATED, device, port);
192 }
193 return null;
194 }
195
196 // Prunes the specified list of ports based on which ports are in the
197 // processed list and returns list of corresponding events.
198 private List<DeviceEvent> pruneOldPorts(Device device,
199 Map<PortNumber, Port> ports,
200 Set<PortNumber> processed) {
201 List<DeviceEvent> events = new ArrayList<>();
202 Iterator<PortNumber> iterator = ports.keySet().iterator();
203 while (iterator.hasNext()) {
204 PortNumber portNumber = iterator.next();
tom24c55cd2014-09-06 10:47:25 -0700205 if (!processed.contains(portNumber)) {
tom29df6f42014-09-05 08:14:14 -0700206 events.add(new DeviceEvent(PORT_REMOVED, device,
207 ports.get(portNumber)));
208 iterator.remove();
209 }
210 }
211 return events;
212 }
213
214 // Gets the map of ports for the specified device; if one does not already
215 // exist, it creates and registers a new one.
216 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
217 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
218 if (ports == null) {
219 ports = new HashMap<>();
220 devicePorts.put(deviceId, ports);
221 }
222 return ports;
tome5ec3fd2014-09-04 15:18:06 -0700223 }
224
tom41a2c5f2014-09-19 09:20:35 -0700225 @Override
226 public DeviceEvent updatePortStatus(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700227 PortDescription portDescription) {
tom46a220d2014-09-05 08:25:56 -0700228 synchronized (this) {
229 Device device = devices.get(deviceId);
230 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
231 Map<PortNumber, Port> ports = getPortMap(deviceId);
232 Port port = ports.get(portDescription.portNumber());
233 return updatePort(device, port, portDescription, ports);
234 }
tome5ec3fd2014-09-04 15:18:06 -0700235 }
236
tom41a2c5f2014-09-19 09:20:35 -0700237 @Override
238 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700239 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
240 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700241 }
242
tom41a2c5f2014-09-19 09:20:35 -0700243 @Override
244 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700245 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
246 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700247 }
248
tom41a2c5f2014-09-19 09:20:35 -0700249 @Override
250 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700251 return availableDevices.contains(deviceId);
252 }
253
tom41a2c5f2014-09-19 09:20:35 -0700254 @Override
tom41a2c5f2014-09-19 09:20:35 -0700255 public DeviceEvent removeDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700256 synchronized (this) {
257 roles.remove(deviceId);
258 Device device = devices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700259 return device == null ? null :
260 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700261 }
262 }
tome5ec3fd2014-09-04 15:18:06 -0700263}