blob: 3559c4b5fe087be53052d8b40a497a9e31e14a2b [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) {
Yuta HIGUCHId631e422014-10-19 00:18:59 -0700162 if (!newRole.equals(MastershipRole.NONE)) {
tomb41d1ac2014-09-24 01:51:24 -0700163 Device device = store.getDevice(deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700164 // FIXME: Device might not be there yet. (eventual consistent)
165 if (device == null) {
166 return;
167 }
tome5ec3fd2014-09-04 15:18:06 -0700168 DeviceProvider provider = getProvider(device.providerId());
169 if (provider != null) {
tom568581d2014-09-08 20:13:36 -0700170 provider.roleChanged(device, newRole);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700171
172 // only trigger event when request was sent to provider
173 // TODO: consider removing this from Device event type?
174 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
tome5ec3fd2014-09-04 15:18:06 -0700175 }
tome5ec3fd2014-09-04 15:18:06 -0700176 }
177 }
178
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700179 // Queries a device for port information.
180 private void queryPortInfo(DeviceId deviceId) {
181 Device device = store.getDevice(deviceId);
182 // FIXME: Device might not be there yet. (eventual consistent)
183 if (device == null) {
184 return;
185 }
186 DeviceProvider provider = getProvider(device.providerId());
187 provider.triggerProbe(device);
188 }
189
tome5ec3fd2014-09-04 15:18:06 -0700190 @Override
191 public void removeDevice(DeviceId deviceId) {
192 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700193 // XXX is this intended to apply to the full global topology?
194 // if so, we probably don't want the fact that we aren't
195 // MASTER to get in the way, as it would do now.
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700196 // FIXME: forward or broadcast and let the Master handler the event.
tome5ec3fd2014-09-04 15:18:06 -0700197 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700198 if (event != null) {
199 log.info("Device {} administratively removed", deviceId);
200 post(event);
201 }
tome5ec3fd2014-09-04 15:18:06 -0700202 }
203
tom7869ad92014-09-09 14:32:08 -0700204 @Override
205 public void addListener(DeviceListener listener) {
206 listenerRegistry.addListener(listener);
207 }
208
209 @Override
210 public void removeListener(DeviceListener listener) {
211 listenerRegistry.removeListener(listener);
212 }
213
214 @Override
alshabibb7b40632014-09-28 21:30:00 -0700215 protected DeviceProviderService createProviderService(
216 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700217 return new InternalDeviceProviderService(provider);
218 }
219
tomd3097b02014-08-26 10:40:29 -0700220 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700221 private class InternalDeviceProviderService
alshabib271a6202014-09-28 22:11:21 -0700222 extends AbstractProviderService<DeviceProvider>
223 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700224
tomcfde0622014-09-09 11:02:42 -0700225 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700226 super(provider);
227 }
228
229 @Override
alshabibb7b40632014-09-28 21:30:00 -0700230 public void deviceConnected(DeviceId deviceId,
231 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700232 checkNotNull(deviceId, DEVICE_ID_NULL);
233 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700234 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700235
236 log.info("Device {} connected", deviceId);
237 // check my Role
238 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700239 log.info("requestedRole, became {} for {}", role, deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700240 if (role != MastershipRole.MASTER) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700241 // TODO: Do we need to explicitly tell the Provider that
242 // this instance is no longer the MASTER? probably not
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700243// Device device = getDevice(deviceId);
244// if (device != null) {
245// // FIXME roleChanged should take DeviceId instead of Device
246// provider().roleChanged(device, role);
247// }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700248 return;
249 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700250 MastershipTerm term = mastershipService.requestTermService()
251 .getMastershipTerm(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700252
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700253 if (!term.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700254 // lost mastership after requestRole told this instance was MASTER.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700255 log.info("lost mastership before getting term info.");
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700256 return;
257 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700258
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700259 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700260 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700261
tome5ec3fd2014-09-04 15:18:06 -0700262 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
alshabib339a3d92014-09-26 17:54:32 -0700263 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700264
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700265 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700266 // that this instance is the master.
267 // Note: event can be null, if mastership was lost between
268 // roleRequest and store update calls.
tom80c0e5e2014-09-08 18:08:58 -0700269 if (event != null) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700270 // TODO: Check switch reconnected case. Is it assured that
271 // event will never be null?
272 // Could there be a situation MastershipService told this
273 // instance is the new Master, but
274 // event returned from the store is null?
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700275
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700276 // FIXME: 1st argument should be deviceId, to allow setting
277 // certain roles even if the store returned null.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700278 log.info("event: {} {}", event.type(), event);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700279 provider().roleChanged(event.subject(), role);
tom568581d2014-09-08 20:13:36 -0700280 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700281 } else {
282 log.info("No event to publish");
tom80c0e5e2014-09-08 18:08:58 -0700283 }
tomd3097b02014-08-26 10:40:29 -0700284 }
285
286 @Override
287 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700288 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700289 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700290
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700291
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700292 DeviceEvent event = null;
293 try {
294 event = store.markOffline(deviceId);
295 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700296 log.warn("Failed to mark {} offline", deviceId);
297 // only the MASTER should be marking off-line in normal cases,
298 // but if I was the last STANDBY connection, etc. and no one else
299 // was there to mark the device offline, this instance may need to
300 // temporarily request for Master Role and mark offline.
301
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700302 //there are times when this node will correctly have mastership, BUT
303 //that isn't reflected in the ClockManager before the device disconnects.
304 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700305
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700306 // FIXME: Store semantics leaking out as IllegalStateException.
307 // Consider revising store API to handle this scenario.
308
309 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700310 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700311 final NodeId myNodeId = clusterService.getLocalNode().id();
312 // TODO: Move this type of check inside device clock manager, etc.
313 if (myNodeId.equals(term.master())) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700314 log.info("Marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700315 deviceClockProviderService.setMastershipTerm(deviceId, term);
316 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700317 } else {
318 log.error("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700319 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700320 } finally {
321 //relinquish master role and ability to be backup.
322 mastershipService.relinquishMastership(deviceId);
323
324 if (event != null) {
325 log.info("Device {} disconnected", deviceId);
326 post(event);
327 }
tom0efbb1d2014-09-09 11:54:28 -0700328 }
tomd3097b02014-08-26 10:40:29 -0700329 }
330
331 @Override
alshabibb7b40632014-09-28 21:30:00 -0700332 public void updatePorts(DeviceId deviceId,
333 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700334 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700335 checkNotNull(portDescriptions,
336 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700337 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700338
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700339 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
340 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700341 for (DeviceEvent event : events) {
342 post(event);
343 }
tomd3097b02014-08-26 10:40:29 -0700344 }
345
346 @Override
alshabibb7b40632014-09-28 21:30:00 -0700347 public void portStatusChanged(DeviceId deviceId,
348 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700349 checkNotNull(deviceId, DEVICE_ID_NULL);
350 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700351 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700352
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700353 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
354 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700355 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700356 log.info("Device {} port {} status changed", deviceId, event
357 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700358 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700359 }
tomd3097b02014-08-26 10:40:29 -0700360 }
tom3f2bbd72014-09-24 12:07:58 -0700361
362 @Override
363 public void unableToAssertRole(DeviceId deviceId, MastershipRole role) {
364 // FIXME: implement response to this notification
alshabibb7b40632014-09-28 21:30:00 -0700365 log.warn("Failed to assert role [{}] onto Device {}", role,
366 deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700367 if (role == MastershipRole.MASTER) {
368 mastershipService.relinquishMastership(deviceId);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700369 // TODO: Shouldn't we be triggering event?
370 //final Device device = getDevice(deviceId);
371 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700372 }
tom3f2bbd72014-09-24 12:07:58 -0700373 }
tomd3097b02014-08-26 10:40:29 -0700374 }
tom32f66842014-08-27 19:27:47 -0700375
tomeadbb462014-09-07 16:10:19 -0700376 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700377 private void post(DeviceEvent event) {
378 if (event != null && eventDispatcher != null) {
379 eventDispatcher.post(event);
380 }
381 }
382
tomb41d1ac2014-09-24 01:51:24 -0700383 // Intercepts mastership events
Ayaka Koshibec4047702014-10-07 14:43:52 -0700384 private class InternalMastershipListener implements MastershipListener {
385
tomb41d1ac2014-09-24 01:51:24 -0700386 @Override
387 public void event(MastershipEvent event) {
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700388 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700389 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700390
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700391 if (myNodeId.equals(event.roleInfo().master())) {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700392 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700393
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700394 if (!myNodeId.equals(term.master())) {
395 // something went wrong in consistency, let go
Yuta HIGUCHI305ec832014-10-24 22:23:17 -0700396 log.warn("Mastership has changed after this event."
397 + "Term Service suggests {} for {}", term, did);
398 // FIXME: Is it possible to let go of MASTER role
399 // but remain on STANDBY list?
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700400 mastershipService.relinquishMastership(did);
401 applyRole(did, MastershipRole.STANDBY);
402 return;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700403 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700404
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700405 // only set the new term if I am the master
406 deviceClockProviderService.setMastershipTerm(did, term);
407
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700408 // FIXME: we should check that the device is connected on our end.
409 // currently, this is not straight forward as the actual switch
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700410 // implementation is hidden from the registry. Maybe we can ask the
411 // provider.
412 // 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 Koshibeea5b4ce2014-10-11 14:17:17 -0700416 //flag the device as online. Is there a better way to do this?
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700417 DeviceEvent devEvent = store.createOrUpdateDevice(device.providerId(), did,
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700418 new DefaultDeviceDescription(
419 did.uri(), device.type(), device.manufacturer(),
420 device.hwVersion(), device.swVersion(),
alshabib7911a052014-10-16 17:49:37 -0700421 device.serialNumber(), device.chassisId()));
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700422 post(devEvent);
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700423 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700424 applyRole(did, MastershipRole.MASTER);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700425 // re-collect device information to fix potential staleness
426 queryPortInfo(did);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700427 } else if (event.roleInfo().backups().contains(myNodeId)) {
Ayaka Koshibec4047702014-10-07 14:43:52 -0700428 applyRole(did, MastershipRole.STANDBY);
tomb41d1ac2014-09-24 01:51:24 -0700429 }
430 }
431 }
tomf80c9722014-09-24 14:49:18 -0700432
433 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700434 private class InternalStoreDelegate
435 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700436 @Override
437 public void notify(DeviceEvent event) {
438 post(event);
439 }
440 }
tomd3097b02014-08-26 10:40:29 -0700441}