tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 2 | |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 3 | import com.google.common.collect.FluentIterable; |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 4 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 5 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 6 | import org.apache.felix.scr.annotations.Activate; |
| 7 | import org.apache.felix.scr.annotations.Component; |
| 8 | import org.apache.felix.scr.annotations.Deactivate; |
| 9 | import org.apache.felix.scr.annotations.Service; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.DefaultDevice; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.DefaultPort; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.Device; |
| 13 | import org.onlab.onos.net.DeviceId; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.Port; |
| 15 | import org.onlab.onos.net.PortNumber; |
| 16 | import org.onlab.onos.net.device.DeviceDescription; |
| 17 | import org.onlab.onos.net.device.DeviceEvent; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.device.DeviceStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.device.PortDescription; |
| 21 | import org.onlab.onos.net.provider.ProviderId; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 22 | import org.onlab.onos.store.AbstractStore; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collections; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 27 | import java.util.HashMap; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 28 | import java.util.HashSet; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 29 | import java.util.Iterator; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Objects; |
| 33 | import java.util.Set; |
| 34 | import java.util.concurrent.ConcurrentHashMap; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 37 | import static com.google.common.base.Predicates.notNull; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 38 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 39 | import static org.slf4j.LoggerFactory.getLogger; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 42 | * Manages inventory of infrastructure devices using trivial in-memory |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 43 | * structures implementation. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 44 | */ |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 45 | @Component(immediate = true) |
| 46 | @Service |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 47 | public class SimpleDeviceStore |
| 48 | extends AbstractStore<DeviceEvent, DeviceStoreDelegate> |
| 49 | implements DeviceStore { |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 50 | |
| 51 | private final Logger log = getLogger(getClass()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 52 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 53 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 54 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 55 | private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>(); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 56 | private final Set<DeviceId> availableDevices = new HashSet<>(); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 57 | private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 58 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 59 | @Activate |
| 60 | public void activate() { |
| 61 | log.info("Started"); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | public void deactivate() { |
| 66 | log.info("Stopped"); |
| 67 | } |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 68 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public int getDeviceCount() { |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 71 | return devices.size(); |
| 72 | } |
| 73 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public Iterable<Device> getDevices() { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 76 | return Collections.unmodifiableSet(new HashSet<Device>(devices.values())); |
| 77 | } |
| 78 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 79 | @Override |
| 80 | public Device getDevice(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 81 | return devices.get(deviceId); |
| 82 | } |
| 83 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 86 | DeviceDescription deviceDescription) { |
| 87 | DefaultDevice device = devices.get(deviceId); |
| 88 | if (device == null) { |
| 89 | return createDevice(providerId, deviceId, deviceDescription); |
| 90 | } |
| 91 | return updateDevice(providerId, device, deviceDescription); |
| 92 | } |
| 93 | |
| 94 | // Creates the device and returns the appropriate event if necessary. |
| 95 | private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId, |
| 96 | DeviceDescription desc) { |
| 97 | DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(), |
| 98 | desc.manufacturer(), |
| 99 | desc.hwVersion(), desc.swVersion(), |
| 100 | desc.serialNumber()); |
| 101 | synchronized (this) { |
| 102 | devices.put(deviceId, device); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 103 | availableDevices.add(deviceId); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 104 | } |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 105 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Updates the device and returns the appropriate event if necessary. |
| 109 | private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device, |
| 110 | DeviceDescription desc) { |
| 111 | // We allow only certain attributes to trigger update |
| 112 | if (!Objects.equals(device.hwVersion(), desc.hwVersion()) || |
| 113 | !Objects.equals(device.swVersion(), desc.swVersion())) { |
| 114 | DefaultDevice updated = new DefaultDevice(providerId, device.id(), |
| 115 | desc.type(), |
| 116 | desc.manufacturer(), |
| 117 | desc.hwVersion(), |
| 118 | desc.swVersion(), |
| 119 | desc.serialNumber()); |
| 120 | synchronized (this) { |
| 121 | devices.put(device.id(), updated); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 122 | availableDevices.add(device.id()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 123 | } |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 124 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, updated, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Otherwise merely attempt to change availability |
| 128 | synchronized (this) { |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 129 | boolean added = availableDevices.add(device.id()); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 130 | return !added ? null : |
| 131 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public DeviceEvent markOffline(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 137 | synchronized (this) { |
| 138 | Device device = devices.get(deviceId); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 139 | boolean removed = device != null && availableDevices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 140 | return !removed ? null : |
| 141 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public List<DeviceEvent> updatePorts(DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 147 | List<PortDescription> portDescriptions) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 148 | List<DeviceEvent> events = new ArrayList<>(); |
| 149 | synchronized (this) { |
| 150 | Device device = devices.get(deviceId); |
| 151 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 152 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 153 | |
| 154 | // Add new ports |
| 155 | Set<PortNumber> processed = new HashSet<>(); |
| 156 | for (PortDescription portDescription : portDescriptions) { |
| 157 | Port port = ports.get(portDescription.portNumber()); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 158 | events.add(port == null ? |
| 159 | createPort(device, portDescription, ports) : |
| 160 | updatePort(device, port, portDescription, ports)); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 161 | processed.add(portDescription.portNumber()); |
| 162 | } |
| 163 | |
| 164 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 165 | } |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 166 | return FluentIterable.from(events).filter(notNull()).toList(); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Creates a new port based on the port description adds it to the map and |
| 170 | // Returns corresponding event. |
| 171 | private DeviceEvent createPort(Device device, PortDescription portDescription, |
| 172 | Map<PortNumber, Port> ports) { |
| 173 | DefaultPort port = new DefaultPort(device, portDescription.portNumber(), |
| 174 | portDescription.isEnabled()); |
| 175 | ports.put(port.number(), port); |
| 176 | return new DeviceEvent(PORT_ADDED, device, port); |
| 177 | } |
| 178 | |
| 179 | // CHecks if the specified port requires update and if so, it replaces the |
| 180 | // existing entry in the map and returns corresponding event. |
| 181 | private DeviceEvent updatePort(Device device, Port port, |
| 182 | PortDescription portDescription, |
| 183 | Map<PortNumber, Port> ports) { |
| 184 | if (port.isEnabled() != portDescription.isEnabled()) { |
| 185 | DefaultPort updatedPort = |
| 186 | new DefaultPort(device, portDescription.portNumber(), |
| 187 | portDescription.isEnabled()); |
| 188 | ports.put(port.number(), updatedPort); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 189 | return new DeviceEvent(PORT_UPDATED, device, updatedPort); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 190 | } |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | // Prunes the specified list of ports based on which ports are in the |
| 195 | // processed list and returns list of corresponding events. |
| 196 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 197 | Map<PortNumber, Port> ports, |
| 198 | Set<PortNumber> processed) { |
| 199 | List<DeviceEvent> events = new ArrayList<>(); |
| 200 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 201 | while (iterator.hasNext()) { |
| 202 | PortNumber portNumber = iterator.next(); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 203 | if (!processed.contains(portNumber)) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 204 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 205 | ports.get(portNumber))); |
| 206 | iterator.remove(); |
| 207 | } |
| 208 | } |
| 209 | return events; |
| 210 | } |
| 211 | |
| 212 | // Gets the map of ports for the specified device; if one does not already |
| 213 | // exist, it creates and registers a new one. |
| 214 | private Map<PortNumber, Port> getPortMap(DeviceId deviceId) { |
| 215 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 216 | if (ports == null) { |
| 217 | ports = new HashMap<>(); |
| 218 | devicePorts.put(deviceId, ports); |
| 219 | } |
| 220 | return ports; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 221 | } |
| 222 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 223 | @Override |
| 224 | public DeviceEvent updatePortStatus(DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 225 | PortDescription portDescription) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 226 | synchronized (this) { |
| 227 | Device device = devices.get(deviceId); |
| 228 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 229 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 230 | Port port = ports.get(portDescription.portNumber()); |
| 231 | return updatePort(device, port, portDescription, ports); |
| 232 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 233 | } |
| 234 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 235 | @Override |
| 236 | public List<Port> getPorts(DeviceId deviceId) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 237 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 238 | return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 239 | } |
| 240 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 241 | @Override |
| 242 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 243 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 244 | return ports == null ? null : ports.get(portNumber); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 245 | } |
| 246 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public boolean isAvailable(DeviceId deviceId) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 249 | return availableDevices.contains(deviceId); |
| 250 | } |
| 251 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 252 | @Override |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 253 | public DeviceEvent removeDevice(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 254 | synchronized (this) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 255 | Device device = devices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 256 | return device == null ? null : |
| 257 | new DeviceEvent(DEVICE_REMOVED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 260 | } |