blob: 527784ff7cae1e4fad7aaa165d828816103d54d0 [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 {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700385 if (mastershipService.getLocalRole(deviceId) == MASTER) {
386 post(store.markOffline(deviceId));
387 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700388 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700389 log.warn("Failed to mark {} offline", deviceId);
390 // only the MASTER should be marking off-line in normal cases,
391 // but if I was the last STANDBY connection, etc. and no one else
392 // was there to mark the device offline, this instance may need to
393 // temporarily request for Master Role and mark offline.
394
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700395 //there are times when this node will correctly have mastership, BUT
396 //that isn't reflected in the ClockManager before the device disconnects.
397 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700398
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700399 // FIXME: Store semantics leaking out as IllegalStateException.
400 // Consider revising store API to handle this scenario.
Madan Jampanide003d92015-05-11 17:14:20 -0700401 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
402 roleFuture.whenComplete((role, error) -> {
403 MastershipTerm term = termService.getMastershipTerm(deviceId);
404 // TODO: Move this type of check inside device clock manager, etc.
405 if (term != null && localNodeId.equals(term.master())) {
406 log.info("Retry marking {} offline", deviceId);
407 deviceClockProviderService.setMastershipTerm(deviceId, term);
408 post(store.markOffline(deviceId));
409 } else {
410 log.info("Failed again marking {} offline. {}", deviceId, role);
411 }
412 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700413 } finally {
414 //relinquish master role and ability to be backup.
415 mastershipService.relinquishMastership(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700416 }
tomd3097b02014-08-26 10:40:29 -0700417 }
418
419 @Override
alshabibb7b40632014-09-28 21:30:00 -0700420 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700421 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700422 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700423 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700424 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700425 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700426 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
427 // Never been a master for this device
428 // any update will be ignored.
429 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
430 return;
431 }
432
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700433 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700434 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700435 for (DeviceEvent event : events) {
436 post(event);
437 }
tomd3097b02014-08-26 10:40:29 -0700438 }
439
440 @Override
alshabibb7b40632014-09-28 21:30:00 -0700441 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700442 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700443 checkNotNull(deviceId, DEVICE_ID_NULL);
444 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700445 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700446
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700447 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
448 // Never been a master for this device
449 // any update will be ignored.
450 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
451 return;
452 }
453
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700454 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700455 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700456 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700457 log.info("Device {} port {} status changed", deviceId, event
458 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700459 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700460 }
tomd3097b02014-08-26 10:40:29 -0700461 }
tom3f2bbd72014-09-24 12:07:58 -0700462
463 @Override
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700464 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
465 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700466 // Several things can happen here:
467 // 1. request and response match
468 // 2. request and response don't match
469 // 3. MastershipRole and requested match (and 1 or 2 are true)
470 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
471 //
472 // 2, 4, and 3 with case 2 are failure modes.
473
tom3f2bbd72014-09-24 12:07:58 -0700474 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700475
476 log.info("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700477 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700478
479 if (requested == null && response == null) {
480 // something was off with DeviceProvider, maybe check channel too?
481 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700482 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700483 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700484 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700485
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700486 if (Objects.equals(requested, response)) {
487 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700488 return;
489 } else {
490 return;
491 // FIXME roleManager got the device to comply, but doesn't agree with
492 // the store; use the store's view, then try to reassert.
493 }
494 } else {
495 // we didn't get back what we asked for. Reelect someone else.
496 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
497 if (response == MastershipRole.MASTER) {
498 mastershipService.relinquishMastership(deviceId);
499 // TODO: Shouldn't we be triggering event?
500 //final Device device = getDevice(deviceId);
501 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
502 }
503 }
tom3f2bbd72014-09-24 12:07:58 -0700504 }
sangho538108b2015-04-08 14:29:20 -0700505
506 @Override
507 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
508 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700509 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700510 checkValidity();
511
512 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700513 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700514 post(event);
515 }
tomd3097b02014-08-26 10:40:29 -0700516 }
tom32f66842014-08-27 19:27:47 -0700517
tomeadbb462014-09-07 16:10:19 -0700518 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700519 private void post(DeviceEvent event) {
520 if (event != null && eventDispatcher != null) {
521 eventDispatcher.post(event);
522 }
523 }
524
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800525 // Applies the specified role to the device; ignores NONE
526 /**
527 * Apply role to device and send probe if MASTER.
528 *
529 * @param deviceId device identifier
530 * @param newRole new role to apply to the device
531 * @return true if the request was sent to provider
532 */
533 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
534 if (newRole.equals(MastershipRole.NONE)) {
535 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700536 return true;
537 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700538
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800539 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800540 if (provider == null) {
541 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
542 return false;
543 }
544 provider.roleChanged(deviceId, newRole);
545
546 if (newRole.equals(MastershipRole.MASTER)) {
547 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800548 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800549 }
550 return true;
551 }
552
553 /**
554 * Reaasert role for specified device connected to this node.
555 *
556 * @param did device identifier
557 * @param nextRole role to apply. If NONE is specified,
558 * it will ask mastership service for a role and apply it.
559 */
560 private void reassertRole(final DeviceId did,
561 final MastershipRole nextRole) {
562
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800563 MastershipRole myNextRole = nextRole;
564 if (myNextRole == NONE) {
565 mastershipService.requestRoleFor(did);
566 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700567 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800568 myNextRole = MASTER;
569 } else {
570 myNextRole = STANDBY;
571 }
572 }
573
574 switch (myNextRole) {
575 case MASTER:
576 final Device device = getDevice(did);
577 if ((device != null) && !isAvailable(did)) {
578 //flag the device as online. Is there a better way to do this?
579 DefaultDeviceDescription deviceDescription
580 = new DefaultDeviceDescription(did.uri(),
581 device.type(),
582 device.manufacturer(),
583 device.hwVersion(),
584 device.swVersion(),
585 device.serialNumber(),
586 device.chassisId());
587 DeviceEvent devEvent =
588 store.createOrUpdateDevice(device.providerId(), did,
589 deviceDescription);
590 post(devEvent);
591 }
592 // TODO: should apply role only if there is mismatch
593 log.info("Applying role {} to {}", myNextRole, did);
594 if (!applyRoleAndProbe(did, MASTER)) {
595 // immediately failed to apply role
596 mastershipService.relinquishMastership(did);
597 // FIXME disconnect?
598 }
599 break;
600 case STANDBY:
601 log.info("Applying role {} to {}", myNextRole, did);
602 if (!applyRoleAndProbe(did, STANDBY)) {
603 // immediately failed to apply role
604 mastershipService.relinquishMastership(did);
605 // FIXME disconnect?
606 }
607 break;
608 case NONE:
609 default:
610 // should never reach here
611 log.error("You didn't see anything. I did not exist.");
612 break;
613 }
614 }
615
616 // Intercepts mastership events
617 private class InternalMastershipListener implements MastershipListener {
618
tomb41d1ac2014-09-24 01:51:24 -0700619 @Override
620 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700621 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
622 // Don't care if backup list changed.
623 return;
624 }
625
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700626 final DeviceId did = event.subject();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700627
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700628 // myRole suggested by MastershipService
629 MastershipRole myNextRole;
Madan Jampanide003d92015-05-11 17:14:20 -0700630 if (localNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700631 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700632 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700633 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700634 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700635 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700636 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700637 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700638 myNextRole = STANDBY;
639 }
Madan Jampanide003d92015-05-11 17:14:20 -0700640 } else if (event.roleInfo().backups().contains(localNodeId)) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700641 myNextRole = STANDBY;
642 } else {
643 myNextRole = NONE;
644 }
645
646
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700647 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700648 if (!isReachable) {
649 // device is not connected to this node
650 if (myNextRole != NONE) {
651 log.warn("Node was instructed to be {} role for {}, "
652 + "but this node cannot reach the device. "
653 + "Relinquishing role. ",
654 myNextRole, did);
655 mastershipService.relinquishMastership(did);
656 }
657 return;
658 }
659
660 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800661 if (store.getDevice(did) != null) {
662 reassertRole(did, myNextRole);
663 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700664 log.debug("Device is not yet/no longer in the store: {}", did);
alshabibfd3cc052015-02-10 15:16:57 -0800665 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700666 }
tomb41d1ac2014-09-24 01:51:24 -0700667 }
tomf80c9722014-09-24 14:49:18 -0700668
669 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700670 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700671 @Override
672 public void notify(DeviceEvent event) {
673 post(event);
674 }
675 }
tomd3097b02014-08-26 10:40:29 -0700676}