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