blob: f45d4de3ada7fb315284c66bc8819c00e8315d45 [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;
Yuta HIGUCHId26354d2014-10-31 14:14:38 -070019import static org.onlab.onos.net.MastershipRole.*;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080020import static org.onlab.util.Tools.namedThreads;
alshabib339a3d92014-09-26 17:54:32 -070021import static org.slf4j.LoggerFactory.getLogger;
22
23import java.util.List;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080024import java.util.concurrent.Executors;
25import java.util.concurrent.ScheduledExecutorService;
26import java.util.concurrent.TimeUnit;
alshabib339a3d92014-09-26 17:54:32 -070027
tomd3097b02014-08-26 10:40:29 -070028import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070031import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070033import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070034import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070035import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070036import org.onlab.onos.event.AbstractListenerRegistry;
37import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070038import org.onlab.onos.mastership.MastershipEvent;
39import org.onlab.onos.mastership.MastershipListener;
40import org.onlab.onos.mastership.MastershipService;
41import org.onlab.onos.mastership.MastershipTerm;
42import org.onlab.onos.mastership.MastershipTermService;
tom32f66842014-08-27 19:27:47 -070043import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070044import org.onlab.onos.net.DeviceId;
45import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070046import org.onlab.onos.net.Port;
47import org.onlab.onos.net.PortNumber;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070048import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070049import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070050import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070051import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070052import org.onlab.onos.net.device.DeviceEvent;
53import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070054import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070055import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070056import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070057import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070058import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070059import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070060import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070061import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070062import org.onlab.onos.net.provider.AbstractProviderService;
63import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070064
tomd3097b02014-08-26 10:40:29 -070065/**
tome4729872014-09-23 00:37:37 -070066 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070067 */
68@Component(immediate = true)
69@Service
tom41a2c5f2014-09-19 09:20:35 -070070public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070071 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
72 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070073
tome5ec3fd2014-09-04 15:18:06 -070074 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
75 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
76 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
77 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
78 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070079
tom5f38b3a2014-08-27 23:50:54 -070080 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070081
alshabib271a6202014-09-28 22:11:21 -070082 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
83 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070084
alshabib339a3d92014-09-26 17:54:32 -070085 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070086
tomc78acee2014-09-24 15:16:55 -070087 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070088
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080089 private ScheduledExecutorService backgroundService;
90
tom41a2c5f2014-09-19 09:20:35 -070091 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070093
tom5f38b3a2014-08-27 23:50:54 -070094 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070095 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070096
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070097 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070098 protected ClusterService clusterService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700101 protected MastershipService mastershipService;
102
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700103 protected MastershipTermService termService;
104
Madan Jampani61056bc2014-09-27 09:07:26 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700106 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700107
tomd3097b02014-08-26 10:40:29 -0700108 @Activate
109 public void activate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800110 backgroundService = Executors.newSingleThreadScheduledExecutor(namedThreads("device-manager-background"));
111
tomf80c9722014-09-24 14:49:18 -0700112 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700113 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700114 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700115 termService = mastershipService.requestTermService();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800116
117 backgroundService.scheduleWithFixedDelay(new Runnable() {
118
119 @Override
120 public void run() {
121 try {
122 mastershipCheck();
123 } catch (Exception e) {
124 log.error("Exception thrown during integrity check", e);
125 }
126 }
127 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700128 log.info("Started");
129 }
130
131 @Deactivate
132 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800133 backgroundService.shutdown();
134
tomf80c9722014-09-24 14:49:18 -0700135 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700136 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700137 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700138 log.info("Stopped");
139 }
140
141 @Override
tomad2d2092014-09-06 23:24:20 -0700142 public int getDeviceCount() {
143 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700144 }
145
146 @Override
tom32f66842014-08-27 19:27:47 -0700147 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700148 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700149 }
150
tom32f66842014-08-27 19:27:47 -0700151 @Override
152 public Device getDevice(DeviceId deviceId) {
153 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700154 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700155 }
156
157 @Override
tomad2d2092014-09-06 23:24:20 -0700158 public MastershipRole getRole(DeviceId deviceId) {
159 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700160 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700161 }
162
163 @Override
tom32f66842014-08-27 19:27:47 -0700164 public List<Port> getPorts(DeviceId deviceId) {
165 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700166 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700167 }
168
169 @Override
170 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
171 checkNotNull(deviceId, DEVICE_ID_NULL);
172 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700173 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700174 }
175
176 @Override
tomff7eb7c2014-09-08 12:49:03 -0700177 public boolean isAvailable(DeviceId deviceId) {
178 checkNotNull(deviceId, DEVICE_ID_NULL);
179 return store.isAvailable(deviceId);
180 }
181
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700182 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700183 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800184 if (deviceId == null) {
185 return false;
186 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700187 DeviceProvider provider = getProvider(deviceId);
188 if (provider != null) {
189 return provider.isReachable(deviceId);
190 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800191 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700192 return false;
193 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700194 }
195
tome5ec3fd2014-09-04 15:18:06 -0700196 @Override
197 public void removeDevice(DeviceId deviceId) {
198 checkNotNull(deviceId, DEVICE_ID_NULL);
199 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700200 if (event != null) {
201 log.info("Device {} administratively removed", deviceId);
202 post(event);
203 }
tome5ec3fd2014-09-04 15:18:06 -0700204 }
205
tom7869ad92014-09-09 14:32:08 -0700206 @Override
207 public void addListener(DeviceListener listener) {
208 listenerRegistry.addListener(listener);
209 }
210
211 @Override
212 public void removeListener(DeviceListener listener) {
213 listenerRegistry.removeListener(listener);
214 }
215
216 @Override
alshabibb7b40632014-09-28 21:30:00 -0700217 protected DeviceProviderService createProviderService(
218 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700219 return new InternalDeviceProviderService(provider);
220 }
221
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800222 /**
223 * Checks if all the reachable devices have a valid mastership role.
224 */
225 private void mastershipCheck() {
226 log.debug("Checking mastership");
227 for (Device device : getDevices()) {
228 final DeviceId deviceId = device.id();
229 log.debug("Checking device {}", deviceId);
230
231 if (!isReachable(deviceId)) {
232 continue;
233 }
234
235 if (mastershipService.getLocalRole(deviceId) != NONE) {
236 continue;
237 }
238
239 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
240
241 // isReachable but was not MASTER or STANDBY, get a role and apply
242 // Note: NONE triggers request to MastershipService
243 reassertRole(deviceId, NONE);
244 }
245 }
246
tomd3097b02014-08-26 10:40:29 -0700247 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700248 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700249 extends AbstractProviderService<DeviceProvider>
250 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700251
tomcfde0622014-09-09 11:02:42 -0700252 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700253 super(provider);
254 }
255
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700256 /**
257 * Apply role in reaction to provider event.
258 *
259 * @param deviceId device identifier
260 * @param newRole new role to apply to the device
261 * @return true if the request was sent to provider
262 */
263 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
264
265 if (newRole.equals(MastershipRole.NONE)) {
266 //no-op
267 return true;
268 }
269
270 DeviceProvider provider = provider();
271 if (provider == null) {
272 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
273 return false;
274 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700275 provider.roleChanged(deviceId, newRole);
276 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700277
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700278 return true;
279 }
280
281
tomd3097b02014-08-26 10:40:29 -0700282 @Override
alshabibb7b40632014-09-28 21:30:00 -0700283 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700284 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700285 checkNotNull(deviceId, DEVICE_ID_NULL);
286 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700287 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700288
289 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700290 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700291
292 // check my Role
293 mastershipService.requestRoleFor(deviceId);
294 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700295 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700296 log.info("Role of this node is STANDBY for {}", deviceId);
297 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700298 // this instance is not the MASTER
299 applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700300 return;
301 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700302 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700303
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700304 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700305 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700306
tome5ec3fd2014-09-04 15:18:06 -0700307 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700308 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700309
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700310 applyRole(deviceId, MastershipRole.MASTER);
311
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700312 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700313 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700314 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700315 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700316 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700317 }
tomd3097b02014-08-26 10:40:29 -0700318 }
319
320 @Override
321 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700322 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700323 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700324
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700325 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700326
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700327 DeviceEvent event = null;
328 try {
329 event = store.markOffline(deviceId);
330 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700331 log.warn("Failed to mark {} offline", deviceId);
332 // only the MASTER should be marking off-line in normal cases,
333 // but if I was the last STANDBY connection, etc. and no one else
334 // was there to mark the device offline, this instance may need to
335 // temporarily request for Master Role and mark offline.
336
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700337 //there are times when this node will correctly have mastership, BUT
338 //that isn't reflected in the ClockManager before the device disconnects.
339 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700340
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700341 // FIXME: Store semantics leaking out as IllegalStateException.
342 // Consider revising store API to handle this scenario.
343
344 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700345 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700346 final NodeId myNodeId = clusterService.getLocalNode().id();
347 // TODO: Move this type of check inside device clock manager, etc.
348 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700349 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700350 deviceClockProviderService.setMastershipTerm(deviceId, term);
351 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700352 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700353 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700354 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700355 } finally {
356 //relinquish master role and ability to be backup.
357 mastershipService.relinquishMastership(deviceId);
358
359 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700360 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700361 post(event);
362 }
tom0efbb1d2014-09-09 11:54:28 -0700363 }
tomd3097b02014-08-26 10:40:29 -0700364 }
365
366 @Override
alshabibb7b40632014-09-28 21:30:00 -0700367 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700368 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700369 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700370 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700371 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700372 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700373 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
374 // Never been a master for this device
375 // any update will be ignored.
376 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
377 return;
378 }
379
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700380 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700381 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700382 for (DeviceEvent event : events) {
383 post(event);
384 }
tomd3097b02014-08-26 10:40:29 -0700385 }
386
387 @Override
alshabibb7b40632014-09-28 21:30:00 -0700388 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700389 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700390 checkNotNull(deviceId, DEVICE_ID_NULL);
391 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700392 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700393
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700394 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
395 // Never been a master for this device
396 // any update will be ignored.
397 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
398 return;
399 }
400
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700401 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700402 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700403 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700404 log.info("Device {} port {} status changed", deviceId, event
405 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700406 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700407 }
tomd3097b02014-08-26 10:40:29 -0700408 }
tom3f2bbd72014-09-24 12:07:58 -0700409
410 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700411 public void receivedRoleReply(
412 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
413 // Several things can happen here:
414 // 1. request and response match
415 // 2. request and response don't match
416 // 3. MastershipRole and requested match (and 1 or 2 are true)
417 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
418 //
419 // 2, 4, and 3 with case 2 are failure modes.
420
tom3f2bbd72014-09-24 12:07:58 -0700421 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700422
423 log.info("got reply to a role request for {}: asked for {}, and got {}",
424 deviceId, requested, response);
425
426 if (requested == null && response == null) {
427 // something was off with DeviceProvider, maybe check channel too?
428 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700429 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700430 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700431 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700432
433 if (requested.equals(response)) {
434 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
435
436 return;
437 } else {
438 return;
439 // FIXME roleManager got the device to comply, but doesn't agree with
440 // the store; use the store's view, then try to reassert.
441 }
442 } else {
443 // we didn't get back what we asked for. Reelect someone else.
444 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
445 if (response == MastershipRole.MASTER) {
446 mastershipService.relinquishMastership(deviceId);
447 // TODO: Shouldn't we be triggering event?
448 //final Device device = getDevice(deviceId);
449 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
450 }
451 }
452
tom3f2bbd72014-09-24 12:07:58 -0700453 }
tomd3097b02014-08-26 10:40:29 -0700454 }
tom32f66842014-08-27 19:27:47 -0700455
tomeadbb462014-09-07 16:10:19 -0700456 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700457 private void post(DeviceEvent event) {
458 if (event != null && eventDispatcher != null) {
459 eventDispatcher.post(event);
460 }
461 }
462
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800463 // Applies the specified role to the device; ignores NONE
464 /**
465 * Apply role to device and send probe if MASTER.
466 *
467 * @param deviceId device identifier
468 * @param newRole new role to apply to the device
469 * @return true if the request was sent to provider
470 */
471 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
472 if (newRole.equals(MastershipRole.NONE)) {
473 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700474 return true;
475 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700476
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800477 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800478 if (provider == null) {
479 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
480 return false;
481 }
482 provider.roleChanged(deviceId, newRole);
483
484 if (newRole.equals(MastershipRole.MASTER)) {
485 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800486 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800487 }
488 return true;
489 }
490
491 /**
492 * Reaasert role for specified device connected to this node.
493 *
494 * @param did device identifier
495 * @param nextRole role to apply. If NONE is specified,
496 * it will ask mastership service for a role and apply it.
497 */
498 private void reassertRole(final DeviceId did,
499 final MastershipRole nextRole) {
500
501 final NodeId myNodeId = clusterService.getLocalNode().id();
502 MastershipRole myNextRole = nextRole;
503 if (myNextRole == NONE) {
504 mastershipService.requestRoleFor(did);
505 MastershipTerm term = termService.getMastershipTerm(did);
506 if (myNodeId.equals(term.master())) {
507 myNextRole = MASTER;
508 } else {
509 myNextRole = STANDBY;
510 }
511 }
512
513 switch (myNextRole) {
514 case MASTER:
515 final Device device = getDevice(did);
516 if ((device != null) && !isAvailable(did)) {
517 //flag the device as online. Is there a better way to do this?
518 DefaultDeviceDescription deviceDescription
519 = new DefaultDeviceDescription(did.uri(),
520 device.type(),
521 device.manufacturer(),
522 device.hwVersion(),
523 device.swVersion(),
524 device.serialNumber(),
525 device.chassisId());
526 DeviceEvent devEvent =
527 store.createOrUpdateDevice(device.providerId(), did,
528 deviceDescription);
529 post(devEvent);
530 }
531 // TODO: should apply role only if there is mismatch
532 log.info("Applying role {} to {}", myNextRole, did);
533 if (!applyRoleAndProbe(did, MASTER)) {
534 // immediately failed to apply role
535 mastershipService.relinquishMastership(did);
536 // FIXME disconnect?
537 }
538 break;
539 case STANDBY:
540 log.info("Applying role {} to {}", myNextRole, did);
541 if (!applyRoleAndProbe(did, STANDBY)) {
542 // immediately failed to apply role
543 mastershipService.relinquishMastership(did);
544 // FIXME disconnect?
545 }
546 break;
547 case NONE:
548 default:
549 // should never reach here
550 log.error("You didn't see anything. I did not exist.");
551 break;
552 }
553 }
554
555 // Intercepts mastership events
556 private class InternalMastershipListener implements MastershipListener {
557
tomb41d1ac2014-09-24 01:51:24 -0700558 @Override
559 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700560
561 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
562 // Don't care if backup list changed.
563 return;
564 }
565
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700566 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700567 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700568
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700569 // myRole suggested by MastershipService
570 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700571 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700572 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700573 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700574 final boolean iHaveControl = myNodeId.equals(term.master());
575 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700576 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700577 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700578 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700579 myNextRole = STANDBY;
580 }
581 } else if (event.roleInfo().backups().contains(myNodeId)) {
582 myNextRole = STANDBY;
583 } else {
584 myNextRole = NONE;
585 }
586
587
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700588 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700589 if (!isReachable) {
590 // device is not connected to this node
591 if (myNextRole != NONE) {
592 log.warn("Node was instructed to be {} role for {}, "
593 + "but this node cannot reach the device. "
594 + "Relinquishing role. ",
595 myNextRole, did);
596 mastershipService.relinquishMastership(did);
597 }
598 return;
599 }
600
601 // device is connected to this node:
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800602 reassertRole(did, myNextRole);
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700603 }
tomb41d1ac2014-09-24 01:51:24 -0700604 }
tomf80c9722014-09-24 14:49:18 -0700605
606 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700607 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700608 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700609 @Override
610 public void notify(DeviceEvent event) {
611 post(event);
612 }
613 }
tomd3097b02014-08-26 10:40:29 -0700614}