tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.device.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 | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 4 | import org.onlab.onos.net.DefaultDevice; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 5 | import org.onlab.onos.net.DefaultPort; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.Device; |
| 7 | import org.onlab.onos.net.DeviceId; |
| 8 | import org.onlab.onos.net.MastershipRole; |
| 9 | import org.onlab.onos.net.Port; |
| 10 | import org.onlab.onos.net.PortNumber; |
| 11 | import org.onlab.onos.net.device.DeviceDescription; |
| 12 | import org.onlab.onos.net.device.DeviceEvent; |
| 13 | import org.onlab.onos.net.device.PortDescription; |
| 14 | import org.onlab.onos.net.provider.ProviderId; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.Collections; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 18 | import java.util.HashMap; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 19 | import java.util.HashSet; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 20 | import java.util.Iterator; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.Objects; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.ConcurrentHashMap; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 28 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 31 | * Manages inventory of infrastructure DEVICES using trivial in-memory |
| 32 | * structures implementation. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 33 | */ |
| 34 | class SimpleDeviceStore { |
| 35 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 36 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 37 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 38 | private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>(); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 39 | private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>(); |
| 40 | private final Set<DeviceId> availableDevices = new HashSet<>(); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 41 | private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 44 | * Returns the number of devices known to the system. |
| 45 | * |
| 46 | * @return number of devices |
| 47 | */ |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 48 | int getDeviceCount() { |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 49 | return devices.size(); |
| 50 | } |
| 51 | |
| 52 | /** |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 53 | * Returns an iterable collection of all devices known to the system. |
| 54 | * |
| 55 | * @return device collection |
| 56 | */ |
| 57 | Iterable<Device> getDevices() { |
| 58 | return Collections.unmodifiableSet(new HashSet<Device>(devices.values())); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the device with the specified identifier. |
| 63 | * |
| 64 | * @param deviceId device identifier |
| 65 | * @return device |
| 66 | */ |
| 67 | Device getDevice(DeviceId deviceId) { |
| 68 | return devices.get(deviceId); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates a new infrastructure device, or updates an existing one using |
| 73 | * the supplied device description. |
| 74 | * |
| 75 | * @param providerId provider identifier |
| 76 | * @param deviceId device identifier |
| 77 | * @param deviceDescription device description |
| 78 | * @return ready to send event describing what occurred; null if no change |
| 79 | */ |
| 80 | DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 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. |
| 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 | |
| 133 | /** |
| 134 | * Removes the specified infrastructure device. |
| 135 | * |
| 136 | * @param deviceId device identifier |
| 137 | * @return ready to send event describing what occurred; null if no change |
| 138 | */ |
| 139 | DeviceEvent markOffline(DeviceId deviceId) { |
| 140 | synchronized (this) { |
| 141 | Device device = devices.get(deviceId); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 142 | boolean removed = device != null && availableDevices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 143 | return !removed ? null : |
| 144 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Updates the ports of the specified infrastructure device using the given |
| 150 | * list of port descriptions. The list is assumed to be comprehensive. |
| 151 | * |
| 152 | * @param deviceId device identifier |
| 153 | * @param portDescriptions list of port descriptions |
| 154 | * @return ready to send events describing what occurred; empty list if no change |
| 155 | */ |
| 156 | List<DeviceEvent> updatePorts(DeviceId deviceId, |
| 157 | List<PortDescription> portDescriptions) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 158 | List<DeviceEvent> events = new ArrayList<>(); |
| 159 | synchronized (this) { |
| 160 | Device device = devices.get(deviceId); |
| 161 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 162 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 163 | |
| 164 | // Add new ports |
| 165 | Set<PortNumber> processed = new HashSet<>(); |
| 166 | for (PortDescription portDescription : portDescriptions) { |
| 167 | Port port = ports.get(portDescription.portNumber()); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 168 | events.add(port == null ? |
| 169 | createPort(device, portDescription, ports) : |
| 170 | updatePort(device, port, portDescription, ports)); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 171 | processed.add(portDescription.portNumber()); |
| 172 | } |
| 173 | |
| 174 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 175 | } |
| 176 | return events; |
| 177 | } |
| 178 | |
| 179 | // Creates a new port based on the port description adds it to the map and |
| 180 | // Returns corresponding event. |
| 181 | private DeviceEvent createPort(Device device, PortDescription portDescription, |
| 182 | Map<PortNumber, Port> ports) { |
| 183 | DefaultPort port = new DefaultPort(device, portDescription.portNumber(), |
| 184 | portDescription.isEnabled()); |
| 185 | ports.put(port.number(), port); |
| 186 | return new DeviceEvent(PORT_ADDED, device, port); |
| 187 | } |
| 188 | |
| 189 | // CHecks if the specified port requires update and if so, it replaces the |
| 190 | // existing entry in the map and returns corresponding event. |
| 191 | private DeviceEvent updatePort(Device device, Port port, |
| 192 | PortDescription portDescription, |
| 193 | Map<PortNumber, Port> ports) { |
| 194 | if (port.isEnabled() != portDescription.isEnabled()) { |
| 195 | DefaultPort updatedPort = |
| 196 | new DefaultPort(device, portDescription.portNumber(), |
| 197 | portDescription.isEnabled()); |
| 198 | ports.put(port.number(), updatedPort); |
| 199 | return new DeviceEvent(PORT_UPDATED, device, port); |
| 200 | } |
| 201 | return null; |
| 202 | } |
| 203 | |
| 204 | // Prunes the specified list of ports based on which ports are in the |
| 205 | // processed list and returns list of corresponding events. |
| 206 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 207 | Map<PortNumber, Port> ports, |
| 208 | Set<PortNumber> processed) { |
| 209 | List<DeviceEvent> events = new ArrayList<>(); |
| 210 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 211 | while (iterator.hasNext()) { |
| 212 | PortNumber portNumber = iterator.next(); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 213 | if (!processed.contains(portNumber)) { |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 214 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 215 | ports.get(portNumber))); |
| 216 | iterator.remove(); |
| 217 | } |
| 218 | } |
| 219 | return events; |
| 220 | } |
| 221 | |
| 222 | // Gets the map of ports for the specified device; if one does not already |
| 223 | // exist, it creates and registers a new one. |
| 224 | private Map<PortNumber, Port> getPortMap(DeviceId deviceId) { |
| 225 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 226 | if (ports == null) { |
| 227 | ports = new HashMap<>(); |
| 228 | devicePorts.put(deviceId, ports); |
| 229 | } |
| 230 | return ports; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Updates the port status of the specified infrastructure device using the |
| 235 | * given port description. |
| 236 | * |
| 237 | * @param deviceId device identifier |
| 238 | * @param portDescription port description |
| 239 | * @return ready to send event describing what occurred; null if no change |
| 240 | */ |
| 241 | DeviceEvent updatePortStatus(DeviceId deviceId, |
| 242 | PortDescription portDescription) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 243 | synchronized (this) { |
| 244 | Device device = devices.get(deviceId); |
| 245 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 246 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 247 | Port port = ports.get(portDescription.portNumber()); |
| 248 | return updatePort(device, port, portDescription, ports); |
| 249 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Returns the list of ports that belong to the specified device. |
| 254 | * |
| 255 | * @param deviceId device identifier |
| 256 | * @return list of device ports |
| 257 | */ |
| 258 | List<Port> getPorts(DeviceId deviceId) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 259 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 260 | return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Returns the specified device port. |
| 265 | * |
| 266 | * @param deviceId device identifier |
| 267 | * @param portNumber port number |
| 268 | * @return device port |
| 269 | */ |
| 270 | Port getPort(DeviceId deviceId, PortNumber portNumber) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame] | 271 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 272 | return ports == null ? null : ports.get(portNumber); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 276 | * Indicates whether the specified device is available/online. |
| 277 | * |
| 278 | * @param deviceId device identifier |
| 279 | * @return true if device is available |
| 280 | */ |
| 281 | boolean isAvailable(DeviceId deviceId) { |
| 282 | return availableDevices.contains(deviceId); |
| 283 | } |
| 284 | |
| 285 | /** |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 286 | * Returns the mastership role determined for this device. |
| 287 | * |
| 288 | * @param deviceId device identifier |
| 289 | * @return mastership role |
| 290 | */ |
| 291 | MastershipRole getRole(DeviceId deviceId) { |
| 292 | MastershipRole role = roles.get(deviceId); |
| 293 | return role != null ? role : MastershipRole.NONE; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Administratively sets the role of the specified device. |
| 298 | * |
| 299 | * @param deviceId device identifier |
| 300 | * @param role mastership role to apply |
| 301 | * @return mastership role change event or null if no change |
| 302 | */ |
| 303 | DeviceEvent setRole(DeviceId deviceId, MastershipRole role) { |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 304 | synchronized (this) { |
| 305 | Device device = getDevice(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 306 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 307 | MastershipRole oldRole = roles.put(deviceId, role); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 308 | return oldRole == role ? null : |
| 309 | new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 310 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Administratively removes the specified device from the store. |
| 315 | * |
| 316 | * @param deviceId device to be removed |
| 317 | */ |
| 318 | DeviceEvent removeDevice(DeviceId deviceId) { |
| 319 | synchronized (this) { |
| 320 | roles.remove(deviceId); |
| 321 | Device device = devices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 322 | return device == null ? null : |
| 323 | new DeviceEvent(DEVICE_REMOVED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 326 | } |