blob: e284052bcedeeaca436b53cdaebc9a871178017a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.device.impl;
tomd3097b02014-08-26 10:40:29 -070017
alshabib339a3d92014-09-26 17:54:32 -070018import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED;
20import static org.slf4j.LoggerFactory.getLogger;
21
22import java.util.List;
23
tomd3097b02014-08-26 10:40:29 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070027import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070029import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070030import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070031import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070032import org.onlab.onos.event.AbstractListenerRegistry;
33import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070034import org.onlab.onos.mastership.MastershipEvent;
35import org.onlab.onos.mastership.MastershipListener;
36import org.onlab.onos.mastership.MastershipService;
37import org.onlab.onos.mastership.MastershipTerm;
38import org.onlab.onos.mastership.MastershipTermService;
tom32f66842014-08-27 19:27:47 -070039import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070040import org.onlab.onos.net.DeviceId;
41import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070042import org.onlab.onos.net.Port;
43import org.onlab.onos.net.PortNumber;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070044import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070045import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070046import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070047import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070048import org.onlab.onos.net.device.DeviceEvent;
49import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070050import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070051import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070052import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070053import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070054import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070055import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070056import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070057import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070058import org.onlab.onos.net.provider.AbstractProviderService;
59import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070060
tomd3097b02014-08-26 10:40:29 -070061/**
tome4729872014-09-23 00:37:37 -070062 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070063 */
64@Component(immediate = true)
65@Service
tom41a2c5f2014-09-19 09:20:35 -070066public class DeviceManager
alshabib271a6202014-09-28 22:11:21 -070067 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
68 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070069
tome5ec3fd2014-09-04 15:18:06 -070070 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
71 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
72 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
73 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
74 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070075
tom5f38b3a2014-08-27 23:50:54 -070076 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070077
alshabib271a6202014-09-28 22:11:21 -070078 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
79 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070080
alshabib339a3d92014-09-26 17:54:32 -070081 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070082
tomc78acee2014-09-24 15:16:55 -070083 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070084
tom41a2c5f2014-09-19 09:20:35 -070085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070087
tom5f38b3a2014-08-27 23:50:54 -070088 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070089 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070090
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070091 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070092 protected ClusterService clusterService;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070095 protected MastershipService mastershipService;
96
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070097 protected MastershipTermService termService;
98
Madan Jampani61056bc2014-09-27 09:07:26 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700100 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700101
tomd3097b02014-08-26 10:40:29 -0700102 @Activate
103 public void activate() {
tomf80c9722014-09-24 14:49:18 -0700104 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700105 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700106 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700107 termService = mastershipService.requestTermService();
tomd3097b02014-08-26 10:40:29 -0700108 log.info("Started");
109 }
110
111 @Deactivate
112 public void deactivate() {
tomf80c9722014-09-24 14:49:18 -0700113 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700114 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700115 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700116 log.info("Stopped");
117 }
118
119 @Override
tomad2d2092014-09-06 23:24:20 -0700120 public int getDeviceCount() {
121 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700122 }
123
124 @Override
tom32f66842014-08-27 19:27:47 -0700125 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700126 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700127 }
128
tom32f66842014-08-27 19:27:47 -0700129 @Override
130 public Device getDevice(DeviceId deviceId) {
131 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700132 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700133 }
134
135 @Override
tomad2d2092014-09-06 23:24:20 -0700136 public MastershipRole getRole(DeviceId deviceId) {
137 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700138 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700139 }
140
141 @Override
tom32f66842014-08-27 19:27:47 -0700142 public List<Port> getPorts(DeviceId deviceId) {
143 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700144 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700145 }
146
147 @Override
148 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
149 checkNotNull(deviceId, DEVICE_ID_NULL);
150 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700151 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700152 }
153
154 @Override
tomff7eb7c2014-09-08 12:49:03 -0700155 public boolean isAvailable(DeviceId deviceId) {
156 checkNotNull(deviceId, DEVICE_ID_NULL);
157 return store.isAvailable(deviceId);
158 }
159
tomb41d1ac2014-09-24 01:51:24 -0700160 // Applies the specified role to the device; ignores NONE
161 private void applyRole(DeviceId deviceId, MastershipRole newRole) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700162 if (newRole.equals(MastershipRole.NONE)) {
163 return;
tome5ec3fd2014-09-04 15:18:06 -0700164 }
tome5ec3fd2014-09-04 15:18:06 -0700165
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700166 Device device = store.getDevice(deviceId);
167 // FIXME: Device might not be there yet. (eventual consistent)
168 if (device == null) {
169 return;
170 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700171
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700172 DeviceProvider provider = getProvider(device.providerId());
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700173 if (provider != null) {
174 provider.roleChanged(device, newRole);
175 // only trigger event when request was sent to provider
176 // TODO: consider removing this from Device event type?
177 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
178
179 if (newRole.equals(MastershipRole.MASTER)) {
180 provider.triggerProbe(device);
181 }
182 }
183 }
184
185 // Check a device for control channel connectivity.
186 private boolean isReachable(Device device) {
187 // FIXME: Device might not be there yet. (eventual consistent)
188 if (device == null) {
189 return false;
190 }
191 DeviceProvider provider = getProvider(device.providerId());
192 return provider.isReachable(device);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700193 }
194
tome5ec3fd2014-09-04 15:18:06 -0700195 @Override
196 public void removeDevice(DeviceId deviceId) {
197 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700198 // XXX is this intended to apply to the full global topology?
199 // if so, we probably don't want the fact that we aren't
200 // MASTER to get in the way, as it would do now.
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700201 // FIXME: forward or broadcast and let the Master handler the event.
tome5ec3fd2014-09-04 15:18:06 -0700202 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700203 if (event != null) {
204 log.info("Device {} administratively removed", deviceId);
205 post(event);
206 }
tome5ec3fd2014-09-04 15:18:06 -0700207 }
208
tom7869ad92014-09-09 14:32:08 -0700209 @Override
210 public void addListener(DeviceListener listener) {
211 listenerRegistry.addListener(listener);
212 }
213
214 @Override
215 public void removeListener(DeviceListener listener) {
216 listenerRegistry.removeListener(listener);
217 }
218
219 @Override
alshabibb7b40632014-09-28 21:30:00 -0700220 protected DeviceProviderService createProviderService(
221 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700222 return new InternalDeviceProviderService(provider);
223 }
224
tomd3097b02014-08-26 10:40:29 -0700225 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700226 private class InternalDeviceProviderService
alshabib271a6202014-09-28 22:11:21 -0700227 extends AbstractProviderService<DeviceProvider>
228 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700229
tomcfde0622014-09-09 11:02:42 -0700230 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700231 super(provider);
232 }
233
234 @Override
alshabibb7b40632014-09-28 21:30:00 -0700235 public void deviceConnected(DeviceId deviceId,
236 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700237 checkNotNull(deviceId, DEVICE_ID_NULL);
238 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700239 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700240
241 log.info("Device {} connected", deviceId);
242 // check my Role
243 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700244 if (role != MastershipRole.MASTER) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700245 // TODO: Do we need to explicitly tell the Provider that
246 // this instance is no longer the MASTER? probably not
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700247// Device device = getDevice(deviceId);
248// if (device != null) {
249// // FIXME roleChanged should take DeviceId instead of Device
250// provider().roleChanged(device, role);
251// }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700252 return;
253 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700254 MastershipTerm term = mastershipService.requestTermService()
255 .getMastershipTerm(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700256
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700257 if (!term.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700258 // lost mastership after requestRole told this instance was MASTER.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700259 log.info("lost mastership before getting term info.");
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700260 return;
261 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700262
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700263 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700264 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700265
tome5ec3fd2014-09-04 15:18:06 -0700266 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
alshabib339a3d92014-09-26 17:54:32 -0700267 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700268
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700269 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700270 // that this instance is the master.
271 // Note: event can be null, if mastership was lost between
272 // roleRequest and store update calls.
tom80c0e5e2014-09-08 18:08:58 -0700273 if (event != null) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700274 // TODO: Check switch reconnected case. Is it assured that
275 // event will never be null?
276 // Could there be a situation MastershipService told this
277 // instance is the new Master, but
278 // event returned from the store is null?
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700279
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700280 // FIXME: 1st argument should be deviceId, to allow setting
281 // certain roles even if the store returned null.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700282 log.info("event: {} {}", event.type(), event);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700283 provider().roleChanged(event.subject(), role);
tom568581d2014-09-08 20:13:36 -0700284 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700285 } else {
286 log.info("No event to publish");
tom80c0e5e2014-09-08 18:08:58 -0700287 }
tomd3097b02014-08-26 10:40:29 -0700288 }
289
290 @Override
291 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700292 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700293 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700294
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700295
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700296 DeviceEvent event = null;
297 try {
298 event = store.markOffline(deviceId);
299 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700300 log.warn("Failed to mark {} offline", deviceId);
301 // only the MASTER should be marking off-line in normal cases,
302 // but if I was the last STANDBY connection, etc. and no one else
303 // was there to mark the device offline, this instance may need to
304 // temporarily request for Master Role and mark offline.
305
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700306 //there are times when this node will correctly have mastership, BUT
307 //that isn't reflected in the ClockManager before the device disconnects.
308 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700309
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700310 // FIXME: Store semantics leaking out as IllegalStateException.
311 // Consider revising store API to handle this scenario.
312
313 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700314 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700315 final NodeId myNodeId = clusterService.getLocalNode().id();
316 // TODO: Move this type of check inside device clock manager, etc.
317 if (myNodeId.equals(term.master())) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700318 log.info("Marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700319 deviceClockProviderService.setMastershipTerm(deviceId, term);
320 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700321 } else {
322 log.error("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700323 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700324 } finally {
325 //relinquish master role and ability to be backup.
326 mastershipService.relinquishMastership(deviceId);
327
328 if (event != null) {
329 log.info("Device {} disconnected", deviceId);
330 post(event);
331 }
tom0efbb1d2014-09-09 11:54:28 -0700332 }
tomd3097b02014-08-26 10:40:29 -0700333 }
334
335 @Override
alshabibb7b40632014-09-28 21:30:00 -0700336 public void updatePorts(DeviceId deviceId,
337 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700338 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700339 checkNotNull(portDescriptions,
340 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700341 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700342
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700343 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
344 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700345 for (DeviceEvent event : events) {
346 post(event);
347 }
tomd3097b02014-08-26 10:40:29 -0700348 }
349
350 @Override
alshabibb7b40632014-09-28 21:30:00 -0700351 public void portStatusChanged(DeviceId deviceId,
352 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700353 checkNotNull(deviceId, DEVICE_ID_NULL);
354 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700355 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700356
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700357 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
358 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700359 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700360 log.info("Device {} port {} status changed", deviceId, event
361 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700362 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700363 }
tomd3097b02014-08-26 10:40:29 -0700364 }
tom3f2bbd72014-09-24 12:07:58 -0700365
366 @Override
367 public void unableToAssertRole(DeviceId deviceId, MastershipRole role) {
368 // FIXME: implement response to this notification
alshabibb7b40632014-09-28 21:30:00 -0700369 log.warn("Failed to assert role [{}] onto Device {}", role,
370 deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700371 if (role == MastershipRole.MASTER) {
372 mastershipService.relinquishMastership(deviceId);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700373 // TODO: Shouldn't we be triggering event?
374 //final Device device = getDevice(deviceId);
375 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700376 }
tom3f2bbd72014-09-24 12:07:58 -0700377 }
tomd3097b02014-08-26 10:40:29 -0700378 }
tom32f66842014-08-27 19:27:47 -0700379
tomeadbb462014-09-07 16:10:19 -0700380 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700381 private void post(DeviceEvent event) {
382 if (event != null && eventDispatcher != null) {
383 eventDispatcher.post(event);
384 }
385 }
386
tomb41d1ac2014-09-24 01:51:24 -0700387 // Intercepts mastership events
Ayaka Koshibec4047702014-10-07 14:43:52 -0700388 private class InternalMastershipListener implements MastershipListener {
389
tomb41d1ac2014-09-24 01:51:24 -0700390 @Override
391 public void event(MastershipEvent event) {
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700392 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700393 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700394
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700395 if (myNodeId.equals(event.roleInfo().master())) {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700396 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700397
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700398 if (!myNodeId.equals(term.master())) {
399 // something went wrong in consistency, let go
Yuta HIGUCHI305ec832014-10-24 22:23:17 -0700400 log.warn("Mastership has changed after this event."
401 + "Term Service suggests {} for {}", term, did);
402 // FIXME: Is it possible to let go of MASTER role
403 // but remain on STANDBY list?
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700404 mastershipService.relinquishMastership(did);
405 applyRole(did, MastershipRole.STANDBY);
406 return;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700407 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700408
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700409 // only set the new term if I am the master
410 deviceClockProviderService.setMastershipTerm(did, term);
411
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700412 // if the device is null here, we are the first master to claim the
413 // device. No worries, the DeviceManager will create one soon.
414 Device device = getDevice(did);
415 if ((device != null) && !isAvailable(did)) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700416 if (!isReachable(device)) {
417 log.warn("Device {} has disconnected after this event", did);
418 mastershipService.relinquishMastership(did);
419 applyRole(did, MastershipRole.STANDBY);
420 return;
421 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700422 //flag the device as online. Is there a better way to do this?
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700423 DeviceEvent devEvent = store.createOrUpdateDevice(device.providerId(), did,
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700424 new DefaultDeviceDescription(
425 did.uri(), device.type(), device.manufacturer(),
426 device.hwVersion(), device.swVersion(),
alshabib7911a052014-10-16 17:49:37 -0700427 device.serialNumber(), device.chassisId()));
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700428 post(devEvent);
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700429 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700430 applyRole(did, MastershipRole.MASTER);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700431 } else if (event.roleInfo().backups().contains(myNodeId)) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700432 if (!isReachable(getDevice(did))) {
433 log.warn("Device {} has disconnected after this event", did);
434 mastershipService.relinquishMastership(did);
435 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700436 applyRole(did, MastershipRole.STANDBY);
tomb41d1ac2014-09-24 01:51:24 -0700437 }
438 }
439 }
tomf80c9722014-09-24 14:49:18 -0700440
441 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700442 private class InternalStoreDelegate
443 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700444 @Override
445 public void notify(DeviceEvent event) {
446 post(event);
447 }
448 }
tomd3097b02014-08-26 10:40:29 -0700449}