blob: c3c9ceb7058feb0c2e8261964e622f1cd68b3083 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.lldp.impl;
alshabib7911a052014-10-16 17:49:37 -070017
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080018import com.google.common.base.Strings;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
alshabib7911a052014-10-16 17:49:37 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080024import org.apache.felix.scr.annotations.Modified;
25import org.apache.felix.scr.annotations.Property;
alshabib7911a052014-10-16 17:49:37 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080028import org.onlab.packet.Ethernet;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070029import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.mastership.MastershipEvent;
33import org.onosproject.mastership.MastershipListener;
34import org.onosproject.mastership.MastershipService;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Port;
39import org.onosproject.net.device.DeviceEvent;
40import org.onosproject.net.device.DeviceListener;
41import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080042import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080043import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.link.LinkProvider;
45import org.onosproject.net.link.LinkProviderRegistry;
46import org.onosproject.net.link.LinkProviderService;
47import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080048import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.packet.PacketProcessor;
50import org.onosproject.net.packet.PacketService;
51import org.onosproject.net.provider.AbstractProvider;
52import org.onosproject.net.provider.ProviderId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080053import org.osgi.service.component.ComponentContext;
alshabib7911a052014-10-16 17:49:37 -070054import org.slf4j.Logger;
55
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080056import java.io.IOException;
57import java.util.Dictionary;
58import java.util.EnumSet;
59import java.util.Map;
60import java.util.concurrent.ConcurrentHashMap;
61import java.util.concurrent.ScheduledExecutorService;
62
63import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
64import static java.util.concurrent.TimeUnit.SECONDS;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070065import static org.onlab.util.Tools.get;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080066import static org.onlab.util.Tools.groupedThreads;
67import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080068
alshabib7911a052014-10-16 17:49:37 -070069/**
70 * Provider which uses an OpenFlow controller to detect network
71 * infrastructure links.
72 */
73@Component(immediate = true)
74public class LLDPLinkProvider extends AbstractProvider implements LinkProvider {
75
Yuta HIGUCHI41289382014-12-19 17:47:12 -080076 private static final String PROP_USE_BDDP = "useBDDP";
Saurav Dasc313c402015-02-27 10:09:47 -080077 private static final String PROP_DISABLE_LD = "disableLinkDiscovery";
Yuta HIGUCHI41289382014-12-19 17:47:12 -080078 private static final String PROP_LLDP_SUPPRESSION = "lldpSuppression";
79
80 private static final String DEFAULT_LLDP_SUPPRESSION_CONFIG = "../config/lldp_suppression.json";
81
alshabib7911a052014-10-16 17:49:37 -070082 private final Logger log = getLogger(getClass());
83
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected CoreService coreService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib7911a052014-10-16 17:49:37 -070088 protected LinkProviderRegistry providerRegistry;
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected DeviceService deviceService;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080094 protected PacketService packetService;
alshabib7911a052014-10-16 17:49:37 -070095
alshabib875d6262014-10-17 16:19:40 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected MastershipService masterService;
98
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected ComponentConfigService cfgService;
101
alshabib7911a052014-10-16 17:49:37 -0700102 private LinkProviderService providerService;
103
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800104 private ScheduledExecutorService executor;
105
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700106 @Property(name = PROP_USE_BDDP, boolValue = true,
107 label = "Use BDDP for link discovery")
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800108 private boolean useBDDP = true;
alshabib7911a052014-10-16 17:49:37 -0700109
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700110 @Property(name = PROP_DISABLE_LD, boolValue = false,
111 label = "Permanently disable link discovery")
112 private boolean disableLinkDiscovery = false;
Saurav Dasc313c402015-02-27 10:09:47 -0800113
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800114 private static final long INIT_DELAY = 5;
115 private static final long DELAY = 5;
alshabib7911a052014-10-16 17:49:37 -0700116
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700117 @Property(name = PROP_LLDP_SUPPRESSION, value = DEFAULT_LLDP_SUPPRESSION_CONFIG,
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800118 label = "Path to LLDP suppression configuration file")
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700119 private String lldpSuppression = DEFAULT_LLDP_SUPPRESSION_CONFIG;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800120
121
alshabib7911a052014-10-16 17:49:37 -0700122 private final InternalLinkProvider listener = new InternalLinkProvider();
123
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800124 private final InternalRoleListener roleListener = new InternalRoleListener();
125
alshabib7911a052014-10-16 17:49:37 -0700126 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
127
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800128 private SuppressionRules rules;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800129 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800130
alshabib7911a052014-10-16 17:49:37 -0700131 /**
132 * Creates an OpenFlow link provider.
133 */
134 public LLDPLinkProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800135 super(new ProviderId("lldp", "org.onosproject.provider.lldp"));
alshabib7911a052014-10-16 17:49:37 -0700136 }
137
138 @Activate
Saurav Dasc313c402015-02-27 10:09:47 -0800139 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700140 cfgService.registerProperties(getClass());
141 appId = coreService.registerApplication("org.onosproject.provider.lldp");
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800142
Saurav Dasc313c402015-02-27 10:09:47 -0800143 // to load configuration at startup
144 modified(context);
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700145 if (disableLinkDiscovery) {
Saurav Dasc313c402015-02-27 10:09:47 -0800146 log.info("Link Discovery has been permanently disabled by configuration");
147 return;
148 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800149
alshabib7911a052014-10-16 17:49:37 -0700150 providerService = providerRegistry.register(this);
151 deviceService.addListener(listener);
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800152 packetService.addProcessor(listener, 0);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800153 masterService.addListener(roleListener);
154
alshabibdfc7afb2014-10-21 20:13:27 -0700155 LinkDiscovery ld;
alshabib5dc5a342014-12-03 14:11:16 -0800156 for (Device device : deviceService.getAvailableDevices()) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800157 if (rules.isSuppressed(device)) {
158 log.debug("LinkDiscovery from {} disabled by configuration", device.id());
159 continue;
160 }
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800161 ld = new LinkDiscovery(device, packetService, masterService,
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700162 providerService, useBDDP);
alshabibdfc7afb2014-10-21 20:13:27 -0700163 discoverers.put(device.id(), ld);
164 for (Port p : deviceService.getPorts(device.id())) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800165 if (rules.isSuppressed(p)) {
166 log.debug("LinkDiscovery from {}@{} disabled by configuration",
167 p.number(), device.id());
168 continue;
169 }
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700170 if (!p.number().isLogical()) {
171 ld.addPort(p);
172 }
alshabibdfc7afb2014-10-21 20:13:27 -0700173 }
174 }
alshabib7911a052014-10-16 17:49:37 -0700175
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800176 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "sync-%d"));
177 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(), INIT_DELAY, DELAY, SECONDS);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800178
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800179 requestPackets();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800180
alshabib7911a052014-10-16 17:49:37 -0700181 log.info("Started");
182 }
183
184 @Deactivate
185 public void deactivate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700186 cfgService.unregisterProperties(getClass(), false);
187 if (disableLinkDiscovery) {
Saurav Dasc313c402015-02-27 10:09:47 -0800188 return;
189 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800190 executor.shutdownNow();
alshabib7911a052014-10-16 17:49:37 -0700191 for (LinkDiscovery ld : discoverers.values()) {
192 ld.stop();
193 }
194 providerRegistry.unregister(this);
195 deviceService.removeListener(listener);
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800196 packetService.removeProcessor(listener);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800197 masterService.removeListener(roleListener);
alshabib7911a052014-10-16 17:49:37 -0700198 providerService = null;
199
200 log.info("Stopped");
201 }
202
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800203 @Modified
204 public void modified(ComponentContext context) {
205 if (context == null) {
Saurav Dasc313c402015-02-27 10:09:47 -0800206 loadSuppressionRules();
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800207 return;
208 }
209 @SuppressWarnings("rawtypes")
210 Dictionary properties = context.getProperties();
211
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700212 String s = get(properties, PROP_DISABLE_LD);
Saurav Dasc313c402015-02-27 10:09:47 -0800213 if (!Strings.isNullOrEmpty(s)) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700214 disableLinkDiscovery = Boolean.valueOf(s);
Saurav Dasc313c402015-02-27 10:09:47 -0800215 }
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700216 s = get(properties, PROP_USE_BDDP);
Saurav Dasc313c402015-02-27 10:09:47 -0800217 if (!Strings.isNullOrEmpty(s)) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800218 useBDDP = Boolean.valueOf(s);
219 }
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700220 s = get(properties, PROP_LLDP_SUPPRESSION);
Saurav Dasc313c402015-02-27 10:09:47 -0800221 if (!Strings.isNullOrEmpty(s)) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700222 lldpSuppression = s;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800223 }
224
225 loadSuppressionRules();
226 }
227
228 private void loadSuppressionRules() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700229 SuppressionRulesStore store = new SuppressionRulesStore(lldpSuppression);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800230 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700231 log.info("Reading suppression rules from {}", lldpSuppression);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800232 rules = store.read();
233 } catch (IOException e) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700234 log.info("Failed to load {}, using built-in rules", lldpSuppression);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800235 // default rule to suppress ROADM to maintain compatibility
236 rules = new SuppressionRules(ImmutableSet.of(),
237 EnumSet.of(Device.Type.ROADM),
238 ImmutableMap.of());
239 }
240
241 // should refresh discoverers when we need dynamic reconfiguration
242 }
243
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800244 private void requestPackets() {
245 TrafficSelector.Builder lldpSelector = DefaultTrafficSelector.builder();
246 lldpSelector.matchEthType(Ethernet.TYPE_LLDP);
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800247 packetService.requestPackets(lldpSelector.build(),
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700248 PacketPriority.CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800249
250 if (useBDDP) {
251 TrafficSelector.Builder bddpSelector = DefaultTrafficSelector.builder();
252 bddpSelector.matchEthType(Ethernet.TYPE_BSN);
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800253 packetService.requestPackets(bddpSelector.build(),
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700254 PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800255 }
256 }
257
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800258 private class InternalRoleListener implements MastershipListener {
259
260 @Override
261 public void event(MastershipEvent event) {
262
263 if (MastershipEvent.Type.BACKUPS_CHANGED.equals(event.type())) {
264 // only need new master events
265 return;
266 }
267
268 DeviceId deviceId = event.subject();
269 Device device = deviceService.getDevice(deviceId);
270 if (device == null) {
271 log.warn("Device {} doesn't exist, or isn't there yet", deviceId);
272 return;
273 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800274 if (rules.isSuppressed(device)) {
275 return;
276 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800277 synchronized (discoverers) {
278 if (!discoverers.containsKey(deviceId)) {
alshabib4785eec2014-12-04 16:45:45 -0800279 // ideally, should never reach here
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800280 log.debug("Device mastership changed ({}) {}",
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700281 event.type(), deviceId);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800282 discoverers.put(deviceId, new LinkDiscovery(device,
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700283 packetService, masterService, providerService,
284 useBDDP));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800285 }
286 }
287 }
288
289 }
alshabib7911a052014-10-16 17:49:37 -0700290
291 private class InternalLinkProvider implements PacketProcessor, DeviceListener {
292
293 @Override
294 public void event(DeviceEvent event) {
295 LinkDiscovery ld = null;
296 Device device = event.subject();
alshabibacd91832014-10-17 14:38:41 -0700297 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700298 if (device == null) {
299 log.error("Device is null.");
300 return;
301 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700302 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700303 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700304 switch (event.type()) {
305 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700306 case DEVICE_UPDATED:
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800307 synchronized (discoverers) {
308 ld = discoverers.get(deviceId);
309 if (ld == null) {
310 if (rules.isSuppressed(device)) {
311 log.debug("LinkDiscovery from {} disabled by configuration", device.id());
312 return;
313 }
314 log.debug("Device added ({}) {}", event.type(),
315 deviceId);
316 discoverers.put(deviceId, new LinkDiscovery(device,
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700317 packetService, masterService,
318 providerService, useBDDP));
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800319 } else {
320 if (ld.isStopped()) {
321 log.debug("Device restarted ({}) {}", event.type(),
322 deviceId);
323 ld.start();
324 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800325 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700326 }
alshabib7911a052014-10-16 17:49:37 -0700327 break;
328 case PORT_ADDED:
329 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700330 if (port.isEnabled()) {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700331 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700332 if (ld == null) {
333 return;
334 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800335 if (rules.isSuppressed(port)) {
336 log.debug("LinkDiscovery from {}@{} disabled by configuration",
337 port.number(), device.id());
338 return;
339 }
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700340 if (!port.number().isLogical()) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700341 log.debug("Port added {}", port);
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700342 ld.addPort(port);
343 }
alshabib7911a052014-10-16 17:49:37 -0700344 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700345 log.debug("Port down {}", port);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700346 ConnectPoint point = new ConnectPoint(deviceId,
alshabibacd91832014-10-17 14:38:41 -0700347 port.number());
alshabib7911a052014-10-16 17:49:37 -0700348 providerService.linksVanished(point);
349 }
350 break;
351 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700352 log.debug("Port removed {}", port);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700353 ConnectPoint point = new ConnectPoint(deviceId,
alshabibacd91832014-10-17 14:38:41 -0700354 port.number());
alshabib7911a052014-10-16 17:49:37 -0700355 providerService.linksVanished(point);
alshabib4785eec2014-12-04 16:45:45 -0800356
alshabib7911a052014-10-16 17:49:37 -0700357 break;
358 case DEVICE_REMOVED:
359 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700360 log.debug("Device removed {}", deviceId);
361 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700362 if (ld == null) {
363 return;
364 }
365 ld.stop();
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700366 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700367 break;
368 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700369 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700370 if (ld == null) {
371 return;
372 }
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700373 if (deviceService.isAvailable(deviceId)) {
374 log.debug("Device up {}", deviceId);
alshabib7911a052014-10-16 17:49:37 -0700375 ld.start();
376 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700377 providerService.linksVanished(deviceId);
378 log.debug("Device down {}", deviceId);
alshabib7911a052014-10-16 17:49:37 -0700379 ld.stop();
380 }
381 break;
alshabib7911a052014-10-16 17:49:37 -0700382 default:
383 log.debug("Unknown event {}", event);
384 }
385 }
386
387 @Override
388 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700389 if (context == null) {
390 return;
391 }
alshabib7911a052014-10-16 17:49:37 -0700392 LinkDiscovery ld = discoverers.get(
393 context.inPacket().receivedFrom().deviceId());
394 if (ld == null) {
395 return;
396 }
397
398 if (ld.handleLLDP(context)) {
399 context.block();
400 }
401 }
402 }
403
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800404 private final class SyncDeviceInfoTask implements Runnable {
405
406 @Override
407 public void run() {
408 if (Thread.currentThread().isInterrupted()) {
409 log.info("Interrupted, quitting");
410 return;
411 }
412 // check what deviceService sees, to see if we are missing anything
413 try {
414 LinkDiscovery ld = null;
415 for (Device dev : deviceService.getDevices()) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800416 if (rules.isSuppressed(dev)) {
417 continue;
418 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800419 DeviceId did = dev.id();
420 synchronized (discoverers) {
421 if (!discoverers.containsKey(did)) {
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800422 ld = new LinkDiscovery(dev, packetService,
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700423 masterService, providerService, useBDDP);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800424 discoverers.put(did, ld);
425 for (Port p : deviceService.getPorts(did)) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800426 if (rules.isSuppressed(p)) {
427 continue;
428 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800429 if (!p.number().isLogical()) {
430 ld.addPort(p);
431 }
432 }
433 }
434 }
435 }
436 } catch (Exception e) {
437 // catch all Exception to avoid Scheduled task being suppressed.
438 log.error("Exception thrown during synchronization process", e);
439 }
440 }
441 }
442
alshabib7911a052014-10-16 17:49:37 -0700443}