blob: 5cdaf99e7ab4c070ad5610b10fc3b22c40cc1193 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.device.impl;
tomd3097b02014-08-26 10:40:29 -070017
alshabibafc514a2014-12-01 14:44:05 -080018import com.google.common.collect.Lists;
tomd3097b02014-08-26 10:40:29 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070024import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.event.AbstractListenerRegistry;
28import org.onosproject.event.EventDeliveryService;
29import org.onosproject.mastership.MastershipEvent;
30import org.onosproject.mastership.MastershipListener;
31import org.onosproject.mastership.MastershipService;
32import org.onosproject.mastership.MastershipTerm;
33import org.onosproject.mastership.MastershipTermService;
34import org.onosproject.net.Device;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.MastershipRole;
37import org.onosproject.net.Port;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.device.DefaultDeviceDescription;
40import org.onosproject.net.device.DefaultPortDescription;
41import org.onosproject.net.device.DeviceAdminService;
42import org.onosproject.net.device.DeviceClockProviderService;
43import org.onosproject.net.device.DeviceDescription;
44import org.onosproject.net.device.DeviceEvent;
45import org.onosproject.net.device.DeviceListener;
46import org.onosproject.net.device.DeviceProvider;
47import org.onosproject.net.device.DeviceProviderRegistry;
48import org.onosproject.net.device.DeviceProviderService;
49import org.onosproject.net.device.DeviceService;
50import org.onosproject.net.device.DeviceStore;
51import org.onosproject.net.device.DeviceStoreDelegate;
52import org.onosproject.net.device.PortDescription;
53import org.onosproject.net.provider.AbstractProviderRegistry;
54import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070055import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070056
Jonathan Hart2f669362015-02-11 16:19:20 -080057import java.util.List;
Jonathan Hart2f669362015-02-11 16:19:20 -080058import java.util.concurrent.ScheduledExecutorService;
59import java.util.concurrent.TimeUnit;
60
61import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080062import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
63import static org.onlab.util.Tools.groupedThreads;
64import static org.onosproject.net.MastershipRole.*;
Jonathan Hart2f669362015-02-11 16:19:20 -080065import static org.slf4j.LoggerFactory.getLogger;
66
tomd3097b02014-08-26 10:40:29 -070067/**
tome4729872014-09-23 00:37:37 -070068 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070069 */
70@Component(immediate = true)
71@Service
tom41a2c5f2014-09-19 09:20:35 -070072public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070073 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
74 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070075
tome5ec3fd2014-09-04 15:18:06 -070076 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
77 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
78 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
79 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
80 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070081
tom5f38b3a2014-08-27 23:50:54 -070082 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070083
alshabib271a6202014-09-28 22:11:21 -070084 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
85 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070086
alshabib339a3d92014-09-26 17:54:32 -070087 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070088
tomc78acee2014-09-24 15:16:55 -070089 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070090
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080091 private ScheduledExecutorService backgroundService;
92
tom41a2c5f2014-09-19 09:20:35 -070093 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070095
tom5f38b3a2014-08-27 23:50:54 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070097 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070098
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700100 protected ClusterService clusterService;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700103 protected MastershipService mastershipService;
104
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700106 protected MastershipTermService termService;
107
Madan Jampani61056bc2014-09-27 09:07:26 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700109 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700110
tomd3097b02014-08-26 10:40:29 -0700111 @Activate
112 public void activate() {
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800113 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background"));
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800114
tomf80c9722014-09-24 14:49:18 -0700115 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700116 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700117 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800118
119 backgroundService.scheduleWithFixedDelay(new Runnable() {
120
121 @Override
122 public void run() {
123 try {
124 mastershipCheck();
125 } catch (Exception e) {
126 log.error("Exception thrown during integrity check", e);
127 }
128 }
129 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700130 log.info("Started");
131 }
132
133 @Deactivate
134 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800135 backgroundService.shutdown();
136
tomf80c9722014-09-24 14:49:18 -0700137 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700138 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700139 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700140 log.info("Stopped");
141 }
142
143 @Override
tomad2d2092014-09-06 23:24:20 -0700144 public int getDeviceCount() {
145 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700146 }
147
148 @Override
tom32f66842014-08-27 19:27:47 -0700149 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700150 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700151 }
152
tom32f66842014-08-27 19:27:47 -0700153 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800154 public Iterable<Device> getAvailableDevices() {
155 return store.getAvailableDevices();
156 }
157
158 @Override
tom32f66842014-08-27 19:27:47 -0700159 public Device getDevice(DeviceId deviceId) {
160 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700161 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700162 }
163
164 @Override
tomad2d2092014-09-06 23:24:20 -0700165 public MastershipRole getRole(DeviceId deviceId) {
166 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700167 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700168 }
169
170 @Override
tom32f66842014-08-27 19:27:47 -0700171 public List<Port> getPorts(DeviceId deviceId) {
172 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700173 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700174 }
175
176 @Override
177 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
178 checkNotNull(deviceId, DEVICE_ID_NULL);
179 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700180 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700181 }
182
183 @Override
tomff7eb7c2014-09-08 12:49:03 -0700184 public boolean isAvailable(DeviceId deviceId) {
185 checkNotNull(deviceId, DEVICE_ID_NULL);
186 return store.isAvailable(deviceId);
187 }
188
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700189 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700190 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800191 if (deviceId == null) {
192 return false;
193 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700194 DeviceProvider provider = getProvider(deviceId);
195 if (provider != null) {
196 return provider.isReachable(deviceId);
197 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800198 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700199 return false;
200 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700201 }
202
tome5ec3fd2014-09-04 15:18:06 -0700203 @Override
204 public void removeDevice(DeviceId deviceId) {
205 checkNotNull(deviceId, DEVICE_ID_NULL);
206 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700207 if (event != null) {
208 log.info("Device {} administratively removed", deviceId);
209 post(event);
210 }
tome5ec3fd2014-09-04 15:18:06 -0700211 }
212
tom7869ad92014-09-09 14:32:08 -0700213 @Override
214 public void addListener(DeviceListener listener) {
215 listenerRegistry.addListener(listener);
216 }
217
218 @Override
219 public void removeListener(DeviceListener listener) {
220 listenerRegistry.removeListener(listener);
221 }
222
223 @Override
alshabibb7b40632014-09-28 21:30:00 -0700224 protected DeviceProviderService createProviderService(
225 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700226 return new InternalDeviceProviderService(provider);
227 }
228
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800229 /**
230 * Checks if all the reachable devices have a valid mastership role.
231 */
232 private void mastershipCheck() {
233 log.debug("Checking mastership");
234 for (Device device : getDevices()) {
235 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800236 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800237
238 if (!isReachable(deviceId)) {
239 continue;
240 }
241
242 if (mastershipService.getLocalRole(deviceId) != NONE) {
243 continue;
244 }
245
246 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
247
248 // isReachable but was not MASTER or STANDBY, get a role and apply
249 // Note: NONE triggers request to MastershipService
250 reassertRole(deviceId, NONE);
251 }
252 }
253
tomd3097b02014-08-26 10:40:29 -0700254 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700255 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700256 extends AbstractProviderService<DeviceProvider>
257 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700258
tomcfde0622014-09-09 11:02:42 -0700259 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700260 super(provider);
261 }
262
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700263 /**
264 * Apply role in reaction to provider event.
265 *
266 * @param deviceId device identifier
267 * @param newRole new role to apply to the device
268 * @return true if the request was sent to provider
269 */
270 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
271
272 if (newRole.equals(MastershipRole.NONE)) {
273 //no-op
274 return true;
275 }
276
277 DeviceProvider provider = provider();
278 if (provider == null) {
279 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
280 return false;
281 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700282 provider.roleChanged(deviceId, newRole);
283 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700284
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700285 return true;
286 }
287
288
tomd3097b02014-08-26 10:40:29 -0700289 @Override
alshabibb7b40632014-09-28 21:30:00 -0700290 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700291 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700292 checkNotNull(deviceId, DEVICE_ID_NULL);
293 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700294 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700295
296 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700297 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700298
299 // check my Role
300 mastershipService.requestRoleFor(deviceId);
301 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700302 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700303 log.info("Role of this node is STANDBY for {}", deviceId);
304 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700305 // this instance is not the MASTER
306 applyRole(deviceId, MastershipRole.STANDBY);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800307 } else {
308 log.info("Role of this node is MASTER for {}", deviceId);
309 // tell clock provider if this instance is the master
310 deviceClockProviderService.setMastershipTerm(deviceId, term);
311 applyRole(deviceId, MastershipRole.MASTER);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700312 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700313
tome5ec3fd2014-09-04 15:18:06 -0700314 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700315 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700316
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700317 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700318 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700319 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700320 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700321 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700322 }
tomd3097b02014-08-26 10:40:29 -0700323 }
324
325 @Override
326 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700327 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700328 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700329
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700330 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700331
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700332 DeviceEvent event = null;
alshabibafc514a2014-12-01 14:44:05 -0800333 List<Port> ports = store.getPorts(deviceId);
334 List<PortDescription> descs = Lists.newArrayList();
335 ports.forEach(port ->
336 descs.add(new DefaultPortDescription(port.number(),
337 false, port.type(),
338 port.portSpeed())));
339 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700340 try {
341 event = store.markOffline(deviceId);
342 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700343 log.warn("Failed to mark {} offline", deviceId);
344 // only the MASTER should be marking off-line in normal cases,
345 // but if I was the last STANDBY connection, etc. and no one else
346 // was there to mark the device offline, this instance may need to
347 // temporarily request for Master Role and mark offline.
348
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700349 //there are times when this node will correctly have mastership, BUT
350 //that isn't reflected in the ClockManager before the device disconnects.
351 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700352
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700353 // FIXME: Store semantics leaking out as IllegalStateException.
354 // Consider revising store API to handle this scenario.
355
356 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700357 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700358 final NodeId myNodeId = clusterService.getLocalNode().id();
359 // TODO: Move this type of check inside device clock manager, etc.
360 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700361 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700362 deviceClockProviderService.setMastershipTerm(deviceId, term);
363 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700364 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700365 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700366 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700367 } finally {
368 //relinquish master role and ability to be backup.
369 mastershipService.relinquishMastership(deviceId);
370
371 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700372 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700373 post(event);
374 }
tom0efbb1d2014-09-09 11:54:28 -0700375 }
tomd3097b02014-08-26 10:40:29 -0700376 }
377
378 @Override
alshabibb7b40632014-09-28 21:30:00 -0700379 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700380 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700381 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700382 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700383 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700384 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700385 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
386 // Never been a master for this device
387 // any update will be ignored.
388 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
389 return;
390 }
391
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700392 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700393 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700394 for (DeviceEvent event : events) {
395 post(event);
396 }
tomd3097b02014-08-26 10:40:29 -0700397 }
398
399 @Override
alshabibb7b40632014-09-28 21:30:00 -0700400 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700401 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700402 checkNotNull(deviceId, DEVICE_ID_NULL);
403 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700404 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700405
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700406 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
407 // Never been a master for this device
408 // any update will be ignored.
409 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
410 return;
411 }
412
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700413 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700414 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700415 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700416 log.info("Device {} port {} status changed", deviceId, event
417 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700418 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700419 }
tomd3097b02014-08-26 10:40:29 -0700420 }
tom3f2bbd72014-09-24 12:07:58 -0700421
422 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700423 public void receivedRoleReply(
424 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
425 // Several things can happen here:
426 // 1. request and response match
427 // 2. request and response don't match
428 // 3. MastershipRole and requested match (and 1 or 2 are true)
429 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
430 //
431 // 2, 4, and 3 with case 2 are failure modes.
432
tom3f2bbd72014-09-24 12:07:58 -0700433 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700434
435 log.info("got reply to a role request for {}: asked for {}, and got {}",
436 deviceId, requested, response);
437
438 if (requested == null && response == null) {
439 // something was off with DeviceProvider, maybe check channel too?
440 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700441 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700442 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700443 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700444
445 if (requested.equals(response)) {
446 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
447
448 return;
449 } else {
450 return;
451 // FIXME roleManager got the device to comply, but doesn't agree with
452 // the store; use the store's view, then try to reassert.
453 }
454 } else {
455 // we didn't get back what we asked for. Reelect someone else.
456 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
457 if (response == MastershipRole.MASTER) {
458 mastershipService.relinquishMastership(deviceId);
459 // TODO: Shouldn't we be triggering event?
460 //final Device device = getDevice(deviceId);
461 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
462 }
463 }
464
tom3f2bbd72014-09-24 12:07:58 -0700465 }
tomd3097b02014-08-26 10:40:29 -0700466 }
tom32f66842014-08-27 19:27:47 -0700467
tomeadbb462014-09-07 16:10:19 -0700468 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700469 private void post(DeviceEvent event) {
470 if (event != null && eventDispatcher != null) {
471 eventDispatcher.post(event);
472 }
473 }
474
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800475 // Applies the specified role to the device; ignores NONE
476 /**
477 * Apply role to device and send probe if MASTER.
478 *
479 * @param deviceId device identifier
480 * @param newRole new role to apply to the device
481 * @return true if the request was sent to provider
482 */
483 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
484 if (newRole.equals(MastershipRole.NONE)) {
485 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700486 return true;
487 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700488
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800489 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800490 if (provider == null) {
491 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
492 return false;
493 }
494 provider.roleChanged(deviceId, newRole);
495
496 if (newRole.equals(MastershipRole.MASTER)) {
497 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800498 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800499 }
500 return true;
501 }
502
503 /**
504 * Reaasert role for specified device connected to this node.
505 *
506 * @param did device identifier
507 * @param nextRole role to apply. If NONE is specified,
508 * it will ask mastership service for a role and apply it.
509 */
510 private void reassertRole(final DeviceId did,
511 final MastershipRole nextRole) {
512
513 final NodeId myNodeId = clusterService.getLocalNode().id();
514 MastershipRole myNextRole = nextRole;
515 if (myNextRole == NONE) {
516 mastershipService.requestRoleFor(did);
517 MastershipTerm term = termService.getMastershipTerm(did);
518 if (myNodeId.equals(term.master())) {
519 myNextRole = MASTER;
520 } else {
521 myNextRole = STANDBY;
522 }
523 }
524
525 switch (myNextRole) {
526 case MASTER:
527 final Device device = getDevice(did);
528 if ((device != null) && !isAvailable(did)) {
529 //flag the device as online. Is there a better way to do this?
530 DefaultDeviceDescription deviceDescription
531 = new DefaultDeviceDescription(did.uri(),
532 device.type(),
533 device.manufacturer(),
534 device.hwVersion(),
535 device.swVersion(),
536 device.serialNumber(),
537 device.chassisId());
538 DeviceEvent devEvent =
539 store.createOrUpdateDevice(device.providerId(), did,
540 deviceDescription);
541 post(devEvent);
542 }
543 // TODO: should apply role only if there is mismatch
544 log.info("Applying role {} to {}", myNextRole, did);
545 if (!applyRoleAndProbe(did, MASTER)) {
546 // immediately failed to apply role
547 mastershipService.relinquishMastership(did);
548 // FIXME disconnect?
549 }
550 break;
551 case STANDBY:
552 log.info("Applying role {} to {}", myNextRole, did);
553 if (!applyRoleAndProbe(did, STANDBY)) {
554 // immediately failed to apply role
555 mastershipService.relinquishMastership(did);
556 // FIXME disconnect?
557 }
558 break;
559 case NONE:
560 default:
561 // should never reach here
562 log.error("You didn't see anything. I did not exist.");
563 break;
564 }
565 }
566
567 // Intercepts mastership events
568 private class InternalMastershipListener implements MastershipListener {
569
tomb41d1ac2014-09-24 01:51:24 -0700570 @Override
571 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700572
573 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
574 // Don't care if backup list changed.
575 return;
576 }
577
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700578 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700579 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700580
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700581 // myRole suggested by MastershipService
582 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700583 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700584 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700585 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700586 final boolean iHaveControl = myNodeId.equals(term.master());
587 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700588 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700589 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700590 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700591 myNextRole = STANDBY;
592 }
593 } else if (event.roleInfo().backups().contains(myNodeId)) {
594 myNextRole = STANDBY;
595 } else {
596 myNextRole = NONE;
597 }
598
599
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700600 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700601 if (!isReachable) {
602 // device is not connected to this node
603 if (myNextRole != NONE) {
604 log.warn("Node was instructed to be {} role for {}, "
605 + "but this node cannot reach the device. "
606 + "Relinquishing role. ",
607 myNextRole, did);
608 mastershipService.relinquishMastership(did);
609 }
610 return;
611 }
612
613 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800614 if (store.getDevice(did) != null) {
615 reassertRole(did, myNextRole);
616 } else {
617 log.warn("Device is not yet/no longer in the store: {}", did);
618 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700619 }
tomb41d1ac2014-09-24 01:51:24 -0700620 }
tomf80c9722014-09-24 14:49:18 -0700621
622 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700623 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700624 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700625 @Override
626 public void notify(DeviceEvent event) {
627 post(event);
628 }
629 }
tomd3097b02014-08-26 10:40:29 -0700630}