blob: 1931e8c00134a8606e0390f5c6bfa81abc8e084e [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;
25import static org.onosproject.security.AppGuard.checkPermission;
26import static org.onosproject.security.AppPermission.Type.DEVICE_READ;
27import static org.slf4j.LoggerFactory.getLogger;
28
29import java.util.Collection;
30import java.util.HashSet;
31import java.util.List;
32import java.util.Objects;
33import java.util.Set;
34import java.util.concurrent.CompletableFuture;
35import java.util.concurrent.ExecutionException;
36import java.util.concurrent.ScheduledExecutorService;
37import java.util.concurrent.TimeUnit;
38import java.util.stream.Collectors;
Madan Jampani565a66a2015-07-25 17:01:13 -070039
tomd3097b02014-08-26 10:40:29 -070040import org.apache.felix.scr.annotations.Activate;
41import org.apache.felix.scr.annotations.Component;
42import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070043import org.apache.felix.scr.annotations.Reference;
44import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070045import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.cluster.ClusterService;
47import org.onosproject.cluster.NodeId;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.mastership.MastershipEvent;
49import org.onosproject.mastership.MastershipListener;
50import org.onosproject.mastership.MastershipService;
51import org.onosproject.mastership.MastershipTerm;
52import org.onosproject.mastership.MastershipTermService;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070053import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.Device;
samuel738dfaf2015-07-11 11:08:57 +080055import org.onosproject.net.Device.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080056import org.onosproject.net.DeviceId;
57import org.onosproject.net.MastershipRole;
58import org.onosproject.net.Port;
59import org.onosproject.net.PortNumber;
Yafit Hadara9a73de2015-09-06 13:52:52 +030060import org.onosproject.net.config.NetworkConfigEvent;
61import org.onosproject.net.config.NetworkConfigListener;
62import org.onosproject.net.config.NetworkConfigService;
63import org.onosproject.net.config.basics.BasicDeviceConfig;
64import org.onosproject.net.config.basics.OpticalPortConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080065import org.onosproject.net.device.DefaultDeviceDescription;
66import org.onosproject.net.device.DefaultPortDescription;
67import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080068import org.onosproject.net.device.DeviceDescription;
69import org.onosproject.net.device.DeviceEvent;
70import org.onosproject.net.device.DeviceListener;
71import org.onosproject.net.device.DeviceProvider;
72import org.onosproject.net.device.DeviceProviderRegistry;
73import org.onosproject.net.device.DeviceProviderService;
74import org.onosproject.net.device.DeviceService;
75import org.onosproject.net.device.DeviceStore;
76import org.onosproject.net.device.DeviceStoreDelegate;
77import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070078import org.onosproject.net.device.PortStatistics;
Yafit Hadara9a73de2015-09-06 13:52:52 +030079import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080080import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070081import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070082
Yafit Hadara9a73de2015-09-06 13:52:52 +030083import com.google.common.util.concurrent.Futures;
Jonathan Hart2f669362015-02-11 16:19:20 -080084
tomd3097b02014-08-26 10:40:29 -070085/**
tome4729872014-09-23 00:37:37 -070086 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070087 */
88@Component(immediate = true)
89@Service
tom41a2c5f2014-09-19 09:20:35 -070090public class DeviceManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070091 extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService>
Thomas Vachuskad16ce182014-10-29 17:25:29 -070092 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070093
tome5ec3fd2014-09-04 15:18:06 -070094 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
95 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
96 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
97 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070098 private static final String PORT_DESC_LIST_NULL = "Port description list cannot be null";
tomd3097b02014-08-26 10:40:29 -070099
tom5f38b3a2014-08-27 23:50:54 -0700100 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -0700101
alshabib339a3d92014-09-26 17:54:32 -0700102 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -0700103
tomc78acee2014-09-24 15:16:55 -0700104 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -0700105 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -0700106
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800107 private ScheduledExecutorService backgroundService;
108
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700109 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
110
tom41a2c5f2014-09-19 09:20:35 -0700111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -0700113
tom5f38b3a2014-08-27 23:50:54 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700115 protected ClusterService clusterService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700118 protected MastershipService mastershipService;
119
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700121 protected MastershipTermService termService;
122
Madan Jampani61056bc2014-09-27 09:07:26 -0700123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700124 protected NetworkConfigService networkConfigService;
125
tomd3097b02014-08-26 10:40:29 -0700126 @Activate
127 public void activate() {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800128 backgroundService = newSingleThreadScheduledExecutor(
129 groupedThreads("onos/device", "manager-background", log));
Madan Jampanide003d92015-05-11 17:14:20 -0700130 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800131
tomf80c9722014-09-24 14:49:18 -0700132 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700133 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700134 mastershipService.addListener(mastershipListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700135 networkConfigService.addListener(networkConfigListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800136
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700137 backgroundService.scheduleWithFixedDelay(() -> {
138 try {
139 mastershipCheck();
140 } catch (Exception e) {
141 log.error("Exception thrown during integrity check", e);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800142 }
143 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700144 log.info("Started");
145 }
146
147 @Deactivate
148 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800149 backgroundService.shutdown();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700150 networkConfigService.removeListener(networkConfigListener);
tomf80c9722014-09-24 14:49:18 -0700151 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700152 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700153 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700154 log.info("Stopped");
155 }
156
157 @Override
tomad2d2092014-09-06 23:24:20 -0700158 public int getDeviceCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900159 checkPermission(DEVICE_READ);
tomad2d2092014-09-06 23:24:20 -0700160 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700161 }
162
163 @Override
tom32f66842014-08-27 19:27:47 -0700164 public Iterable<Device> getDevices() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900165 checkPermission(DEVICE_READ);
tome5ec3fd2014-09-04 15:18:06 -0700166 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700167 }
168
tom32f66842014-08-27 19:27:47 -0700169 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800170 public Iterable<Device> getAvailableDevices() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900171 checkPermission(DEVICE_READ);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800172 return store.getAvailableDevices();
173 }
174
175 @Override
tom32f66842014-08-27 19:27:47 -0700176 public Device getDevice(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900177 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700178 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700179 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700180 }
181
182 @Override
tomad2d2092014-09-06 23:24:20 -0700183 public MastershipRole getRole(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900184 checkPermission(DEVICE_READ);
tomad2d2092014-09-06 23:24:20 -0700185 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700186 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700187 }
188
189 @Override
tom32f66842014-08-27 19:27:47 -0700190 public List<Port> getPorts(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900191 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700192 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700193 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700194 }
195
196 @Override
sangho538108b2015-04-08 14:29:20 -0700197 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900198 checkPermission(DEVICE_READ);
sangho538108b2015-04-08 14:29:20 -0700199 checkNotNull(deviceId, DEVICE_ID_NULL);
200 return store.getPortStatistics(deviceId);
201 }
202
203 @Override
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200204 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900205 checkPermission(DEVICE_READ);
Dusan Pajin11ff4a82015-08-20 18:03:05 +0200206 checkNotNull(deviceId, DEVICE_ID_NULL);
207 return store.getPortDeltaStatistics(deviceId);
208 }
209
210 @Override
tom32f66842014-08-27 19:27:47 -0700211 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900212 checkPermission(DEVICE_READ);
tom32f66842014-08-27 19:27:47 -0700213 checkNotNull(deviceId, DEVICE_ID_NULL);
214 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700215 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700216 }
217
218 @Override
tomff7eb7c2014-09-08 12:49:03 -0700219 public boolean isAvailable(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900220 checkPermission(DEVICE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900221
tomff7eb7c2014-09-08 12:49:03 -0700222 checkNotNull(deviceId, DEVICE_ID_NULL);
223 return store.isAvailable(deviceId);
224 }
225
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700226 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700227 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800228 if (deviceId == null) {
229 return false;
230 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700231 DeviceProvider provider = getProvider(deviceId);
232 if (provider != null) {
233 return provider.isReachable(deviceId);
234 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800235 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700236 return false;
237 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700238 }
239
tome5ec3fd2014-09-04 15:18:06 -0700240 @Override
241 public void removeDevice(DeviceId deviceId) {
242 checkNotNull(deviceId, DEVICE_ID_NULL);
243 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700244 if (event != null) {
245 log.info("Device {} administratively removed", deviceId);
246 post(event);
247 }
tome5ec3fd2014-09-04 15:18:06 -0700248 }
249
tom7869ad92014-09-09 14:32:08 -0700250 @Override
Saurav Dasa2d37502016-03-25 17:50:40 -0700251 public void changePortState(DeviceId deviceId, PortNumber portNumber,
252 boolean enable) {
253 checkNotNull(deviceId, DEVICE_ID_NULL);
254 checkNotNull(deviceId, PORT_NUMBER_NULL);
255 DeviceProvider provider = getProvider(deviceId);
256 if (provider != null) {
257 log.warn("Port {} on device {} being administratively brought {}",
258 portNumber, deviceId,
259 (enable) ? "UP" : "DOWN");
260 provider.changePortState(deviceId, portNumber, enable);
261 } else {
262 log.warn("Provider not found for {}", deviceId);
263 }
264 }
265
266 @Override
samuele1fa7322015-07-14 16:35:16 +0800267 protected DeviceProviderService createProviderService(
268 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700269 return new InternalDeviceProviderService(provider);
270 }
271
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800272 /**
273 * Checks if all the reachable devices have a valid mastership role.
274 */
275 private void mastershipCheck() {
276 log.debug("Checking mastership");
277 for (Device device : getDevices()) {
278 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800279 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800280
281 if (!isReachable(deviceId)) {
282 continue;
283 }
284
285 if (mastershipService.getLocalRole(deviceId) != NONE) {
286 continue;
287 }
288
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700289 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800290
291 // isReachable but was not MASTER or STANDBY, get a role and apply
292 // Note: NONE triggers request to MastershipService
293 reassertRole(deviceId, NONE);
294 }
295 }
296
tomd3097b02014-08-26 10:40:29 -0700297 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700298 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700299 extends AbstractProviderService<DeviceProvider>
300 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700301
tomcfde0622014-09-09 11:02:42 -0700302 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700303 super(provider);
304 }
305
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700306 /**
307 * Apply role in reaction to provider event.
308 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700309 * @param deviceId device identifier
310 * @param newRole new role to apply to the device
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700311 * @return true if the request was sent to provider
312 */
313 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
314
315 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800316 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700317 return true;
318 }
319
320 DeviceProvider provider = provider();
321 if (provider == null) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700322 log.warn("Provider for {} was not found. Cannot apply role {}",
323 deviceId, newRole);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700324 return false;
325 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700326 provider.roleChanged(deviceId, newRole);
327 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700328
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700329 return true;
330 }
331
tomd3097b02014-08-26 10:40:29 -0700332 @Override
alshabibb7b40632014-09-28 21:30:00 -0700333 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700334 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700335 checkNotNull(deviceId, DEVICE_ID_NULL);
336 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700337 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700338
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700339 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
340 if (!isAllowed(cfg)) {
341 log.warn("Device {} is not allowed", deviceId);
342 return;
343 }
344 // Generate updated description and establish my Role
345 deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription);
Madan Jampani565a66a2015-07-25 17:01:13 -0700346 Futures.getUnchecked(mastershipService.requestRoleFor(deviceId)
347 .thenAccept(role -> {
348 log.info("Local role is {} for {}", role, deviceId);
349 applyRole(deviceId, role);
350 }));
HIGUCHI Yuta11530fb2015-05-27 13:10:20 -0700351
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700352 DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId,
353 deviceDescription);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700354 log.info("Device {} connected", deviceId);
tom80c0e5e2014-09-08 18:08:58 -0700355 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700356 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700357 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700358 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700359
tomd3097b02014-08-26 10:40:29 -0700360 }
361
362 @Override
363 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700364 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700365 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700366
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700367 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700368
alshabibafc514a2014-12-01 14:44:05 -0800369 List<Port> ports = store.getPorts(deviceId);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300370 final Device device = getDevice(deviceId);
371
372 List<PortDescription> descs = ports.stream().map(
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200373 port -> (!(Device.Type.ROADM.equals(device.type()) ||
Aneesha Paillad5a8f012016-05-04 11:45:17 -0700374 (Device.Type.OTN.equals(device.type())) ||
375 (Device.Type.FIBER_SWITCH.equals(device.type())))) ?
Yafit Hadara9a73de2015-09-06 13:52:52 +0300376 new DefaultPortDescription(port.number(), false,
377 port.type(), port.portSpeed()) :
378 OpticalPortOperator.descriptionOf(port, false)
379 ).collect(Collectors.toList());
380
alshabibafc514a2014-12-01 14:44:05 -0800381 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700382 try {
Madan Jampani565a66a2015-07-25 17:01:13 -0700383 if (mastershipService.isLocalMaster(deviceId)) {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700384 post(store.markOffline(deviceId));
385 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700386 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700387 log.warn("Failed to mark {} offline", deviceId);
388 // only the MASTER should be marking off-line in normal cases,
samuele1fa7322015-07-14 16:35:16 +0800389 // but if I was the last STANDBY connection, etc. and no one else
390 // was there to mark the device offline, this instance may need to
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700391 // temporarily request for Master Role and mark offline.
392
samuele1fa7322015-07-14 16:35:16 +0800393 //there are times when this node will correctly have mastership, BUT
394 //that isn't reflected in the ClockManager before the device disconnects.
395 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700396
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700397 // FIXME: Store semantics leaking out as IllegalStateException.
samuele1fa7322015-07-14 16:35:16 +0800398 // Consider revising store API to handle this scenario.
399 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700400 roleFuture.whenComplete((role, error) -> {
samuele1fa7322015-07-14 16:35:16 +0800401 MastershipTerm term = termService.getMastershipTerm(deviceId);
402 // TODO: Move this type of check inside device clock manager, etc.
403 if (term != null && localNodeId.equals(term.master())) {
404 log.info("Retry marking {} offline", deviceId);
samuele1fa7322015-07-14 16:35:16 +0800405 post(store.markOffline(deviceId));
406 } else {
407 log.info("Failed again marking {} offline. {}", deviceId, role);
408 }
409 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700410 } finally {
Madan Jampanic6e574f2015-05-29 13:41:52 -0700411 try {
samuele1fa7322015-07-14 16:35:16 +0800412 //relinquish master role and ability to be backup.
Madan Jampanic6e574f2015-05-29 13:41:52 -0700413 mastershipService.relinquishMastership(deviceId).get();
414 } catch (InterruptedException e) {
samuele1fa7322015-07-14 16:35:16 +0800415 log.warn("Interrupted while reliquishing role for {}", deviceId);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700416 Thread.currentThread().interrupt();
417 } catch (ExecutionException e) {
samuele1fa7322015-07-14 16:35:16 +0800418 log.error("Exception thrown while relinquishing role for {}", deviceId, e);
Madan Jampanic6e574f2015-05-29 13:41:52 -0700419 }
tom0efbb1d2014-09-09 11:54:28 -0700420 }
tomd3097b02014-08-26 10:40:29 -0700421 }
422
423 @Override
alshabibb7b40632014-09-28 21:30:00 -0700424 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700425 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700426 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700427 checkNotNull(portDescriptions, PORT_DESC_LIST_NULL);
tomeadbb462014-09-07 16:10:19 -0700428 checkValidity();
Madan Jampani565a66a2015-07-25 17:01:13 -0700429 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700430 // Never been a master for this device
431 // any update will be ignored.
samuele1fa7322015-07-14 16:35:16 +0800432 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700433 return;
434 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700435 portDescriptions = portDescriptions.stream()
436 .map(e -> consolidate(deviceId, e))
437 .collect(Collectors.toList());
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700438 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
samuele1fa7322015-07-14 16:35:16 +0800439 deviceId, portDescriptions);
Thomas Vachuska5923b9a2016-01-26 10:52:57 -0800440 if (events != null) {
441 for (DeviceEvent event : events) {
442 post(event);
443 }
tom32f66842014-08-27 19:27:47 -0700444 }
tomd3097b02014-08-26 10:40:29 -0700445 }
446
447 @Override
alshabibb7b40632014-09-28 21:30:00 -0700448 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700449 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700450 checkNotNull(deviceId, DEVICE_ID_NULL);
451 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700452 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700453
Madan Jampani565a66a2015-07-25 17:01:13 -0700454 if (!mastershipService.isLocalMaster(deviceId)) {
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700455 // Never been a master for this device
456 // any update will be ignored.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700457 log.trace("Ignoring {} port update on standby node. {}", deviceId,
458 portDescription);
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700459 return;
460 }
Thomas Vachuskafa8aa2f2015-10-13 11:56:59 -0700461 Device device = nullIsNotFound(getDevice(deviceId), "Device not found");
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200462 if ((Device.Type.ROADM.equals(device.type())) ||
463 (Device.Type.OTN.equals(device.type()))) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300464 Port port = getPort(deviceId, portDescription.portNumber());
465 portDescription = OpticalPortOperator.descriptionOf(port, portDescription.isEnabled());
466 }
467
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700468 portDescription = consolidate(deviceId, portDescription);
samuele1fa7322015-07-14 16:35:16 +0800469 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
470 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700471 if (event != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700472 log.info("Device {} port {} status changed", deviceId, event.port().number());
tom0efbb1d2014-09-09 11:54:28 -0700473 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700474 }
tomd3097b02014-08-26 10:40:29 -0700475 }
tom3f2bbd72014-09-24 12:07:58 -0700476
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700477 // merges the appropriate PortConfig with the description.
478 private PortDescription consolidate(DeviceId did, PortDescription desc) {
479 switch (desc.type()) {
480 case COPPER:
481 case VIRTUAL:
482 return desc;
483 default:
484 OpticalPortConfig opc = networkConfigService.getConfig(
485 new ConnectPoint(did, desc.portNumber()), OpticalPortConfig.class);
486 return OpticalPortOperator.combine(opc, desc);
487 }
488 }
489
tom3f2bbd72014-09-24 12:07:58 -0700490 @Override
samuele1fa7322015-07-14 16:35:16 +0800491 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700492 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700493 // Several things can happen here:
494 // 1. request and response match
495 // 2. request and response don't match
496 // 3. MastershipRole and requested match (and 1 or 2 are true)
497 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
498 //
499 // 2, 4, and 3 with case 2 are failure modes.
500
tom3f2bbd72014-09-24 12:07:58 -0700501 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700502
Madan Jampanif2af7712015-05-29 18:43:52 -0700503 log.debug("got reply to a role request for {}: asked for {}, and got {}",
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700504 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700505
506 if (requested == null && response == null) {
samuele1fa7322015-07-14 16:35:16 +0800507 // something was off with DeviceProvider, maybe check channel too?
Shashikanth VH387a1ca2016-02-09 20:35:21 +0530508 log.warn("Failed to assert role onto Device {}", deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700509 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700510 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700511 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700512
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700513 if (Objects.equals(requested, response)) {
samuele1fa7322015-07-14 16:35:16 +0800514 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700515 return;
516 } else {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800517 log.warn("Role mismatch on {}. set to {}, but store demands {}",
518 deviceId, response, mastershipService.getLocalRole(deviceId));
519 // roleManager got the device to comply, but doesn't agree with
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700520 // the store; use the store's view, then try to reassert.
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800521 backgroundService.execute(() -> reassertRole(deviceId, mastershipService.getLocalRole(deviceId)));
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800522 return;
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700523 }
524 } else {
525 // we didn't get back what we asked for. Reelect someone else.
samuele1fa7322015-07-14 16:35:16 +0800526 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700527 if (response == MastershipRole.MASTER) {
528 mastershipService.relinquishMastership(deviceId);
529 // TODO: Shouldn't we be triggering event?
samuele1fa7322015-07-14 16:35:16 +0800530 //final Device device = getDevice(deviceId);
531 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700532 }
533 }
tom3f2bbd72014-09-24 12:07:58 -0700534 }
sangho538108b2015-04-08 14:29:20 -0700535
536 @Override
samuele1fa7322015-07-14 16:35:16 +0800537 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
sangho538108b2015-04-08 14:29:20 -0700538 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700539 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700540 checkValidity();
541
samuele1fa7322015-07-14 16:35:16 +0800542 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
543 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700544 post(event);
545 }
tomd3097b02014-08-26 10:40:29 -0700546 }
tom32f66842014-08-27 19:27:47 -0700547
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700548 // by default allowed, otherwise check flag
549 private boolean isAllowed(BasicDeviceConfig cfg) {
550 return (cfg == null || cfg.isAllowed());
551 }
552
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800553 // Applies the specified role to the device; ignores NONE
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700554
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800555 /**
556 * Apply role to device and send probe if MASTER.
557 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700558 * @param deviceId device identifier
559 * @param newRole new role to apply to the device
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800560 * @return true if the request was sent to provider
561 */
562 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
563 if (newRole.equals(MastershipRole.NONE)) {
samuele1fa7322015-07-14 16:35:16 +0800564 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700565 return true;
566 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700567
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800568 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800569 if (provider == null) {
samuele1fa7322015-07-14 16:35:16 +0800570 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800571 return false;
572 }
573 provider.roleChanged(deviceId, newRole);
574
575 if (newRole.equals(MastershipRole.MASTER)) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800576 log.debug("sent TriggerProbe({})", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800577 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800578 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800579 }
580 return true;
581 }
582
583 /**
584 * Reaasert role for specified device connected to this node.
585 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700586 * @param did device identifier
587 * @param nextRole role to apply. If NONE is specified,
588 * it will ask mastership service for a role and apply it.
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800589 */
samuele1fa7322015-07-14 16:35:16 +0800590 private void reassertRole(final DeviceId did,
591 final MastershipRole nextRole) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800592
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800593 MastershipRole myNextRole = nextRole;
594 if (myNextRole == NONE) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800595 try {
596 mastershipService.requestRoleFor(did).get();
597 MastershipTerm term = termService.getMastershipTerm(did);
598 if (term != null && localNodeId.equals(term.master())) {
599 myNextRole = MASTER;
600 } else {
601 myNextRole = STANDBY;
602 }
603 } catch (InterruptedException e) {
604 Thread.currentThread().interrupt();
605 log.error("Interrupted waiting for Mastership", e);
606 } catch (ExecutionException e) {
607 log.error("Encountered an error waiting for Mastership", e);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800608 }
609 }
610
611 switch (myNextRole) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700612 case MASTER:
613 final Device device = getDevice(did);
614 if ((device != null) && !isAvailable(did)) {
615 //flag the device as online. Is there a better way to do this?
616 DefaultDeviceDescription deviceDescription
617 = new DefaultDeviceDescription(did.uri(),
618 device.type(),
619 device.manufacturer(),
620 device.hwVersion(),
621 device.swVersion(),
622 device.serialNumber(),
623 device.chassisId());
624 DeviceEvent devEvent =
625 store.createOrUpdateDevice(device.providerId(), did,
626 deviceDescription);
627 post(devEvent);
628 }
629 // TODO: should apply role only if there is mismatch
630 log.debug("Applying role {} to {}", myNextRole, did);
631 if (!applyRoleAndProbe(did, MASTER)) {
632 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
633 // immediately failed to apply role
634 mastershipService.relinquishMastership(did);
635 // FIXME disconnect?
636 }
637 break;
638 case STANDBY:
639 log.debug("Applying role {} to {}", myNextRole, did);
640 if (!applyRoleAndProbe(did, STANDBY)) {
641 log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
642 // immediately failed to apply role
643 mastershipService.relinquishMastership(did);
644 // FIXME disconnect?
645 }
646 break;
647 case NONE:
648 default:
649 // should never reach here
650 log.error("You didn't see anything. I did not exist.");
651 break;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800652 }
653 }
654
Madan Jampani328371d2015-05-29 14:06:27 -0700655 private void handleMastershipEvent(MastershipEvent event) {
656 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
657 // Don't care if backup list changed.
658 return;
659 }
660
661 final DeviceId did = event.subject();
662
663 // myRole suggested by MastershipService
664 MastershipRole myNextRole;
665 if (localNodeId.equals(event.roleInfo().master())) {
666 // confirm latest info
667 MastershipTerm term = termService.getMastershipTerm(did);
samuele1fa7322015-07-14 16:35:16 +0800668 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Madan Jampani328371d2015-05-29 14:06:27 -0700669 if (iHaveControl) {
Madan Jampani328371d2015-05-29 14:06:27 -0700670 myNextRole = MASTER;
671 } else {
672 myNextRole = STANDBY;
673 }
674 } else if (event.roleInfo().backups().contains(localNodeId)) {
675 myNextRole = STANDBY;
676 } else {
677 myNextRole = NONE;
678 }
679
Madan Jampani328371d2015-05-29 14:06:27 -0700680 final boolean isReachable = isReachable(did);
681 if (!isReachable) {
682 // device is not connected to this node
683 if (myNextRole != NONE) {
684 log.warn("Node was instructed to be {} role for {}, "
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700685 + "but this node cannot reach the device. "
686 + "Relinquishing role. ",
samuele1fa7322015-07-14 16:35:16 +0800687 myNextRole, did);
Madan Jampani328371d2015-05-29 14:06:27 -0700688 mastershipService.relinquishMastership(did);
689 }
690 return;
691 }
692
693 // device is connected to this node:
694 if (store.getDevice(did) != null) {
695 reassertRole(did, myNextRole);
696 } else {
697 log.debug("Device is not yet/no longer in the store: {}", did);
698 }
699 }
700
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800701 // Intercepts mastership events
702 private class InternalMastershipListener implements MastershipListener {
703
tomb41d1ac2014-09-24 01:51:24 -0700704 @Override
705 public void event(MastershipEvent event) {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800706 backgroundService.execute(() -> {
Madan Jampani328371d2015-05-29 14:06:27 -0700707 try {
708 handleMastershipEvent(event);
709 } catch (Exception e) {
710 log.warn("Failed to handle {}", event, e);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700711 }
Madan Jampani328371d2015-05-29 14:06:27 -0700712 });
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700713 }
tomb41d1ac2014-09-24 01:51:24 -0700714 }
tomf80c9722014-09-24 14:49:18 -0700715
716 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700717 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700718 @Override
719 public void notify(DeviceEvent event) {
720 post(event);
721 }
722 }
samuel738dfaf2015-07-11 11:08:57 +0800723
724 @Override
725 public Iterable<Device> getDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900726 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800727 Set<Device> results = new HashSet<>();
728 Iterable<Device> devices = store.getDevices();
729 if (devices != null) {
730 devices.forEach(d -> {
731 if (type.equals(d.type())) {
732 results.add(d);
733 }
734 });
735 }
736 return results;
737 }
738
739 @Override
740 public Iterable<Device> getAvailableDevices(Type type) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900741 checkPermission(DEVICE_READ);
samuel738dfaf2015-07-11 11:08:57 +0800742 Set<Device> results = new HashSet<>();
743 Iterable<Device> availableDevices = store.getAvailableDevices();
744 if (availableDevices != null) {
745 availableDevices.forEach(d -> {
746 if (type.equals(d.type())) {
747 results.add(d);
748 }
749 });
750 }
751 return results;
752 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700753
754 private class InternalNetworkConfigListener implements NetworkConfigListener {
755 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700756 public boolean isRelevant(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700757 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
758 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
759 && (event.configClass().equals(BasicDeviceConfig.class)
760 || event.configClass().equals(OpticalPortConfig.class));
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700761 }
762
763 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700764 public void event(NetworkConfigEvent event) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700765 DeviceEvent de = null;
766 if (event.configClass().equals(BasicDeviceConfig.class)) {
Thomas Vachuska138de8b2016-01-11 21:31:38 -0800767 log.debug("Detected device network config event {}", event.type());
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700768 DeviceId did = (DeviceId) event.subject();
769 BasicDeviceConfig cfg = networkConfigService.getConfig(did, BasicDeviceConfig.class);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700770
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700771 if (!isAllowed(cfg)) {
772 kickOutBadDevice(did);
773 } else {
774 Device dev = getDevice(did);
775 DeviceDescription desc = (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev);
776 desc = BasicDeviceOperator.combine(cfg, desc);
Thomas Vachuska1627dc82015-11-13 12:22:14 -0800777 if (desc != null && getProvider(did) != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700778 de = store.createOrUpdateDevice(getProvider(did).id(), did, desc);
779 }
780 }
781 }
782 if (event.configClass().equals(OpticalPortConfig.class)) {
783 ConnectPoint cpt = (ConnectPoint) event.subject();
784 DeviceId did = cpt.deviceId();
785 Port dpt = getPort(did, cpt.port());
786
787 if (dpt != null) {
788 OpticalPortConfig opc = networkConfigService.getConfig(cpt, OpticalPortConfig.class);
789 PortDescription desc = OpticalPortOperator.descriptionOf(dpt);
790 desc = OpticalPortOperator.combine(opc, desc);
Thomas Vachuska1627dc82015-11-13 12:22:14 -0800791 if (desc != null && getProvider(did) != null) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700792 de = store.updatePortStatus(getProvider(did).id(), did, desc);
793 }
794 }
795 }
796
797 if (de != null) {
798 post(de);
799 }
800 }
801
802 // checks if the specified device is allowed by the BasicDeviceConfig
803 // and if not, removes it
804 private void kickOutBadDevice(DeviceId deviceId) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700805 Device badDevice = getDevice(deviceId);
806 if (badDevice != null) {
807 removeDevice(deviceId);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700808 }
809 }
810 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700811
tomd3097b02014-08-26 10:40:29 -0700812}