blob: ae3bc5aacaf87f675de9e59584ec02240dfc826c [file] [log] [blame]
tom202175a2014-09-19 19:00:11 -07001package org.onlab.onos.net.trivial.impl;
tome5ec3fd2014-09-04 15:18:06 -07002
Yuta HIGUCHIf5479702014-09-25 00:09:24 -07003import com.google.common.collect.FluentIterable;
tom46a220d2014-09-05 08:25:56 -07004import com.google.common.collect.ImmutableList;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -07005
tom41a2c5f2014-09-19 09:20:35 -07006import org.apache.felix.scr.annotations.Activate;
7import org.apache.felix.scr.annotations.Component;
8import org.apache.felix.scr.annotations.Deactivate;
9import org.apache.felix.scr.annotations.Service;
tome5ec3fd2014-09-04 15:18:06 -070010import org.onlab.onos.net.DefaultDevice;
tom29df6f42014-09-05 08:14:14 -070011import org.onlab.onos.net.DefaultPort;
tome5ec3fd2014-09-04 15:18:06 -070012import org.onlab.onos.net.Device;
13import org.onlab.onos.net.DeviceId;
14import org.onlab.onos.net.MastershipRole;
15import org.onlab.onos.net.Port;
16import org.onlab.onos.net.PortNumber;
17import org.onlab.onos.net.device.DeviceDescription;
18import org.onlab.onos.net.device.DeviceEvent;
tom41a2c5f2014-09-19 09:20:35 -070019import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070020import org.onlab.onos.net.device.DeviceStoreDelegate;
tome5ec3fd2014-09-04 15:18:06 -070021import org.onlab.onos.net.device.PortDescription;
22import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070023import org.onlab.onos.store.AbstractStore;
tom41a2c5f2014-09-19 09:20:35 -070024import org.slf4j.Logger;
tome5ec3fd2014-09-04 15:18:06 -070025
26import java.util.ArrayList;
27import java.util.Collections;
tom29df6f42014-09-05 08:14:14 -070028import java.util.HashMap;
tome5ec3fd2014-09-04 15:18:06 -070029import java.util.HashSet;
tom29df6f42014-09-05 08:14:14 -070030import java.util.Iterator;
tome5ec3fd2014-09-04 15:18:06 -070031import java.util.List;
32import java.util.Map;
33import java.util.Objects;
34import java.util.Set;
35import java.util.concurrent.ConcurrentHashMap;
36
37import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHIf5479702014-09-25 00:09:24 -070038import static com.google.common.base.Predicates.notNull;
tom29df6f42014-09-05 08:14:14 -070039import static org.onlab.onos.net.device.DeviceEvent.Type.*;
tom41a2c5f2014-09-19 09:20:35 -070040import static org.slf4j.LoggerFactory.getLogger;
tome5ec3fd2014-09-04 15:18:06 -070041
42/**
tome4729872014-09-23 00:37:37 -070043 * Manages inventory of infrastructure devices using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070044 * structures implementation.
tome5ec3fd2014-09-04 15:18:06 -070045 */
tom41a2c5f2014-09-19 09:20:35 -070046@Component(immediate = true)
47@Service
tomf80c9722014-09-24 14:49:18 -070048public class SimpleDeviceStore
49 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
50 implements DeviceStore {
tom41a2c5f2014-09-19 09:20:35 -070051
52 private final Logger log = getLogger(getClass());
tome5ec3fd2014-09-04 15:18:06 -070053
tom29df6f42014-09-05 08:14:14 -070054 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
55
tome5ec3fd2014-09-04 15:18:06 -070056 private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>();
tom249829a2014-09-04 15:28:04 -070057 private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>();
58 private final Set<DeviceId> availableDevices = new HashSet<>();
tom29df6f42014-09-05 08:14:14 -070059 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
tome5ec3fd2014-09-04 15:18:06 -070060
tom41a2c5f2014-09-19 09:20:35 -070061 @Activate
62 public void activate() {
63 log.info("Started");
64 }
65
66 @Deactivate
67 public void deactivate() {
68 log.info("Stopped");
69 }
tom5bcc9462014-09-19 10:11:31 -070070
tom41a2c5f2014-09-19 09:20:35 -070071 @Override
72 public int getDeviceCount() {
tomad2d2092014-09-06 23:24:20 -070073 return devices.size();
74 }
75
tom41a2c5f2014-09-19 09:20:35 -070076 @Override
77 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -070078 return Collections.unmodifiableSet(new HashSet<Device>(devices.values()));
79 }
80
tom41a2c5f2014-09-19 09:20:35 -070081 @Override
82 public Device getDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -070083 return devices.get(deviceId);
84 }
85
tom41a2c5f2014-09-19 09:20:35 -070086 @Override
87 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -070088 DeviceDescription deviceDescription) {
89 DefaultDevice device = devices.get(deviceId);
90 if (device == null) {
91 return createDevice(providerId, deviceId, deviceDescription);
92 }
93 return updateDevice(providerId, device, deviceDescription);
94 }
95
96 // Creates the device and returns the appropriate event if necessary.
97 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
98 DeviceDescription desc) {
99 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
100 desc.manufacturer(),
101 desc.hwVersion(), desc.swVersion(),
102 desc.serialNumber());
103 synchronized (this) {
104 devices.put(deviceId, device);
tom249829a2014-09-04 15:28:04 -0700105 availableDevices.add(deviceId);
tom80c0e5e2014-09-08 18:08:58 -0700106
107 // For now claim the device as a master automatically.
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700108 // roles.put(deviceId, MastershipRole.MASTER);
tome5ec3fd2014-09-04 15:18:06 -0700109 }
tom29df6f42014-09-05 08:14:14 -0700110 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700111 }
112
113 // Updates the device and returns the appropriate event if necessary.
114 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
115 DeviceDescription desc) {
116 // We allow only certain attributes to trigger update
117 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
118 !Objects.equals(device.swVersion(), desc.swVersion())) {
119 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
120 desc.type(),
121 desc.manufacturer(),
122 desc.hwVersion(),
123 desc.swVersion(),
124 desc.serialNumber());
125 synchronized (this) {
126 devices.put(device.id(), updated);
tom249829a2014-09-04 15:28:04 -0700127 availableDevices.add(device.id());
tome5ec3fd2014-09-04 15:18:06 -0700128 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700129 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, updated, null);
tome5ec3fd2014-09-04 15:18:06 -0700130 }
131
132 // Otherwise merely attempt to change availability
133 synchronized (this) {
tom249829a2014-09-04 15:28:04 -0700134 boolean added = availableDevices.add(device.id());
tom29df6f42014-09-05 08:14:14 -0700135 return !added ? null :
136 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700137 }
138 }
139
tom41a2c5f2014-09-19 09:20:35 -0700140 @Override
141 public DeviceEvent markOffline(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700142 synchronized (this) {
143 Device device = devices.get(deviceId);
tom89b63c52014-09-16 09:19:51 -0700144 boolean removed = device != null && availableDevices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700145 return !removed ? null :
146 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700147 }
148 }
149
tom41a2c5f2014-09-19 09:20:35 -0700150 @Override
151 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700152 List<PortDescription> portDescriptions) {
tom29df6f42014-09-05 08:14:14 -0700153 List<DeviceEvent> events = new ArrayList<>();
154 synchronized (this) {
155 Device device = devices.get(deviceId);
156 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
157 Map<PortNumber, Port> ports = getPortMap(deviceId);
158
159 // Add new ports
160 Set<PortNumber> processed = new HashSet<>();
161 for (PortDescription portDescription : portDescriptions) {
162 Port port = ports.get(portDescription.portNumber());
tom24c55cd2014-09-06 10:47:25 -0700163 events.add(port == null ?
164 createPort(device, portDescription, ports) :
165 updatePort(device, port, portDescription, ports));
tom29df6f42014-09-05 08:14:14 -0700166 processed.add(portDescription.portNumber());
167 }
168
169 events.addAll(pruneOldPorts(device, ports, processed));
170 }
Yuta HIGUCHIf5479702014-09-25 00:09:24 -0700171 return FluentIterable.from(events).filter(notNull()).toList();
tom29df6f42014-09-05 08:14:14 -0700172 }
173
174 // Creates a new port based on the port description adds it to the map and
175 // Returns corresponding event.
176 private DeviceEvent createPort(Device device, PortDescription portDescription,
177 Map<PortNumber, Port> ports) {
178 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
179 portDescription.isEnabled());
180 ports.put(port.number(), port);
181 return new DeviceEvent(PORT_ADDED, device, port);
182 }
183
184 // CHecks if the specified port requires update and if so, it replaces the
185 // existing entry in the map and returns corresponding event.
186 private DeviceEvent updatePort(Device device, Port port,
187 PortDescription portDescription,
188 Map<PortNumber, Port> ports) {
189 if (port.isEnabled() != portDescription.isEnabled()) {
190 DefaultPort updatedPort =
191 new DefaultPort(device, portDescription.portNumber(),
192 portDescription.isEnabled());
193 ports.put(port.number(), updatedPort);
194 return new DeviceEvent(PORT_UPDATED, device, port);
195 }
196 return null;
197 }
198
199 // Prunes the specified list of ports based on which ports are in the
200 // processed list and returns list of corresponding events.
201 private List<DeviceEvent> pruneOldPorts(Device device,
202 Map<PortNumber, Port> ports,
203 Set<PortNumber> processed) {
204 List<DeviceEvent> events = new ArrayList<>();
205 Iterator<PortNumber> iterator = ports.keySet().iterator();
206 while (iterator.hasNext()) {
207 PortNumber portNumber = iterator.next();
tom24c55cd2014-09-06 10:47:25 -0700208 if (!processed.contains(portNumber)) {
tom29df6f42014-09-05 08:14:14 -0700209 events.add(new DeviceEvent(PORT_REMOVED, device,
210 ports.get(portNumber)));
211 iterator.remove();
212 }
213 }
214 return events;
215 }
216
217 // Gets the map of ports for the specified device; if one does not already
218 // exist, it creates and registers a new one.
219 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
220 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
221 if (ports == null) {
222 ports = new HashMap<>();
223 devicePorts.put(deviceId, ports);
224 }
225 return ports;
tome5ec3fd2014-09-04 15:18:06 -0700226 }
227
tom41a2c5f2014-09-19 09:20:35 -0700228 @Override
229 public DeviceEvent updatePortStatus(DeviceId deviceId,
tome5ec3fd2014-09-04 15:18:06 -0700230 PortDescription portDescription) {
tom46a220d2014-09-05 08:25:56 -0700231 synchronized (this) {
232 Device device = devices.get(deviceId);
233 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
234 Map<PortNumber, Port> ports = getPortMap(deviceId);
235 Port port = ports.get(portDescription.portNumber());
236 return updatePort(device, port, portDescription, ports);
237 }
tome5ec3fd2014-09-04 15:18:06 -0700238 }
239
tom41a2c5f2014-09-19 09:20:35 -0700240 @Override
241 public List<Port> getPorts(DeviceId deviceId) {
tom46a220d2014-09-05 08:25:56 -0700242 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
243 return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values());
tome5ec3fd2014-09-04 15:18:06 -0700244 }
245
tom41a2c5f2014-09-19 09:20:35 -0700246 @Override
247 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
tom46a220d2014-09-05 08:25:56 -0700248 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
249 return ports == null ? null : ports.get(portNumber);
tome5ec3fd2014-09-04 15:18:06 -0700250 }
251
tom41a2c5f2014-09-19 09:20:35 -0700252 @Override
253 public boolean isAvailable(DeviceId deviceId) {
tomff7eb7c2014-09-08 12:49:03 -0700254 return availableDevices.contains(deviceId);
255 }
256
tom41a2c5f2014-09-19 09:20:35 -0700257 @Override
tom41a2c5f2014-09-19 09:20:35 -0700258 public DeviceEvent removeDevice(DeviceId deviceId) {
tome5ec3fd2014-09-04 15:18:06 -0700259 synchronized (this) {
260 roles.remove(deviceId);
261 Device device = devices.remove(deviceId);
tom29df6f42014-09-05 08:14:14 -0700262 return device == null ? null :
263 new DeviceEvent(DEVICE_REMOVED, device, null);
tome5ec3fd2014-09-04 15:18:06 -0700264 }
265 }
tome5ec3fd2014-09-04 15:18:06 -0700266}