blob: eec4e758b5427ed26193ae129891993673dff0b9 [file] [log] [blame]
tombe988312014-09-19 18:38:47 -07001package org.onlab.onos.net.device.impl;
tomd3097b02014-08-26 10:40:29 -07002
alshabib339a3d92014-09-26 17:54:32 -07003import static com.google.common.base.Preconditions.checkNotNull;
4import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED;
5import static org.slf4j.LoggerFactory.getLogger;
6
7import java.util.List;
8
tomd3097b02014-08-26 10:40:29 -07009import org.apache.felix.scr.annotations.Activate;
10import org.apache.felix.scr.annotations.Component;
11import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070012import org.apache.felix.scr.annotations.Reference;
13import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070014import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070015import org.onlab.onos.cluster.ClusterService;
16import org.onlab.onos.cluster.MastershipEvent;
17import org.onlab.onos.cluster.MastershipListener;
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070018import org.onlab.onos.cluster.MastershipService;
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070019import org.onlab.onos.cluster.MastershipTermService;
Madan Jampani61056bc2014-09-27 09:07:26 -070020import org.onlab.onos.cluster.MastershipTerm;
tom96dfcab2014-08-28 09:26:03 -070021import org.onlab.onos.event.AbstractListenerRegistry;
22import org.onlab.onos.event.EventDeliveryService;
tom32f66842014-08-27 19:27:47 -070023import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070024import org.onlab.onos.net.DeviceId;
25import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070026import org.onlab.onos.net.Port;
27import org.onlab.onos.net.PortNumber;
tome5ec3fd2014-09-04 15:18:06 -070028import org.onlab.onos.net.device.DeviceAdminService;
tomd3097b02014-08-26 10:40:29 -070029import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070030import org.onlab.onos.net.device.DeviceEvent;
31import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070032import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070033import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070034import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070035import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070036import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070037import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070038import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070039import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070040import org.onlab.onos.net.provider.AbstractProviderService;
Yuta HIGUCHI68b34942014-10-02 23:23:21 -070041import org.onlab.onos.store.ClockProviderService;
tomd3097b02014-08-26 10:40:29 -070042import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070043
tomd3097b02014-08-26 10:40:29 -070044/**
tome4729872014-09-23 00:37:37 -070045 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070046 */
47@Component(immediate = true)
48@Service
tom41a2c5f2014-09-19 09:20:35 -070049public class DeviceManager
alshabib271a6202014-09-28 22:11:21 -070050 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
51 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070052
tome5ec3fd2014-09-04 15:18:06 -070053 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
54 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
55 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
56 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
57 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070058
tom5f38b3a2014-08-27 23:50:54 -070059 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070060
alshabib271a6202014-09-28 22:11:21 -070061 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
62 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070063
alshabib339a3d92014-09-26 17:54:32 -070064 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070065
tomc78acee2014-09-24 15:16:55 -070066 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070067
tom41a2c5f2014-09-19 09:20:35 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070070
tom5f38b3a2014-08-27 23:50:54 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070072 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070073
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070075 protected ClusterService clusterService;
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070078 protected MastershipService mastershipService;
79
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070080 protected MastershipTermService termService;
81
Madan Jampani61056bc2014-09-27 09:07:26 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI68b34942014-10-02 23:23:21 -070083 protected ClockProviderService clockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -070084
tomd3097b02014-08-26 10:40:29 -070085 @Activate
86 public void activate() {
tomf80c9722014-09-24 14:49:18 -070087 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -070088 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -070089 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070090 termService = mastershipService.requestTermService();
tomd3097b02014-08-26 10:40:29 -070091 log.info("Started");
92 }
93
94 @Deactivate
95 public void deactivate() {
tomf80c9722014-09-24 14:49:18 -070096 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -070097 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -070098 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -070099 log.info("Stopped");
100 }
101
102 @Override
tomad2d2092014-09-06 23:24:20 -0700103 public int getDeviceCount() {
104 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700105 }
106
107 @Override
tom32f66842014-08-27 19:27:47 -0700108 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700109 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700110 }
111
tom32f66842014-08-27 19:27:47 -0700112 @Override
113 public Device getDevice(DeviceId deviceId) {
114 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700115 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700116 }
117
118 @Override
tomad2d2092014-09-06 23:24:20 -0700119 public MastershipRole getRole(DeviceId deviceId) {
120 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700121 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700122 }
123
124 @Override
tom32f66842014-08-27 19:27:47 -0700125 public List<Port> getPorts(DeviceId deviceId) {
126 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700127 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700128 }
129
130 @Override
131 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
132 checkNotNull(deviceId, DEVICE_ID_NULL);
133 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700134 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700135 }
136
137 @Override
tomff7eb7c2014-09-08 12:49:03 -0700138 public boolean isAvailable(DeviceId deviceId) {
139 checkNotNull(deviceId, DEVICE_ID_NULL);
140 return store.isAvailable(deviceId);
141 }
142
tomb41d1ac2014-09-24 01:51:24 -0700143 // Applies the specified role to the device; ignores NONE
144 private void applyRole(DeviceId deviceId, MastershipRole newRole) {
145 if (newRole != MastershipRole.NONE) {
146 Device device = store.getDevice(deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700147 // FIXME: Device might not be there yet. (eventual consistent)
148 if (device == null) {
149 return;
150 }
tome5ec3fd2014-09-04 15:18:06 -0700151 DeviceProvider provider = getProvider(device.providerId());
152 if (provider != null) {
tom568581d2014-09-08 20:13:36 -0700153 provider.roleChanged(device, newRole);
tome5ec3fd2014-09-04 15:18:06 -0700154 }
tomb41d1ac2014-09-24 01:51:24 -0700155 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
tome5ec3fd2014-09-04 15:18:06 -0700156 }
157 }
158
159 @Override
160 public void removeDevice(DeviceId deviceId) {
161 checkNotNull(deviceId, DEVICE_ID_NULL);
162 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700163 if (event != null) {
164 log.info("Device {} administratively removed", deviceId);
165 post(event);
166 }
tome5ec3fd2014-09-04 15:18:06 -0700167 }
168
tom7869ad92014-09-09 14:32:08 -0700169 @Override
170 public void addListener(DeviceListener listener) {
171 listenerRegistry.addListener(listener);
172 }
173
174 @Override
175 public void removeListener(DeviceListener listener) {
176 listenerRegistry.removeListener(listener);
177 }
178
179 @Override
alshabibb7b40632014-09-28 21:30:00 -0700180 protected DeviceProviderService createProviderService(
181 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700182 return new InternalDeviceProviderService(provider);
183 }
184
tomd3097b02014-08-26 10:40:29 -0700185 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700186 private class InternalDeviceProviderService
alshabib271a6202014-09-28 22:11:21 -0700187 extends AbstractProviderService<DeviceProvider>
188 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700189
tomcfde0622014-09-09 11:02:42 -0700190 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700191 super(provider);
192 }
193
194 @Override
alshabibb7b40632014-09-28 21:30:00 -0700195 public void deviceConnected(DeviceId deviceId,
196 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700197 checkNotNull(deviceId, DEVICE_ID_NULL);
198 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700199 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700200
201 log.info("Device {} connected", deviceId);
202 // check my Role
203 MastershipRole role = mastershipService.requestRoleFor(deviceId);
204
205 if (role != MastershipRole.MASTER) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700206 // TODO: Do we need to explicitly tell the Provider that
207 // this instance is no longer the MASTER? probably not
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700208 return;
209 }
210
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700211 MastershipTerm term = mastershipService.requestTermService()
212 .getMastershipTerm(deviceId);
213 if (!term.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700214 // lost mastership after requestRole told this instance was MASTER.
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700215 return;
216 }
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700217 // tell clock provider if this instance is the master
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700218 clockProviderService.setMastershipTerm(deviceId, term);
219
tome5ec3fd2014-09-04 15:18:06 -0700220 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
alshabib339a3d92014-09-26 17:54:32 -0700221 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700222
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700223 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700224 // that this instance is the master.
225 // Note: event can be null, if mastership was lost between
226 // roleRequest and store update calls.
tom80c0e5e2014-09-08 18:08:58 -0700227 if (event != null) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700228 // TODO: Check switch reconnected case. Is it assured that
229 // event will never be null?
230 // Could there be a situation MastershipService told this
231 // instance is the new Master, but
232 // event returned from the store is null?
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700233
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700234 // TODO: Confirm: Mastership could be lost after requestRole
235 // and createOrUpdateDevice call.
236 // In that case STANDBY node can
237 // claim itself to be master against the Device.
238 // Will the Node, chosen by the MastershipService, retry
239 // to get the MASTER role when that happen?
240
241 // FIXME: 1st argument should be deviceId, to allow setting
242 // certain roles even if the store returned null.
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700243 provider().roleChanged(event.subject(), role);
tom568581d2014-09-08 20:13:36 -0700244 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700245 }
tomd3097b02014-08-26 10:40:29 -0700246 }
247
248 @Override
249 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700250 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700251 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700252
253 // FIXME: only the MASTER should be marking off-line in normal cases,
254 // but if I was the last STANDBY connection, etc. and no one else
255 // was there to mark the device offline, this instance may need to
256 // temporarily request for Master Role and mark offline.
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700257 if (!mastershipService.getLocalRole(deviceId).equals(MastershipRole.MASTER)) {
258 log.debug("Device {} disconnected, but I am not the master", deviceId);
259 return;
260 }
tome5ec3fd2014-09-04 15:18:06 -0700261 DeviceEvent event = store.markOffline(deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700262
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700263 mastershipService.relinquishMastership(deviceId);
264
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700265 //we're no longer capable of mastership.
tom0efbb1d2014-09-09 11:54:28 -0700266 if (event != null) {
267 log.info("Device {} disconnected", deviceId);
268 post(event);
269 }
tomd3097b02014-08-26 10:40:29 -0700270 }
271
272 @Override
alshabibb7b40632014-09-28 21:30:00 -0700273 public void updatePorts(DeviceId deviceId,
274 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700275 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700276 checkNotNull(portDescriptions,
277 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700278 checkValidity();
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700279 this.provider().id();
280 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
281 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700282 for (DeviceEvent event : events) {
283 post(event);
284 }
tomd3097b02014-08-26 10:40:29 -0700285 }
286
287 @Override
alshabibb7b40632014-09-28 21:30:00 -0700288 public void portStatusChanged(DeviceId deviceId,
289 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700290 checkNotNull(deviceId, DEVICE_ID_NULL);
291 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700292 checkValidity();
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700293 DeviceEvent event = store.updatePortStatus(this.provider().id(),
294 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700295 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700296 log.info("Device {} port {} status changed", deviceId, event
297 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700298 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700299 }
tomd3097b02014-08-26 10:40:29 -0700300 }
tom3f2bbd72014-09-24 12:07:58 -0700301
302 @Override
303 public void unableToAssertRole(DeviceId deviceId, MastershipRole role) {
304 // FIXME: implement response to this notification
alshabibb7b40632014-09-28 21:30:00 -0700305 log.warn("Failed to assert role [{}] onto Device {}", role,
306 deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700307 if (role == MastershipRole.MASTER) {
308 mastershipService.relinquishMastership(deviceId);
309 }
tom3f2bbd72014-09-24 12:07:58 -0700310 }
tomd3097b02014-08-26 10:40:29 -0700311 }
tom32f66842014-08-27 19:27:47 -0700312
tomeadbb462014-09-07 16:10:19 -0700313 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700314 private void post(DeviceEvent event) {
315 if (event != null && eventDispatcher != null) {
316 eventDispatcher.post(event);
317 }
318 }
319
tomb41d1ac2014-09-24 01:51:24 -0700320 // Intercepts mastership events
alshabib271a6202014-09-28 22:11:21 -0700321 private class InternalMastershipListener
322 implements MastershipListener {
tomb41d1ac2014-09-24 01:51:24 -0700323 @Override
324 public void event(MastershipEvent event) {
tomb41d1ac2014-09-24 01:51:24 -0700325 if (event.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700326
alshabibb7b40632014-09-28 21:30:00 -0700327 MastershipTerm term = mastershipService.requestTermService()
328 .getMastershipTerm(event.subject());
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700329
330 if (term.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700331 // only set the new term if I am the master
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700332 clockProviderService.setMastershipTerm(event.subject(), term);
333 }
334
tomb41d1ac2014-09-24 01:51:24 -0700335 applyRole(event.subject(), MastershipRole.MASTER);
alshabib339a3d92014-09-26 17:54:32 -0700336 } else {
337 applyRole(event.subject(), MastershipRole.STANDBY);
tomb41d1ac2014-09-24 01:51:24 -0700338 }
339 }
340 }
tomf80c9722014-09-24 14:49:18 -0700341
342 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700343 private class InternalStoreDelegate
344 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700345 @Override
346 public void notify(DeviceEvent event) {
347 post(event);
348 }
349 }
tomd3097b02014-08-26 10:40:29 -0700350}