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