tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
| 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 | /** |
| 31 | * Manages inventory of infrastructure devices. |
| 32 | */ |
| 33 | class SimpleDeviceStore { |
| 34 | |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 35 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 36 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 37 | private final Map<DeviceId, DefaultDevice> devices = new ConcurrentHashMap<>(); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 38 | private final Map<DeviceId, MastershipRole> roles = new ConcurrentHashMap<>(); |
| 39 | private final Set<DeviceId> availableDevices = new HashSet<>(); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 40 | private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Returns an iterable collection of all devices known to the system. |
| 44 | * |
| 45 | * @return device collection |
| 46 | */ |
| 47 | Iterable<Device> getDevices() { |
| 48 | return Collections.unmodifiableSet(new HashSet<Device>(devices.values())); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the device with the specified identifier. |
| 53 | * |
| 54 | * @param deviceId device identifier |
| 55 | * @return device |
| 56 | */ |
| 57 | Device getDevice(DeviceId deviceId) { |
| 58 | return devices.get(deviceId); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates a new infrastructure device, or updates an existing one using |
| 63 | * the supplied device description. |
| 64 | * |
| 65 | * @param providerId provider identifier |
| 66 | * @param deviceId device identifier |
| 67 | * @param deviceDescription device description |
| 68 | * @return ready to send event describing what occurred; null if no change |
| 69 | */ |
| 70 | DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 71 | DeviceDescription deviceDescription) { |
| 72 | DefaultDevice device = devices.get(deviceId); |
| 73 | if (device == null) { |
| 74 | return createDevice(providerId, deviceId, deviceDescription); |
| 75 | } |
| 76 | return updateDevice(providerId, device, deviceDescription); |
| 77 | } |
| 78 | |
| 79 | // Creates the device and returns the appropriate event if necessary. |
| 80 | private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId, |
| 81 | DeviceDescription desc) { |
| 82 | DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(), |
| 83 | desc.manufacturer(), |
| 84 | desc.hwVersion(), desc.swVersion(), |
| 85 | desc.serialNumber()); |
| 86 | synchronized (this) { |
| 87 | devices.put(deviceId, device); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 88 | availableDevices.add(deviceId); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 89 | } |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 90 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Updates the device and returns the appropriate event if necessary. |
| 94 | private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device, |
| 95 | DeviceDescription desc) { |
| 96 | // We allow only certain attributes to trigger update |
| 97 | if (!Objects.equals(device.hwVersion(), desc.hwVersion()) || |
| 98 | !Objects.equals(device.swVersion(), desc.swVersion())) { |
| 99 | DefaultDevice updated = new DefaultDevice(providerId, device.id(), |
| 100 | desc.type(), |
| 101 | desc.manufacturer(), |
| 102 | desc.hwVersion(), |
| 103 | desc.swVersion(), |
| 104 | desc.serialNumber()); |
| 105 | synchronized (this) { |
| 106 | devices.put(device.id(), updated); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 107 | availableDevices.add(device.id()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 108 | } |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 109 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Otherwise merely attempt to change availability |
| 113 | synchronized (this) { |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 114 | boolean added = availableDevices.add(device.id()); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 115 | return !added ? null : |
| 116 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Removes the specified infrastructure device. |
| 122 | * |
| 123 | * @param deviceId device identifier |
| 124 | * @return ready to send event describing what occurred; null if no change |
| 125 | */ |
| 126 | DeviceEvent markOffline(DeviceId deviceId) { |
| 127 | synchronized (this) { |
| 128 | Device device = devices.get(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 129 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 130 | boolean removed = availableDevices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 131 | return !removed ? null : |
| 132 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Updates the ports of the specified infrastructure device using the given |
| 138 | * list of port descriptions. The list is assumed to be comprehensive. |
| 139 | * |
| 140 | * @param deviceId device identifier |
| 141 | * @param portDescriptions list of port descriptions |
| 142 | * @return ready to send events describing what occurred; empty list if no change |
| 143 | */ |
| 144 | List<DeviceEvent> updatePorts(DeviceId deviceId, |
| 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()); |
| 156 | DeviceEvent event = port == null ? |
| 157 | createPort(device, portDescription, ports) : |
| 158 | updatePort(device, port, portDescription, ports); |
| 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(); |
| 201 | if (processed.contains(portNumber)) { |
| 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 | |
| 221 | /** |
| 222 | * Updates the port status of the specified infrastructure device using the |
| 223 | * given port description. |
| 224 | * |
| 225 | * @param deviceId device identifier |
| 226 | * @param portDescription port description |
| 227 | * @return ready to send event describing what occurred; null if no change |
| 228 | */ |
| 229 | DeviceEvent updatePortStatus(DeviceId deviceId, |
| 230 | PortDescription portDescription) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame^] | 231 | 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 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns the list of ports that belong to the specified device. |
| 242 | * |
| 243 | * @param deviceId device identifier |
| 244 | * @return list of device ports |
| 245 | */ |
| 246 | List<Port> getPorts(DeviceId deviceId) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame^] | 247 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 248 | return ports == null ? new ArrayList<Port>() : ImmutableList.copyOf(ports.values()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns the specified device port. |
| 253 | * |
| 254 | * @param deviceId device identifier |
| 255 | * @param portNumber port number |
| 256 | * @return device port |
| 257 | */ |
| 258 | Port getPort(DeviceId deviceId, PortNumber portNumber) { |
tom | 46a220d | 2014-09-05 08:25:56 -0700 | [diff] [blame^] | 259 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 260 | return ports == null ? null : ports.get(portNumber); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Returns the mastership role determined for this device. |
| 265 | * |
| 266 | * @param deviceId device identifier |
| 267 | * @return mastership role |
| 268 | */ |
| 269 | MastershipRole getRole(DeviceId deviceId) { |
| 270 | MastershipRole role = roles.get(deviceId); |
| 271 | return role != null ? role : MastershipRole.NONE; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Administratively sets the role of the specified device. |
| 276 | * |
| 277 | * @param deviceId device identifier |
| 278 | * @param role mastership role to apply |
| 279 | * @return mastership role change event or null if no change |
| 280 | */ |
| 281 | DeviceEvent setRole(DeviceId deviceId, MastershipRole role) { |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 282 | synchronized (this) { |
| 283 | Device device = getDevice(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 284 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 285 | MastershipRole oldRole = roles.put(deviceId, role); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 286 | return oldRole == role ? null : |
| 287 | new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null); |
tom | 249829a | 2014-09-04 15:28:04 -0700 | [diff] [blame] | 288 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Administratively removes the specified device from the store. |
| 293 | * |
| 294 | * @param deviceId device to be removed |
| 295 | */ |
| 296 | DeviceEvent removeDevice(DeviceId deviceId) { |
| 297 | synchronized (this) { |
| 298 | roles.remove(deviceId); |
| 299 | Device device = devices.remove(deviceId); |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 300 | return device == null ? null : |
| 301 | new DeviceEvent(DEVICE_REMOVED, device, null); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
tom | 29df6f4 | 2014-09-05 08:14:14 -0700 | [diff] [blame] | 304 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 305 | } |