tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.device.impl; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 8 | import org.apache.felix.scr.annotations.Service; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 9 | import org.onlab.onos.cluster.ClusterService; |
| 10 | import org.onlab.onos.cluster.MastershipEvent; |
| 11 | import org.onlab.onos.cluster.MastershipListener; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 12 | import org.onlab.onos.cluster.MastershipService; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 13 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 14 | import org.onlab.onos.event.EventDeliveryService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.Device; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.DeviceId; |
| 17 | import org.onlab.onos.net.MastershipRole; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.Port; |
| 19 | import org.onlab.onos.net.PortNumber; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.device.DeviceAdminService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.device.DeviceDescription; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.device.DeviceEvent; |
| 23 | import org.onlab.onos.net.device.DeviceListener; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.device.DeviceProviderService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.device.DeviceService; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.device.DeviceStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 29 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.device.PortDescription; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 33 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.List; |
| 36 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 37 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 38 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 39 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 40 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 41 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 42 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 43 | */ |
| 44 | @Component(immediate = true) |
| 45 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 46 | public class DeviceManager |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 47 | extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService> |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 48 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 49 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 50 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 51 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 52 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 53 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
| 54 | private static final String ROLE_NULL = "Role cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 55 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 57 | |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 58 | protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 59 | listenerRegistry = new AbstractListenerRegistry<>(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 60 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 61 | private DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
| 62 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 63 | private final MastershipListener mastershipListener = new InnerMastershipListener(); |
| 64 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 67 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 69 | protected EventDeliveryService eventDispatcher; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 70 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 72 | protected ClusterService clusterService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 75 | protected MastershipService mastershipService; |
| 76 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 77 | @Activate |
| 78 | public void activate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 79 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 80 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 81 | mastershipService.addListener(mastershipListener); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 82 | log.info("Started"); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 87 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 88 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 89 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 90 | log.info("Stopped"); |
| 91 | } |
| 92 | |
| 93 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 94 | public int getDeviceCount() { |
| 95 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 99 | public Iterable<Device> getDevices() { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 100 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 101 | } |
| 102 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public Device getDevice(DeviceId deviceId) { |
| 105 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 106 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 110 | public MastershipRole getRole(DeviceId deviceId) { |
| 111 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 112 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 116 | public List<Port> getPorts(DeviceId deviceId) { |
| 117 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 118 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 123 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 124 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 125 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 129 | public boolean isAvailable(DeviceId deviceId) { |
| 130 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 131 | return store.isAvailable(deviceId); |
| 132 | } |
| 133 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 134 | // Applies the specified role to the device; ignores NONE |
| 135 | private void applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 136 | if (newRole != MastershipRole.NONE) { |
| 137 | Device device = store.getDevice(deviceId); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 138 | DeviceProvider provider = getProvider(device.providerId()); |
| 139 | if (provider != null) { |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 140 | provider.roleChanged(device, newRole); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 141 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 142 | post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void removeDevice(DeviceId deviceId) { |
| 148 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 149 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 150 | if (event != null) { |
| 151 | log.info("Device {} administratively removed", deviceId); |
| 152 | post(event); |
| 153 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 154 | } |
| 155 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void addListener(DeviceListener listener) { |
| 158 | listenerRegistry.addListener(listener); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void removeListener(DeviceListener listener) { |
| 163 | listenerRegistry.removeListener(listener); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | protected DeviceProviderService createProviderService(DeviceProvider provider) { |
| 168 | return new InternalDeviceProviderService(provider); |
| 169 | } |
| 170 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 171 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 172 | private class InternalDeviceProviderService |
| 173 | extends AbstractProviderService<DeviceProvider> |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 174 | implements DeviceProviderService { |
| 175 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 176 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 177 | super(provider); |
| 178 | } |
| 179 | |
| 180 | @Override |
tom | d1c0649 | 2014-09-03 11:07:58 -0700 | [diff] [blame] | 181 | public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 182 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 183 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 184 | checkValidity(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 185 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), |
| 186 | deviceId, deviceDescription); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 187 | |
| 188 | // If there was a change of any kind, trigger role selection process. |
| 189 | if (event != null) { |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 190 | log.info("Device {} connected", deviceId); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 191 | mastershipService.requestRoleFor(deviceId); |
| 192 | provider().roleChanged(event.subject(), |
| 193 | mastershipService.getLocalRole(deviceId)); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 194 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 195 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 200 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 201 | checkValidity(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 202 | DeviceEvent event = store.markOffline(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 203 | if (event != null) { |
| 204 | log.info("Device {} disconnected", deviceId); |
| 205 | post(event); |
| 206 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 210 | public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { |
| 211 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 212 | checkNotNull(portDescriptions, "Port descriptions list cannot be null"); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 213 | checkValidity(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 214 | List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions); |
| 215 | for (DeviceEvent event : events) { |
| 216 | post(event); |
| 217 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 221 | public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { |
| 222 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 223 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 224 | checkValidity(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 225 | DeviceEvent event = store.updatePortStatus(deviceId, portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 226 | if (event != null) { |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 227 | log.info("Device {} port {} status changed", deviceId, |
| 228 | event.port().number()); |
| 229 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 230 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 231 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 232 | |
| 233 | @Override |
| 234 | public void unableToAssertRole(DeviceId deviceId, MastershipRole role) { |
| 235 | // FIXME: implement response to this notification |
| 236 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 237 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 238 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 239 | // Posts the specified event to the local event dispatcher. |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 240 | private void post(DeviceEvent event) { |
| 241 | if (event != null && eventDispatcher != null) { |
| 242 | eventDispatcher.post(event); |
| 243 | } |
| 244 | } |
| 245 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 246 | // Intercepts mastership events |
| 247 | private class InnerMastershipListener implements MastershipListener { |
| 248 | @Override |
| 249 | public void event(MastershipEvent event) { |
| 250 | // FIXME: for now we're taking action only on becoming master |
| 251 | if (event.master().equals(clusterService.getLocalNode().id())) { |
| 252 | applyRole(event.subject(), MastershipRole.MASTER); |
| 253 | } |
| 254 | } |
| 255 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 256 | |
| 257 | // Store delegate to re-post events emitted from the store. |
| 258 | private class InternalStoreDelegate implements DeviceStoreDelegate { |
| 259 | @Override |
| 260 | public void notify(DeviceEvent event) { |
| 261 | post(event); |
| 262 | } |
| 263 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 264 | } |