blob: b0b3abe2b79751a76ad10bd350d8690fac006f1f [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;
Ray Milkeya4122362015-08-18 15:19:08 -070030import org.onosproject.net.config.NetworkConfigEvent;
31import org.onosproject.net.config.NetworkConfigListener;
32import org.onosproject.net.config.NetworkConfigService;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070033import org.onosproject.net.config.basics.BasicDeviceConfig;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070034import org.onosproject.net.config.basics.OpticalPortConfig;
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;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070040import org.onosproject.net.ConnectPoint;
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;
47import org.onosproject.net.device.DefaultDeviceDescription;
48import org.onosproject.net.device.DefaultPortDescription;
49import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.device.DeviceDescription;
51import org.onosproject.net.device.DeviceEvent;
52import org.onosproject.net.device.DeviceListener;
53import org.onosproject.net.device.DeviceProvider;
54import org.onosproject.net.device.DeviceProviderRegistry;
55import org.onosproject.net.device.DeviceProviderService;
56import org.onosproject.net.device.DeviceService;
57import org.onosproject.net.device.DeviceStore;
58import org.onosproject.net.device.DeviceStoreDelegate;
59import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070060import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070062import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070063
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070064import java.util.Collection;
65import java.util.HashSet;
66import java.util.List;
67import java.util.Objects;
68import java.util.Set;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070069import java.util.stream.Collectors;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070070import 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;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070076import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
77import static org.onlab.util.Tools.groupedThreads;
78import static org.onosproject.net.MastershipRole.*;
79import static org.onosproject.security.AppGuard.checkPermission;
80import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090081import static org.onosproject.security.AppPermission.Type.*;
Jonathan Hart2f669362015-02-11 16:19:20 -080082
tomd3097b02014-08-26 10:40:29 -070083/**
tome4729872014-09-23 00:37:37 -070084 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070085 */
86@Component(immediate = true)
87@Service
tom41a2c5f2014-09-19 09:20:35 -070088public class DeviceManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070089 extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService>
Thomas Vachuskad16ce182014-10-29 17:25:29 -070090 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070091
tome5ec3fd2014-09-04 15:18:06 -070092 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
93 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
94 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
95 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070096 private static final String PORT_DESC_LIST_NULL = "Port description list 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 Yoonb856b812015-08-10 03:47:19 +0900156 checkPermission(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 Yoonb856b812015-08-10 03:47:19 +0900162 checkPermission(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 Yoonb856b812015-08-10 03:47:19 +0900168 checkPermission(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 Yoonb856b812015-08-10 03:47:19 +0900174 checkPermission(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 Yoonb856b812015-08-10 03:47:19 +0900181 checkPermission(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 Yoonb856b812015-08-10 03:47:19 +0900188 checkPermission(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 Yoonb856b812015-08-10 03:47:19 +0900195 checkPermission(DEVICE_READ);
sangho538108b2015-04-08 14:29:20 -0700196 checkNotNull(deviceId, DEVICE_ID_NULL);
197 return store.getPortStatistics(deviceId);
198 }
199
200 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200201 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900202 checkPermission(DEVICE_READ);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200203 checkNotNull(deviceId, DEVICE_ID_NULL);
204 return store.getPortDeltaStatistics(deviceId);
205 }
206
207 @Override
tom32f66842014-08-27 19:27:47 -0700208 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900209 checkPermission(DEVICE_READ);
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 Yoonb856b812015-08-10 03:47:19 +0900217 checkPermission(DEVICE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900218
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
samuele1fa7322015-07-14 16:35:16 +0800248 protected DeviceProviderService createProviderService(
249 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700250 return new InternalDeviceProviderService(provider);
251 }
252
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800253 /**
254 * Checks if all the reachable devices have a valid mastership role.
255 */
256 private void mastershipCheck() {
257 log.debug("Checking mastership");
258 for (Device device : getDevices()) {
259 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800260 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800261
262 if (!isReachable(deviceId)) {
263 continue;
264 }
265
266 if (mastershipService.getLocalRole(deviceId) != NONE) {
267 continue;
268 }
269
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700270 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800271
272 // isReachable but was not MASTER or STANDBY, get a role and apply
273 // Note: NONE triggers request to MastershipService
274 reassertRole(deviceId, NONE);
275 }
276 }
277
tomd3097b02014-08-26 10:40:29 -0700278 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700279 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700280 extends AbstractProviderService<DeviceProvider>
281 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700282
tomcfde0622014-09-09 11:02:42 -0700283 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700284 super(provider);
285 }
286
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700287 /**
288 * Apply role in reaction to provider event.
289 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700290 * @param deviceId device identifier
291 * @param newRole new role to apply to the device
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700292 * @return true if the request was sent to provider
293 */
294 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
295
296 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800297 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700298 return true;
299 }
300
301 DeviceProvider provider = provider();
302 if (provider == null) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700303 log.warn("Provider for {} was not found. Cannot apply role {}",
304 deviceId, newRole);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700305 return false;
306 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700307 provider.roleChanged(deviceId, newRole);
308 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700309
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700310 return true;
311 }
312
tomd3097b02014-08-26 10:40:29 -0700313 @Override
alshabibb7b40632014-09-28 21:30:00 -0700314 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700315 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700316 checkNotNull(deviceId, DEVICE_ID_NULL);
317 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700318 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700319
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700320 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
321 if (!isAllowed(cfg)) {
322 log.warn("Device {} is not allowed", deviceId);
323 return;
324 }
325 // Generate updated description and establish my Role
326 deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription);
Madan Jampani565a66a2015-07-25 17:01:13 -0700327 Futures.getUnchecked(mastershipService.requestRoleFor(deviceId)
328 .thenAccept(role -> {
329 log.info("Local role is {} for {}", role, deviceId);
330 applyRole(deviceId, role);
331 }));
HIGUCHI Yuta11530fb2015-05-27 13:10:20 -0700332
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700333 DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId,
334 deviceDescription);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700335 log.info("Device {} connected", deviceId);
tom80c0e5e2014-09-08 18:08:58 -0700336 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700337 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700338 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700339 }
tomd3097b02014-08-26 10:40:29 -0700340 }
341
342 @Override
343 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700344 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700345 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700346
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700347 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700348
alshabibafc514a2014-12-01 14:44:05 -0800349 List<Port> ports = store.getPorts(deviceId);
350 List<PortDescription> descs = Lists.newArrayList();
samuele1fa7322015-07-14 16:35:16 +0800351 ports.forEach(port ->
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700352 descs.add(new DefaultPortDescription(port.number(),
353 false, port.type(),
354 port.portSpeed())));
alshabibafc514a2014-12-01 14:44:05 -0800355 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700356 try {
Madan Jampani565a66a2015-07-25 17:01:13 -0700357 if (mastershipService.isLocalMaster(deviceId)) {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700358 post(store.markOffline(deviceId));
359 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700360 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700361 log.warn("Failed to mark {} offline", deviceId);
362 // only the MASTER should be marking off-line in normal cases,
samuele1fa7322015-07-14 16:35:16 +0800363 // but if I was the last STANDBY connection, etc. and no one else
364 // was there to mark the device offline, this instance may need to
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700365 // temporarily request for Master Role and mark offline.
366
samuele1fa7322015-07-14 16:35:16 +0800367 //there are times when this node will correctly have mastership, BUT
368 //that isn't reflected in the ClockManager before the device disconnects.
369 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700370
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700371 // FIXME: Store semantics leaking out as IllegalStateException.
samuele1fa7322015-07-14 16:35:16 +0800372 // Consider revising store API to handle this scenario.
373 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700374 roleFuture.whenComplete((role, error) -> {
samuele1fa7322015-07-14 16:35:16 +0800375 MastershipTerm term = termService.getMastershipTerm(deviceId);
376 // TODO: Move this type of check inside device clock manager, etc.
377 if (term != null && localNodeId.equals(term.master())) {
378 log.info("Retry marking {} offline", deviceId);
samuele1fa7322015-07-14 16:35:16 +0800379 post(store.markOffline(deviceId));
380 } else {
381 log.info("Failed again marking {} offline. {}", deviceId, role);
382 }
383 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700384 } finally {
Madan Jampanic6e574f2015-05-29 13:41:52 -0700385 try {
samuele1fa7322015-07-14 16:35:16 +0800386 //relinquish master role and ability to be backup.
Madan Jampanic6e574f2015-05-29 13:41:52 -0700387 mastershipService.relinquishMastership(deviceId).get();
388 } catch (InterruptedException e) {
samuele1fa7322015-07-14 16:35:16 +0800389 log.warn("Interrupted while reliquishing role for {}", deviceId);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700390 Thread.currentThread().interrupt();
391 } catch (ExecutionException e) {
samuele1fa7322015-07-14 16:35:16 +0800392 log.error("Exception thrown while relinquishing role for {}", deviceId, e);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700393 }
tom0efbb1d2014-09-09 11:54:28 -0700394 }
tomd3097b02014-08-26 10:40:29 -0700395 }
396
397 @Override
alshabibb7b40632014-09-28 21:30:00 -0700398 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700399 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700400 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700401 checkNotNull(portDescriptions, PORT_DESC_LIST_NULL);
tomeadbb462014-09-07 16:10:19 -0700402 checkValidity();
Madan Jampani565a66a2015-07-25 17:01:13 -0700403 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700404 // Never been a master for this device
405 // any update will be ignored.
samuele1fa7322015-07-14 16:35:16 +0800406 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700407 return;
408 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700409 portDescriptions = portDescriptions.stream()
410 .map(e -> consolidate(deviceId, e))
411 .collect(Collectors.toList());
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700412 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
samuele1fa7322015-07-14 16:35:16 +0800413 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700414 for (DeviceEvent event : events) {
415 post(event);
416 }
tomd3097b02014-08-26 10:40:29 -0700417 }
418
419 @Override
alshabibb7b40632014-09-28 21:30:00 -0700420 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700421 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700422 checkNotNull(deviceId, DEVICE_ID_NULL);
423 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700424 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700425
Madan Jampani565a66a2015-07-25 17:01:13 -0700426 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700427 // Never been a master for this device
428 // any update will be ignored.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700429 log.trace("Ignoring {} port update on standby node. {}", deviceId,
430 portDescription);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700431 return;
432 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700433 portDescription = consolidate(deviceId, portDescription);
samuele1fa7322015-07-14 16:35:16 +0800434 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
435 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700436 if (event != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700437 log.info("Device {} port {} status changed", deviceId, event.port().number());
tom0efbb1d2014-09-09 11:54:28 -0700438 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700439 }
tomd3097b02014-08-26 10:40:29 -0700440 }
tom3f2bbd72014-09-24 12:07:58 -0700441
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700442 // merges the appropriate PortConfig with the description.
443 private PortDescription consolidate(DeviceId did, PortDescription desc) {
444 switch (desc.type()) {
445 case COPPER:
446 case VIRTUAL:
447 return desc;
448 default:
449 OpticalPortConfig opc = networkConfigService.getConfig(
450 new ConnectPoint(did, desc.portNumber()), OpticalPortConfig.class);
451 return OpticalPortOperator.combine(opc, desc);
452 }
453 }
454
tom3f2bbd72014-09-24 12:07:58 -0700455 @Override
samuele1fa7322015-07-14 16:35:16 +0800456 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700457 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700458 // Several things can happen here:
459 // 1. request and response match
460 // 2. request and response don't match
461 // 3. MastershipRole and requested match (and 1 or 2 are true)
462 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
463 //
464 // 2, 4, and 3 with case 2 are failure modes.
465
tom3f2bbd72014-09-24 12:07:58 -0700466 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700467
Madan Jampanif2af7712015-05-29 18:43:52 -0700468 log.debug("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700469 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700470
471 if (requested == null && response == null) {
samuele1fa7322015-07-14 16:35:16 +0800472 // something was off with DeviceProvider, maybe check channel too?
473 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700474 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700475 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700476 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700477
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700478 if (Objects.equals(requested, response)) {
samuele1fa7322015-07-14 16:35:16 +0800479 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700480 return;
481 } else {
482 return;
samuele1fa7322015-07-14 16:35:16 +0800483 // FIXME roleManager got the device to comply, but doesn't agree with
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700484 // the store; use the store's view, then try to reassert.
485 }
486 } else {
487 // we didn't get back what we asked for. Reelect someone else.
samuele1fa7322015-07-14 16:35:16 +0800488 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700489 if (response == MastershipRole.MASTER) {
490 mastershipService.relinquishMastership(deviceId);
491 // TODO: Shouldn't we be triggering event?
samuele1fa7322015-07-14 16:35:16 +0800492 //final Device device = getDevice(deviceId);
493 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700494 }
495 }
tom3f2bbd72014-09-24 12:07:58 -0700496 }
sangho538108b2015-04-08 14:29:20 -0700497
498 @Override
samuele1fa7322015-07-14 16:35:16 +0800499 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
sangho538108b2015-04-08 14:29:20 -0700500 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700501 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700502 checkValidity();
503
samuele1fa7322015-07-14 16:35:16 +0800504 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
505 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700506 post(event);
507 }
tomd3097b02014-08-26 10:40:29 -0700508 }
tom32f66842014-08-27 19:27:47 -0700509
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700510 // by default allowed, otherwise check flag
511 private boolean isAllowed(BasicDeviceConfig cfg) {
512 return (cfg == null || cfg.isAllowed());
513 }
514
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800515 // Applies the specified role to the device; ignores NONE
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700516
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800517 /**
518 * Apply role to device and send probe if MASTER.
519 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700520 * @param deviceId device identifier
521 * @param newRole new role to apply to the device
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800522 * @return true if the request was sent to provider
523 */
524 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
525 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800526 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700527 return true;
528 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700529
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800530 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800531 if (provider == null) {
samuele1fa7322015-07-14 16:35:16 +0800532 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800533 return false;
534 }
535 provider.roleChanged(deviceId, newRole);
536
537 if (newRole.equals(MastershipRole.MASTER)) {
538 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800539 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800540 }
541 return true;
542 }
543
544 /**
545 * Reaasert role for specified device connected to this node.
546 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700547 * @param did device identifier
548 * @param nextRole role to apply. If NONE is specified,
549 * it will ask mastership service for a role and apply it.
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800550 */
samuele1fa7322015-07-14 16:35:16 +0800551 private void reassertRole(final DeviceId did,
552 final MastershipRole nextRole) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800553
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800554 MastershipRole myNextRole = nextRole;
555 if (myNextRole == NONE) {
556 mastershipService.requestRoleFor(did);
557 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700558 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800559 myNextRole = MASTER;
560 } else {
561 myNextRole = STANDBY;
562 }
563 }
564
565 switch (myNextRole) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700566 case MASTER:
567 final Device device = getDevice(did);
568 if ((device != null) && !isAvailable(did)) {
569 //flag the device as online. Is there a better way to do this?
570 DefaultDeviceDescription deviceDescription
571 = new DefaultDeviceDescription(did.uri(),
572 device.type(),
573 device.manufacturer(),
574 device.hwVersion(),
575 device.swVersion(),
576 device.serialNumber(),
577 device.chassisId());
578 DeviceEvent devEvent =
579 store.createOrUpdateDevice(device.providerId(), did,
580 deviceDescription);
581 post(devEvent);
582 }
583 // TODO: should apply role only if there is mismatch
584 log.debug("Applying role {} to {}", myNextRole, did);
585 if (!applyRoleAndProbe(did, MASTER)) {
586 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
587 // immediately failed to apply role
588 mastershipService.relinquishMastership(did);
589 // FIXME disconnect?
590 }
591 break;
592 case STANDBY:
593 log.debug("Applying role {} to {}", myNextRole, did);
594 if (!applyRoleAndProbe(did, STANDBY)) {
595 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
596 // immediately failed to apply role
597 mastershipService.relinquishMastership(did);
598 // FIXME disconnect?
599 }
600 break;
601 case NONE:
602 default:
603 // should never reach here
604 log.error("You didn't see anything. I did not exist.");
605 break;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800606 }
607 }
608
Madan Jampani328371d2015-05-29 14:06:27 -0700609 private void handleMastershipEvent(MastershipEvent event) {
610 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
611 // Don't care if backup list changed.
612 return;
613 }
614
615 final DeviceId did = event.subject();
616
617 // myRole suggested by MastershipService
618 MastershipRole myNextRole;
619 if (localNodeId.equals(event.roleInfo().master())) {
620 // confirm latest info
621 MastershipTerm term = termService.getMastershipTerm(did);
samuele1fa7322015-07-14 16:35:16 +0800622 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Madan Jampani328371d2015-05-29 14:06:27 -0700623 if (iHaveControl) {
Madan Jampani328371d2015-05-29 14:06:27 -0700624 myNextRole = MASTER;
625 } else {
626 myNextRole = STANDBY;
627 }
628 } else if (event.roleInfo().backups().contains(localNodeId)) {
629 myNextRole = STANDBY;
630 } else {
631 myNextRole = NONE;
632 }
633
Madan Jampani328371d2015-05-29 14:06:27 -0700634 final boolean isReachable = isReachable(did);
635 if (!isReachable) {
636 // device is not connected to this node
637 if (myNextRole != NONE) {
638 log.warn("Node was instructed to be {} role for {}, "
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700639 + "but this node cannot reach the device. "
640 + "Relinquishing role. ",
samuele1fa7322015-07-14 16:35:16 +0800641 myNextRole, did);
Madan Jampani328371d2015-05-29 14:06:27 -0700642 mastershipService.relinquishMastership(did);
643 }
644 return;
645 }
646
647 // device is connected to this node:
648 if (store.getDevice(did) != null) {
649 reassertRole(did, myNextRole);
650 } else {
651 log.debug("Device is not yet/no longer in the store: {}", did);
652 }
653 }
654
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800655 // Intercepts mastership events
656 private class InternalMastershipListener implements MastershipListener {
657
tomb41d1ac2014-09-24 01:51:24 -0700658 @Override
659 public void event(MastershipEvent event) {
Madan Jampani328371d2015-05-29 14:06:27 -0700660 backgroundService.submit(() -> {
661 try {
662 handleMastershipEvent(event);
663 } catch (Exception e) {
664 log.warn("Failed to handle {}", event, e);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700665 }
Madan Jampani328371d2015-05-29 14:06:27 -0700666 });
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700667 }
tomb41d1ac2014-09-24 01:51:24 -0700668 }
tomf80c9722014-09-24 14:49:18 -0700669
670 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700671 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700672 @Override
673 public void notify(DeviceEvent event) {
674 post(event);
675 }
676 }
samuel738dfaf2015-07-11 11:08:57 +0800677
678 @Override
679 public Iterable<Device> getDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900680 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800681 Set<Device> results = new HashSet<>();
682 Iterable<Device> devices = store.getDevices();
683 if (devices != null) {
684 devices.forEach(d -> {
685 if (type.equals(d.type())) {
686 results.add(d);
687 }
688 });
689 }
690 return results;
691 }
692
693 @Override
694 public Iterable<Device> getAvailableDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900695 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800696 Set<Device> results = new HashSet<>();
697 Iterable<Device> availableDevices = store.getAvailableDevices();
698 if (availableDevices != null) {
699 availableDevices.forEach(d -> {
700 if (type.equals(d.type())) {
701 results.add(d);
702 }
703 });
704 }
705 return results;
706 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700707
708 private class InternalNetworkConfigListener implements NetworkConfigListener {
709 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700710 public boolean isRelevant(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700711 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
712 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
713 && (event.configClass().equals(BasicDeviceConfig.class)
714 || event.configClass().equals(OpticalPortConfig.class));
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700715 }
716
717 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700718 public void event(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700719 DeviceEvent de = null;
720 if (event.configClass().equals(BasicDeviceConfig.class)) {
721 log.info("Detected Device network config event {}", event.type());
722 DeviceId did = (DeviceId) event.subject();
723 BasicDeviceConfig cfg = networkConfigService.getConfig(did, BasicDeviceConfig.class);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700724
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700725 if (!isAllowed(cfg)) {
726 kickOutBadDevice(did);
727 } else {
728 Device dev = getDevice(did);
729 DeviceDescription desc = (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev);
730 desc = BasicDeviceOperator.combine(cfg, desc);
731 if (getProvider(did) != null) {
732 de = store.createOrUpdateDevice(getProvider(did).id(), did, desc);
733 }
734 }
735 }
736 if (event.configClass().equals(OpticalPortConfig.class)) {
737 ConnectPoint cpt = (ConnectPoint) event.subject();
738 DeviceId did = cpt.deviceId();
739 Port dpt = getPort(did, cpt.port());
740
741 if (dpt != null) {
742 OpticalPortConfig opc = networkConfigService.getConfig(cpt, OpticalPortConfig.class);
743 PortDescription desc = OpticalPortOperator.descriptionOf(dpt);
744 desc = OpticalPortOperator.combine(opc, desc);
745 if (getProvider(did) != null) {
746 de = store.updatePortStatus(getProvider(did).id(), did, desc);
747 }
748 }
749 }
750
751 if (de != null) {
752 post(de);
753 }
754 }
755
756 // checks if the specified device is allowed by the BasicDeviceConfig
757 // and if not, removes it
758 private void kickOutBadDevice(DeviceId deviceId) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700759 Device badDevice = getDevice(deviceId);
760 if (badDevice != null) {
761 removeDevice(deviceId);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700762 }
763 }
764 }
tomd3097b02014-08-26 10:40:29 -0700765}