blob: b1390d1722bfb9e9fcf9a745e04c7133ad0ad100 [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
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070018import com.google.common.collect.Lists;
Madan Jampani565a66a2015-07-25 17:01:13 -070019import com.google.common.util.concurrent.Futures;
20
tomd3097b02014-08-26 10:40:29 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070026import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cluster.ClusterService;
28import org.onosproject.cluster.NodeId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070029import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Changhoon Yoon541ef712015-05-23 17:18:34 +090030import org.onosproject.core.Permission;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070031import org.onosproject.incubator.net.config.NetworkConfigEvent;
32import org.onosproject.incubator.net.config.NetworkConfigListener;
33import org.onosproject.incubator.net.config.NetworkConfigService;
34import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.mastership.MastershipEvent;
36import org.onosproject.mastership.MastershipListener;
37import org.onosproject.mastership.MastershipService;
38import org.onosproject.mastership.MastershipTerm;
39import org.onosproject.mastership.MastershipTermService;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070040import org.onosproject.net.DefaultAnnotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.Device;
samuel738dfaf2015-07-11 11:08:57 +080042import org.onosproject.net.Device.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.DeviceId;
44import org.onosproject.net.MastershipRole;
45import org.onosproject.net.Port;
46import org.onosproject.net.PortNumber;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070047import org.onosproject.net.SparseAnnotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.device.DefaultDeviceDescription;
49import org.onosproject.net.device.DefaultPortDescription;
50import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.device.DeviceDescription;
52import org.onosproject.net.device.DeviceEvent;
53import org.onosproject.net.device.DeviceListener;
54import org.onosproject.net.device.DeviceProvider;
55import org.onosproject.net.device.DeviceProviderRegistry;
56import org.onosproject.net.device.DeviceProviderService;
57import org.onosproject.net.device.DeviceService;
58import org.onosproject.net.device.DeviceStore;
59import org.onosproject.net.device.DeviceStoreDelegate;
60import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070061import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080062import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070063import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070064
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070065import java.util.Collection;
66import java.util.HashSet;
67import java.util.List;
68import java.util.Objects;
69import java.util.Set;
70import java.util.concurrent.CompletableFuture;
71import java.util.concurrent.ExecutionException;
72import java.util.concurrent.ScheduledExecutorService;
73import java.util.concurrent.TimeUnit;
74
75import static com.google.common.base.Preconditions.checkNotNull;
76import static com.google.common.base.Preconditions.checkState;
77import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
78import static org.onlab.util.Tools.groupedThreads;
79import static org.onosproject.net.MastershipRole.*;
80import static org.onosproject.security.AppGuard.checkPermission;
81import static org.slf4j.LoggerFactory.getLogger;
Jonathan Hart2f669362015-02-11 16:19:20 -080082
samuele1fa7322015-07-14 16:35:16 +080083
tomd3097b02014-08-26 10:40:29 -070084/**
tome4729872014-09-23 00:37:37 -070085 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070086 */
87@Component(immediate = true)
88@Service
tom41a2c5f2014-09-19 09:20:35 -070089public class DeviceManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070090 extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService>
Thomas Vachuskad16ce182014-10-29 17:25:29 -070091 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070092
tome5ec3fd2014-09-04 15:18:06 -070093 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
94 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
95 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
96 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
tomd3097b02014-08-26 10:40:29 -070097
tom5f38b3a2014-08-27 23:50:54 -070098 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070099
alshabib339a3d92014-09-26 17:54:32 -0700100 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -0700101
tomc78acee2014-09-24 15:16:55 -0700102 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -0700103 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -0700104
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800105 private ScheduledExecutorService backgroundService;
106
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700107 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
108
tom41a2c5f2014-09-19 09:20:35 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -0700111
tom5f38b3a2014-08-27 23:50:54 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700113 protected ClusterService clusterService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700116 protected MastershipService mastershipService;
117
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700119 protected MastershipTermService termService;
120
Madan Jampani61056bc2014-09-27 09:07:26 -0700121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700122 protected NetworkConfigService networkConfigService;
123
tomd3097b02014-08-26 10:40:29 -0700124 @Activate
125 public void activate() {
samuele1fa7322015-07-14 16:35:16 +0800126 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background"));
Madan Jampanide003d92015-05-11 17:14:20 -0700127 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800128
tomf80c9722014-09-24 14:49:18 -0700129 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700130 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700131 mastershipService.addListener(mastershipListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700132 networkConfigService.addListener(networkConfigListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800133
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700134 backgroundService.scheduleWithFixedDelay(() -> {
135 try {
136 mastershipCheck();
137 } catch (Exception e) {
138 log.error("Exception thrown during integrity check", e);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800139 }
140 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700141 log.info("Started");
142 }
143
144 @Deactivate
145 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800146 backgroundService.shutdown();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700147 networkConfigService.removeListener(networkConfigListener);
tomf80c9722014-09-24 14:49:18 -0700148 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700149 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700150 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700151 log.info("Stopped");
152 }
153
154 @Override
tomad2d2092014-09-06 23:24:20 -0700155 public int getDeviceCount() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900156 checkPermission(Permission.DEVICE_READ);
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);
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);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800169 return store.getAvailableDevices();
170 }
171
172 @Override
tom32f66842014-08-27 19:27:47 -0700173 public Device getDevice(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900174 checkPermission(Permission.DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700175 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700176 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700177 }
178
179 @Override
tomad2d2092014-09-06 23:24:20 -0700180 public MastershipRole getRole(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900181 checkPermission(Permission.DEVICE_READ);
tomad2d2092014-09-06 23:24:20 -0700182 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700183 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700184 }
185
186 @Override
tom32f66842014-08-27 19:27:47 -0700187 public List<Port> getPorts(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900188 checkPermission(Permission.DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700189 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700190 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700191 }
192
193 @Override
sangho538108b2015-04-08 14:29:20 -0700194 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900195 checkPermission(Permission.DEVICE_READ);
sangho538108b2015-04-08 14:29:20 -0700196 checkNotNull(deviceId, DEVICE_ID_NULL);
197 return store.getPortStatistics(deviceId);
198 }
199
200 @Override
tom32f66842014-08-27 19:27:47 -0700201 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900202 checkPermission(Permission.DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700203 checkNotNull(deviceId, DEVICE_ID_NULL);
204 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700205 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700206 }
207
208 @Override
tomff7eb7c2014-09-08 12:49:03 -0700209 public boolean isAvailable(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900210 checkPermission(Permission.DEVICE_READ);
211
tomff7eb7c2014-09-08 12:49:03 -0700212 checkNotNull(deviceId, DEVICE_ID_NULL);
213 return store.isAvailable(deviceId);
214 }
215
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700216 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700217 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800218 if (deviceId == null) {
219 return false;
220 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700221 DeviceProvider provider = getProvider(deviceId);
222 if (provider != null) {
223 return provider.isReachable(deviceId);
224 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800225 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700226 return false;
227 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700228 }
229
tome5ec3fd2014-09-04 15:18:06 -0700230 @Override
231 public void removeDevice(DeviceId deviceId) {
232 checkNotNull(deviceId, DEVICE_ID_NULL);
233 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700234 if (event != null) {
235 log.info("Device {} administratively removed", deviceId);
236 post(event);
237 }
tome5ec3fd2014-09-04 15:18:06 -0700238 }
239
tom7869ad92014-09-09 14:32:08 -0700240 @Override
samuele1fa7322015-07-14 16:35:16 +0800241 protected DeviceProviderService createProviderService(
242 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700243 return new InternalDeviceProviderService(provider);
244 }
245
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800246 /**
247 * Checks if all the reachable devices have a valid mastership role.
248 */
249 private void mastershipCheck() {
250 log.debug("Checking mastership");
251 for (Device device : getDevices()) {
252 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800253 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800254
255 if (!isReachable(deviceId)) {
256 continue;
257 }
258
259 if (mastershipService.getLocalRole(deviceId) != NONE) {
260 continue;
261 }
262
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700263 log.info("{} is reachable but did not have a valid role, reasserting",
264 deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800265
266 // isReachable but was not MASTER or STANDBY, get a role and apply
267 // Note: NONE triggers request to MastershipService
268 reassertRole(deviceId, NONE);
269 }
270 }
271
tomd3097b02014-08-26 10:40:29 -0700272 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700273 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700274 extends AbstractProviderService<DeviceProvider>
275 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700276
tomcfde0622014-09-09 11:02:42 -0700277 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700278 super(provider);
279 }
280
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700281 /**
282 * Apply role in reaction to provider event.
283 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700284 * @param deviceId device identifier
285 * @param newRole new role to apply to the device
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700286 * @return true if the request was sent to provider
287 */
288 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
289
290 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800291 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700292 return true;
293 }
294
295 DeviceProvider provider = provider();
296 if (provider == null) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700297 log.warn("Provider for {} was not found. Cannot apply role {}",
298 deviceId, newRole);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700299 return false;
300 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700301 provider.roleChanged(deviceId, newRole);
302 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700303
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700304 return true;
305 }
306
samuele1fa7322015-07-14 16:35:16 +0800307
tomd3097b02014-08-26 10:40:29 -0700308 @Override
alshabibb7b40632014-09-28 21:30:00 -0700309 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700310 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700311 checkNotNull(deviceId, DEVICE_ID_NULL);
312 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700313 checkValidity();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700314 deviceDescription = validateDevice(deviceDescription, deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700315
Madan Jampani565a66a2015-07-25 17:01:13 -0700316 // Establish my Role
317 Futures.getUnchecked(mastershipService.requestRoleFor(deviceId)
318 .thenAccept(role -> {
319 log.info("Local role is {} for {}", role, deviceId);
320 applyRole(deviceId, role);
321 }));
HIGUCHI Yuta11530fb2015-05-27 13:10:20 -0700322
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700323 DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId,
324 deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700325 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700326 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700327 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700328 }
tomd3097b02014-08-26 10:40:29 -0700329 }
330
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700331 // returns a DeviceDescription made from the union of the BasicDeviceConfig
332 // annotations if it exists
333 private DeviceDescription validateDevice(DeviceDescription deviceDescription, DeviceId deviceId) {
334 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
335 checkState(cfg == null || cfg.isAllowed(), "Device " + deviceId + " is not allowed");
336 log.info("Device {} connected", deviceId);
337 if (cfg != null) {
338 SparseAnnotations finalSparse = processAnnotations(cfg, deviceDescription, deviceId);
339 if (cfg.type() != Type.SWITCH) {
340 deviceDescription = new DefaultDeviceDescription(deviceDescription,
341 cfg.type(), finalSparse);
342 } else {
343 deviceDescription = new DefaultDeviceDescription(deviceDescription,
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700344 deviceDescription.type(), finalSparse);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700345 }
346 }
347 return deviceDescription;
348 }
349
tomd3097b02014-08-26 10:40:29 -0700350 @Override
351 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700352 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700353 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700354
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700355 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700356
alshabibafc514a2014-12-01 14:44:05 -0800357 List<Port> ports = store.getPorts(deviceId);
358 List<PortDescription> descs = Lists.newArrayList();
samuele1fa7322015-07-14 16:35:16 +0800359 ports.forEach(port ->
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700360 descs.add(new DefaultPortDescription(port.number(),
361 false, port.type(),
362 port.portSpeed())));
alshabibafc514a2014-12-01 14:44:05 -0800363 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700364 try {
Madan Jampani565a66a2015-07-25 17:01:13 -0700365 if (mastershipService.isLocalMaster(deviceId)) {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700366 post(store.markOffline(deviceId));
367 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700368 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700369 log.warn("Failed to mark {} offline", deviceId);
370 // only the MASTER should be marking off-line in normal cases,
samuele1fa7322015-07-14 16:35:16 +0800371 // but if I was the last STANDBY connection, etc. and no one else
372 // was there to mark the device offline, this instance may need to
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700373 // temporarily request for Master Role and mark offline.
374
samuele1fa7322015-07-14 16:35:16 +0800375 //there are times when this node will correctly have mastership, BUT
376 //that isn't reflected in the ClockManager before the device disconnects.
377 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700378
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700379 // FIXME: Store semantics leaking out as IllegalStateException.
samuele1fa7322015-07-14 16:35:16 +0800380 // Consider revising store API to handle this scenario.
381 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700382 roleFuture.whenComplete((role, error) -> {
samuele1fa7322015-07-14 16:35:16 +0800383 MastershipTerm term = termService.getMastershipTerm(deviceId);
384 // TODO: Move this type of check inside device clock manager, etc.
385 if (term != null && localNodeId.equals(term.master())) {
386 log.info("Retry marking {} offline", deviceId);
samuele1fa7322015-07-14 16:35:16 +0800387 post(store.markOffline(deviceId));
388 } else {
389 log.info("Failed again marking {} offline. {}", deviceId, role);
390 }
391 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700392 } finally {
Madan Jampanic6e574f2015-05-29 13:41:52 -0700393 try {
samuele1fa7322015-07-14 16:35:16 +0800394 //relinquish master role and ability to be backup.
Madan Jampanic6e574f2015-05-29 13:41:52 -0700395 mastershipService.relinquishMastership(deviceId).get();
396 } catch (InterruptedException e) {
samuele1fa7322015-07-14 16:35:16 +0800397 log.warn("Interrupted while reliquishing role for {}", deviceId);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700398 Thread.currentThread().interrupt();
399 } catch (ExecutionException e) {
samuele1fa7322015-07-14 16:35:16 +0800400 log.error("Exception thrown while relinquishing role for {}", deviceId, e);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700401 }
tom0efbb1d2014-09-09 11:54:28 -0700402 }
tomd3097b02014-08-26 10:40:29 -0700403 }
404
405 @Override
alshabibb7b40632014-09-28 21:30:00 -0700406 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700407 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700408 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700409 checkNotNull(portDescriptions,
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700410 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700411 checkValidity();
Madan Jampani565a66a2015-07-25 17:01:13 -0700412 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700413 // Never been a master for this device
414 // any update will be ignored.
samuele1fa7322015-07-14 16:35:16 +0800415 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700416 return;
417 }
418
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700419 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
samuele1fa7322015-07-14 16:35:16 +0800420 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700421 for (DeviceEvent event : events) {
422 post(event);
423 }
tomd3097b02014-08-26 10:40:29 -0700424 }
425
426 @Override
alshabibb7b40632014-09-28 21:30:00 -0700427 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700428 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700429 checkNotNull(deviceId, DEVICE_ID_NULL);
430 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700431 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700432
Madan Jampani565a66a2015-07-25 17:01:13 -0700433 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700434 // Never been a master for this device
435 // any update will be ignored.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700436 log.trace("Ignoring {} port update on standby node. {}", deviceId,
437 portDescription);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700438 return;
439 }
440
samuele1fa7322015-07-14 16:35:16 +0800441 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
442 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700443 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700444 log.info("Device {} port {} status changed", deviceId, event
445 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700446 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700447 }
tomd3097b02014-08-26 10:40:29 -0700448 }
tom3f2bbd72014-09-24 12:07:58 -0700449
450 @Override
samuele1fa7322015-07-14 16:35:16 +0800451 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700452 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700453 // Several things can happen here:
454 // 1. request and response match
455 // 2. request and response don't match
456 // 3. MastershipRole and requested match (and 1 or 2 are true)
457 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
458 //
459 // 2, 4, and 3 with case 2 are failure modes.
460
tom3f2bbd72014-09-24 12:07:58 -0700461 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700462
Madan Jampanif2af7712015-05-29 18:43:52 -0700463 log.debug("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700464 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700465
466 if (requested == null && response == null) {
samuele1fa7322015-07-14 16:35:16 +0800467 // something was off with DeviceProvider, maybe check channel too?
468 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700469 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700470 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700471 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700472
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700473 if (Objects.equals(requested, response)) {
samuele1fa7322015-07-14 16:35:16 +0800474 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700475 return;
476 } else {
477 return;
samuele1fa7322015-07-14 16:35:16 +0800478 // FIXME roleManager got the device to comply, but doesn't agree with
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700479 // the store; use the store's view, then try to reassert.
480 }
481 } else {
482 // we didn't get back what we asked for. Reelect someone else.
samuele1fa7322015-07-14 16:35:16 +0800483 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700484 if (response == MastershipRole.MASTER) {
485 mastershipService.relinquishMastership(deviceId);
486 // TODO: Shouldn't we be triggering event?
samuele1fa7322015-07-14 16:35:16 +0800487 //final Device device = getDevice(deviceId);
488 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700489 }
490 }
tom3f2bbd72014-09-24 12:07:58 -0700491 }
sangho538108b2015-04-08 14:29:20 -0700492
493 @Override
samuele1fa7322015-07-14 16:35:16 +0800494 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
sangho538108b2015-04-08 14:29:20 -0700495 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700496 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700497 checkValidity();
498
samuele1fa7322015-07-14 16:35:16 +0800499 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
500 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700501 post(event);
502 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700503
504 // supplements or replaces deviceDescription annotations with
505 // BasicDeviceConfig annotations
506 private SparseAnnotations processAnnotations(BasicDeviceConfig cfg, DeviceDescription deviceDescription,
507 DeviceId deviceId) {
508 SparseAnnotations originalAnnotations = deviceDescription.annotations();
509 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
510 if (cfg.driver() != deviceId.toString()) {
511 newBuilder.set(cfg.DRIVER, cfg.driver());
512 }
513 if (cfg.type() != Type.SWITCH) {
514 newBuilder.set(cfg.TYPE, cfg.type().toString());
515 }
516 if (cfg.name() != null) {
517 newBuilder.set(cfg.NAME, cfg.name());
518 }
519 if (cfg.latitude() != -1) {
520 newBuilder.set(cfg.LATITUDE, Double.toString(cfg.latitude()));
521 }
522 if (cfg.longitude() != -1) {
523 newBuilder.set(cfg.LONGITUDE, Double.toString(cfg.longitude()));
524 }
525 if (cfg.rackAddress() != null) {
526 newBuilder.set(cfg.RACK_ADDRESS, cfg.rackAddress());
527 }
528 if (cfg.owner() != null) {
529 newBuilder.set(cfg.OWNER, cfg.owner());
530 }
531 DefaultAnnotations newAnnotations = newBuilder.build();
532 return DefaultAnnotations.union(originalAnnotations, newAnnotations);
533 }
tomd3097b02014-08-26 10:40:29 -0700534 }
tom32f66842014-08-27 19:27:47 -0700535
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800536 // Applies the specified role to the device; ignores NONE
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700537
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800538 /**
539 * Apply role to device and send probe if MASTER.
540 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700541 * @param deviceId device identifier
542 * @param newRole new role to apply to the device
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800543 * @return true if the request was sent to provider
544 */
545 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
546 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800547 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700548 return true;
549 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700550
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800551 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800552 if (provider == null) {
samuele1fa7322015-07-14 16:35:16 +0800553 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800554 return false;
555 }
556 provider.roleChanged(deviceId, newRole);
557
558 if (newRole.equals(MastershipRole.MASTER)) {
559 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800560 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800561 }
562 return true;
563 }
564
565 /**
566 * Reaasert role for specified device connected to this node.
567 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700568 * @param did device identifier
569 * @param nextRole role to apply. If NONE is specified,
570 * it will ask mastership service for a role and apply it.
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800571 */
samuele1fa7322015-07-14 16:35:16 +0800572 private void reassertRole(final DeviceId did,
573 final MastershipRole nextRole) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800574
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800575 MastershipRole myNextRole = nextRole;
576 if (myNextRole == NONE) {
577 mastershipService.requestRoleFor(did);
578 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700579 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800580 myNextRole = MASTER;
581 } else {
582 myNextRole = STANDBY;
583 }
584 }
585
586 switch (myNextRole) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700587 case MASTER:
588 final Device device = getDevice(did);
589 if ((device != null) && !isAvailable(did)) {
590 //flag the device as online. Is there a better way to do this?
591 DefaultDeviceDescription deviceDescription
592 = new DefaultDeviceDescription(did.uri(),
593 device.type(),
594 device.manufacturer(),
595 device.hwVersion(),
596 device.swVersion(),
597 device.serialNumber(),
598 device.chassisId());
599 DeviceEvent devEvent =
600 store.createOrUpdateDevice(device.providerId(), did,
601 deviceDescription);
602 post(devEvent);
603 }
604 // TODO: should apply role only if there is mismatch
605 log.debug("Applying role {} to {}", myNextRole, did);
606 if (!applyRoleAndProbe(did, MASTER)) {
607 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
608 // immediately failed to apply role
609 mastershipService.relinquishMastership(did);
610 // FIXME disconnect?
611 }
612 break;
613 case STANDBY:
614 log.debug("Applying role {} to {}", myNextRole, did);
615 if (!applyRoleAndProbe(did, STANDBY)) {
616 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
617 // immediately failed to apply role
618 mastershipService.relinquishMastership(did);
619 // FIXME disconnect?
620 }
621 break;
622 case NONE:
623 default:
624 // should never reach here
625 log.error("You didn't see anything. I did not exist.");
626 break;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800627 }
628 }
629
Madan Jampani328371d2015-05-29 14:06:27 -0700630 private void handleMastershipEvent(MastershipEvent event) {
631 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
632 // Don't care if backup list changed.
633 return;
634 }
635
636 final DeviceId did = event.subject();
637
638 // myRole suggested by MastershipService
639 MastershipRole myNextRole;
640 if (localNodeId.equals(event.roleInfo().master())) {
641 // confirm latest info
642 MastershipTerm term = termService.getMastershipTerm(did);
samuele1fa7322015-07-14 16:35:16 +0800643 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Madan Jampani328371d2015-05-29 14:06:27 -0700644 if (iHaveControl) {
Madan Jampani328371d2015-05-29 14:06:27 -0700645 myNextRole = MASTER;
646 } else {
647 myNextRole = STANDBY;
648 }
649 } else if (event.roleInfo().backups().contains(localNodeId)) {
650 myNextRole = STANDBY;
651 } else {
652 myNextRole = NONE;
653 }
654
samuele1fa7322015-07-14 16:35:16 +0800655
Madan Jampani328371d2015-05-29 14:06:27 -0700656 final boolean isReachable = isReachable(did);
657 if (!isReachable) {
658 // device is not connected to this node
659 if (myNextRole != NONE) {
660 log.warn("Node was instructed to be {} role for {}, "
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700661 + "but this node cannot reach the device. "
662 + "Relinquishing role. ",
samuele1fa7322015-07-14 16:35:16 +0800663 myNextRole, did);
Madan Jampani328371d2015-05-29 14:06:27 -0700664 mastershipService.relinquishMastership(did);
665 }
666 return;
667 }
668
669 // device is connected to this node:
670 if (store.getDevice(did) != null) {
671 reassertRole(did, myNextRole);
672 } else {
673 log.debug("Device is not yet/no longer in the store: {}", did);
674 }
675 }
676
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800677 // Intercepts mastership events
678 private class InternalMastershipListener implements MastershipListener {
679
tomb41d1ac2014-09-24 01:51:24 -0700680 @Override
681 public void event(MastershipEvent event) {
Madan Jampani328371d2015-05-29 14:06:27 -0700682 backgroundService.submit(() -> {
683 try {
684 handleMastershipEvent(event);
685 } catch (Exception e) {
686 log.warn("Failed to handle {}", event, e);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700687 }
Madan Jampani328371d2015-05-29 14:06:27 -0700688 });
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700689 }
tomb41d1ac2014-09-24 01:51:24 -0700690 }
tomf80c9722014-09-24 14:49:18 -0700691
692 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700693 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700694 @Override
695 public void notify(DeviceEvent event) {
696 post(event);
697 }
698 }
samuel738dfaf2015-07-11 11:08:57 +0800699
700 @Override
701 public Iterable<Device> getDevices(Type type) {
702 checkPermission(Permission.DEVICE_READ);
703 Set<Device> results = new HashSet<>();
704 Iterable<Device> devices = store.getDevices();
705 if (devices != null) {
706 devices.forEach(d -> {
707 if (type.equals(d.type())) {
708 results.add(d);
709 }
710 });
711 }
712 return results;
713 }
714
715 @Override
716 public Iterable<Device> getAvailableDevices(Type type) {
717 checkPermission(Permission.DEVICE_READ);
718 Set<Device> results = new HashSet<>();
719 Iterable<Device> availableDevices = store.getAvailableDevices();
720 if (availableDevices != null) {
721 availableDevices.forEach(d -> {
722 if (type.equals(d.type())) {
723 results.add(d);
724 }
725 });
726 }
727 return results;
728 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700729
730 private class InternalNetworkConfigListener implements NetworkConfigListener {
731 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700732 public boolean isRelevant(NetworkConfigEvent event) {
733 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
734 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
735 && event.configClass().equals(BasicDeviceConfig.class);
736 }
737
738 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700739 public void event(NetworkConfigEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700740 log.info("Detected Device network config event {}", event.type());
741 kickOutBadDevice(((DeviceId) event.subject()));
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700742 }
743 }
744
745 // checks if the specified device is allowed by the BasicDeviceConfig
746 // and if not, removes it
747 private void kickOutBadDevice(DeviceId deviceId) {
748 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
749 if (!cfg.isAllowed()) {
750 Device badDevice = getDevice(deviceId);
751 if (badDevice != null) {
752 removeDevice(deviceId);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700753 }
754 }
755 }
tomd3097b02014-08-26 10:40:29 -0700756}