blob: 0267c4f6fde050a58c3644234a45787355de148d [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;
HIGUCHI Yuta11530fb2015-05-27 13:10:20 -070064import java.util.concurrent.ExecutionException;
Jonathan Hart2f669362015-02-11 16:19:20 -080065import java.util.concurrent.ScheduledExecutorService;
66import java.util.concurrent.TimeUnit;
67
68import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080069import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
70import static org.onlab.util.Tools.groupedThreads;
71import static org.onosproject.net.MastershipRole.*;
Jonathan Hart2f669362015-02-11 16:19:20 -080072import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090073import static org.onosproject.security.AppGuard.checkPermission;
74
Jonathan Hart2f669362015-02-11 16:19:20 -080075
tomd3097b02014-08-26 10:40:29 -070076/**
tome4729872014-09-23 00:37:37 -070077 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070078 */
79@Component(immediate = true)
80@Service
tom41a2c5f2014-09-19 09:20:35 -070081public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070082 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
83 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070084
tome5ec3fd2014-09-04 15:18:06 -070085 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
86 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
87 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
88 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
tomd3097b02014-08-26 10:40:29 -070089
tom5f38b3a2014-08-27 23:50:54 -070090 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070091
Simon Huntff663742015-05-14 13:33:05 -070092 protected final ListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
93 new ListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070094
alshabib339a3d92014-09-26 17:54:32 -070095 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070096
tomc78acee2014-09-24 15:16:55 -070097 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -070098 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -070099
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800100 private ScheduledExecutorService backgroundService;
101
tom41a2c5f2014-09-19 09:20:35 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -0700104
tom5f38b3a2014-08-27 23:50:54 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -0700106 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -0700107
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700109 protected ClusterService clusterService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700112 protected MastershipService mastershipService;
113
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700115 protected MastershipTermService termService;
116
Madan Jampani61056bc2014-09-27 09:07:26 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700118 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700119
tomd3097b02014-08-26 10:40:29 -0700120 @Activate
121 public void activate() {
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800122 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background"));
Madan Jampanide003d92015-05-11 17:14:20 -0700123 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800124
tomf80c9722014-09-24 14:49:18 -0700125 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700126 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700127 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800128
129 backgroundService.scheduleWithFixedDelay(new Runnable() {
130
131 @Override
132 public void run() {
133 try {
134 mastershipCheck();
135 } catch (Exception e) {
136 log.error("Exception thrown during integrity check", e);
137 }
138 }
139 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700140 log.info("Started");
141 }
142
143 @Deactivate
144 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800145 backgroundService.shutdown();
146
tomf80c9722014-09-24 14:49:18 -0700147 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700148 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700149 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700150 log.info("Stopped");
151 }
152
153 @Override
tomad2d2092014-09-06 23:24:20 -0700154 public int getDeviceCount() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900155 checkPermission(Permission.DEVICE_READ);
156
tomad2d2092014-09-06 23:24:20 -0700157 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700158 }
159
160 @Override
tom32f66842014-08-27 19:27:47 -0700161 public Iterable<Device> getDevices() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900162 checkPermission(Permission.DEVICE_READ);
163
tome5ec3fd2014-09-04 15:18:06 -0700164 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700165 }
166
tom32f66842014-08-27 19:27:47 -0700167 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800168 public Iterable<Device> getAvailableDevices() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900169 checkPermission(Permission.DEVICE_READ);
170
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800171 return store.getAvailableDevices();
172 }
173
174 @Override
tom32f66842014-08-27 19:27:47 -0700175 public Device getDevice(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900176 checkPermission(Permission.DEVICE_READ);
177
tom32f66842014-08-27 19:27:47 -0700178 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700179 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700180 }
181
182 @Override
tomad2d2092014-09-06 23:24:20 -0700183 public MastershipRole getRole(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900184 checkPermission(Permission.DEVICE_READ);
185
tomad2d2092014-09-06 23:24:20 -0700186 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700187 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700188 }
189
190 @Override
tom32f66842014-08-27 19:27:47 -0700191 public List<Port> getPorts(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900192 checkPermission(Permission.DEVICE_READ);
193
tom32f66842014-08-27 19:27:47 -0700194 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700195 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700196 }
197
198 @Override
sangho538108b2015-04-08 14:29:20 -0700199 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900200 checkPermission(Permission.DEVICE_READ);
201
sangho538108b2015-04-08 14:29:20 -0700202 checkNotNull(deviceId, DEVICE_ID_NULL);
203 return store.getPortStatistics(deviceId);
204 }
205
206 @Override
tom32f66842014-08-27 19:27:47 -0700207 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900208 checkPermission(Permission.DEVICE_READ);
209
tom32f66842014-08-27 19:27:47 -0700210 checkNotNull(deviceId, DEVICE_ID_NULL);
211 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700212 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700213 }
214
215 @Override
tomff7eb7c2014-09-08 12:49:03 -0700216 public boolean isAvailable(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900217 checkPermission(Permission.DEVICE_READ);
218
tomff7eb7c2014-09-08 12:49:03 -0700219 checkNotNull(deviceId, DEVICE_ID_NULL);
220 return store.isAvailable(deviceId);
221 }
222
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700223 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700224 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800225 if (deviceId == null) {
226 return false;
227 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700228 DeviceProvider provider = getProvider(deviceId);
229 if (provider != null) {
230 return provider.isReachable(deviceId);
231 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800232 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700233 return false;
234 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700235 }
236
tome5ec3fd2014-09-04 15:18:06 -0700237 @Override
238 public void removeDevice(DeviceId deviceId) {
239 checkNotNull(deviceId, DEVICE_ID_NULL);
240 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700241 if (event != null) {
242 log.info("Device {} administratively removed", deviceId);
243 post(event);
244 }
tome5ec3fd2014-09-04 15:18:06 -0700245 }
246
tom7869ad92014-09-09 14:32:08 -0700247 @Override
248 public void addListener(DeviceListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900249 checkPermission(Permission.DEVICE_EVENT);
250
tom7869ad92014-09-09 14:32:08 -0700251 listenerRegistry.addListener(listener);
252 }
253
254 @Override
255 public void removeListener(DeviceListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900256 checkPermission(Permission.DEVICE_EVENT);
257
tom7869ad92014-09-09 14:32:08 -0700258 listenerRegistry.removeListener(listener);
259 }
260
261 @Override
alshabibb7b40632014-09-28 21:30:00 -0700262 protected DeviceProviderService createProviderService(
263 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700264 return new InternalDeviceProviderService(provider);
265 }
266
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800267 /**
268 * Checks if all the reachable devices have a valid mastership role.
269 */
270 private void mastershipCheck() {
271 log.debug("Checking mastership");
272 for (Device device : getDevices()) {
273 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800274 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800275
276 if (!isReachable(deviceId)) {
277 continue;
278 }
279
280 if (mastershipService.getLocalRole(deviceId) != NONE) {
281 continue;
282 }
283
284 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
285
286 // isReachable but was not MASTER or STANDBY, get a role and apply
287 // Note: NONE triggers request to MastershipService
288 reassertRole(deviceId, NONE);
289 }
290 }
291
tomd3097b02014-08-26 10:40:29 -0700292 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700293 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700294 extends AbstractProviderService<DeviceProvider>
295 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700296
tomcfde0622014-09-09 11:02:42 -0700297 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700298 super(provider);
299 }
300
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700301 /**
302 * Apply role in reaction to provider event.
303 *
304 * @param deviceId device identifier
305 * @param newRole new role to apply to the device
306 * @return true if the request was sent to provider
307 */
308 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
309
310 if (newRole.equals(MastershipRole.NONE)) {
311 //no-op
312 return true;
313 }
314
315 DeviceProvider provider = provider();
316 if (provider == null) {
317 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
318 return false;
319 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700320 provider.roleChanged(deviceId, newRole);
321 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700322
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700323 return true;
324 }
325
326
tomd3097b02014-08-26 10:40:29 -0700327 @Override
alshabibb7b40632014-09-28 21:30:00 -0700328 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700329 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700330 checkNotNull(deviceId, DEVICE_ID_NULL);
331 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700332 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700333
334 log.info("Device {} connected", deviceId);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700335 // check my Role
HIGUCHI Yuta11530fb2015-05-27 13:10:20 -0700336 CompletableFuture<MastershipRole> role = mastershipService.requestRoleFor(deviceId);
337 try {
338 // Device subsystem must wait for role assignment
339 // to avoid losing Device information.
340 // (This node could be the only Node connected to the Device.)
341 role.get();
342 } catch (InterruptedException e) {
343 log.warn("Interrupted while waiting role-assignment for {}", deviceId);
344 Thread.currentThread().interrupt();
345 } catch (ExecutionException e) {
346 log.error("Exception thrown while waiting role-assignment for {}",
347 deviceId, e);
348 }
349
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700350 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700351 if (term == null || !localNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700352 log.info("Role of this node is STANDBY for {}", deviceId);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700353 applyRole(deviceId, MastershipRole.STANDBY);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800354 } else {
355 log.info("Role of this node is MASTER for {}", deviceId);
356 // tell clock provider if this instance is the master
357 deviceClockProviderService.setMastershipTerm(deviceId, term);
358 applyRole(deviceId, MastershipRole.MASTER);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700359 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700360
tome5ec3fd2014-09-04 15:18:06 -0700361 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700362 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700363
tom80c0e5e2014-09-08 18:08:58 -0700364 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700365 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700366 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700367 }
tomd3097b02014-08-26 10:40:29 -0700368 }
369
370 @Override
371 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700372 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700373 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700374
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700375 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700376
alshabibafc514a2014-12-01 14:44:05 -0800377 List<Port> ports = store.getPorts(deviceId);
378 List<PortDescription> descs = Lists.newArrayList();
379 ports.forEach(port ->
380 descs.add(new DefaultPortDescription(port.number(),
381 false, port.type(),
382 port.portSpeed())));
383 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700384 try {
Madan Jampanide003d92015-05-11 17:14:20 -0700385 post(store.markOffline(deviceId));
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700386 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700387 log.warn("Failed to mark {} offline", deviceId);
388 // only the MASTER should be marking off-line in normal cases,
389 // but if I was the last STANDBY connection, etc. and no one else
390 // was there to mark the device offline, this instance may need to
391 // temporarily request for Master Role and mark offline.
392
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700393 //there are times when this node will correctly have mastership, BUT
394 //that isn't reflected in the ClockManager before the device disconnects.
395 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700396
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700397 // FIXME: Store semantics leaking out as IllegalStateException.
398 // Consider revising store API to handle this scenario.
Madan Jampanide003d92015-05-11 17:14:20 -0700399 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
400 roleFuture.whenComplete((role, error) -> {
401 MastershipTerm term = termService.getMastershipTerm(deviceId);
402 // TODO: Move this type of check inside device clock manager, etc.
403 if (term != null && localNodeId.equals(term.master())) {
404 log.info("Retry marking {} offline", deviceId);
405 deviceClockProviderService.setMastershipTerm(deviceId, term);
406 post(store.markOffline(deviceId));
407 } else {
408 log.info("Failed again marking {} offline. {}", deviceId, role);
409 }
410 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700411 } finally {
412 //relinquish master role and ability to be backup.
413 mastershipService.relinquishMastership(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700414 }
tomd3097b02014-08-26 10:40:29 -0700415 }
416
417 @Override
alshabibb7b40632014-09-28 21:30:00 -0700418 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700419 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700420 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700421 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700422 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700423 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700424 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
425 // Never been a master for this device
426 // any update will be ignored.
427 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
428 return;
429 }
430
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700431 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700432 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700433 for (DeviceEvent event : events) {
434 post(event);
435 }
tomd3097b02014-08-26 10:40:29 -0700436 }
437
438 @Override
alshabibb7b40632014-09-28 21:30:00 -0700439 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700440 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700441 checkNotNull(deviceId, DEVICE_ID_NULL);
442 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700443 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700444
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700445 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
446 // Never been a master for this device
447 // any update will be ignored.
448 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
449 return;
450 }
451
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700452 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700453 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700454 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700455 log.info("Device {} port {} status changed", deviceId, event
456 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700457 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700458 }
tomd3097b02014-08-26 10:40:29 -0700459 }
tom3f2bbd72014-09-24 12:07:58 -0700460
461 @Override
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700462 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
463 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700464 // Several things can happen here:
465 // 1. request and response match
466 // 2. request and response don't match
467 // 3. MastershipRole and requested match (and 1 or 2 are true)
468 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
469 //
470 // 2, 4, and 3 with case 2 are failure modes.
471
tom3f2bbd72014-09-24 12:07:58 -0700472 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700473
474 log.info("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700475 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700476
477 if (requested == null && response == null) {
478 // something was off with DeviceProvider, maybe check channel too?
479 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700480 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700481 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700482 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700483
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700484 if (Objects.equals(requested, response)) {
485 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700486 return;
487 } else {
488 return;
489 // FIXME roleManager got the device to comply, but doesn't agree with
490 // the store; use the store's view, then try to reassert.
491 }
492 } else {
493 // we didn't get back what we asked for. Reelect someone else.
494 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
495 if (response == MastershipRole.MASTER) {
496 mastershipService.relinquishMastership(deviceId);
497 // TODO: Shouldn't we be triggering event?
498 //final Device device = getDevice(deviceId);
499 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
500 }
501 }
tom3f2bbd72014-09-24 12:07:58 -0700502 }
sangho538108b2015-04-08 14:29:20 -0700503
504 @Override
505 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
506 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700507 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700508 checkValidity();
509
510 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700511 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700512 post(event);
513 }
tomd3097b02014-08-26 10:40:29 -0700514 }
tom32f66842014-08-27 19:27:47 -0700515
tomeadbb462014-09-07 16:10:19 -0700516 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700517 private void post(DeviceEvent event) {
518 if (event != null && eventDispatcher != null) {
519 eventDispatcher.post(event);
520 }
521 }
522
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800523 // Applies the specified role to the device; ignores NONE
524 /**
525 * Apply role to device and send probe if MASTER.
526 *
527 * @param deviceId device identifier
528 * @param newRole new role to apply to the device
529 * @return true if the request was sent to provider
530 */
531 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
532 if (newRole.equals(MastershipRole.NONE)) {
533 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700534 return true;
535 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700536
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800537 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800538 if (provider == null) {
539 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
540 return false;
541 }
542 provider.roleChanged(deviceId, newRole);
543
544 if (newRole.equals(MastershipRole.MASTER)) {
545 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800546 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800547 }
548 return true;
549 }
550
551 /**
552 * Reaasert role for specified device connected to this node.
553 *
554 * @param did device identifier
555 * @param nextRole role to apply. If NONE is specified,
556 * it will ask mastership service for a role and apply it.
557 */
558 private void reassertRole(final DeviceId did,
559 final MastershipRole nextRole) {
560
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800561 MastershipRole myNextRole = nextRole;
562 if (myNextRole == NONE) {
563 mastershipService.requestRoleFor(did);
564 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700565 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800566 myNextRole = MASTER;
567 } else {
568 myNextRole = STANDBY;
569 }
570 }
571
572 switch (myNextRole) {
573 case MASTER:
574 final Device device = getDevice(did);
575 if ((device != null) && !isAvailable(did)) {
576 //flag the device as online. Is there a better way to do this?
577 DefaultDeviceDescription deviceDescription
578 = new DefaultDeviceDescription(did.uri(),
579 device.type(),
580 device.manufacturer(),
581 device.hwVersion(),
582 device.swVersion(),
583 device.serialNumber(),
584 device.chassisId());
585 DeviceEvent devEvent =
586 store.createOrUpdateDevice(device.providerId(), did,
587 deviceDescription);
588 post(devEvent);
589 }
590 // TODO: should apply role only if there is mismatch
591 log.info("Applying role {} to {}", myNextRole, did);
592 if (!applyRoleAndProbe(did, MASTER)) {
593 // immediately failed to apply role
594 mastershipService.relinquishMastership(did);
595 // FIXME disconnect?
596 }
597 break;
598 case STANDBY:
599 log.info("Applying role {} to {}", myNextRole, did);
600 if (!applyRoleAndProbe(did, STANDBY)) {
601 // immediately failed to apply role
602 mastershipService.relinquishMastership(did);
603 // FIXME disconnect?
604 }
605 break;
606 case NONE:
607 default:
608 // should never reach here
609 log.error("You didn't see anything. I did not exist.");
610 break;
611 }
612 }
613
614 // Intercepts mastership events
615 private class InternalMastershipListener implements MastershipListener {
616
tomb41d1ac2014-09-24 01:51:24 -0700617 @Override
618 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700619 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
620 // Don't care if backup list changed.
621 return;
622 }
623
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700624 final DeviceId did = event.subject();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700625
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700626 // myRole suggested by MastershipService
627 MastershipRole myNextRole;
Madan Jampanide003d92015-05-11 17:14:20 -0700628 if (localNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700629 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700630 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700631 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700632 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700633 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700634 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700635 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700636 myNextRole = STANDBY;
637 }
Madan Jampanide003d92015-05-11 17:14:20 -0700638 } else if (event.roleInfo().backups().contains(localNodeId)) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700639 myNextRole = STANDBY;
640 } else {
641 myNextRole = NONE;
642 }
643
644
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700645 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700646 if (!isReachable) {
647 // device is not connected to this node
648 if (myNextRole != NONE) {
649 log.warn("Node was instructed to be {} role for {}, "
650 + "but this node cannot reach the device. "
651 + "Relinquishing role. ",
652 myNextRole, did);
653 mastershipService.relinquishMastership(did);
654 }
655 return;
656 }
657
658 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800659 if (store.getDevice(did) != null) {
660 reassertRole(did, myNextRole);
661 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700662 log.debug("Device is not yet/no longer in the store: {}", did);
alshabibfd3cc052015-02-10 15:16:57 -0800663 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700664 }
tomb41d1ac2014-09-24 01:51:24 -0700665 }
tomf80c9722014-09-24 14:49:18 -0700666
667 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700668 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700669 @Override
670 public void notify(DeviceEvent event) {
671 post(event);
672 }
673 }
tomd3097b02014-08-26 10:40:29 -0700674}