blob: a703270110b55a15c790e300b5dd9111d5aee676 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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;
Madan Jampanide003d92015-05-11 17:14:20 -070019
tomd3097b02014-08-26 10:40:29 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070025import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cluster.ClusterService;
27import org.onosproject.cluster.NodeId;
Changhoon Yoon541ef712015-05-23 17:18:34 +090028import org.onosproject.core.Permission;
Simon Huntff663742015-05-14 13:33:05 -070029import org.onosproject.event.ListenerRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.event.EventDeliveryService;
31import org.onosproject.mastership.MastershipEvent;
32import org.onosproject.mastership.MastershipListener;
33import org.onosproject.mastership.MastershipService;
34import org.onosproject.mastership.MastershipTerm;
35import org.onosproject.mastership.MastershipTermService;
36import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.MastershipRole;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.device.DefaultDeviceDescription;
42import org.onosproject.net.device.DefaultPortDescription;
43import org.onosproject.net.device.DeviceAdminService;
44import org.onosproject.net.device.DeviceClockProviderService;
45import org.onosproject.net.device.DeviceDescription;
46import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceProvider;
49import org.onosproject.net.device.DeviceProviderRegistry;
50import org.onosproject.net.device.DeviceProviderService;
51import org.onosproject.net.device.DeviceService;
52import org.onosproject.net.device.DeviceStore;
53import org.onosproject.net.device.DeviceStoreDelegate;
54import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070055import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080056import org.onosproject.net.provider.AbstractProviderRegistry;
57import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070058import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070059
sangho538108b2015-04-08 14:29:20 -070060import java.util.Collection;
Jonathan Hart2f669362015-02-11 16:19:20 -080061import java.util.List;
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070062import java.util.Objects;
Madan Jampanide003d92015-05-11 17:14:20 -070063import java.util.concurrent.CompletableFuture;
Jonathan Hart2f669362015-02-11 16:19:20 -080064import java.util.concurrent.ScheduledExecutorService;
65import java.util.concurrent.TimeUnit;
66
67import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080068import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
69import static org.onlab.util.Tools.groupedThreads;
70import static org.onosproject.net.MastershipRole.*;
Jonathan Hart2f669362015-02-11 16:19:20 -080071import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090072import static org.onosproject.security.AppGuard.checkPermission;
73
Jonathan Hart2f669362015-02-11 16:19:20 -080074
tomd3097b02014-08-26 10:40:29 -070075/**
tome4729872014-09-23 00:37:37 -070076 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070077 */
78@Component(immediate = true)
79@Service
tom41a2c5f2014-09-19 09:20:35 -070080public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070081 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
82 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070083
tome5ec3fd2014-09-04 15:18:06 -070084 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
85 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
86 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
87 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
tomd3097b02014-08-26 10:40:29 -070088
tom5f38b3a2014-08-27 23:50:54 -070089 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070090
Simon Huntff663742015-05-14 13:33:05 -070091 protected final ListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
92 new ListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070093
alshabib339a3d92014-09-26 17:54:32 -070094 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070095
tomc78acee2014-09-24 15:16:55 -070096 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -070097 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -070098
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080099 private ScheduledExecutorService backgroundService;
100
tom41a2c5f2014-09-19 09:20:35 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -0700103
tom5f38b3a2014-08-27 23:50:54 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -0700105 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -0700106
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700108 protected ClusterService clusterService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700111 protected MastershipService mastershipService;
112
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700114 protected MastershipTermService termService;
115
Madan Jampani61056bc2014-09-27 09:07:26 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700117 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700118
tomd3097b02014-08-26 10:40:29 -0700119 @Activate
120 public void activate() {
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800121 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background"));
Madan Jampanide003d92015-05-11 17:14:20 -0700122 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800123
tomf80c9722014-09-24 14:49:18 -0700124 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700125 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700126 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800127
128 backgroundService.scheduleWithFixedDelay(new Runnable() {
129
130 @Override
131 public void run() {
132 try {
133 mastershipCheck();
134 } catch (Exception e) {
135 log.error("Exception thrown during integrity check", e);
136 }
137 }
138 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700139 log.info("Started");
140 }
141
142 @Deactivate
143 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800144 backgroundService.shutdown();
145
tomf80c9722014-09-24 14:49:18 -0700146 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700147 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700148 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700149 log.info("Stopped");
150 }
151
152 @Override
tomad2d2092014-09-06 23:24:20 -0700153 public int getDeviceCount() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900154 checkPermission(Permission.DEVICE_READ);
155
tomad2d2092014-09-06 23:24:20 -0700156 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700157 }
158
159 @Override
tom32f66842014-08-27 19:27:47 -0700160 public Iterable<Device> getDevices() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900161 checkPermission(Permission.DEVICE_READ);
162
tome5ec3fd2014-09-04 15:18:06 -0700163 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700164 }
165
tom32f66842014-08-27 19:27:47 -0700166 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800167 public Iterable<Device> getAvailableDevices() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900168 checkPermission(Permission.DEVICE_READ);
169
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800170 return store.getAvailableDevices();
171 }
172
173 @Override
tom32f66842014-08-27 19:27:47 -0700174 public Device getDevice(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900175 checkPermission(Permission.DEVICE_READ);
176
tom32f66842014-08-27 19:27:47 -0700177 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700178 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700179 }
180
181 @Override
tomad2d2092014-09-06 23:24:20 -0700182 public MastershipRole getRole(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900183 checkPermission(Permission.DEVICE_READ);
184
tomad2d2092014-09-06 23:24:20 -0700185 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700186 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700187 }
188
189 @Override
tom32f66842014-08-27 19:27:47 -0700190 public List<Port> getPorts(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900191 checkPermission(Permission.DEVICE_READ);
192
tom32f66842014-08-27 19:27:47 -0700193 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700194 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700195 }
196
197 @Override
sangho538108b2015-04-08 14:29:20 -0700198 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900199 checkPermission(Permission.DEVICE_READ);
200
sangho538108b2015-04-08 14:29:20 -0700201 checkNotNull(deviceId, DEVICE_ID_NULL);
202 return store.getPortStatistics(deviceId);
203 }
204
205 @Override
tom32f66842014-08-27 19:27:47 -0700206 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900207 checkPermission(Permission.DEVICE_READ);
208
tom32f66842014-08-27 19:27:47 -0700209 checkNotNull(deviceId, DEVICE_ID_NULL);
210 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700211 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700212 }
213
214 @Override
tomff7eb7c2014-09-08 12:49:03 -0700215 public boolean isAvailable(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900216 checkPermission(Permission.DEVICE_READ);
217
tomff7eb7c2014-09-08 12:49:03 -0700218 checkNotNull(deviceId, DEVICE_ID_NULL);
219 return store.isAvailable(deviceId);
220 }
221
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700222 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700223 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800224 if (deviceId == null) {
225 return false;
226 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700227 DeviceProvider provider = getProvider(deviceId);
228 if (provider != null) {
229 return provider.isReachable(deviceId);
230 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800231 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700232 return false;
233 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700234 }
235
tome5ec3fd2014-09-04 15:18:06 -0700236 @Override
237 public void removeDevice(DeviceId deviceId) {
238 checkNotNull(deviceId, DEVICE_ID_NULL);
239 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700240 if (event != null) {
241 log.info("Device {} administratively removed", deviceId);
242 post(event);
243 }
tome5ec3fd2014-09-04 15:18:06 -0700244 }
245
tom7869ad92014-09-09 14:32:08 -0700246 @Override
247 public void addListener(DeviceListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900248 checkPermission(Permission.DEVICE_EVENT);
249
tom7869ad92014-09-09 14:32:08 -0700250 listenerRegistry.addListener(listener);
251 }
252
253 @Override
254 public void removeListener(DeviceListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900255 checkPermission(Permission.DEVICE_EVENT);
256
tom7869ad92014-09-09 14:32:08 -0700257 listenerRegistry.removeListener(listener);
258 }
259
260 @Override
alshabibb7b40632014-09-28 21:30:00 -0700261 protected DeviceProviderService createProviderService(
262 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700263 return new InternalDeviceProviderService(provider);
264 }
265
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800266 /**
267 * Checks if all the reachable devices have a valid mastership role.
268 */
269 private void mastershipCheck() {
270 log.debug("Checking mastership");
271 for (Device device : getDevices()) {
272 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800273 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800274
275 if (!isReachable(deviceId)) {
276 continue;
277 }
278
279 if (mastershipService.getLocalRole(deviceId) != NONE) {
280 continue;
281 }
282
283 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
284
285 // isReachable but was not MASTER or STANDBY, get a role and apply
286 // Note: NONE triggers request to MastershipService
287 reassertRole(deviceId, NONE);
288 }
289 }
290
tomd3097b02014-08-26 10:40:29 -0700291 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700292 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700293 extends AbstractProviderService<DeviceProvider>
294 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700295
tomcfde0622014-09-09 11:02:42 -0700296 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700297 super(provider);
298 }
299
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700300 /**
301 * Apply role in reaction to provider event.
302 *
303 * @param deviceId device identifier
304 * @param newRole new role to apply to the device
305 * @return true if the request was sent to provider
306 */
307 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
308
309 if (newRole.equals(MastershipRole.NONE)) {
310 //no-op
311 return true;
312 }
313
314 DeviceProvider provider = provider();
315 if (provider == null) {
316 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
317 return false;
318 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700319 provider.roleChanged(deviceId, newRole);
320 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700321
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700322 return true;
323 }
324
325
tomd3097b02014-08-26 10:40:29 -0700326 @Override
alshabibb7b40632014-09-28 21:30:00 -0700327 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700328 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700329 checkNotNull(deviceId, DEVICE_ID_NULL);
330 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700331 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700332
333 log.info("Device {} connected", deviceId);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700334 // check my Role
335 mastershipService.requestRoleFor(deviceId);
336 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700337 if (term == null || !localNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700338 log.info("Role of this node is STANDBY for {}", deviceId);
339 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700340 // this instance is not the MASTER
341 applyRole(deviceId, MastershipRole.STANDBY);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800342 } else {
343 log.info("Role of this node is MASTER for {}", deviceId);
344 // tell clock provider if this instance is the master
345 deviceClockProviderService.setMastershipTerm(deviceId, term);
346 applyRole(deviceId, MastershipRole.MASTER);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700347 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700348
tome5ec3fd2014-09-04 15:18:06 -0700349 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700350 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700351
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700352 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700353 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700354 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700355 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700356 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700357 }
tomd3097b02014-08-26 10:40:29 -0700358 }
359
360 @Override
361 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700362 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700363 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700364
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700365 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700366
alshabibafc514a2014-12-01 14:44:05 -0800367 List<Port> ports = store.getPorts(deviceId);
368 List<PortDescription> descs = Lists.newArrayList();
369 ports.forEach(port ->
370 descs.add(new DefaultPortDescription(port.number(),
371 false, port.type(),
372 port.portSpeed())));
373 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700374 try {
Madan Jampanide003d92015-05-11 17:14:20 -0700375 post(store.markOffline(deviceId));
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700376 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700377 log.warn("Failed to mark {} offline", deviceId);
378 // only the MASTER should be marking off-line in normal cases,
379 // but if I was the last STANDBY connection, etc. and no one else
380 // was there to mark the device offline, this instance may need to
381 // temporarily request for Master Role and mark offline.
382
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700383 //there are times when this node will correctly have mastership, BUT
384 //that isn't reflected in the ClockManager before the device disconnects.
385 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700386
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700387 // FIXME: Store semantics leaking out as IllegalStateException.
388 // Consider revising store API to handle this scenario.
Madan Jampanide003d92015-05-11 17:14:20 -0700389 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
390 roleFuture.whenComplete((role, error) -> {
391 MastershipTerm term = termService.getMastershipTerm(deviceId);
392 // TODO: Move this type of check inside device clock manager, etc.
393 if (term != null && localNodeId.equals(term.master())) {
394 log.info("Retry marking {} offline", deviceId);
395 deviceClockProviderService.setMastershipTerm(deviceId, term);
396 post(store.markOffline(deviceId));
397 } else {
398 log.info("Failed again marking {} offline. {}", deviceId, role);
399 }
400 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700401 } finally {
402 //relinquish master role and ability to be backup.
403 mastershipService.relinquishMastership(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700404 }
tomd3097b02014-08-26 10:40:29 -0700405 }
406
407 @Override
alshabibb7b40632014-09-28 21:30:00 -0700408 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700409 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700410 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700411 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700412 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700413 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700414 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
415 // Never been a master for this device
416 // any update will be ignored.
417 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
418 return;
419 }
420
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700421 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700422 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700423 for (DeviceEvent event : events) {
424 post(event);
425 }
tomd3097b02014-08-26 10:40:29 -0700426 }
427
428 @Override
alshabibb7b40632014-09-28 21:30:00 -0700429 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700430 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700431 checkNotNull(deviceId, DEVICE_ID_NULL);
432 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700433 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700434
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700435 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
436 // Never been a master for this device
437 // any update will be ignored.
438 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
439 return;
440 }
441
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700442 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700443 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700444 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700445 log.info("Device {} port {} status changed", deviceId, event
446 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700447 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700448 }
tomd3097b02014-08-26 10:40:29 -0700449 }
tom3f2bbd72014-09-24 12:07:58 -0700450
451 @Override
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700452 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
453 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700454 // Several things can happen here:
455 // 1. request and response match
456 // 2. request and response don't match
457 // 3. MastershipRole and requested match (and 1 or 2 are true)
458 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
459 //
460 // 2, 4, and 3 with case 2 are failure modes.
461
tom3f2bbd72014-09-24 12:07:58 -0700462 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700463
464 log.info("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700465 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700466
467 if (requested == null && response == null) {
468 // something was off with DeviceProvider, maybe check channel too?
469 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700470 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700471 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700472 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700473
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700474 if (Objects.equals(requested, response)) {
475 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700476 return;
477 } else {
478 return;
479 // FIXME roleManager got the device to comply, but doesn't agree with
480 // the store; use the store's view, then try to reassert.
481 }
482 } else {
483 // we didn't get back what we asked for. Reelect someone else.
484 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
485 if (response == MastershipRole.MASTER) {
486 mastershipService.relinquishMastership(deviceId);
487 // TODO: Shouldn't we be triggering event?
488 //final Device device = getDevice(deviceId);
489 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
490 }
491 }
tom3f2bbd72014-09-24 12:07:58 -0700492 }
sangho538108b2015-04-08 14:29:20 -0700493
494 @Override
495 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
496 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700497 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700498 checkValidity();
499
500 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700501 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700502 post(event);
503 }
tomd3097b02014-08-26 10:40:29 -0700504 }
tom32f66842014-08-27 19:27:47 -0700505
tomeadbb462014-09-07 16:10:19 -0700506 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700507 private void post(DeviceEvent event) {
508 if (event != null && eventDispatcher != null) {
509 eventDispatcher.post(event);
510 }
511 }
512
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800513 // Applies the specified role to the device; ignores NONE
514 /**
515 * Apply role to device and send probe if MASTER.
516 *
517 * @param deviceId device identifier
518 * @param newRole new role to apply to the device
519 * @return true if the request was sent to provider
520 */
521 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
522 if (newRole.equals(MastershipRole.NONE)) {
523 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700524 return true;
525 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700526
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800527 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800528 if (provider == null) {
529 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
530 return false;
531 }
532 provider.roleChanged(deviceId, newRole);
533
534 if (newRole.equals(MastershipRole.MASTER)) {
535 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800536 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800537 }
538 return true;
539 }
540
541 /**
542 * Reaasert role for specified device connected to this node.
543 *
544 * @param did device identifier
545 * @param nextRole role to apply. If NONE is specified,
546 * it will ask mastership service for a role and apply it.
547 */
548 private void reassertRole(final DeviceId did,
549 final MastershipRole nextRole) {
550
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800551 MastershipRole myNextRole = nextRole;
552 if (myNextRole == NONE) {
553 mastershipService.requestRoleFor(did);
554 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700555 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800556 myNextRole = MASTER;
557 } else {
558 myNextRole = STANDBY;
559 }
560 }
561
562 switch (myNextRole) {
563 case MASTER:
564 final Device device = getDevice(did);
565 if ((device != null) && !isAvailable(did)) {
566 //flag the device as online. Is there a better way to do this?
567 DefaultDeviceDescription deviceDescription
568 = new DefaultDeviceDescription(did.uri(),
569 device.type(),
570 device.manufacturer(),
571 device.hwVersion(),
572 device.swVersion(),
573 device.serialNumber(),
574 device.chassisId());
575 DeviceEvent devEvent =
576 store.createOrUpdateDevice(device.providerId(), did,
577 deviceDescription);
578 post(devEvent);
579 }
580 // TODO: should apply role only if there is mismatch
581 log.info("Applying role {} to {}", myNextRole, did);
582 if (!applyRoleAndProbe(did, MASTER)) {
583 // immediately failed to apply role
584 mastershipService.relinquishMastership(did);
585 // FIXME disconnect?
586 }
587 break;
588 case STANDBY:
589 log.info("Applying role {} to {}", myNextRole, did);
590 if (!applyRoleAndProbe(did, STANDBY)) {
591 // immediately failed to apply role
592 mastershipService.relinquishMastership(did);
593 // FIXME disconnect?
594 }
595 break;
596 case NONE:
597 default:
598 // should never reach here
599 log.error("You didn't see anything. I did not exist.");
600 break;
601 }
602 }
603
604 // Intercepts mastership events
605 private class InternalMastershipListener implements MastershipListener {
606
tomb41d1ac2014-09-24 01:51:24 -0700607 @Override
608 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700609 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
610 // Don't care if backup list changed.
611 return;
612 }
613
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700614 final DeviceId did = event.subject();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700615
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700616 // myRole suggested by MastershipService
617 MastershipRole myNextRole;
Madan Jampanide003d92015-05-11 17:14:20 -0700618 if (localNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700619 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700620 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700621 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700622 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700623 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700624 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700625 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700626 myNextRole = STANDBY;
627 }
Madan Jampanide003d92015-05-11 17:14:20 -0700628 } else if (event.roleInfo().backups().contains(localNodeId)) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700629 myNextRole = STANDBY;
630 } else {
631 myNextRole = NONE;
632 }
633
634
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700635 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700636 if (!isReachable) {
637 // device is not connected to this node
638 if (myNextRole != NONE) {
639 log.warn("Node was instructed to be {} role for {}, "
640 + "but this node cannot reach the device. "
641 + "Relinquishing role. ",
642 myNextRole, did);
643 mastershipService.relinquishMastership(did);
644 }
645 return;
646 }
647
648 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800649 if (store.getDevice(did) != null) {
650 reassertRole(did, myNextRole);
651 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700652 log.debug("Device is not yet/no longer in the store: {}", did);
alshabibfd3cc052015-02-10 15:16:57 -0800653 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700654 }
tomb41d1ac2014-09-24 01:51:24 -0700655 }
tomf80c9722014-09-24 14:49:18 -0700656
657 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700658 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700659 @Override
660 public void notify(DeviceEvent event) {
661 post(event);
662 }
663 }
tomd3097b02014-08-26 10:40:29 -0700664}