blob: 76d46b02361d22b81189f5d9ab63695712cf453b [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;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070016import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070017import org.onlab.onos.event.AbstractListenerRegistry;
18import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070019import org.onlab.onos.mastership.MastershipEvent;
20import org.onlab.onos.mastership.MastershipListener;
21import org.onlab.onos.mastership.MastershipService;
22import org.onlab.onos.mastership.MastershipTerm;
23import org.onlab.onos.mastership.MastershipTermService;
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;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070029import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070030import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070031import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070032import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070033import org.onlab.onos.net.device.DeviceEvent;
34import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070035import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070036import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070037import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070038import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070039import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070040import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070041import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070042import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070043import org.onlab.onos.net.provider.AbstractProviderService;
44import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070045
tomd3097b02014-08-26 10:40:29 -070046/**
tome4729872014-09-23 00:37:37 -070047 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070048 */
49@Component(immediate = true)
50@Service
tom41a2c5f2014-09-19 09:20:35 -070051public class DeviceManager
alshabib271a6202014-09-28 22:11:21 -070052 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
53 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070054
tome5ec3fd2014-09-04 15:18:06 -070055 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
56 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
57 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
58 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
59 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070060
tom5f38b3a2014-08-27 23:50:54 -070061 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070062
alshabib271a6202014-09-28 22:11:21 -070063 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
64 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070065
alshabib339a3d92014-09-26 17:54:32 -070066 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070067
tomc78acee2014-09-24 15:16:55 -070068 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070069
tom41a2c5f2014-09-19 09:20:35 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070072
tom5f38b3a2014-08-27 23:50:54 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070074 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070075
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070077 protected ClusterService clusterService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070080 protected MastershipService mastershipService;
81
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070082 protected MastershipTermService termService;
83
Madan Jampani61056bc2014-09-27 09:07:26 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070085 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -070086
tomd3097b02014-08-26 10:40:29 -070087 @Activate
88 public void activate() {
tomf80c9722014-09-24 14:49:18 -070089 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -070090 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -070091 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070092 termService = mastershipService.requestTermService();
tomd3097b02014-08-26 10:40:29 -070093 log.info("Started");
94 }
95
96 @Deactivate
97 public void deactivate() {
tomf80c9722014-09-24 14:49:18 -070098 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -070099 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700100 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700101 log.info("Stopped");
102 }
103
104 @Override
tomad2d2092014-09-06 23:24:20 -0700105 public int getDeviceCount() {
106 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700107 }
108
109 @Override
tom32f66842014-08-27 19:27:47 -0700110 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700111 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700112 }
113
tom32f66842014-08-27 19:27:47 -0700114 @Override
115 public Device getDevice(DeviceId deviceId) {
116 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700117 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700118 }
119
120 @Override
tomad2d2092014-09-06 23:24:20 -0700121 public MastershipRole getRole(DeviceId deviceId) {
122 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700123 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700124 }
125
126 @Override
tom32f66842014-08-27 19:27:47 -0700127 public List<Port> getPorts(DeviceId deviceId) {
128 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700129 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700130 }
131
132 @Override
133 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
134 checkNotNull(deviceId, DEVICE_ID_NULL);
135 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700136 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700137 }
138
139 @Override
tomff7eb7c2014-09-08 12:49:03 -0700140 public boolean isAvailable(DeviceId deviceId) {
141 checkNotNull(deviceId, DEVICE_ID_NULL);
142 return store.isAvailable(deviceId);
143 }
144
tomb41d1ac2014-09-24 01:51:24 -0700145 // Applies the specified role to the device; ignores NONE
146 private void applyRole(DeviceId deviceId, MastershipRole newRole) {
Yuta HIGUCHId631e422014-10-19 00:18:59 -0700147 if (!newRole.equals(MastershipRole.NONE)) {
tomb41d1ac2014-09-24 01:51:24 -0700148 Device device = store.getDevice(deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700149 // FIXME: Device might not be there yet. (eventual consistent)
150 if (device == null) {
151 return;
152 }
tome5ec3fd2014-09-04 15:18:06 -0700153 DeviceProvider provider = getProvider(device.providerId());
154 if (provider != null) {
tom568581d2014-09-08 20:13:36 -0700155 provider.roleChanged(device, newRole);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700156
157 // only trigger event when request was sent to provider
158 // TODO: consider removing this from Device event type?
159 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
tome5ec3fd2014-09-04 15:18:06 -0700160 }
tome5ec3fd2014-09-04 15:18:06 -0700161 }
162 }
163
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700164 // Queries a device for port information.
165 private void queryPortInfo(DeviceId deviceId) {
166 Device device = store.getDevice(deviceId);
167 // FIXME: Device might not be there yet. (eventual consistent)
168 if (device == null) {
169 return;
170 }
171 DeviceProvider provider = getProvider(device.providerId());
172 provider.triggerProbe(device);
173 }
174
tome5ec3fd2014-09-04 15:18:06 -0700175 @Override
176 public void removeDevice(DeviceId deviceId) {
177 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700178 // XXX is this intended to apply to the full global topology?
179 // if so, we probably don't want the fact that we aren't
180 // MASTER to get in the way, as it would do now.
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700181 // FIXME: forward or broadcast and let the Master handler the event.
tome5ec3fd2014-09-04 15:18:06 -0700182 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700183 if (event != null) {
184 log.info("Device {} administratively removed", deviceId);
185 post(event);
186 }
tome5ec3fd2014-09-04 15:18:06 -0700187 }
188
tom7869ad92014-09-09 14:32:08 -0700189 @Override
190 public void addListener(DeviceListener listener) {
191 listenerRegistry.addListener(listener);
192 }
193
194 @Override
195 public void removeListener(DeviceListener listener) {
196 listenerRegistry.removeListener(listener);
197 }
198
199 @Override
alshabibb7b40632014-09-28 21:30:00 -0700200 protected DeviceProviderService createProviderService(
201 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700202 return new InternalDeviceProviderService(provider);
203 }
204
tomd3097b02014-08-26 10:40:29 -0700205 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700206 private class InternalDeviceProviderService
alshabib271a6202014-09-28 22:11:21 -0700207 extends AbstractProviderService<DeviceProvider>
208 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700209
tomcfde0622014-09-09 11:02:42 -0700210 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700211 super(provider);
212 }
213
214 @Override
alshabibb7b40632014-09-28 21:30:00 -0700215 public void deviceConnected(DeviceId deviceId,
216 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700217 checkNotNull(deviceId, DEVICE_ID_NULL);
218 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700219 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700220
221 log.info("Device {} connected", deviceId);
222 // check my Role
223 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700224 log.info("requestedRole, became {} for {}", role, deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700225 if (role != MastershipRole.MASTER) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700226 // TODO: Do we need to explicitly tell the Provider that
227 // this instance is no longer the MASTER? probably not
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700228// Device device = getDevice(deviceId);
229// if (device != null) {
230// // FIXME roleChanged should take DeviceId instead of Device
231// provider().roleChanged(device, role);
232// }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700233 return;
234 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700235 MastershipTerm term = mastershipService.requestTermService()
236 .getMastershipTerm(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700237
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700238 if (!term.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700239 // lost mastership after requestRole told this instance was MASTER.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700240 log.info("lost mastership before getting term info.");
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700241 return;
242 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700243
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700244 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700245 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700246
tome5ec3fd2014-09-04 15:18:06 -0700247 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
alshabib339a3d92014-09-26 17:54:32 -0700248 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700249
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700250 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700251 // that this instance is the master.
252 // Note: event can be null, if mastership was lost between
253 // roleRequest and store update calls.
tom80c0e5e2014-09-08 18:08:58 -0700254 if (event != null) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700255 // TODO: Check switch reconnected case. Is it assured that
256 // event will never be null?
257 // Could there be a situation MastershipService told this
258 // instance is the new Master, but
259 // event returned from the store is null?
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700260
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700261 // FIXME: 1st argument should be deviceId, to allow setting
262 // certain roles even if the store returned null.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700263 log.info("event: {} {}", event.type(), event);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700264 provider().roleChanged(event.subject(), role);
tom568581d2014-09-08 20:13:36 -0700265 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700266 } else {
267 log.info("No event to publish");
tom80c0e5e2014-09-08 18:08:58 -0700268 }
tomd3097b02014-08-26 10:40:29 -0700269 }
270
271 @Override
272 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700273 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700274 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700275
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700276
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700277 DeviceEvent event = null;
278 try {
279 event = store.markOffline(deviceId);
280 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700281 log.warn("Failed to mark {} offline", deviceId);
282 // only the MASTER should be marking off-line in normal cases,
283 // but if I was the last STANDBY connection, etc. and no one else
284 // was there to mark the device offline, this instance may need to
285 // temporarily request for Master Role and mark offline.
286
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700287 //there are times when this node will correctly have mastership, BUT
288 //that isn't reflected in the ClockManager before the device disconnects.
289 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700290
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700291 // FIXME: Store semantics leaking out as IllegalStateException.
292 // Consider revising store API to handle this scenario.
293
294 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700295 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700296 final NodeId myNodeId = clusterService.getLocalNode().id();
297 // TODO: Move this type of check inside device clock manager, etc.
298 if (myNodeId.equals(term.master())) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700299 log.info("Marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700300 deviceClockProviderService.setMastershipTerm(deviceId, term);
301 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700302 } else {
303 log.error("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700304 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700305 } finally {
306 //relinquish master role and ability to be backup.
307 mastershipService.relinquishMastership(deviceId);
308
309 if (event != null) {
310 log.info("Device {} disconnected", deviceId);
311 post(event);
312 }
tom0efbb1d2014-09-09 11:54:28 -0700313 }
tomd3097b02014-08-26 10:40:29 -0700314 }
315
316 @Override
alshabibb7b40632014-09-28 21:30:00 -0700317 public void updatePorts(DeviceId deviceId,
318 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700319 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700320 checkNotNull(portDescriptions,
321 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700322 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700323
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700324 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
325 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700326 for (DeviceEvent event : events) {
327 post(event);
328 }
tomd3097b02014-08-26 10:40:29 -0700329 }
330
331 @Override
alshabibb7b40632014-09-28 21:30:00 -0700332 public void portStatusChanged(DeviceId deviceId,
333 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700334 checkNotNull(deviceId, DEVICE_ID_NULL);
335 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700336 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700337
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700338 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
339 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700340 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700341 log.info("Device {} port {} status changed", deviceId, event
342 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700343 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700344 }
tomd3097b02014-08-26 10:40:29 -0700345 }
tom3f2bbd72014-09-24 12:07:58 -0700346
347 @Override
348 public void unableToAssertRole(DeviceId deviceId, MastershipRole role) {
349 // FIXME: implement response to this notification
alshabibb7b40632014-09-28 21:30:00 -0700350 log.warn("Failed to assert role [{}] onto Device {}", role,
351 deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700352 if (role == MastershipRole.MASTER) {
353 mastershipService.relinquishMastership(deviceId);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700354 // TODO: Shouldn't we be triggering event?
355 //final Device device = getDevice(deviceId);
356 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700357 }
tom3f2bbd72014-09-24 12:07:58 -0700358 }
tomd3097b02014-08-26 10:40:29 -0700359 }
tom32f66842014-08-27 19:27:47 -0700360
tomeadbb462014-09-07 16:10:19 -0700361 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700362 private void post(DeviceEvent event) {
363 if (event != null && eventDispatcher != null) {
364 eventDispatcher.post(event);
365 }
366 }
367
tomb41d1ac2014-09-24 01:51:24 -0700368 // Intercepts mastership events
Ayaka Koshibec4047702014-10-07 14:43:52 -0700369 private class InternalMastershipListener implements MastershipListener {
370
tomb41d1ac2014-09-24 01:51:24 -0700371 @Override
372 public void event(MastershipEvent event) {
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700373 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700374 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700375
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700376 if (myNodeId.equals(event.roleInfo().master())) {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700377 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700378
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700379 if (!myNodeId.equals(term.master())) {
380 // something went wrong in consistency, let go
Yuta HIGUCHI305ec832014-10-24 22:23:17 -0700381 log.warn("Mastership has changed after this event."
382 + "Term Service suggests {} for {}", term, did);
383 // FIXME: Is it possible to let go of MASTER role
384 // but remain on STANDBY list?
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700385 mastershipService.relinquishMastership(did);
386 applyRole(did, MastershipRole.STANDBY);
387 return;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700388 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700389
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700390 // only set the new term if I am the master
391 deviceClockProviderService.setMastershipTerm(did, term);
392
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700393 // FIXME: we should check that the device is connected on our end.
394 // currently, this is not straight forward as the actual switch
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700395 // implementation is hidden from the registry. Maybe we can ask the
396 // provider.
397 // if the device is null here, we are the first master to claim the
398 // device. No worries, the DeviceManager will create one soon.
399 Device device = getDevice(did);
400 if ((device != null) && !isAvailable(did)) {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700401 //flag the device as online. Is there a better way to do this?
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700402 DeviceEvent devEvent = store.createOrUpdateDevice(device.providerId(), did,
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700403 new DefaultDeviceDescription(
404 did.uri(), device.type(), device.manufacturer(),
405 device.hwVersion(), device.swVersion(),
alshabib7911a052014-10-16 17:49:37 -0700406 device.serialNumber(), device.chassisId()));
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700407 post(devEvent);
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700408 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700409 applyRole(did, MastershipRole.MASTER);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700410 // re-collect device information to fix potential staleness
411 queryPortInfo(did);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700412 } else if (event.roleInfo().backups().contains(myNodeId)) {
Ayaka Koshibec4047702014-10-07 14:43:52 -0700413 applyRole(did, MastershipRole.STANDBY);
tomb41d1ac2014-09-24 01:51:24 -0700414 }
415 }
416 }
tomf80c9722014-09-24 14:49:18 -0700417
418 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700419 private class InternalStoreDelegate
420 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700421 @Override
422 public void notify(DeviceEvent event) {
423 post(event);
424 }
425 }
tomd3097b02014-08-26 10:40:29 -0700426}