blob: 258f57cddb0894f8f93231128127714449251239 [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;
Yuta HIGUCHId26354d2014-10-31 14:14:38 -070020import static org.onlab.onos.net.MastershipRole.*;
alshabib339a3d92014-09-26 17:54:32 -070021import static org.slf4j.LoggerFactory.getLogger;
22
23import java.util.List;
24
tomd3097b02014-08-26 10:40:29 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070030import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070031import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070032import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070033import org.onlab.onos.event.AbstractListenerRegistry;
34import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070035import org.onlab.onos.mastership.MastershipEvent;
36import org.onlab.onos.mastership.MastershipListener;
37import org.onlab.onos.mastership.MastershipService;
38import org.onlab.onos.mastership.MastershipTerm;
39import org.onlab.onos.mastership.MastershipTermService;
tom32f66842014-08-27 19:27:47 -070040import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070041import org.onlab.onos.net.DeviceId;
42import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070043import org.onlab.onos.net.Port;
44import org.onlab.onos.net.PortNumber;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070045import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070046import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070047import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070048import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070049import org.onlab.onos.net.device.DeviceEvent;
50import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070051import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070052import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070053import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070054import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070055import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070056import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070057import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070058import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070059import org.onlab.onos.net.provider.AbstractProviderService;
60import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070061
tomd3097b02014-08-26 10:40:29 -070062/**
tome4729872014-09-23 00:37:37 -070063 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070064 */
65@Component(immediate = true)
66@Service
tom41a2c5f2014-09-19 09:20:35 -070067public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070068 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
69 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070070
tome5ec3fd2014-09-04 15:18:06 -070071 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
72 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
73 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
74 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
75 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070076
tom5f38b3a2014-08-27 23:50:54 -070077 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070078
alshabib271a6202014-09-28 22:11:21 -070079 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
80 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070081
alshabib339a3d92014-09-26 17:54:32 -070082 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070083
tomc78acee2014-09-24 15:16:55 -070084 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070085
tom41a2c5f2014-09-19 09:20:35 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070088
tom5f38b3a2014-08-27 23:50:54 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070090 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070091
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070093 protected ClusterService clusterService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070096 protected MastershipService mastershipService;
97
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070098 protected MastershipTermService termService;
99
Madan Jampani61056bc2014-09-27 09:07:26 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700101 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700102
tomd3097b02014-08-26 10:40:29 -0700103 @Activate
104 public void activate() {
tomf80c9722014-09-24 14:49:18 -0700105 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700106 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700107 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700108 termService = mastershipService.requestTermService();
tomd3097b02014-08-26 10:40:29 -0700109 log.info("Started");
110 }
111
112 @Deactivate
113 public void deactivate() {
tomf80c9722014-09-24 14:49:18 -0700114 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700115 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700116 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700117 log.info("Stopped");
118 }
119
120 @Override
tomad2d2092014-09-06 23:24:20 -0700121 public int getDeviceCount() {
122 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700123 }
124
125 @Override
tom32f66842014-08-27 19:27:47 -0700126 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700127 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700128 }
129
tom32f66842014-08-27 19:27:47 -0700130 @Override
131 public Device getDevice(DeviceId deviceId) {
132 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700133 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700134 }
135
136 @Override
tomad2d2092014-09-06 23:24:20 -0700137 public MastershipRole getRole(DeviceId deviceId) {
138 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700139 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700140 }
141
142 @Override
tom32f66842014-08-27 19:27:47 -0700143 public List<Port> getPorts(DeviceId deviceId) {
144 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700145 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700146 }
147
148 @Override
149 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
150 checkNotNull(deviceId, DEVICE_ID_NULL);
151 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700152 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700153 }
154
155 @Override
tomff7eb7c2014-09-08 12:49:03 -0700156 public boolean isAvailable(DeviceId deviceId) {
157 checkNotNull(deviceId, DEVICE_ID_NULL);
158 return store.isAvailable(deviceId);
159 }
160
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700161 // Check a device for control channel connectivity.
162 private boolean isReachable(Device device) {
163 // FIXME: Device might not be there yet. (eventual consistent)
164 if (device == null) {
165 return false;
166 }
167 DeviceProvider provider = getProvider(device.providerId());
168 return provider.isReachable(device);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700169 }
170
tome5ec3fd2014-09-04 15:18:06 -0700171 @Override
172 public void removeDevice(DeviceId deviceId) {
173 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700174 // XXX is this intended to apply to the full global topology?
175 // if so, we probably don't want the fact that we aren't
176 // MASTER to get in the way, as it would do now.
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700177 // FIXME: forward or broadcast and let the Master handler the event.
tome5ec3fd2014-09-04 15:18:06 -0700178 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700179 if (event != null) {
180 log.info("Device {} administratively removed", deviceId);
181 post(event);
182 }
tome5ec3fd2014-09-04 15:18:06 -0700183 }
184
tom7869ad92014-09-09 14:32:08 -0700185 @Override
186 public void addListener(DeviceListener listener) {
187 listenerRegistry.addListener(listener);
188 }
189
190 @Override
191 public void removeListener(DeviceListener listener) {
192 listenerRegistry.removeListener(listener);
193 }
194
195 @Override
alshabibb7b40632014-09-28 21:30:00 -0700196 protected DeviceProviderService createProviderService(
197 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700198 return new InternalDeviceProviderService(provider);
199 }
200
tomd3097b02014-08-26 10:40:29 -0700201 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700202 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700203 extends AbstractProviderService<DeviceProvider>
204 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700205
tomcfde0622014-09-09 11:02:42 -0700206 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700207 super(provider);
208 }
209
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700210 /**
211 * Apply role in reaction to provider event.
212 *
213 * @param deviceId device identifier
214 * @param newRole new role to apply to the device
215 * @return true if the request was sent to provider
216 */
217 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
218
219 if (newRole.equals(MastershipRole.NONE)) {
220 //no-op
221 return true;
222 }
223
224 DeviceProvider provider = provider();
225 if (provider == null) {
226 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
227 return false;
228 }
229 // FIXME roleChanged should take DeviceId instead of Device
230 Device device = store.getDevice(deviceId);
231 if (device == null) {
232 log.warn("{} was not there. Cannot apply role {}", deviceId, newRole);
233 return false;
234 }
235 provider.roleChanged(device, newRole);
236
237 // not triggering prove when triggered by provider service
238 return true;
239 }
240
241
tomd3097b02014-08-26 10:40:29 -0700242 @Override
alshabibb7b40632014-09-28 21:30:00 -0700243 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700244 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700245 checkNotNull(deviceId, DEVICE_ID_NULL);
246 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700247 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700248
249 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700250 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700251
252 // check my Role
253 mastershipService.requestRoleFor(deviceId);
254 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700255 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700256 log.info("Role of this node is STANDBY for {}", deviceId);
257 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700258 // this instance is not the MASTER
259 applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700260 return;
261 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700262 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700263
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700264 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700265 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700266
tome5ec3fd2014-09-04 15:18:06 -0700267 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700268 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700269
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700270 applyRole(deviceId, MastershipRole.MASTER);
271
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700272 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700273 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700274 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700275 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700276 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700277 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700278 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, store.getDevice(deviceId)));
tom80c0e5e2014-09-08 18:08:58 -0700279 }
tomd3097b02014-08-26 10:40:29 -0700280 }
281
282 @Override
283 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700284 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700285 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700286
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700287 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700288
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700289 DeviceEvent event = null;
290 try {
291 event = store.markOffline(deviceId);
292 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700293 log.warn("Failed to mark {} offline", deviceId);
294 // only the MASTER should be marking off-line in normal cases,
295 // but if I was the last STANDBY connection, etc. and no one else
296 // was there to mark the device offline, this instance may need to
297 // temporarily request for Master Role and mark offline.
298
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700299 //there are times when this node will correctly have mastership, BUT
300 //that isn't reflected in the ClockManager before the device disconnects.
301 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700302
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700303 // FIXME: Store semantics leaking out as IllegalStateException.
304 // Consider revising store API to handle this scenario.
305
306 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700307 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700308 final NodeId myNodeId = clusterService.getLocalNode().id();
309 // TODO: Move this type of check inside device clock manager, etc.
310 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700311 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700312 deviceClockProviderService.setMastershipTerm(deviceId, term);
313 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700314 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700315 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700316 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700317 } finally {
318 //relinquish master role and ability to be backup.
319 mastershipService.relinquishMastership(deviceId);
320
321 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700322 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700323 post(event);
324 }
tom0efbb1d2014-09-09 11:54:28 -0700325 }
tomd3097b02014-08-26 10:40:29 -0700326 }
327
328 @Override
alshabibb7b40632014-09-28 21:30:00 -0700329 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700330 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700331 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700332 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700333 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700334 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700335
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700336 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
337 // Never been a master for this device
338 // any update will be ignored.
339 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
340 return;
341 }
342
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700343 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700344 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,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700352 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 HIGUCHI13c0b872014-10-30 18:09:22 -0700357 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
358 // Never been a master for this device
359 // any update will be ignored.
360 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
361 return;
362 }
363
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700364 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700365 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700366 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700367 log.info("Device {} port {} status changed", deviceId, event
368 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700369 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700370 }
tomd3097b02014-08-26 10:40:29 -0700371 }
tom3f2bbd72014-09-24 12:07:58 -0700372
373 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700374 public void receivedRoleReply(
375 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
376 // Several things can happen here:
377 // 1. request and response match
378 // 2. request and response don't match
379 // 3. MastershipRole and requested match (and 1 or 2 are true)
380 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
381 //
382 // 2, 4, and 3 with case 2 are failure modes.
383
tom3f2bbd72014-09-24 12:07:58 -0700384 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700385
386 log.info("got reply to a role request for {}: asked for {}, and got {}",
387 deviceId, requested, response);
388
389 if (requested == null && response == null) {
390 // something was off with DeviceProvider, maybe check channel too?
391 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700392 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700393 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700394 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700395
396 if (requested.equals(response)) {
397 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
398
399 return;
400 } else {
401 return;
402 // FIXME roleManager got the device to comply, but doesn't agree with
403 // the store; use the store's view, then try to reassert.
404 }
405 } else {
406 // we didn't get back what we asked for. Reelect someone else.
407 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
408 if (response == MastershipRole.MASTER) {
409 mastershipService.relinquishMastership(deviceId);
410 // TODO: Shouldn't we be triggering event?
411 //final Device device = getDevice(deviceId);
412 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
413 }
414 }
415
tom3f2bbd72014-09-24 12:07:58 -0700416 }
tomd3097b02014-08-26 10:40:29 -0700417 }
tom32f66842014-08-27 19:27:47 -0700418
tomeadbb462014-09-07 16:10:19 -0700419 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700420 private void post(DeviceEvent event) {
421 if (event != null && eventDispatcher != null) {
422 eventDispatcher.post(event);
423 }
424 }
425
tomb41d1ac2014-09-24 01:51:24 -0700426 // Intercepts mastership events
Ayaka Koshibec4047702014-10-07 14:43:52 -0700427 private class InternalMastershipListener implements MastershipListener {
428
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700429 // Applies the specified role to the device; ignores NONE
430 /**
431 * Apply role in reaction to mastership event.
432 *
433 * @param deviceId device identifier
434 * @param newRole new role to apply to the device
435 * @return true if the request was sent to provider
436 */
437 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
438 if (newRole.equals(MastershipRole.NONE)) {
439 //no-op
440 return true;
441 }
442
443 Device device = store.getDevice(deviceId);
444 // FIXME: Device might not be there yet. (eventual consistent)
445 if (device == null) {
446 log.warn("{} was not there. Cannot apply role {}", deviceId, newRole);
447 return false;
448 }
449
450 DeviceProvider provider = getProvider(device.providerId());
451 if (provider == null) {
452 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
453 return false;
454 }
455 // FIXME roleChanged should take DeviceId instead of Device
456 provider.roleChanged(device, newRole);
457
458 if (newRole.equals(MastershipRole.MASTER)) {
459 // only trigger event when request was sent to provider
460 // TODO: consider removing this from Device event type?
461 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
462
463 provider.triggerProbe(device);
464 }
465 return true;
466 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700467
tomb41d1ac2014-09-24 01:51:24 -0700468 @Override
469 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700470
471 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
472 // Don't care if backup list changed.
473 return;
474 }
475
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700476 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700477 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700478
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700479 // myRole suggested by MastershipService
480 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700481 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700482 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700483 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700484 final boolean iHaveControl = myNodeId.equals(term.master());
485 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700486 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700487 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700488 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700489 myNextRole = STANDBY;
490 }
491 } else if (event.roleInfo().backups().contains(myNodeId)) {
492 myNextRole = STANDBY;
493 } else {
494 myNextRole = NONE;
495 }
496
497
498 final Device device = getDevice(did);
499 final boolean isReachable = isReachable(device);
500 if (!isReachable) {
501 // device is not connected to this node
502 if (myNextRole != NONE) {
503 log.warn("Node was instructed to be {} role for {}, "
504 + "but this node cannot reach the device. "
505 + "Relinquishing role. ",
506 myNextRole, did);
507 mastershipService.relinquishMastership(did);
508 }
509 return;
510 }
511
512 // device is connected to this node:
513
514 if (myNextRole == NONE) {
515 mastershipService.requestRoleFor(did);
516 MastershipTerm term = termService.getMastershipTerm(did);
517 if (myNodeId.equals(term.master())) {
518 myNextRole = MASTER;
519 } else {
520 myNextRole = STANDBY;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700521 }
tomb41d1ac2014-09-24 01:51:24 -0700522 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700523
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700524 switch (myNextRole) {
525 case MASTER:
526 if ((device != null) && !isAvailable(did)) {
527 //flag the device as online. Is there a better way to do this?
528 DefaultDeviceDescription deviceDescription
529 = new DefaultDeviceDescription(did.uri(),
530 device.type(),
531 device.manufacturer(),
532 device.hwVersion(),
533 device.swVersion(),
534 device.serialNumber(),
535 device.chassisId());
536 DeviceEvent devEvent =
537 store.createOrUpdateDevice(device.providerId(), did,
538 deviceDescription);
539 post(devEvent);
540 }
541 // TODO: should apply role only if there is mismatch
542 log.info("Applying role {} to {}", myNextRole, did);
543 applyRole(did, MASTER);
544 break;
545 case STANDBY:
546 log.info("Applying role {} to {}", myNextRole, did);
547 applyRole(did, STANDBY);
548 break;
549 case NONE:
550 default:
551 // should never reach here
552 log.error("You didn't see anything. I did not exist.");
553 break;
554 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700555 }
tomb41d1ac2014-09-24 01:51:24 -0700556 }
tomf80c9722014-09-24 14:49:18 -0700557
558 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700559 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700560 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700561 @Override
562 public void notify(DeviceEvent event) {
563 post(event);
564 }
565 }
tomd3097b02014-08-26 10:40:29 -0700566}