blob: e35dc0c575d4dfc2c6013aeab611cba89babbbc0 [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
Yafit Hadara9a73de2015-09-06 13:52:52 +030018import static com.google.common.base.Preconditions.checkNotNull;
19import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
20import static org.onlab.util.Tools.groupedThreads;
21import static org.onosproject.net.MastershipRole.MASTER;
22import static org.onosproject.net.MastershipRole.NONE;
23import static org.onosproject.net.MastershipRole.STANDBY;
24import static org.onosproject.security.AppGuard.checkPermission;
25import static org.onosproject.security.AppPermission.Type.DEVICE_READ;
26import static org.slf4j.LoggerFactory.getLogger;
27
28import java.util.Collection;
29import java.util.HashSet;
30import java.util.List;
31import java.util.Objects;
32import java.util.Set;
33import java.util.concurrent.CompletableFuture;
34import java.util.concurrent.ExecutionException;
35import java.util.concurrent.ScheduledExecutorService;
36import java.util.concurrent.TimeUnit;
37import java.util.stream.Collectors;
Madan Jampani565a66a2015-07-25 17:01:13 -070038
tomd3097b02014-08-26 10:40:29 -070039import org.apache.felix.scr.annotations.Activate;
40import org.apache.felix.scr.annotations.Component;
41import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070042import org.apache.felix.scr.annotations.Reference;
43import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070044import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.cluster.ClusterService;
46import org.onosproject.cluster.NodeId;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.mastership.MastershipEvent;
48import org.onosproject.mastership.MastershipListener;
49import org.onosproject.mastership.MastershipService;
50import org.onosproject.mastership.MastershipTerm;
51import org.onosproject.mastership.MastershipTermService;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070052import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.Device;
samuel738dfaf2015-07-11 11:08:57 +080054import org.onosproject.net.Device.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.DeviceId;
56import org.onosproject.net.MastershipRole;
57import org.onosproject.net.Port;
58import org.onosproject.net.PortNumber;
Yafit Hadara9a73de2015-09-06 13:52:52 +030059import org.onosproject.net.config.NetworkConfigEvent;
60import org.onosproject.net.config.NetworkConfigListener;
61import org.onosproject.net.config.NetworkConfigService;
62import org.onosproject.net.config.basics.BasicDeviceConfig;
63import org.onosproject.net.config.basics.OpticalPortConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080064import org.onosproject.net.device.DefaultDeviceDescription;
65import org.onosproject.net.device.DefaultPortDescription;
66import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080067import org.onosproject.net.device.DeviceDescription;
68import org.onosproject.net.device.DeviceEvent;
69import org.onosproject.net.device.DeviceListener;
70import org.onosproject.net.device.DeviceProvider;
71import org.onosproject.net.device.DeviceProviderRegistry;
72import org.onosproject.net.device.DeviceProviderService;
73import org.onosproject.net.device.DeviceService;
74import org.onosproject.net.device.DeviceStore;
75import org.onosproject.net.device.DeviceStoreDelegate;
76import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070077import org.onosproject.net.device.PortStatistics;
Yafit Hadara9a73de2015-09-06 13:52:52 +030078import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080079import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070080import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070081
Yafit Hadara9a73de2015-09-06 13:52:52 +030082import com.google.common.util.concurrent.Futures;
Jonathan Hart2f669362015-02-11 16:19:20 -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";
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070097 private static final String PORT_DESC_LIST_NULL = "Port description list cannot be null";
tomd3097b02014-08-26 10:40:29 -070098
tom5f38b3a2014-08-27 23:50:54 -070099 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -0700100
alshabib339a3d92014-09-26 17:54:32 -0700101 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -0700102
tomc78acee2014-09-24 15:16:55 -0700103 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -0700104 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -0700105
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800106 private ScheduledExecutorService backgroundService;
107
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700108 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
109
tom41a2c5f2014-09-19 09:20:35 -0700110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -0700112
tom5f38b3a2014-08-27 23:50:54 -0700113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700114 protected ClusterService clusterService;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700117 protected MastershipService mastershipService;
118
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700120 protected MastershipTermService termService;
121
Madan Jampani61056bc2014-09-27 09:07:26 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700123 protected NetworkConfigService networkConfigService;
124
tomd3097b02014-08-26 10:40:29 -0700125 @Activate
126 public void activate() {
samuele1fa7322015-07-14 16:35:16 +0800127 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background"));
Madan Jampanide003d92015-05-11 17:14:20 -0700128 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800129
tomf80c9722014-09-24 14:49:18 -0700130 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700131 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700132 mastershipService.addListener(mastershipListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700133 networkConfigService.addListener(networkConfigListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800134
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700135 backgroundService.scheduleWithFixedDelay(() -> {
136 try {
137 mastershipCheck();
138 } catch (Exception e) {
139 log.error("Exception thrown during integrity check", e);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800140 }
141 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700142 log.info("Started");
143 }
144
145 @Deactivate
146 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800147 backgroundService.shutdown();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700148 networkConfigService.removeListener(networkConfigListener);
tomf80c9722014-09-24 14:49:18 -0700149 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700150 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700151 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700152 log.info("Stopped");
153 }
154
155 @Override
tomad2d2092014-09-06 23:24:20 -0700156 public int getDeviceCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900157 checkPermission(DEVICE_READ);
tomad2d2092014-09-06 23:24:20 -0700158 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700159 }
160
161 @Override
tom32f66842014-08-27 19:27:47 -0700162 public Iterable<Device> getDevices() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900163 checkPermission(DEVICE_READ);
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 Yoonb856b812015-08-10 03:47:19 +0900169 checkPermission(DEVICE_READ);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800170 return store.getAvailableDevices();
171 }
172
173 @Override
tom32f66842014-08-27 19:27:47 -0700174 public Device getDevice(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900175 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700176 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700177 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700178 }
179
180 @Override
tomad2d2092014-09-06 23:24:20 -0700181 public MastershipRole getRole(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900182 checkPermission(DEVICE_READ);
tomad2d2092014-09-06 23:24:20 -0700183 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700184 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700185 }
186
187 @Override
tom32f66842014-08-27 19:27:47 -0700188 public List<Port> getPorts(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900189 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700190 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700191 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700192 }
193
194 @Override
sangho538108b2015-04-08 14:29:20 -0700195 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900196 checkPermission(DEVICE_READ);
sangho538108b2015-04-08 14:29:20 -0700197 checkNotNull(deviceId, DEVICE_ID_NULL);
198 return store.getPortStatistics(deviceId);
199 }
200
201 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200202 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900203 checkPermission(DEVICE_READ);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200204 checkNotNull(deviceId, DEVICE_ID_NULL);
205 return store.getPortDeltaStatistics(deviceId);
206 }
207
208 @Override
tom32f66842014-08-27 19:27:47 -0700209 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900210 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700211 checkNotNull(deviceId, DEVICE_ID_NULL);
212 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700213 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700214 }
215
216 @Override
tomff7eb7c2014-09-08 12:49:03 -0700217 public boolean isAvailable(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900218 checkPermission(DEVICE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900219
tomff7eb7c2014-09-08 12:49:03 -0700220 checkNotNull(deviceId, DEVICE_ID_NULL);
221 return store.isAvailable(deviceId);
222 }
223
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700224 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700225 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800226 if (deviceId == null) {
227 return false;
228 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700229 DeviceProvider provider = getProvider(deviceId);
230 if (provider != null) {
231 return provider.isReachable(deviceId);
232 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800233 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700234 return false;
235 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700236 }
237
tome5ec3fd2014-09-04 15:18:06 -0700238 @Override
239 public void removeDevice(DeviceId deviceId) {
240 checkNotNull(deviceId, DEVICE_ID_NULL);
241 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700242 if (event != null) {
243 log.info("Device {} administratively removed", deviceId);
244 post(event);
245 }
tome5ec3fd2014-09-04 15:18:06 -0700246 }
247
tom7869ad92014-09-09 14:32:08 -0700248 @Override
samuele1fa7322015-07-14 16:35:16 +0800249 protected DeviceProviderService createProviderService(
250 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700251 return new InternalDeviceProviderService(provider);
252 }
253
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800254 /**
255 * Checks if all the reachable devices have a valid mastership role.
256 */
257 private void mastershipCheck() {
258 log.debug("Checking mastership");
259 for (Device device : getDevices()) {
260 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800261 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800262
263 if (!isReachable(deviceId)) {
264 continue;
265 }
266
267 if (mastershipService.getLocalRole(deviceId) != NONE) {
268 continue;
269 }
270
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700271 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800272
273 // isReachable but was not MASTER or STANDBY, get a role and apply
274 // Note: NONE triggers request to MastershipService
275 reassertRole(deviceId, NONE);
276 }
277 }
278
tomd3097b02014-08-26 10:40:29 -0700279 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700280 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700281 extends AbstractProviderService<DeviceProvider>
282 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700283
tomcfde0622014-09-09 11:02:42 -0700284 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700285 super(provider);
286 }
287
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700288 /**
289 * Apply role in reaction to provider event.
290 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700291 * @param deviceId device identifier
292 * @param newRole new role to apply to the device
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700293 * @return true if the request was sent to provider
294 */
295 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
296
297 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800298 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700299 return true;
300 }
301
302 DeviceProvider provider = provider();
303 if (provider == null) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700304 log.warn("Provider for {} was not found. Cannot apply role {}",
305 deviceId, newRole);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700306 return false;
307 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700308 provider.roleChanged(deviceId, newRole);
309 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700310
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700311 return true;
312 }
313
tomd3097b02014-08-26 10:40:29 -0700314 @Override
alshabibb7b40632014-09-28 21:30:00 -0700315 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700316 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700317 checkNotNull(deviceId, DEVICE_ID_NULL);
318 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700319 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700320
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700321 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
322 if (!isAllowed(cfg)) {
323 log.warn("Device {} is not allowed", deviceId);
324 return;
325 }
326 // Generate updated description and establish my Role
327 deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription);
Madan Jampani565a66a2015-07-25 17:01:13 -0700328 Futures.getUnchecked(mastershipService.requestRoleFor(deviceId)
329 .thenAccept(role -> {
330 log.info("Local role is {} for {}", role, deviceId);
331 applyRole(deviceId, role);
332 }));
HIGUCHI Yuta11530fb2015-05-27 13:10:20 -0700333
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700334 DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId,
335 deviceDescription);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700336 log.info("Device {} connected", deviceId);
tom80c0e5e2014-09-08 18:08:58 -0700337 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700338 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700339 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700340 }
tomd3097b02014-08-26 10:40:29 -0700341 }
342
343 @Override
344 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700345 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700346 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700347
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700348 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700349
alshabibafc514a2014-12-01 14:44:05 -0800350 List<Port> ports = store.getPorts(deviceId);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300351 final Device device = getDevice(deviceId);
352
353 List<PortDescription> descs = ports.stream().map(
354 port -> (!(Device.Type.ROADM.equals(device.type()))) ?
355 new DefaultPortDescription(port.number(), false,
356 port.type(), port.portSpeed()) :
357 OpticalPortOperator.descriptionOf(port, false)
358 ).collect(Collectors.toList());
359
alshabibafc514a2014-12-01 14:44:05 -0800360 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700361 try {
Madan Jampani565a66a2015-07-25 17:01:13 -0700362 if (mastershipService.isLocalMaster(deviceId)) {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700363 post(store.markOffline(deviceId));
364 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700365 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700366 log.warn("Failed to mark {} offline", deviceId);
367 // only the MASTER should be marking off-line in normal cases,
samuele1fa7322015-07-14 16:35:16 +0800368 // but if I was the last STANDBY connection, etc. and no one else
369 // was there to mark the device offline, this instance may need to
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700370 // temporarily request for Master Role and mark offline.
371
samuele1fa7322015-07-14 16:35:16 +0800372 //there are times when this node will correctly have mastership, BUT
373 //that isn't reflected in the ClockManager before the device disconnects.
374 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700375
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700376 // FIXME: Store semantics leaking out as IllegalStateException.
samuele1fa7322015-07-14 16:35:16 +0800377 // Consider revising store API to handle this scenario.
378 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700379 roleFuture.whenComplete((role, error) -> {
samuele1fa7322015-07-14 16:35:16 +0800380 MastershipTerm term = termService.getMastershipTerm(deviceId);
381 // TODO: Move this type of check inside device clock manager, etc.
382 if (term != null && localNodeId.equals(term.master())) {
383 log.info("Retry marking {} offline", deviceId);
samuele1fa7322015-07-14 16:35:16 +0800384 post(store.markOffline(deviceId));
385 } else {
386 log.info("Failed again marking {} offline. {}", deviceId, role);
387 }
388 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700389 } finally {
Madan Jampanic6e574f2015-05-29 13:41:52 -0700390 try {
samuele1fa7322015-07-14 16:35:16 +0800391 //relinquish master role and ability to be backup.
Madan Jampanic6e574f2015-05-29 13:41:52 -0700392 mastershipService.relinquishMastership(deviceId).get();
393 } catch (InterruptedException e) {
samuele1fa7322015-07-14 16:35:16 +0800394 log.warn("Interrupted while reliquishing role for {}", deviceId);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700395 Thread.currentThread().interrupt();
396 } catch (ExecutionException e) {
samuele1fa7322015-07-14 16:35:16 +0800397 log.error("Exception thrown while relinquishing role for {}", deviceId, e);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700398 }
tom0efbb1d2014-09-09 11:54:28 -0700399 }
tomd3097b02014-08-26 10:40:29 -0700400 }
401
402 @Override
alshabibb7b40632014-09-28 21:30:00 -0700403 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700404 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700405 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700406 checkNotNull(portDescriptions, PORT_DESC_LIST_NULL);
tomeadbb462014-09-07 16:10:19 -0700407 checkValidity();
Madan Jampani565a66a2015-07-25 17:01:13 -0700408 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700409 // Never been a master for this device
410 // any update will be ignored.
samuele1fa7322015-07-14 16:35:16 +0800411 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700412 return;
413 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700414 portDescriptions = portDescriptions.stream()
415 .map(e -> consolidate(deviceId, e))
416 .collect(Collectors.toList());
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700417 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
samuele1fa7322015-07-14 16:35:16 +0800418 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700419 for (DeviceEvent event : events) {
420 post(event);
421 }
tomd3097b02014-08-26 10:40:29 -0700422 }
423
424 @Override
alshabibb7b40632014-09-28 21:30:00 -0700425 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700426 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700427 checkNotNull(deviceId, DEVICE_ID_NULL);
428 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700429 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700430
Madan Jampani565a66a2015-07-25 17:01:13 -0700431 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700432 // Never been a master for this device
433 // any update will be ignored.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700434 log.trace("Ignoring {} port update on standby node. {}", deviceId,
435 portDescription);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700436 return;
437 }
Yafit Hadara9a73de2015-09-06 13:52:52 +0300438 final Device device = getDevice(deviceId);
439 if ((Device.Type.ROADM.equals(device.type()))) {
440 Port port = getPort(deviceId, portDescription.portNumber());
441 portDescription = OpticalPortOperator.descriptionOf(port, portDescription.isEnabled());
442 }
443
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700444 portDescription = consolidate(deviceId, portDescription);
samuele1fa7322015-07-14 16:35:16 +0800445 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
446 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700447 if (event != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700448 log.info("Device {} port {} status changed", deviceId, event.port().number());
tom0efbb1d2014-09-09 11:54:28 -0700449 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700450 }
tomd3097b02014-08-26 10:40:29 -0700451 }
tom3f2bbd72014-09-24 12:07:58 -0700452
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700453 // merges the appropriate PortConfig with the description.
454 private PortDescription consolidate(DeviceId did, PortDescription desc) {
455 switch (desc.type()) {
456 case COPPER:
457 case VIRTUAL:
458 return desc;
459 default:
460 OpticalPortConfig opc = networkConfigService.getConfig(
461 new ConnectPoint(did, desc.portNumber()), OpticalPortConfig.class);
462 return OpticalPortOperator.combine(opc, desc);
463 }
464 }
465
tom3f2bbd72014-09-24 12:07:58 -0700466 @Override
samuele1fa7322015-07-14 16:35:16 +0800467 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700468 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700469 // Several things can happen here:
470 // 1. request and response match
471 // 2. request and response don't match
472 // 3. MastershipRole and requested match (and 1 or 2 are true)
473 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
474 //
475 // 2, 4, and 3 with case 2 are failure modes.
476
tom3f2bbd72014-09-24 12:07:58 -0700477 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700478
Madan Jampanif2af7712015-05-29 18:43:52 -0700479 log.debug("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700480 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700481
482 if (requested == null && response == null) {
samuele1fa7322015-07-14 16:35:16 +0800483 // something was off with DeviceProvider, maybe check channel too?
484 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700485 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700486 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700487 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700488
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700489 if (Objects.equals(requested, response)) {
samuele1fa7322015-07-14 16:35:16 +0800490 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700491 return;
492 } else {
493 return;
samuele1fa7322015-07-14 16:35:16 +0800494 // FIXME roleManager got the device to comply, but doesn't agree with
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700495 // the store; use the store's view, then try to reassert.
496 }
497 } else {
498 // we didn't get back what we asked for. Reelect someone else.
samuele1fa7322015-07-14 16:35:16 +0800499 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700500 if (response == MastershipRole.MASTER) {
501 mastershipService.relinquishMastership(deviceId);
502 // TODO: Shouldn't we be triggering event?
samuele1fa7322015-07-14 16:35:16 +0800503 //final Device device = getDevice(deviceId);
504 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700505 }
506 }
tom3f2bbd72014-09-24 12:07:58 -0700507 }
sangho538108b2015-04-08 14:29:20 -0700508
509 @Override
samuele1fa7322015-07-14 16:35:16 +0800510 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
sangho538108b2015-04-08 14:29:20 -0700511 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700512 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700513 checkValidity();
514
samuele1fa7322015-07-14 16:35:16 +0800515 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
516 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700517 post(event);
518 }
tomd3097b02014-08-26 10:40:29 -0700519 }
tom32f66842014-08-27 19:27:47 -0700520
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700521 // by default allowed, otherwise check flag
522 private boolean isAllowed(BasicDeviceConfig cfg) {
523 return (cfg == null || cfg.isAllowed());
524 }
525
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800526 // Applies the specified role to the device; ignores NONE
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700527
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800528 /**
529 * Apply role to device and send probe if MASTER.
530 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700531 * @param deviceId device identifier
532 * @param newRole new role to apply to the device
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800533 * @return true if the request was sent to provider
534 */
535 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
536 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800537 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700538 return true;
539 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700540
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800541 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800542 if (provider == null) {
samuele1fa7322015-07-14 16:35:16 +0800543 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800544 return false;
545 }
546 provider.roleChanged(deviceId, newRole);
547
548 if (newRole.equals(MastershipRole.MASTER)) {
549 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800550 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800551 }
552 return true;
553 }
554
555 /**
556 * Reaasert role for specified device connected to this node.
557 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700558 * @param did device identifier
559 * @param nextRole role to apply. If NONE is specified,
560 * it will ask mastership service for a role and apply it.
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800561 */
samuele1fa7322015-07-14 16:35:16 +0800562 private void reassertRole(final DeviceId did,
563 final MastershipRole nextRole) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800564
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800565 MastershipRole myNextRole = nextRole;
566 if (myNextRole == NONE) {
567 mastershipService.requestRoleFor(did);
568 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700569 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800570 myNextRole = MASTER;
571 } else {
572 myNextRole = STANDBY;
573 }
574 }
575
576 switch (myNextRole) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700577 case MASTER:
578 final Device device = getDevice(did);
579 if ((device != null) && !isAvailable(did)) {
580 //flag the device as online. Is there a better way to do this?
581 DefaultDeviceDescription deviceDescription
582 = new DefaultDeviceDescription(did.uri(),
583 device.type(),
584 device.manufacturer(),
585 device.hwVersion(),
586 device.swVersion(),
587 device.serialNumber(),
588 device.chassisId());
589 DeviceEvent devEvent =
590 store.createOrUpdateDevice(device.providerId(), did,
591 deviceDescription);
592 post(devEvent);
593 }
594 // TODO: should apply role only if there is mismatch
595 log.debug("Applying role {} to {}", myNextRole, did);
596 if (!applyRoleAndProbe(did, MASTER)) {
597 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
598 // immediately failed to apply role
599 mastershipService.relinquishMastership(did);
600 // FIXME disconnect?
601 }
602 break;
603 case STANDBY:
604 log.debug("Applying role {} to {}", myNextRole, did);
605 if (!applyRoleAndProbe(did, STANDBY)) {
606 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
607 // immediately failed to apply role
608 mastershipService.relinquishMastership(did);
609 // FIXME disconnect?
610 }
611 break;
612 case NONE:
613 default:
614 // should never reach here
615 log.error("You didn't see anything. I did not exist.");
616 break;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800617 }
618 }
619
Madan Jampani328371d2015-05-29 14:06:27 -0700620 private void handleMastershipEvent(MastershipEvent event) {
621 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
622 // Don't care if backup list changed.
623 return;
624 }
625
626 final DeviceId did = event.subject();
627
628 // myRole suggested by MastershipService
629 MastershipRole myNextRole;
630 if (localNodeId.equals(event.roleInfo().master())) {
631 // confirm latest info
632 MastershipTerm term = termService.getMastershipTerm(did);
samuele1fa7322015-07-14 16:35:16 +0800633 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Madan Jampani328371d2015-05-29 14:06:27 -0700634 if (iHaveControl) {
Madan Jampani328371d2015-05-29 14:06:27 -0700635 myNextRole = MASTER;
636 } else {
637 myNextRole = STANDBY;
638 }
639 } else if (event.roleInfo().backups().contains(localNodeId)) {
640 myNextRole = STANDBY;
641 } else {
642 myNextRole = NONE;
643 }
644
Madan Jampani328371d2015-05-29 14:06:27 -0700645 final boolean isReachable = isReachable(did);
646 if (!isReachable) {
647 // device is not connected to this node
648 if (myNextRole != NONE) {
649 log.warn("Node was instructed to be {} role for {}, "
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700650 + "but this node cannot reach the device. "
651 + "Relinquishing role. ",
samuele1fa7322015-07-14 16:35:16 +0800652 myNextRole, did);
Madan Jampani328371d2015-05-29 14:06:27 -0700653 mastershipService.relinquishMastership(did);
654 }
655 return;
656 }
657
658 // device is connected to this node:
659 if (store.getDevice(did) != null) {
660 reassertRole(did, myNextRole);
661 } else {
662 log.debug("Device is not yet/no longer in the store: {}", did);
663 }
664 }
665
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800666 // Intercepts mastership events
667 private class InternalMastershipListener implements MastershipListener {
668
tomb41d1ac2014-09-24 01:51:24 -0700669 @Override
670 public void event(MastershipEvent event) {
Madan Jampani328371d2015-05-29 14:06:27 -0700671 backgroundService.submit(() -> {
672 try {
673 handleMastershipEvent(event);
674 } catch (Exception e) {
675 log.warn("Failed to handle {}", event, e);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700676 }
Madan Jampani328371d2015-05-29 14:06:27 -0700677 });
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700678 }
tomb41d1ac2014-09-24 01:51:24 -0700679 }
tomf80c9722014-09-24 14:49:18 -0700680
681 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700682 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700683 @Override
684 public void notify(DeviceEvent event) {
685 post(event);
686 }
687 }
samuel738dfaf2015-07-11 11:08:57 +0800688
689 @Override
690 public Iterable<Device> getDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900691 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800692 Set<Device> results = new HashSet<>();
693 Iterable<Device> devices = store.getDevices();
694 if (devices != null) {
695 devices.forEach(d -> {
696 if (type.equals(d.type())) {
697 results.add(d);
698 }
699 });
700 }
701 return results;
702 }
703
704 @Override
705 public Iterable<Device> getAvailableDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900706 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800707 Set<Device> results = new HashSet<>();
708 Iterable<Device> availableDevices = store.getAvailableDevices();
709 if (availableDevices != null) {
710 availableDevices.forEach(d -> {
711 if (type.equals(d.type())) {
712 results.add(d);
713 }
714 });
715 }
716 return results;
717 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700718
719 private class InternalNetworkConfigListener implements NetworkConfigListener {
720 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700721 public boolean isRelevant(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700722 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
723 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
724 && (event.configClass().equals(BasicDeviceConfig.class)
725 || event.configClass().equals(OpticalPortConfig.class));
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700726 }
727
728 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700729 public void event(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700730 DeviceEvent de = null;
731 if (event.configClass().equals(BasicDeviceConfig.class)) {
732 log.info("Detected Device network config event {}", event.type());
733 DeviceId did = (DeviceId) event.subject();
734 BasicDeviceConfig cfg = networkConfigService.getConfig(did, BasicDeviceConfig.class);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700735
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700736 if (!isAllowed(cfg)) {
737 kickOutBadDevice(did);
738 } else {
739 Device dev = getDevice(did);
740 DeviceDescription desc = (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev);
741 desc = BasicDeviceOperator.combine(cfg, desc);
742 if (getProvider(did) != null) {
743 de = store.createOrUpdateDevice(getProvider(did).id(), did, desc);
744 }
745 }
746 }
747 if (event.configClass().equals(OpticalPortConfig.class)) {
748 ConnectPoint cpt = (ConnectPoint) event.subject();
749 DeviceId did = cpt.deviceId();
750 Port dpt = getPort(did, cpt.port());
751
752 if (dpt != null) {
753 OpticalPortConfig opc = networkConfigService.getConfig(cpt, OpticalPortConfig.class);
754 PortDescription desc = OpticalPortOperator.descriptionOf(dpt);
755 desc = OpticalPortOperator.combine(opc, desc);
756 if (getProvider(did) != null) {
757 de = store.updatePortStatus(getProvider(did).id(), did, desc);
758 }
759 }
760 }
761
762 if (de != null) {
763 post(de);
764 }
765 }
766
767 // checks if the specified device is allowed by the BasicDeviceConfig
768 // and if not, removes it
769 private void kickOutBadDevice(DeviceId deviceId) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700770 Device badDevice = getDevice(deviceId);
771 if (badDevice != null) {
772 removeDevice(deviceId);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700773 }
774 }
775 }
tomd3097b02014-08-26 10:40:29 -0700776}