blob: 0519b3feac2c4c52bf74b050afc149eac8ab5353 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070020import com.google.common.collect.Maps;
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;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070038import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.Port;
40import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
42import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080043import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080044import org.onosproject.net.flow.TrafficSelector;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070045import org.onosproject.net.link.DefaultLinkDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.link.LinkProvider;
47import org.onosproject.net.link.LinkProviderRegistry;
48import org.onosproject.net.link.LinkProviderService;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070049import org.onosproject.net.link.LinkService;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080051import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.packet.PacketProcessor;
53import org.onosproject.net.packet.PacketService;
54import org.onosproject.net.provider.AbstractProvider;
55import org.onosproject.net.provider.ProviderId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080056import org.osgi.service.component.ComponentContext;
alshabib7911a052014-10-16 17:49:37 -070057import org.slf4j.Logger;
58
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080059import java.io.IOException;
60import java.util.Dictionary;
61import java.util.EnumSet;
62import java.util.Map;
Thomas Vachuska05453c92015-09-09 14:40:49 -070063import java.util.Properties;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080064import java.util.concurrent.ConcurrentHashMap;
65import java.util.concurrent.ScheduledExecutorService;
66
Thomas Vachuska05453c92015-09-09 14:40:49 -070067import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080068import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
69import static java.util.concurrent.TimeUnit.SECONDS;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070070import static org.onlab.util.Tools.get;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080071import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070072import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080073import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080074
alshabib7911a052014-10-16 17:49:37 -070075/**
Thomas Vachuska05453c92015-09-09 14:40:49 -070076 * Provider which uses LLDP and BDDP packets to detect network infrastructure links.
alshabib7911a052014-10-16 17:49:37 -070077 */
78@Component(immediate = true)
79public class LLDPLinkProvider extends AbstractProvider implements LinkProvider {
80
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070081 private static final String PROVIDER_NAME = "org.onosproject.provider.lldp";
82
Thomas Vachuska05453c92015-09-09 14:40:49 -070083 private static final String FORMAT =
84 "Settings: enabled={}, useBDDP={}, probeRate={}, " +
85 "staleLinkAge={}, lldpSuppression={}";
Yuta HIGUCHI41289382014-12-19 17:47:12 -080086
alshabib7911a052014-10-16 17:49:37 -070087 private final Logger log = getLogger(getClass());
88
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080089 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected CoreService coreService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib7911a052014-10-16 17:49:37 -070093 protected LinkProviderRegistry providerRegistry;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected DeviceService deviceService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070099 protected LinkService linkService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800102 protected PacketService packetService;
alshabib7911a052014-10-16 17:49:37 -0700103
alshabib875d6262014-10-17 16:19:40 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected MastershipService masterService;
106
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected ComponentConfigService cfgService;
109
alshabib7911a052014-10-16 17:49:37 -0700110 private LinkProviderService providerService;
111
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800112 private ScheduledExecutorService executor;
113
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700114 // TODO: Add sanity checking for the configurable params based on the delays
115 private static final long DEVICE_SYNC_DELAY = 5;
116 private static final long LINK_PRUNER_DELAY = 3;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700117
118 private static final String PROP_ENABLED = "enabled";
119 @Property(name = PROP_ENABLED, boolValue = true,
120 label = "If false, link discovery is disabled")
121 private boolean enabled = false;
122
123 private static final String PROP_USE_BDDP = "useBDDP";
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700124 @Property(name = PROP_USE_BDDP, boolValue = true,
125 label = "Use BDDP for link discovery")
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800126 private boolean useBDDP = true;
alshabib7911a052014-10-16 17:49:37 -0700127
Thomas Vachuska05453c92015-09-09 14:40:49 -0700128 private static final String PROP_PROBE_RATE = "probeRate";
129 private static final int DEFAULT_PROBE_RATE = 3_000;
130 @Property(name = PROP_PROBE_RATE, intValue = DEFAULT_PROBE_RATE,
131 label = "LLDP and BDDP probe rate specified in millis")
132 private int probeRate = DEFAULT_PROBE_RATE;
Saurav Dasc313c402015-02-27 10:09:47 -0800133
Thomas Vachuska05453c92015-09-09 14:40:49 -0700134 private static final String PROP_STALE_LINK_AGE = "staleLinkAge";
135 private static final int DEFAULT_STALE_LINK_AGE = 10_000;
136 @Property(name = PROP_STALE_LINK_AGE, intValue = DEFAULT_STALE_LINK_AGE,
137 label = "Number of millis beyond which links will be considered stale")
138 private int staleLinkAge = DEFAULT_STALE_LINK_AGE;
alshabib7911a052014-10-16 17:49:37 -0700139
Thomas Vachuska05453c92015-09-09 14:40:49 -0700140 // FIXME: convert to use network config subsystem instead
141 private static final String PROP_LLDP_SUPPRESSION = "lldpSuppression";
142 private static final String DEFAULT_LLDP_SUPPRESSION_CONFIG = "../config/lldp_suppression.json";
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700143 @Property(name = PROP_LLDP_SUPPRESSION, value = DEFAULT_LLDP_SUPPRESSION_CONFIG,
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800144 label = "Path to LLDP suppression configuration file")
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700145 private String lldpSuppression = DEFAULT_LLDP_SUPPRESSION_CONFIG;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800146
Thomas Vachuska05453c92015-09-09 14:40:49 -0700147 private final DiscoveryContext context = new InternalDiscoveryContext();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800148 private final InternalRoleListener roleListener = new InternalRoleListener();
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700149 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
150 private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800151
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700152 // Device link discovery helpers.
alshabib7911a052014-10-16 17:49:37 -0700153 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
154
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700155 // Most recent time a tracked link was seen; links are tracked if their
156 // destination connection point is mastered by this controller instance.
157 private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap();
158
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800159 private SuppressionRules rules;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800160 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800161
alshabib7911a052014-10-16 17:49:37 -0700162 /**
163 * Creates an OpenFlow link provider.
164 */
165 public LLDPLinkProvider() {
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700166 super(new ProviderId("lldp", PROVIDER_NAME));
alshabib7911a052014-10-16 17:49:37 -0700167 }
168
169 @Activate
Saurav Dasc313c402015-02-27 10:09:47 -0800170 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700171 cfgService.registerProperties(getClass());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700172 appId = coreService.registerApplication(PROVIDER_NAME);
Saurav Dasc313c402015-02-27 10:09:47 -0800173 modified(context);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700174 log.info("Started");
175 }
176
177 @Deactivate
178 public void deactivate() {
179 cfgService.unregisterProperties(getClass(), false);
180 disable();
181 log.info("Stopped");
182 }
183
184 @Modified
185 public void modified(ComponentContext context) {
186 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
187
188 boolean newEnabled, newUseBddp;
189 int newProbeRate, newStaleLinkAge;
190 String newLldpSuppression;
191 try {
192 String s = get(properties, PROP_ENABLED);
193 newEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
194
195 s = get(properties, PROP_USE_BDDP);
196 newUseBddp = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
197
198 s = get(properties, PROP_PROBE_RATE);
199 newProbeRate = isNullOrEmpty(s) ? probeRate : Integer.parseInt(s.trim());
200
201 s = get(properties, PROP_STALE_LINK_AGE);
202 newStaleLinkAge = isNullOrEmpty(s) ? staleLinkAge : Integer.parseInt(s.trim());
203
204 s = get(properties, PROP_LLDP_SUPPRESSION);
205 newLldpSuppression = isNullOrEmpty(s) ? DEFAULT_LLDP_SUPPRESSION_CONFIG : s;
206
207 } catch (NumberFormatException e) {
208 log.warn(e.getMessage());
209 newEnabled = enabled;
210 newUseBddp = useBDDP;
211 newProbeRate = probeRate;
212 newStaleLinkAge = staleLinkAge;
213 newLldpSuppression = lldpSuppression;
Saurav Dasc313c402015-02-27 10:09:47 -0800214 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800215
Thomas Vachuska05453c92015-09-09 14:40:49 -0700216 boolean wasEnabled = enabled;
217
218 enabled = newEnabled;
219 useBDDP = newUseBddp;
220 probeRate = newProbeRate;
221 staleLinkAge = newStaleLinkAge;
222 lldpSuppression = newLldpSuppression;
223
224 if (!wasEnabled && enabled) {
225 enable();
226 } else if (wasEnabled && !enabled) {
227 disable();
228 }
229
230 log.info(FORMAT, enabled, useBDDP, probeRate, staleLinkAge, lldpSuppression);
231 }
232
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700233 /**
234 * Enables link discovery processing.
235 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700236 private void enable() {
alshabib7911a052014-10-16 17:49:37 -0700237 providerService = providerRegistry.register(this);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800238 masterService.addListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700239 deviceService.addListener(deviceListener);
240 packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800241
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700242 loadDevices();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700243
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700244 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/link", "discovery-%d"));
245 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(),
246 DEVICE_SYNC_DELAY, DEVICE_SYNC_DELAY, SECONDS);
247 executor.scheduleAtFixedRate(new LinkPrunerTask(),
248 LINK_PRUNER_DELAY, LINK_PRUNER_DELAY, SECONDS);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700249
250 loadSuppressionRules();
251 requestIntercepts();
252 }
253
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700254 /**
255 * Disables link discovery processing.
256 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700257 private void disable() {
258 withdrawIntercepts();
259
260 providerRegistry.unregister(this);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700261 masterService.removeListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700262 deviceService.removeListener(deviceListener);
263 packetService.removeProcessor(packetProcessor);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700264
265 if (executor != null) {
266 executor.shutdownNow();
267 }
268 discoverers.values().forEach(LinkDiscovery::stop);
269 discoverers.clear();
270
271 providerService = null;
272 }
273
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700274 /**
275 * Loads available devices and registers their ports to be probed.
276 */
277 private void loadDevices() {
alshabib5dc5a342014-12-03 14:11:16 -0800278 for (Device device : deviceService.getAvailableDevices()) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800279 if (rules.isSuppressed(device)) {
280 log.debug("LinkDiscovery from {} disabled by configuration", device.id());
281 continue;
282 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700283 LinkDiscovery ld = new LinkDiscovery(device, context);
alshabibdfc7afb2014-10-21 20:13:27 -0700284 discoverers.put(device.id(), ld);
Jonathan Hart45066bc2015-07-28 11:18:34 -0700285 addPorts(ld, device.id());
alshabibdfc7afb2014-10-21 20:13:27 -0700286 }
alshabib7911a052014-10-16 17:49:37 -0700287 }
288
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700289 /**
290 * Adds ports of the specified device to the specified discovery helper.
291 */
Jonathan Hart45066bc2015-07-28 11:18:34 -0700292 private void addPorts(LinkDiscovery discoverer, DeviceId deviceId) {
293 for (Port p : deviceService.getPorts(deviceId)) {
294 if (rules.isSuppressed(p)) {
295 continue;
296 }
297 if (!p.number().isLogical()) {
298 discoverer.addPort(p);
299 }
300 }
301 }
302
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700303
304 /**
305 * Loads LLDP suppression rules.
306 */
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800307 private void loadSuppressionRules() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700308 // FIXME: convert to use network configuration
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700309 SuppressionRulesStore store = new SuppressionRulesStore(lldpSuppression);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800310 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700311 log.info("Reading suppression rules from {}", lldpSuppression);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800312 rules = store.read();
313 } catch (IOException e) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700314 log.info("Failed to load {}, using built-in rules", lldpSuppression);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800315 // default rule to suppress ROADM to maintain compatibility
316 rules = new SuppressionRules(ImmutableSet.of(),
317 EnumSet.of(Device.Type.ROADM),
318 ImmutableMap.of());
319 }
320
321 // should refresh discoverers when we need dynamic reconfiguration
322 }
323
Charles M.C. Chane148de82015-05-06 12:38:21 +0800324 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700325 * Requests packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800326 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700327 private void requestIntercepts() {
328 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
329 selector.matchEthType(Ethernet.TYPE_LLDP);
330 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800331
Thomas Vachuska27bee092015-06-23 19:03:10 -0700332 selector.matchEthType(Ethernet.TYPE_BSN);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800333 if (useBDDP) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700334 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
335 } else {
336 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800337 }
338 }
339
Thomas Vachuska27bee092015-06-23 19:03:10 -0700340 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700341 * Withdraws packet intercepts.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700342 */
343 private void withdrawIntercepts() {
344 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
345 selector.matchEthType(Ethernet.TYPE_LLDP);
346 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
347 selector.matchEthType(Ethernet.TYPE_BSN);
348 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
349 }
350
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700351 /**
352 * Processes device mastership role changes.
353 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800354 private class InternalRoleListener implements MastershipListener {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800355 @Override
356 public void event(MastershipEvent event) {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800357 if (MastershipEvent.Type.BACKUPS_CHANGED.equals(event.type())) {
358 // only need new master events
359 return;
360 }
361
362 DeviceId deviceId = event.subject();
363 Device device = deviceService.getDevice(deviceId);
364 if (device == null) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700365 log.debug("Device {} doesn't exist, or isn't there yet", deviceId);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800366 return;
367 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800368 if (rules.isSuppressed(device)) {
369 return;
370 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700371 discoverers.computeIfAbsent(deviceId, k -> new LinkDiscovery(device, context));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800372 }
373
374 }
alshabib7911a052014-10-16 17:49:37 -0700375
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700376 /**
377 * Processes device events.
378 */
379 private class InternalDeviceListener implements DeviceListener {
alshabib7911a052014-10-16 17:49:37 -0700380 @Override
381 public void event(DeviceEvent event) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700382 LinkDiscovery ld;
alshabib7911a052014-10-16 17:49:37 -0700383 Device device = event.subject();
alshabibacd91832014-10-17 14:38:41 -0700384 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700385 if (device == null) {
386 log.error("Device is null.");
387 return;
388 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700389 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700390 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700391 switch (event.type()) {
392 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700393 case DEVICE_UPDATED:
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800394 synchronized (discoverers) {
395 ld = discoverers.get(deviceId);
396 if (ld == null) {
397 if (rules.isSuppressed(device)) {
398 log.debug("LinkDiscovery from {} disabled by configuration", device.id());
399 return;
400 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700401 log.debug("Device added ({}) {}", event.type(), deviceId);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700402 discoverers.put(deviceId, new LinkDiscovery(device, context));
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800403 } else {
404 if (ld.isStopped()) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700405 log.debug("Device restarted ({}) {}", event.type(), deviceId);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800406 ld.start();
407 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800408 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700409 }
alshabib7911a052014-10-16 17:49:37 -0700410 break;
411 case PORT_ADDED:
412 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700413 if (port.isEnabled()) {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700414 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700415 if (ld == null) {
416 return;
417 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800418 if (rules.isSuppressed(port)) {
419 log.debug("LinkDiscovery from {}@{} disabled by configuration",
420 port.number(), device.id());
421 return;
422 }
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700423 if (!port.number().isLogical()) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700424 log.debug("Port added {}", port);
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700425 ld.addPort(port);
426 }
alshabib7911a052014-10-16 17:49:37 -0700427 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700428 log.debug("Port down {}", port);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700429 ConnectPoint point = new ConnectPoint(deviceId, port.number());
alshabib7911a052014-10-16 17:49:37 -0700430 providerService.linksVanished(point);
431 }
432 break;
433 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700434 log.debug("Port removed {}", port);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700435 ConnectPoint point = new ConnectPoint(deviceId, port.number());
alshabib7911a052014-10-16 17:49:37 -0700436 providerService.linksVanished(point);
alshabib4785eec2014-12-04 16:45:45 -0800437
alshabib7911a052014-10-16 17:49:37 -0700438 break;
439 case DEVICE_REMOVED:
440 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700441 log.debug("Device removed {}", deviceId);
442 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700443 if (ld == null) {
444 return;
445 }
446 ld.stop();
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700447 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700448 break;
449 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700450 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700451 if (ld == null) {
452 return;
453 }
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700454 if (deviceService.isAvailable(deviceId)) {
455 log.debug("Device up {}", deviceId);
alshabib7911a052014-10-16 17:49:37 -0700456 ld.start();
457 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700458 providerService.linksVanished(deviceId);
459 log.debug("Device down {}", deviceId);
alshabib7911a052014-10-16 17:49:37 -0700460 ld.stop();
461 }
462 break;
Jonathan Hart9de692c2015-04-23 11:45:47 -0700463 case PORT_STATS_UPDATED:
464 break;
alshabib7911a052014-10-16 17:49:37 -0700465 default:
466 log.debug("Unknown event {}", event);
467 }
468 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700469 }
alshabib7911a052014-10-16 17:49:37 -0700470
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700471 /**
472 * Processes incoming packets.
473 */
474 private class InternalPacketProcessor implements PacketProcessor {
alshabib7911a052014-10-16 17:49:37 -0700475 @Override
476 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700477 if (context == null) {
478 return;
479 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700480 LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId());
alshabib7911a052014-10-16 17:49:37 -0700481 if (ld == null) {
482 return;
483 }
484
485 if (ld.handleLLDP(context)) {
486 context.block();
487 }
488 }
489 }
490
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700491 /**
492 * Auxiliary task to keep device ports up to date.
493 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800494 private final class SyncDeviceInfoTask implements Runnable {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800495 @Override
496 public void run() {
497 if (Thread.currentThread().isInterrupted()) {
498 log.info("Interrupted, quitting");
499 return;
500 }
501 // check what deviceService sees, to see if we are missing anything
502 try {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800503 for (Device dev : deviceService.getDevices()) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800504 if (rules.isSuppressed(dev)) {
505 continue;
506 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800507 DeviceId did = dev.id();
508 synchronized (discoverers) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700509 LinkDiscovery ld = discoverers
510 .computeIfAbsent(did, k -> new LinkDiscovery(dev, context));
511 addPorts(ld, did);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800512 }
513 }
514 } catch (Exception e) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700515 // Catch all exceptions to avoid task being suppressed
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800516 log.error("Exception thrown during synchronization process", e);
517 }
518 }
519 }
520
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700521 /**
522 * Auxiliary task for pruning stale links.
523 */
524 private class LinkPrunerTask implements Runnable {
525 @Override
526 public void run() {
527 if (Thread.currentThread().isInterrupted()) {
528 log.info("Interrupted, quitting");
529 return;
530 }
531
532 try {
533 // TODO: There is still a slight possibility of mastership
534 // change occurring right with link going stale. This will
535 // result in the stale link not being pruned.
536 Maps.filterEntries(linkTimes, e -> {
537 if (!masterService.isLocalMaster(e.getKey().dst().deviceId())) {
538 return true;
539 }
540 if (isStale(e.getValue())) {
541 providerService.linkVanished(new DefaultLinkDescription(e.getKey().src(),
542 e.getKey().dst(),
543 DIRECT));
544 return true;
545 }
546 return false;
547 }).clear();
548
549 } catch (Exception e) {
550 // Catch all exceptions to avoid task being suppressed
551 log.error("Exception thrown during link pruning process", e);
552 }
553 }
554
555 private boolean isStale(long lastSeen) {
556 return lastSeen < System.currentTimeMillis() - staleLinkAge;
557 }
558 }
559
560 /**
561 * Provides processing context for the device link discovery helpers.
562 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700563 private class InternalDiscoveryContext implements DiscoveryContext {
564 @Override
565 public MastershipService mastershipService() {
566 return masterService;
567 }
568
569 @Override
570 public LinkProviderService providerService() {
571 return providerService;
572 }
573
574 @Override
575 public PacketService packetService() {
576 return packetService;
577 }
578
579 @Override
580 public long probeRate() {
581 return probeRate;
582 }
583
584 @Override
Thomas Vachuska05453c92015-09-09 14:40:49 -0700585 public boolean useBDDP() {
586 return useBDDP;
587 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700588
589 @Override
590 public void touchLink(LinkKey key) {
591 linkTimes.put(key, System.currentTimeMillis());
592 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700593 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700594
alshabib7911a052014-10-16 17:49:37 -0700595}