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 | |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 3 | import com.google.common.collect.ImmutableList; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 4 | import org.apache.felix.scr.annotations.Activate; |
| 5 | import org.apache.felix.scr.annotations.Component; |
| 6 | import org.apache.felix.scr.annotations.Deactivate; |
| 7 | import org.apache.felix.scr.annotations.Service; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.DefaultDevice; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.DefaultPort; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.Device; |
| 11 | import org.onlab.onos.net.DeviceId; |
| 12 | import org.onlab.onos.net.MastershipRole; |
| 13 | import org.onlab.onos.net.Port; |
| 14 | import org.onlab.onos.net.PortNumber; |
| 15 | import org.onlab.onos.net.device.DeviceDescription; |
| 16 | import org.onlab.onos.net.device.DeviceEvent; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.device.DeviceStore; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.device.PortDescription; |
| 19 | import org.onlab.onos.net.provider.ProviderId; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 20 | import org.slf4j.Logger; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collections; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 24 | import java.util.HashMap; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 25 | import java.util.HashSet; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 26 | import java.util.Iterator; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | import java.util.Objects; |
| 30 | import java.util.Set; |
| 31 | import java.util.concurrent.ConcurrentHashMap; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 34 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 35 | import static org.slf4j.LoggerFactory.getLogger; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 38 | * Manages inventory of infrastructure devices using trivial in-memory |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 39 | * structures implementation. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 40 | */ |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class SimpleDeviceStore implements DeviceStore { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 46 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 47 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 48 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 49 | private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>(); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 50 | private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>(); |
| 51 | private final Set<DeviceId> availableDevices = new HashSet<>(); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 52 | private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 53 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 54 | @Activate |
| 55 | public void activate() { |
| 56 | log.info("Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | public void deactivate() { |
| 61 | log.info("Stopped"); |
| 62 | } |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 63 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public int getDeviceCount() { |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 66 | return devices.size(); |
| 67 | } |
| 68 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public Iterable<Device> getDevices() { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 71 | return Collections.unmodifiableSet(new HashSet<Device>(devices.values())); |
| 72 | } |
| 73 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public Device getDevice(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 76 | return devices.get(deviceId); |
| 77 | } |
| 78 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 79 | @Override |
| 80 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 81 | 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); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 98 | availableDevices.add(deviceId); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 99 | |
| 100 | // For now claim the device as a master automatically. |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 101 | // roles.put(deviceId, MastershipRole.MASTER); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 102 | } |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 103 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 104 | } |
| 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); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 120 | availableDevices.add(device.id()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 121 | } |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 122 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Otherwise merely attempt to change availability |
| 126 | synchronized (this) { |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 127 | boolean added = availableDevices.add(device.id()); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 128 | return !added ? null : |
| 129 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public DeviceEvent markOffline(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 135 | synchronized (this) { |
| 136 | Device device = devices.get(deviceId); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 137 | boolean removed = device != null && availableDevices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 138 | return !removed ? null : |
| 139 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public List<DeviceEvent> updatePorts(DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 145 | List<PortDescription> portDescriptions) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 146 | 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()); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 156 | events.add(port == null ? |
| 157 | createPort(device, portDescription, ports) : |
| 158 | updatePort(device, port, portDescription, ports)); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 159 | 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(); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 201 | if (!processed.contains(portNumber)) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 202 | 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; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 219 | } |
| 220 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public DeviceEvent updatePortStatus(DeviceId deviceId, |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 223 | PortDescription portDescription) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 224 | 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 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 231 | } |
| 232 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 233 | @Override |
| 234 | public List<Port> getPorts(DeviceId deviceId) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 235 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 236 | return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 237 | } |
| 238 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 241 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 242 | return ports == null ? null : ports.get(portNumber); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 243 | } |
| 244 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 245 | @Override |
| 246 | public boolean isAvailable(DeviceId deviceId) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 247 | return availableDevices.contains(deviceId); |
| 248 | } |
| 249 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 250 | @Override |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 251 | public DeviceEvent removeDevice(DeviceId deviceId) { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 252 | synchronized (this) { |
| 253 | roles.remove(deviceId); |
| 254 | Device device = devices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 255 | return device == null ? null : |
| 256 | new DeviceEvent(DEVICE_REMOVED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 259 | } |