blob: 3c9b71d00031ee67aad4d77d5d1e6fc7ada25cc2 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Thomas Vachuskafa8aa2f2015-10-13 11:56:59 -070021import static org.onlab.util.Tools.nullIsNotFound;
Yafit Hadara9a73de2015-09-06 13:52:52 +030022import static org.onosproject.net.MastershipRole.MASTER;
23import static org.onosproject.net.MastershipRole.NONE;
24import static org.onosproject.net.MastershipRole.STANDBY;
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -070025import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
26import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
Yafit Hadara9a73de2015-09-06 13:52:52 +030027import static org.onosproject.security.AppGuard.checkPermission;
28import static org.onosproject.security.AppPermission.Type.DEVICE_READ;
29import static org.slf4j.LoggerFactory.getLogger;
30
31import java.util.Collection;
32import java.util.HashSet;
33import java.util.List;
34import java.util.Objects;
35import java.util.Set;
36import java.util.concurrent.CompletableFuture;
37import java.util.concurrent.ExecutionException;
38import java.util.concurrent.ScheduledExecutorService;
39import java.util.concurrent.TimeUnit;
40import java.util.stream.Collectors;
Madan Jampani565a66a2015-07-25 17:01:13 -070041
tomd3097b02014-08-26 10:40:29 -070042import org.apache.felix.scr.annotations.Activate;
43import org.apache.felix.scr.annotations.Component;
44import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070045import org.apache.felix.scr.annotations.Reference;
46import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070047import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.cluster.ClusterService;
49import org.onosproject.cluster.NodeId;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.mastership.MastershipEvent;
51import org.onosproject.mastership.MastershipListener;
52import org.onosproject.mastership.MastershipService;
53import org.onosproject.mastership.MastershipTerm;
54import org.onosproject.mastership.MastershipTermService;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070055import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080056import org.onosproject.net.Device;
samuel738dfaf2015-07-11 11:08:57 +080057import org.onosproject.net.Device.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080058import org.onosproject.net.DeviceId;
59import org.onosproject.net.MastershipRole;
60import org.onosproject.net.Port;
61import org.onosproject.net.PortNumber;
Yafit Hadara9a73de2015-09-06 13:52:52 +030062import org.onosproject.net.config.NetworkConfigEvent;
63import org.onosproject.net.config.NetworkConfigListener;
64import org.onosproject.net.config.NetworkConfigService;
65import org.onosproject.net.config.basics.BasicDeviceConfig;
66import org.onosproject.net.config.basics.OpticalPortConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080067import org.onosproject.net.device.DefaultDeviceDescription;
68import org.onosproject.net.device.DefaultPortDescription;
69import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.device.DeviceDescription;
71import org.onosproject.net.device.DeviceEvent;
72import org.onosproject.net.device.DeviceListener;
73import org.onosproject.net.device.DeviceProvider;
74import org.onosproject.net.device.DeviceProviderRegistry;
75import org.onosproject.net.device.DeviceProviderService;
76import org.onosproject.net.device.DeviceService;
77import org.onosproject.net.device.DeviceStore;
78import org.onosproject.net.device.DeviceStoreDelegate;
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -070079import org.onosproject.net.device.OchPortDescription;
80import org.onosproject.net.device.OduCltPortDescription;
81import org.onosproject.net.device.OmsPortDescription;
82import org.onosproject.net.device.OtuPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080083import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070084import org.onosproject.net.device.PortStatistics;
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -070085import org.onosproject.net.optical.device.OmsPortHelper;
86import org.onosproject.net.optical.device.OtuPortHelper;
Yafit Hadara9a73de2015-09-06 13:52:52 +030087import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080088import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070089import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070090
Yafit Hadara9a73de2015-09-06 13:52:52 +030091import com.google.common.util.concurrent.Futures;
Jonathan Hart2f669362015-02-11 16:19:20 -080092
tomd3097b02014-08-26 10:40:29 -070093/**
tome4729872014-09-23 00:37:37 -070094 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070095 */
96@Component(immediate = true)
97@Service
tom41a2c5f2014-09-19 09:20:35 -070098public class DeviceManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070099 extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService>
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700100 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -0700101
tome5ec3fd2014-09-04 15:18:06 -0700102 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
103 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
104 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
105 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700106 private static final String PORT_DESC_LIST_NULL = "Port description list cannot be null";
tomd3097b02014-08-26 10:40:29 -0700107
tom5f38b3a2014-08-27 23:50:54 -0700108 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -0700109
alshabib339a3d92014-09-26 17:54:32 -0700110 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -0700111
tomc78acee2014-09-24 15:16:55 -0700112 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -0700113 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -0700114
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800115 private ScheduledExecutorService backgroundService;
116
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700117 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
118
tom41a2c5f2014-09-19 09:20:35 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -0700121
tom5f38b3a2014-08-27 23:50:54 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700123 protected ClusterService clusterService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700126 protected MastershipService mastershipService;
127
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700129 protected MastershipTermService termService;
130
Madan Jampani61056bc2014-09-27 09:07:26 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700132 protected NetworkConfigService networkConfigService;
133
tomd3097b02014-08-26 10:40:29 -0700134 @Activate
135 public void activate() {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800136 backgroundService = newSingleThreadScheduledExecutor(
137 groupedThreads("onos/device", "manager-background", log));
Madan Jampanide003d92015-05-11 17:14:20 -0700138 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800139
tomf80c9722014-09-24 14:49:18 -0700140 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700141 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700142 mastershipService.addListener(mastershipListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700143 networkConfigService.addListener(networkConfigListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800144
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700145 backgroundService.scheduleWithFixedDelay(() -> {
146 try {
147 mastershipCheck();
148 } catch (Exception e) {
149 log.error("Exception thrown during integrity check", e);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800150 }
151 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700152 log.info("Started");
153 }
154
155 @Deactivate
156 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800157 backgroundService.shutdown();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700158 networkConfigService.removeListener(networkConfigListener);
tomf80c9722014-09-24 14:49:18 -0700159 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700160 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700161 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700162 log.info("Stopped");
163 }
164
165 @Override
tomad2d2092014-09-06 23:24:20 -0700166 public int getDeviceCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900167 checkPermission(DEVICE_READ);
tomad2d2092014-09-06 23:24:20 -0700168 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700169 }
170
171 @Override
tom32f66842014-08-27 19:27:47 -0700172 public Iterable<Device> getDevices() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900173 checkPermission(DEVICE_READ);
tome5ec3fd2014-09-04 15:18:06 -0700174 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700175 }
176
tom32f66842014-08-27 19:27:47 -0700177 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800178 public Iterable<Device> getAvailableDevices() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900179 checkPermission(DEVICE_READ);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800180 return store.getAvailableDevices();
181 }
182
183 @Override
tom32f66842014-08-27 19:27:47 -0700184 public Device getDevice(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900185 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700186 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700187 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700188 }
189
190 @Override
tomad2d2092014-09-06 23:24:20 -0700191 public MastershipRole getRole(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900192 checkPermission(DEVICE_READ);
tomad2d2092014-09-06 23:24:20 -0700193 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700194 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700195 }
196
197 @Override
tom32f66842014-08-27 19:27:47 -0700198 public List<Port> getPorts(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900199 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700200 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700201 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700202 }
203
204 @Override
sangho538108b2015-04-08 14:29:20 -0700205 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900206 checkPermission(DEVICE_READ);
sangho538108b2015-04-08 14:29:20 -0700207 checkNotNull(deviceId, DEVICE_ID_NULL);
208 return store.getPortStatistics(deviceId);
209 }
210
211 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200212 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900213 checkPermission(DEVICE_READ);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200214 checkNotNull(deviceId, DEVICE_ID_NULL);
215 return store.getPortDeltaStatistics(deviceId);
216 }
217
218 @Override
tom32f66842014-08-27 19:27:47 -0700219 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900220 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700221 checkNotNull(deviceId, DEVICE_ID_NULL);
222 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700223 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700224 }
225
226 @Override
tomff7eb7c2014-09-08 12:49:03 -0700227 public boolean isAvailable(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900228 checkPermission(DEVICE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900229
tomff7eb7c2014-09-08 12:49:03 -0700230 checkNotNull(deviceId, DEVICE_ID_NULL);
231 return store.isAvailable(deviceId);
232 }
233
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700234 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700235 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800236 if (deviceId == null) {
237 return false;
238 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700239 DeviceProvider provider = getProvider(deviceId);
240 if (provider != null) {
241 return provider.isReachable(deviceId);
242 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800243 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700244 return false;
245 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700246 }
247
tome5ec3fd2014-09-04 15:18:06 -0700248 @Override
249 public void removeDevice(DeviceId deviceId) {
250 checkNotNull(deviceId, DEVICE_ID_NULL);
251 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700252 if (event != null) {
253 log.info("Device {} administratively removed", deviceId);
254 post(event);
255 }
tome5ec3fd2014-09-04 15:18:06 -0700256 }
257
tom7869ad92014-09-09 14:32:08 -0700258 @Override
Saurav Dasa2d37502016-03-25 17:50:40 -0700259 public void changePortState(DeviceId deviceId, PortNumber portNumber,
260 boolean enable) {
261 checkNotNull(deviceId, DEVICE_ID_NULL);
262 checkNotNull(deviceId, PORT_NUMBER_NULL);
263 DeviceProvider provider = getProvider(deviceId);
264 if (provider != null) {
265 log.warn("Port {} on device {} being administratively brought {}",
266 portNumber, deviceId,
267 (enable) ? "UP" : "DOWN");
268 provider.changePortState(deviceId, portNumber, enable);
269 } else {
270 log.warn("Provider not found for {}", deviceId);
271 }
272 }
273
274 @Override
samuele1fa7322015-07-14 16:35:16 +0800275 protected DeviceProviderService createProviderService(
276 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700277 return new InternalDeviceProviderService(provider);
278 }
279
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800280 /**
281 * Checks if all the reachable devices have a valid mastership role.
282 */
283 private void mastershipCheck() {
284 log.debug("Checking mastership");
285 for (Device device : getDevices()) {
286 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800287 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800288
289 if (!isReachable(deviceId)) {
290 continue;
291 }
292
293 if (mastershipService.getLocalRole(deviceId) != NONE) {
294 continue;
295 }
296
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700297 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800298
299 // isReachable but was not MASTER or STANDBY, get a role and apply
300 // Note: NONE triggers request to MastershipService
301 reassertRole(deviceId, NONE);
302 }
303 }
304
tomd3097b02014-08-26 10:40:29 -0700305 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700306 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700307 extends AbstractProviderService<DeviceProvider>
308 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700309
tomcfde0622014-09-09 11:02:42 -0700310 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700311 super(provider);
312 }
313
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700314 /**
315 * Apply role in reaction to provider event.
316 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700317 * @param deviceId device identifier
318 * @param newRole new role to apply to the device
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700319 * @return true if the request was sent to provider
320 */
321 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
322
323 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800324 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700325 return true;
326 }
327
328 DeviceProvider provider = provider();
329 if (provider == null) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700330 log.warn("Provider for {} was not found. Cannot apply role {}",
331 deviceId, newRole);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700332 return false;
333 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700334 provider.roleChanged(deviceId, newRole);
335 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700336
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700337 return true;
338 }
339
tomd3097b02014-08-26 10:40:29 -0700340 @Override
alshabibb7b40632014-09-28 21:30:00 -0700341 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700342 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700343 checkNotNull(deviceId, DEVICE_ID_NULL);
344 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700345 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700346
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700347 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
348 if (!isAllowed(cfg)) {
349 log.warn("Device {} is not allowed", deviceId);
350 return;
351 }
352 // Generate updated description and establish my Role
353 deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription);
Madan Jampani565a66a2015-07-25 17:01:13 -0700354 Futures.getUnchecked(mastershipService.requestRoleFor(deviceId)
355 .thenAccept(role -> {
356 log.info("Local role is {} for {}", role, deviceId);
357 applyRole(deviceId, role);
358 }));
HIGUCHI Yuta11530fb2015-05-27 13:10:20 -0700359
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700360 DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId,
361 deviceDescription);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700362 log.info("Device {} connected", deviceId);
tom80c0e5e2014-09-08 18:08:58 -0700363 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700364 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700365 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700366 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700367
tomd3097b02014-08-26 10:40:29 -0700368 }
369
370 @Override
371 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700372 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700373 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700374
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700375 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700376
alshabibafc514a2014-12-01 14:44:05 -0800377 List<Port> ports = store.getPorts(deviceId);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300378 final Device device = getDevice(deviceId);
379
380 List<PortDescription> descs = ports.stream().map(
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200381 port -> (!(Device.Type.ROADM.equals(device.type()) ||
Aneesha Paillad5a8f012016-05-04 11:45:17 -0700382 (Device.Type.OTN.equals(device.type())) ||
383 (Device.Type.FIBER_SWITCH.equals(device.type())))) ?
Yafit Hadara9a73de2015-09-06 13:52:52 +0300384 new DefaultPortDescription(port.number(), false,
385 port.type(), port.portSpeed()) :
386 OpticalPortOperator.descriptionOf(port, false)
387 ).collect(Collectors.toList());
388
alshabibafc514a2014-12-01 14:44:05 -0800389 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700390 try {
Madan Jampani565a66a2015-07-25 17:01:13 -0700391 if (mastershipService.isLocalMaster(deviceId)) {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700392 post(store.markOffline(deviceId));
393 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700394 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700395 log.warn("Failed to mark {} offline", deviceId);
396 // only the MASTER should be marking off-line in normal cases,
samuele1fa7322015-07-14 16:35:16 +0800397 // but if I was the last STANDBY connection, etc. and no one else
398 // was there to mark the device offline, this instance may need to
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700399 // temporarily request for Master Role and mark offline.
400
samuele1fa7322015-07-14 16:35:16 +0800401 //there are times when this node will correctly have mastership, BUT
402 //that isn't reflected in the ClockManager before the device disconnects.
403 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700404
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700405 // FIXME: Store semantics leaking out as IllegalStateException.
samuele1fa7322015-07-14 16:35:16 +0800406 // Consider revising store API to handle this scenario.
407 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700408 roleFuture.whenComplete((role, error) -> {
samuele1fa7322015-07-14 16:35:16 +0800409 MastershipTerm term = termService.getMastershipTerm(deviceId);
410 // TODO: Move this type of check inside device clock manager, etc.
411 if (term != null && localNodeId.equals(term.master())) {
412 log.info("Retry marking {} offline", deviceId);
samuele1fa7322015-07-14 16:35:16 +0800413 post(store.markOffline(deviceId));
414 } else {
415 log.info("Failed again marking {} offline. {}", deviceId, role);
416 }
417 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700418 } finally {
Madan Jampanic6e574f2015-05-29 13:41:52 -0700419 try {
samuele1fa7322015-07-14 16:35:16 +0800420 //relinquish master role and ability to be backup.
Madan Jampanic6e574f2015-05-29 13:41:52 -0700421 mastershipService.relinquishMastership(deviceId).get();
422 } catch (InterruptedException e) {
samuele1fa7322015-07-14 16:35:16 +0800423 log.warn("Interrupted while reliquishing role for {}", deviceId);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700424 Thread.currentThread().interrupt();
425 } catch (ExecutionException e) {
samuele1fa7322015-07-14 16:35:16 +0800426 log.error("Exception thrown while relinquishing role for {}", deviceId, e);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700427 }
tom0efbb1d2014-09-09 11:54:28 -0700428 }
tomd3097b02014-08-26 10:40:29 -0700429 }
430
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -0700431 /**
432 * Transforms optical specific PortDescription to generic PortDescription.
433 *
434 * @param descr PortDescription
435 * @return generic PortDescription
436 * @deprecated in Goldeneye (1.6.0)
437 */
438 @Deprecated
439 private PortDescription ensureGeneric(PortDescription descr) {
440 switch (descr.type()) {
441 case OCH:
442 if (descr instanceof OchPortDescription) {
443 OchPortDescription och = (OchPortDescription) descr;
444 return ochPortDescription(och,
445 och.signalType(),
446 och.isTunable(),
447 och.lambda(),
448 och.annotations());
449 }
450 break;
451 case ODUCLT:
452 if (descr instanceof OduCltPortDescription) {
453 OduCltPortDescription clt = (OduCltPortDescription) descr;
454 return oduCltPortDescription(clt,
455 clt.signalType(),
456 clt.annotations());
457 }
458 break;
459 case OMS:
460 if (descr instanceof OmsPortDescription) {
461 OmsPortDescription oms = (OmsPortDescription) descr;
462 return OmsPortHelper.omsPortDescription(oms,
463 oms.minFrequency(),
464 oms.maxFrequency(),
465 oms.grid(),
466 oms.annotations());
467 }
468 break;
469 case OTU:
470 if (descr instanceof OtuPortDescription) {
471 OtuPortDescription otu = (OtuPortDescription) descr;
472 return OtuPortHelper.otuPortDescription(otu,
473 otu.signalType(),
474 otu.annotations());
475 }
476 break;
477
478 default:
479 // no-op
480 break;
481 }
482 return descr;
483 }
484
tomd3097b02014-08-26 10:40:29 -0700485 @Override
alshabibb7b40632014-09-28 21:30:00 -0700486 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700487 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700488 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700489 checkNotNull(portDescriptions, PORT_DESC_LIST_NULL);
tomeadbb462014-09-07 16:10:19 -0700490 checkValidity();
Madan Jampani565a66a2015-07-25 17:01:13 -0700491 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700492 // Never been a master for this device
493 // any update will be ignored.
samuele1fa7322015-07-14 16:35:16 +0800494 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700495 return;
496 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700497 portDescriptions = portDescriptions.stream()
498 .map(e -> consolidate(deviceId, e))
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -0700499 .map(this::ensureGeneric)
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700500 .collect(Collectors.toList());
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700501 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
samuele1fa7322015-07-14 16:35:16 +0800502 deviceId, portDescriptions);
Thomas Vachuska5923b9a2016-01-26 10:52:57 -0800503 if (events != null) {
504 for (DeviceEvent event : events) {
505 post(event);
506 }
tom32f66842014-08-27 19:27:47 -0700507 }
tomd3097b02014-08-26 10:40:29 -0700508 }
509
510 @Override
alshabibb7b40632014-09-28 21:30:00 -0700511 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700512 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700513 checkNotNull(deviceId, DEVICE_ID_NULL);
514 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700515 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700516
Madan Jampani565a66a2015-07-25 17:01:13 -0700517 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700518 // Never been a master for this device
519 // any update will be ignored.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700520 log.trace("Ignoring {} port update on standby node. {}", deviceId,
521 portDescription);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700522 return;
523 }
Thomas Vachuskafa8aa2f2015-10-13 11:56:59 -0700524 Device device = nullIsNotFound(getDevice(deviceId), "Device not found");
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200525 if ((Device.Type.ROADM.equals(device.type())) ||
526 (Device.Type.OTN.equals(device.type()))) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300527 Port port = getPort(deviceId, portDescription.portNumber());
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -0700528 // FIXME This is ignoring all other info in portDescription given as input??
Yafit Hadara9a73de2015-09-06 13:52:52 +0300529 portDescription = OpticalPortOperator.descriptionOf(port, portDescription.isEnabled());
530 }
531
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700532 portDescription = consolidate(deviceId, portDescription);
samuele1fa7322015-07-14 16:35:16 +0800533 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -0700534 deviceId,
535 ensureGeneric(portDescription));
tomff7eb7c2014-09-08 12:49:03 -0700536 if (event != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700537 log.info("Device {} port {} status changed", deviceId, event.port().number());
tom0efbb1d2014-09-09 11:54:28 -0700538 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700539 }
tomd3097b02014-08-26 10:40:29 -0700540 }
tom3f2bbd72014-09-24 12:07:58 -0700541
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700542 // merges the appropriate PortConfig with the description.
543 private PortDescription consolidate(DeviceId did, PortDescription desc) {
544 switch (desc.type()) {
545 case COPPER:
546 case VIRTUAL:
547 return desc;
548 default:
549 OpticalPortConfig opc = networkConfigService.getConfig(
550 new ConnectPoint(did, desc.portNumber()), OpticalPortConfig.class);
551 return OpticalPortOperator.combine(opc, desc);
552 }
553 }
554
tom3f2bbd72014-09-24 12:07:58 -0700555 @Override
samuele1fa7322015-07-14 16:35:16 +0800556 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700557 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700558 // Several things can happen here:
559 // 1. request and response match
560 // 2. request and response don't match
561 // 3. MastershipRole and requested match (and 1 or 2 are true)
562 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
563 //
564 // 2, 4, and 3 with case 2 are failure modes.
565
tom3f2bbd72014-09-24 12:07:58 -0700566 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700567
Madan Jampanif2af7712015-05-29 18:43:52 -0700568 log.debug("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700569 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700570
571 if (requested == null && response == null) {
samuele1fa7322015-07-14 16:35:16 +0800572 // something was off with DeviceProvider, maybe check channel too?
Shashikanth VH387a1ca2016-02-09 20:35:21 +0530573 log.warn("Failed to assert role onto Device {}", deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700574 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700575 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700576 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700577
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700578 if (Objects.equals(requested, response)) {
samuele1fa7322015-07-14 16:35:16 +0800579 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700580 return;
581 } else {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800582 log.warn("Role mismatch on {}. set to {}, but store demands {}",
583 deviceId, response, mastershipService.getLocalRole(deviceId));
584 // roleManager got the device to comply, but doesn't agree with
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700585 // the store; use the store's view, then try to reassert.
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800586 backgroundService.execute(() -> reassertRole(deviceId, mastershipService.getLocalRole(deviceId)));
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800587 return;
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700588 }
589 } else {
590 // we didn't get back what we asked for. Reelect someone else.
samuele1fa7322015-07-14 16:35:16 +0800591 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700592 if (response == MastershipRole.MASTER) {
593 mastershipService.relinquishMastership(deviceId);
594 // TODO: Shouldn't we be triggering event?
samuele1fa7322015-07-14 16:35:16 +0800595 //final Device device = getDevice(deviceId);
596 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700597 }
598 }
tom3f2bbd72014-09-24 12:07:58 -0700599 }
sangho538108b2015-04-08 14:29:20 -0700600
601 @Override
samuele1fa7322015-07-14 16:35:16 +0800602 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
sangho538108b2015-04-08 14:29:20 -0700603 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700604 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700605 checkValidity();
606
samuele1fa7322015-07-14 16:35:16 +0800607 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
608 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700609 post(event);
610 }
tomd3097b02014-08-26 10:40:29 -0700611 }
tom32f66842014-08-27 19:27:47 -0700612
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700613 // by default allowed, otherwise check flag
614 private boolean isAllowed(BasicDeviceConfig cfg) {
615 return (cfg == null || cfg.isAllowed());
616 }
617
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800618 // Applies the specified role to the device; ignores NONE
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700619
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800620 /**
621 * Apply role to device and send probe if MASTER.
622 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700623 * @param deviceId device identifier
624 * @param newRole new role to apply to the device
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800625 * @return true if the request was sent to provider
626 */
627 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
628 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800629 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700630 return true;
631 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700632
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800633 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800634 if (provider == null) {
samuele1fa7322015-07-14 16:35:16 +0800635 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800636 return false;
637 }
638 provider.roleChanged(deviceId, newRole);
639
640 if (newRole.equals(MastershipRole.MASTER)) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800641 log.debug("sent TriggerProbe({})", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800642 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800643 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800644 }
645 return true;
646 }
647
648 /**
649 * Reaasert role for specified device connected to this node.
650 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700651 * @param did device identifier
652 * @param nextRole role to apply. If NONE is specified,
653 * it will ask mastership service for a role and apply it.
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800654 */
samuele1fa7322015-07-14 16:35:16 +0800655 private void reassertRole(final DeviceId did,
656 final MastershipRole nextRole) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800657
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800658 MastershipRole myNextRole = nextRole;
659 if (myNextRole == NONE) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800660 try {
661 mastershipService.requestRoleFor(did).get();
662 MastershipTerm term = termService.getMastershipTerm(did);
663 if (term != null && localNodeId.equals(term.master())) {
664 myNextRole = MASTER;
665 } else {
666 myNextRole = STANDBY;
667 }
668 } catch (InterruptedException e) {
669 Thread.currentThread().interrupt();
670 log.error("Interrupted waiting for Mastership", e);
671 } catch (ExecutionException e) {
672 log.error("Encountered an error waiting for Mastership", e);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800673 }
674 }
675
676 switch (myNextRole) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700677 case MASTER:
678 final Device device = getDevice(did);
679 if ((device != null) && !isAvailable(did)) {
680 //flag the device as online. Is there a better way to do this?
681 DefaultDeviceDescription deviceDescription
682 = new DefaultDeviceDescription(did.uri(),
683 device.type(),
684 device.manufacturer(),
685 device.hwVersion(),
686 device.swVersion(),
687 device.serialNumber(),
688 device.chassisId());
689 DeviceEvent devEvent =
690 store.createOrUpdateDevice(device.providerId(), did,
691 deviceDescription);
692 post(devEvent);
693 }
694 // TODO: should apply role only if there is mismatch
695 log.debug("Applying role {} to {}", myNextRole, did);
696 if (!applyRoleAndProbe(did, MASTER)) {
697 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
698 // immediately failed to apply role
699 mastershipService.relinquishMastership(did);
700 // FIXME disconnect?
701 }
702 break;
703 case STANDBY:
704 log.debug("Applying role {} to {}", myNextRole, did);
705 if (!applyRoleAndProbe(did, STANDBY)) {
706 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
707 // immediately failed to apply role
708 mastershipService.relinquishMastership(did);
709 // FIXME disconnect?
710 }
711 break;
712 case NONE:
713 default:
714 // should never reach here
715 log.error("You didn't see anything. I did not exist.");
716 break;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800717 }
718 }
719
Madan Jampani328371d2015-05-29 14:06:27 -0700720 private void handleMastershipEvent(MastershipEvent event) {
721 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
722 // Don't care if backup list changed.
723 return;
724 }
725
726 final DeviceId did = event.subject();
727
728 // myRole suggested by MastershipService
729 MastershipRole myNextRole;
730 if (localNodeId.equals(event.roleInfo().master())) {
731 // confirm latest info
732 MastershipTerm term = termService.getMastershipTerm(did);
samuele1fa7322015-07-14 16:35:16 +0800733 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Madan Jampani328371d2015-05-29 14:06:27 -0700734 if (iHaveControl) {
Madan Jampani328371d2015-05-29 14:06:27 -0700735 myNextRole = MASTER;
736 } else {
737 myNextRole = STANDBY;
738 }
739 } else if (event.roleInfo().backups().contains(localNodeId)) {
740 myNextRole = STANDBY;
741 } else {
742 myNextRole = NONE;
743 }
744
Madan Jampani328371d2015-05-29 14:06:27 -0700745 final boolean isReachable = isReachable(did);
746 if (!isReachable) {
747 // device is not connected to this node
748 if (myNextRole != NONE) {
749 log.warn("Node was instructed to be {} role for {}, "
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700750 + "but this node cannot reach the device. "
751 + "Relinquishing role. ",
samuele1fa7322015-07-14 16:35:16 +0800752 myNextRole, did);
Madan Jampani328371d2015-05-29 14:06:27 -0700753 mastershipService.relinquishMastership(did);
754 }
755 return;
756 }
757
758 // device is connected to this node:
759 if (store.getDevice(did) != null) {
760 reassertRole(did, myNextRole);
761 } else {
762 log.debug("Device is not yet/no longer in the store: {}", did);
763 }
764 }
765
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800766 // Intercepts mastership events
767 private class InternalMastershipListener implements MastershipListener {
768
tomb41d1ac2014-09-24 01:51:24 -0700769 @Override
770 public void event(MastershipEvent event) {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800771 backgroundService.execute(() -> {
Madan Jampani328371d2015-05-29 14:06:27 -0700772 try {
773 handleMastershipEvent(event);
774 } catch (Exception e) {
775 log.warn("Failed to handle {}", event, e);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700776 }
Madan Jampani328371d2015-05-29 14:06:27 -0700777 });
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700778 }
tomb41d1ac2014-09-24 01:51:24 -0700779 }
tomf80c9722014-09-24 14:49:18 -0700780
781 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700782 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700783 @Override
784 public void notify(DeviceEvent event) {
785 post(event);
786 }
787 }
samuel738dfaf2015-07-11 11:08:57 +0800788
789 @Override
790 public Iterable<Device> getDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900791 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800792 Set<Device> results = new HashSet<>();
793 Iterable<Device> devices = store.getDevices();
794 if (devices != null) {
795 devices.forEach(d -> {
796 if (type.equals(d.type())) {
797 results.add(d);
798 }
799 });
800 }
801 return results;
802 }
803
804 @Override
805 public Iterable<Device> getAvailableDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900806 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800807 Set<Device> results = new HashSet<>();
808 Iterable<Device> availableDevices = store.getAvailableDevices();
809 if (availableDevices != null) {
810 availableDevices.forEach(d -> {
811 if (type.equals(d.type())) {
812 results.add(d);
813 }
814 });
815 }
816 return results;
817 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700818
819 private class InternalNetworkConfigListener implements NetworkConfigListener {
820 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700821 public boolean isRelevant(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700822 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
823 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
824 && (event.configClass().equals(BasicDeviceConfig.class)
825 || event.configClass().equals(OpticalPortConfig.class));
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700826 }
827
828 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700829 public void event(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700830 DeviceEvent de = null;
831 if (event.configClass().equals(BasicDeviceConfig.class)) {
Thomas Vachuska138de8b2016-01-11 21:31:38 -0800832 log.debug("Detected device network config event {}", event.type());
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700833 DeviceId did = (DeviceId) event.subject();
834 BasicDeviceConfig cfg = networkConfigService.getConfig(did, BasicDeviceConfig.class);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700835
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700836 if (!isAllowed(cfg)) {
837 kickOutBadDevice(did);
838 } else {
839 Device dev = getDevice(did);
840 DeviceDescription desc = (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev);
841 desc = BasicDeviceOperator.combine(cfg, desc);
Thomas Vachuska1627dc82015-11-13 12:22:14 -0800842 if (desc != null && getProvider(did) != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700843 de = store.createOrUpdateDevice(getProvider(did).id(), did, desc);
844 }
845 }
846 }
847 if (event.configClass().equals(OpticalPortConfig.class)) {
848 ConnectPoint cpt = (ConnectPoint) event.subject();
849 DeviceId did = cpt.deviceId();
850 Port dpt = getPort(did, cpt.port());
851
852 if (dpt != null) {
853 OpticalPortConfig opc = networkConfigService.getConfig(cpt, OpticalPortConfig.class);
854 PortDescription desc = OpticalPortOperator.descriptionOf(dpt);
855 desc = OpticalPortOperator.combine(opc, desc);
Thomas Vachuska1627dc82015-11-13 12:22:14 -0800856 if (desc != null && getProvider(did) != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700857 de = store.updatePortStatus(getProvider(did).id(), did, desc);
858 }
859 }
860 }
861
862 if (de != null) {
863 post(de);
864 }
865 }
866
867 // checks if the specified device is allowed by the BasicDeviceConfig
868 // and if not, removes it
869 private void kickOutBadDevice(DeviceId deviceId) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700870 Device badDevice = getDevice(deviceId);
871 if (badDevice != null) {
872 removeDevice(deviceId);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700873 }
874 }
875 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700876
tomd3097b02014-08-26 10:40:29 -0700877}