blob: 425adca3a157160b99f2545c96ffa0e4d06b57ac [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;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070021import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070022import org.onlab.onos.event.AbstractListenerRegistry;
23import org.onlab.onos.event.EventDeliveryService;
tom32f66842014-08-27 19:27:47 -070024import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070025import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070027import org.onlab.onos.net.Port;
28import org.onlab.onos.net.PortNumber;
tome5ec3fd2014-09-04 15:18:06 -070029import org.onlab.onos.net.device.DeviceAdminService;
tomd3097b02014-08-26 10:40:29 -070030import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070031import org.onlab.onos.net.device.DeviceEvent;
32import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070033import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070034import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070035import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070036import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070037import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070038import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070039import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070040import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070041import org.onlab.onos.net.provider.AbstractProviderService;
Yuta HIGUCHI68b34942014-10-02 23:23:21 -070042import org.onlab.onos.store.ClockProviderService;
tomd3097b02014-08-26 10:40:29 -070043import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070044
tomd3097b02014-08-26 10:40:29 -070045/**
tome4729872014-09-23 00:37:37 -070046 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070047 */
48@Component(immediate = true)
49@Service
tom41a2c5f2014-09-19 09:20:35 -070050public class DeviceManager
alshabib271a6202014-09-28 22:11:21 -070051 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
52 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070053
tome5ec3fd2014-09-04 15:18:06 -070054 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
55 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
56 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
57 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
58 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070059
tom5f38b3a2014-08-27 23:50:54 -070060 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070061
alshabib271a6202014-09-28 22:11:21 -070062 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
63 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070064
alshabib339a3d92014-09-26 17:54:32 -070065 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070066
tomc78acee2014-09-24 15:16:55 -070067 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070068
tom41a2c5f2014-09-19 09:20:35 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070071
tom5f38b3a2014-08-27 23:50:54 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070073 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070074
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070076 protected ClusterService clusterService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070079 protected MastershipService mastershipService;
80
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070081 protected MastershipTermService termService;
82
Madan Jampani61056bc2014-09-27 09:07:26 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI68b34942014-10-02 23:23:21 -070084 protected ClockProviderService clockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -070085
tomd3097b02014-08-26 10:40:29 -070086 @Activate
87 public void activate() {
tomf80c9722014-09-24 14:49:18 -070088 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -070089 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -070090 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070091 termService = mastershipService.requestTermService();
tomd3097b02014-08-26 10:40:29 -070092 log.info("Started");
93 }
94
95 @Deactivate
96 public void deactivate() {
tomf80c9722014-09-24 14:49:18 -070097 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -070098 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -070099 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700100 log.info("Stopped");
101 }
102
103 @Override
tomad2d2092014-09-06 23:24:20 -0700104 public int getDeviceCount() {
105 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700106 }
107
108 @Override
tom32f66842014-08-27 19:27:47 -0700109 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700110 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700111 }
112
tom32f66842014-08-27 19:27:47 -0700113 @Override
114 public Device getDevice(DeviceId deviceId) {
115 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700116 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700117 }
118
119 @Override
tomad2d2092014-09-06 23:24:20 -0700120 public MastershipRole getRole(DeviceId deviceId) {
121 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700122 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700123 }
124
125 @Override
tom32f66842014-08-27 19:27:47 -0700126 public List<Port> getPorts(DeviceId deviceId) {
127 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700128 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700129 }
130
131 @Override
132 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
133 checkNotNull(deviceId, DEVICE_ID_NULL);
134 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700135 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700136 }
137
138 @Override
tomff7eb7c2014-09-08 12:49:03 -0700139 public boolean isAvailable(DeviceId deviceId) {
140 checkNotNull(deviceId, DEVICE_ID_NULL);
141 return store.isAvailable(deviceId);
142 }
143
tomb41d1ac2014-09-24 01:51:24 -0700144 // Applies the specified role to the device; ignores NONE
145 private void applyRole(DeviceId deviceId, MastershipRole newRole) {
146 if (newRole != MastershipRole.NONE) {
147 Device device = store.getDevice(deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700148 // FIXME: Device might not be there yet. (eventual consistent)
149 if (device == null) {
150 return;
151 }
tome5ec3fd2014-09-04 15:18:06 -0700152 DeviceProvider provider = getProvider(device.providerId());
153 if (provider != null) {
tom568581d2014-09-08 20:13:36 -0700154 provider.roleChanged(device, newRole);
tome5ec3fd2014-09-04 15:18:06 -0700155 }
tomb41d1ac2014-09-24 01:51:24 -0700156 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
tome5ec3fd2014-09-04 15:18:06 -0700157 }
158 }
159
160 @Override
161 public void removeDevice(DeviceId deviceId) {
162 checkNotNull(deviceId, DEVICE_ID_NULL);
163 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700164 if (event != null) {
165 log.info("Device {} administratively removed", deviceId);
166 post(event);
167 }
tome5ec3fd2014-09-04 15:18:06 -0700168 }
169
tom7869ad92014-09-09 14:32:08 -0700170 @Override
171 public void addListener(DeviceListener listener) {
172 listenerRegistry.addListener(listener);
173 }
174
175 @Override
176 public void removeListener(DeviceListener listener) {
177 listenerRegistry.removeListener(listener);
178 }
179
180 @Override
alshabibb7b40632014-09-28 21:30:00 -0700181 protected DeviceProviderService createProviderService(
182 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700183 return new InternalDeviceProviderService(provider);
184 }
185
tomd3097b02014-08-26 10:40:29 -0700186 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700187 private class InternalDeviceProviderService
alshabib271a6202014-09-28 22:11:21 -0700188 extends AbstractProviderService<DeviceProvider>
189 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700190
tomcfde0622014-09-09 11:02:42 -0700191 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700192 super(provider);
193 }
194
195 @Override
alshabibb7b40632014-09-28 21:30:00 -0700196 public void deviceConnected(DeviceId deviceId,
197 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700198 checkNotNull(deviceId, DEVICE_ID_NULL);
199 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700200 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700201
202 log.info("Device {} connected", deviceId);
203 // check my Role
204 MastershipRole role = mastershipService.requestRoleFor(deviceId);
205
206 if (role != MastershipRole.MASTER) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700207 // TODO: Do we need to explicitly tell the Provider that
208 // this instance is no longer the MASTER? probably not
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700209 return;
210 }
211
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700212 MastershipTerm term = mastershipService.requestTermService()
213 .getMastershipTerm(deviceId);
214 if (!term.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700215 // lost mastership after requestRole told this instance was MASTER.
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700216 return;
217 }
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700218 // tell clock provider if this instance is the master
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700219 clockProviderService.setMastershipTerm(deviceId, term);
220
tome5ec3fd2014-09-04 15:18:06 -0700221 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
alshabib339a3d92014-09-26 17:54:32 -0700222 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700223
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700224 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700225 // that this instance is the master.
226 // Note: event can be null, if mastership was lost between
227 // roleRequest and store update calls.
tom80c0e5e2014-09-08 18:08:58 -0700228 if (event != null) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700229 // TODO: Check switch reconnected case. Is it assured that
230 // event will never be null?
231 // Could there be a situation MastershipService told this
232 // instance is the new Master, but
233 // event returned from the store is null?
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700234
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700235 // TODO: Confirm: Mastership could be lost after requestRole
236 // and createOrUpdateDevice call.
237 // In that case STANDBY node can
238 // claim itself to be master against the Device.
239 // Will the Node, chosen by the MastershipService, retry
240 // to get the MASTER role when that happen?
241
242 // FIXME: 1st argument should be deviceId, to allow setting
243 // certain roles even if the store returned null.
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700244 provider().roleChanged(event.subject(), role);
tom568581d2014-09-08 20:13:36 -0700245 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700246 }
tomd3097b02014-08-26 10:40:29 -0700247 }
248
249 @Override
250 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700251 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700252 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700253
254 // FIXME: only the MASTER should be marking off-line in normal cases,
255 // but if I was the last STANDBY connection, etc. and no one else
256 // was there to mark the device offline, this instance may need to
257 // temporarily request for Master Role and mark offline.
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700258 if (!mastershipService.getLocalRole(deviceId).equals(MastershipRole.MASTER)) {
259 log.debug("Device {} disconnected, but I am not the master", deviceId);
260 return;
261 }
tome5ec3fd2014-09-04 15:18:06 -0700262 DeviceEvent event = store.markOffline(deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700263
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700264 mastershipService.relinquishMastership(deviceId);
265
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700266 //we're no longer capable of mastership.
tom0efbb1d2014-09-09 11:54:28 -0700267 if (event != null) {
268 log.info("Device {} disconnected", deviceId);
269 post(event);
270 }
tomd3097b02014-08-26 10:40:29 -0700271 }
272
273 @Override
alshabibb7b40632014-09-28 21:30:00 -0700274 public void updatePorts(DeviceId deviceId,
275 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700276 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700277 checkNotNull(portDescriptions,
278 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700279 checkValidity();
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700280 this.provider().id();
281 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
282 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700283 for (DeviceEvent event : events) {
284 post(event);
285 }
tomd3097b02014-08-26 10:40:29 -0700286 }
287
288 @Override
alshabibb7b40632014-09-28 21:30:00 -0700289 public void portStatusChanged(DeviceId deviceId,
290 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700291 checkNotNull(deviceId, DEVICE_ID_NULL);
292 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700293 checkValidity();
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700294 DeviceEvent event = store.updatePortStatus(this.provider().id(),
295 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700296 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700297 log.info("Device {} port {} status changed", deviceId, event
298 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700299 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700300 }
tomd3097b02014-08-26 10:40:29 -0700301 }
tom3f2bbd72014-09-24 12:07:58 -0700302
303 @Override
304 public void unableToAssertRole(DeviceId deviceId, MastershipRole role) {
305 // FIXME: implement response to this notification
alshabibb7b40632014-09-28 21:30:00 -0700306 log.warn("Failed to assert role [{}] onto Device {}", role,
307 deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700308 if (role == MastershipRole.MASTER) {
309 mastershipService.relinquishMastership(deviceId);
310 }
tom3f2bbd72014-09-24 12:07:58 -0700311 }
tomd3097b02014-08-26 10:40:29 -0700312 }
tom32f66842014-08-27 19:27:47 -0700313
tomeadbb462014-09-07 16:10:19 -0700314 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700315 private void post(DeviceEvent event) {
316 if (event != null && eventDispatcher != null) {
317 eventDispatcher.post(event);
318 }
319 }
320
tomb41d1ac2014-09-24 01:51:24 -0700321 // Intercepts mastership events
alshabib271a6202014-09-28 22:11:21 -0700322 private class InternalMastershipListener
323 implements MastershipListener {
tomb41d1ac2014-09-24 01:51:24 -0700324 @Override
325 public void event(MastershipEvent event) {
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -0700326 final NodeId myNodeId = clusterService.getLocalNode().id();
327 if (myNodeId.equals(event.master())) {
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700328
alshabibb7b40632014-09-28 21:30:00 -0700329 MastershipTerm term = mastershipService.requestTermService()
330 .getMastershipTerm(event.subject());
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700331
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -0700332 if (term.master().equals(myNodeId)) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700333 // only set the new term if I am the master
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700334 clockProviderService.setMastershipTerm(event.subject(), term);
335 }
336
tomb41d1ac2014-09-24 01:51:24 -0700337 applyRole(event.subject(), MastershipRole.MASTER);
alshabib339a3d92014-09-26 17:54:32 -0700338 } else {
339 applyRole(event.subject(), MastershipRole.STANDBY);
tomb41d1ac2014-09-24 01:51:24 -0700340 }
341 }
342 }
tomf80c9722014-09-24 14:49:18 -0700343
344 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700345 private class InternalStoreDelegate
346 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700347 @Override
348 public void notify(DeviceEvent event) {
349 post(event);
350 }
351 }
tomd3097b02014-08-26 10:40:29 -0700352}