blob: b99e4be3a01111335c457c2809e9d4c51e1727d4 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Maps;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080021import org.onlab.packet.Ethernet;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070022import org.onosproject.cfg.ComponentConfigService;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080023import org.onosproject.cluster.ClusterMetadataService;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070024import org.onosproject.cluster.ClusterService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.mastership.MastershipEvent;
28import org.onosproject.mastership.MastershipListener;
29import org.onosproject.mastership.MastershipService;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.Device;
32import org.onosproject.net.DeviceId;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070033import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.Port;
Naoki Shiota399a0b32015-11-15 20:36:13 -060035import org.onosproject.net.config.ConfigFactory;
36import org.onosproject.net.config.NetworkConfigEvent;
37import org.onosproject.net.config.NetworkConfigListener;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070038import org.onosproject.net.config.NetworkConfigRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.device.DeviceEvent;
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080040import org.onosproject.net.device.DeviceEvent.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import 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.LinkProviderRegistry;
47import org.onosproject.net.link.LinkProviderService;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070048import org.onosproject.net.link.LinkService;
Ray Milkeyd9bbde82016-06-09 11:35:00 -070049import org.onosproject.net.link.ProbedLinkProvider;
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;
Ray Milkey957390e2016-02-09 10:02:46 -080056import org.onosproject.provider.lldpcommon.LinkDiscovery;
Ray Milkeyd9bbde82016-06-09 11:35:00 -070057import org.onosproject.provider.lldpcommon.LinkDiscoveryContext;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080058import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059import org.osgi.service.component.annotations.Activate;
60import org.osgi.service.component.annotations.Component;
61import org.osgi.service.component.annotations.Deactivate;
62import org.osgi.service.component.annotations.Modified;
63import org.osgi.service.component.annotations.Reference;
64import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib7911a052014-10-16 17:49:37 -070065import org.slf4j.Logger;
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067import java.util.Dictionary;
68import java.util.EnumSet;
69import java.util.LinkedList;
70import java.util.List;
71import java.util.Map;
72import java.util.Optional;
73import java.util.Properties;
74import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
76import java.util.concurrent.ExecutorService;
77import java.util.concurrent.ScheduledExecutorService;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080078
79import static com.google.common.base.Strings.isNullOrEmpty;
80import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
81import static java.util.concurrent.TimeUnit.SECONDS;
82import static org.onlab.packet.Ethernet.TYPE_BSN;
83import static org.onlab.packet.Ethernet.TYPE_LLDP;
84import static org.onlab.util.Tools.get;
85import static org.onlab.util.Tools.groupedThreads;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080086import static org.onosproject.net.Link.Type.DIRECT;
87import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
88import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
89import static org.onosproject.net.config.basics.SubjectFactories.DEVICE_SUBJECT_FACTORY;
Thomas Vachuska4167c3f2018-10-16 07:16:31 -070090import static org.onosproject.provider.lldp.impl.OsgiPropertyConstants.*;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080091import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080092
alshabib7911a052014-10-16 17:49:37 -070093/**
Thomas Vachuska05453c92015-09-09 14:40:49 -070094 * Provider which uses LLDP and BDDP packets to detect network infrastructure links.
alshabib7911a052014-10-16 17:49:37 -070095 */
Thomas Vachuska4167c3f2018-10-16 07:16:31 -070096@Component(immediate = true,
97 property = {
98 PROP_ENABLED + ":Boolean=" + ENABLED_DEFAULT,
99 PROP_USE_BDDP + ":Boolean=" + USE_BDDP_DEFAULT,
100 PROP_PROBE_RATE + ":Integer=" + PROBE_RATE_DEFAULT,
101 PROP_STALE_LINK_AGE + ":Integer=" + STALE_LINK_AGE_DEFAULT,
Ray Milkeyd17309c2018-10-18 09:34:54 -0700102 PROP_DISCOVERY_DELAY + ":Integer=" + DISCOVERY_DELAY_DEFAULT,
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000103 PROP_USE_STALE_LINK_AGE + ":Boolean=" + USE_STALE_LINK_AGE_DEFAULT,
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700104 })
Ayaka Koshibe48229222016-05-16 18:04:26 -0700105public class LldpLinkProvider extends AbstractProvider implements ProbedLinkProvider {
alshabib7911a052014-10-16 17:49:37 -0700106
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700107 private static final String PROVIDER_NAME = "org.onosproject.provider.lldp";
108
Thomas Vachuska05453c92015-09-09 14:40:49 -0700109 private static final String FORMAT =
110 "Settings: enabled={}, useBDDP={}, probeRate={}, " +
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000111 "staleLinkAge={}, maxLLDPage={}, useStaleLinkAge={}";
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800112
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700113 // When a Device/Port has this annotation, do not send out LLDP/BDDP
114 public static final String NO_LLDP = "no-lldp";
115
alshabib7911a052014-10-16 17:49:37 -0700116 private final Logger log = getLogger(getClass());
117
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700118 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800119 protected CoreService coreService;
120
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700121 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib7911a052014-10-16 17:49:37 -0700122 protected LinkProviderRegistry providerRegistry;
123
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700124 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib7911a052014-10-16 17:49:37 -0700125 protected DeviceService deviceService;
126
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700128 protected LinkService linkService;
129
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800131 protected PacketService packetService;
alshabib7911a052014-10-16 17:49:37 -0700132
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib875d6262014-10-17 16:19:40 -0700134 protected MastershipService masterService;
135
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700137 protected ComponentConfigService cfgService;
138
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700140 protected ClusterService clusterService;
141
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700143 protected NetworkConfigRegistry cfgRegistry;
144
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800146 protected ClusterMetadataService clusterMetadataService;
147
alshabib7911a052014-10-16 17:49:37 -0700148 private LinkProviderService providerService;
149
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800150 private ScheduledExecutorService executor;
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700151 protected ExecutorService eventExecutor;
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800152
Ray Milkeye80e18f2016-06-02 16:44:14 -0700153 private boolean shuttingDown = false;
154
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700155 // TODO: Add sanity checking for the configurable params based on the delays
156 private static final long DEVICE_SYNC_DELAY = 5;
157 private static final long LINK_PRUNER_DELAY = 3;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700158
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700159 /** If false, link discovery is disabled. */
Ray Milkey7e7bd862018-10-18 08:53:34 -0700160 protected boolean enabled = false;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700161
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700162 /** Use BDDP for link discovery. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700163 protected boolean useBddp = USE_BDDP_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700164
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700165 /** LLDP and BDDP probe rate specified in millis. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700166 protected int probeRate = PROBE_RATE_DEFAULT;
Saurav Dasc313c402015-02-27 10:09:47 -0800167
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700168 /** Number of millis beyond which links will be considered stale. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700169 protected int staleLinkAge = STALE_LINK_AGE_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700170
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700171 /** Number of millis beyond which an LLDP packet will not be accepted. */
Ray Milkeyd17309c2018-10-18 09:34:54 -0700172 private int maxDiscoveryDelayMs = DISCOVERY_DELAY_DEFAULT;
Samuel Jero31e16f52018-09-21 10:34:28 -0400173
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000174 /** If false, StaleLinkAge capability is disabled. */
175 private boolean useStaleLinkAge = USE_STALE_LINK_AGE_DEFAULT;
176
Ray Milkey957390e2016-02-09 10:02:46 -0800177 private final LinkDiscoveryContext context = new InternalDiscoveryContext();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800178 private final InternalRoleListener roleListener = new InternalRoleListener();
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700179 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
180 private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800181
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700182 // Device link discovery helpers.
alshabib7911a052014-10-16 17:49:37 -0700183 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
184
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700185 // Most recent time a tracked link was seen; links are tracked if their
186 // destination connection point is mastered by this controller instance.
187 private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap();
188
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800189 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800190
Naoki Shiota399a0b32015-11-15 20:36:13 -0600191 static final SuppressionRules DEFAULT_RULES
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800192 = new SuppressionRules(EnumSet.of(Device.Type.ROADM,
193 Device.Type.FIBER_SWITCH,
194 Device.Type.OPTICAL_AMPLIFIER,
Andrea Campanellafa0f6cc2018-12-18 15:17:53 +0100195 Device.Type.OTN,
Andrea Campanella1c24fb92018-12-20 16:43:59 +0100196 Device.Type.OLS,
197 Device.Type.TERMINAL_DEVICE),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600198 ImmutableMap.of(NO_LLDP, SuppressionRules.ANY_VALUE));
199
200 private SuppressionRules rules = LldpLinkProvider.DEFAULT_RULES;
201
202 public static final String CONFIG_KEY = "suppression";
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800203 public static final String FEATURE_NAME = "linkDiscovery";
Naoki Shiota399a0b32015-11-15 20:36:13 -0600204
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800205 private final Set<ConfigFactory<?, ?>> factories = ImmutableSet.of(
Naoki Shiota399a0b32015-11-15 20:36:13 -0600206 new ConfigFactory<ApplicationId, SuppressionConfig>(APP_SUBJECT_FACTORY,
207 SuppressionConfig.class,
208 CONFIG_KEY) {
209 @Override
210 public SuppressionConfig createConfig() {
211 return new SuppressionConfig();
212 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800213 },
214 new ConfigFactory<DeviceId, LinkDiscoveryFromDevice>(DEVICE_SUBJECT_FACTORY,
215 LinkDiscoveryFromDevice.class, FEATURE_NAME) {
216 @Override
217 public LinkDiscoveryFromDevice createConfig() {
218 return new LinkDiscoveryFromDevice();
219 }
220 },
221 new ConfigFactory<ConnectPoint, LinkDiscoveryFromPort>(CONNECT_POINT_SUBJECT_FACTORY,
222 LinkDiscoveryFromPort.class, FEATURE_NAME) {
223 @Override
224 public LinkDiscoveryFromPort createConfig() {
225 return new LinkDiscoveryFromPort();
226 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600227 }
228 );
229
230 private final InternalConfigListener cfgListener = new InternalConfigListener();
231
alshabib7911a052014-10-16 17:49:37 -0700232 /**
233 * Creates an OpenFlow link provider.
234 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800235 public LldpLinkProvider() {
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700236 super(new ProviderId("lldp", PROVIDER_NAME));
alshabib7911a052014-10-16 17:49:37 -0700237 }
238
Sho SHIMIZU9efeb812016-08-18 09:29:20 -0700239 private String buildSrcMac() {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700240 String defMac = ProbedLinkProvider.defaultMac();
Ray Milkey55f513f2017-12-01 15:58:21 -0800241 if (clusterMetadataService == null) {
242 log.debug("No cluster metadata service is available. Using default value {}", defMac);
243 return defMac;
244 }
245
246 String srcMac = ProbedLinkProvider.fingerprintMac(clusterMetadataService.getClusterMetadata());
Ayaka Koshibe48229222016-05-16 18:04:26 -0700247 if (srcMac.equals(defMac)) {
248 log.warn("Couldn't generate fingerprint. Using default value {}", defMac);
249 return defMac;
250 }
251 log.trace("Generated MAC address {}", srcMac);
252 return srcMac;
253 }
254
alshabib7911a052014-10-16 17:49:37 -0700255 @Activate
Saurav Dasc313c402015-02-27 10:09:47 -0800256 public void activate(ComponentContext context) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700257 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/linkevents", "events-%d", log));
Ray Milkeye80e18f2016-06-02 16:44:14 -0700258 shuttingDown = false;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700259 cfgService.registerProperties(getClass());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700260 appId = coreService.registerApplication(PROVIDER_NAME);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600261
262 cfgRegistry.addListener(cfgListener);
263 factories.forEach(cfgRegistry::registerConfigFactory);
264
Madan Jampanic6371882016-06-03 21:30:17 -0700265 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600266 if (cfg == null) {
267 // If no configuration is found, register default.
Madan Jampanic6371882016-06-03 21:30:17 -0700268 cfg = this.setDefaultSuppressionConfig();
Naoki Shiota399a0b32015-11-15 20:36:13 -0600269 }
270 cfgListener.reconfigureSuppressionRules(cfg);
271
Saurav Dasc313c402015-02-27 10:09:47 -0800272 modified(context);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700273 log.info("Started");
274 }
275
Thomas Vachuskaaad8b1d2015-12-11 10:36:53 -0800276 private SuppressionConfig setDefaultSuppressionConfig() {
277 SuppressionConfig cfg = cfgRegistry.addConfig(appId, SuppressionConfig.class);
278 cfg.deviceTypes(DEFAULT_RULES.getSuppressedDeviceType())
279 .annotation(DEFAULT_RULES.getSuppressedAnnotation())
280 .apply();
281 return cfg;
282 }
283
Thomas Vachuska05453c92015-09-09 14:40:49 -0700284 @Deactivate
285 public void deactivate() {
Ray Milkeye80e18f2016-06-02 16:44:14 -0700286 shuttingDown = true;
Naoki Shiota399a0b32015-11-15 20:36:13 -0600287 cfgRegistry.removeListener(cfgListener);
288 factories.forEach(cfgRegistry::unregisterConfigFactory);
289
Thomas Vachuska05453c92015-09-09 14:40:49 -0700290 cfgService.unregisterProperties(getClass(), false);
291 disable();
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700292 eventExecutor.shutdownNow();
293 eventExecutor = null;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700294 log.info("Stopped");
295 }
296
297 @Modified
298 public void modified(ComponentContext context) {
299 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
300
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000301 boolean newEnabled, newUseBddp, newUseStaleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400302 int newProbeRate, newStaleLinkAge, newDiscoveryDelay;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700303 try {
304 String s = get(properties, PROP_ENABLED);
305 newEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
306
307 s = get(properties, PROP_USE_BDDP);
308 newUseBddp = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
309
310 s = get(properties, PROP_PROBE_RATE);
311 newProbeRate = isNullOrEmpty(s) ? probeRate : Integer.parseInt(s.trim());
312
313 s = get(properties, PROP_STALE_LINK_AGE);
314 newStaleLinkAge = isNullOrEmpty(s) ? staleLinkAge : Integer.parseInt(s.trim());
315
Samuel Jero31e16f52018-09-21 10:34:28 -0400316 s = get(properties, PROP_DISCOVERY_DELAY);
317 newDiscoveryDelay = isNullOrEmpty(s) ? maxDiscoveryDelayMs : Integer.parseInt(s.trim());
318
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000319 s = get(properties, PROP_USE_STALE_LINK_AGE);
320 newUseStaleLinkAge = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
321
Thomas Vachuska05453c92015-09-09 14:40:49 -0700322 } catch (NumberFormatException e) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700323 log.warn("Component configuration had invalid values", e);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700324 newEnabled = enabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800325 newUseBddp = useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700326 newProbeRate = probeRate;
327 newStaleLinkAge = staleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400328 newDiscoveryDelay = maxDiscoveryDelayMs;
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000329 newUseStaleLinkAge = useStaleLinkAge;
Saurav Dasc313c402015-02-27 10:09:47 -0800330 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800331
Thomas Vachuska05453c92015-09-09 14:40:49 -0700332 boolean wasEnabled = enabled;
333
334 enabled = newEnabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800335 useBddp = newUseBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700336 probeRate = newProbeRate;
337 staleLinkAge = newStaleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400338 maxDiscoveryDelayMs = newDiscoveryDelay;
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000339 useStaleLinkAge = newUseStaleLinkAge;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700340
341 if (!wasEnabled && enabled) {
342 enable();
343 } else if (wasEnabled && !enabled) {
344 disable();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700345 } else {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700346 if (enabled) {
347 // update all discovery helper state
348 loadDevices();
349 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700350 }
351
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000352 log.info(FORMAT, enabled, useBddp, probeRate, staleLinkAge, maxDiscoveryDelayMs, useStaleLinkAge);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700353 }
354
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700355 /**
356 * Enables link discovery processing.
357 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700358 private void enable() {
alshabib7911a052014-10-16 17:49:37 -0700359 providerService = providerRegistry.register(this);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800360 masterService.addListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700361 deviceService.addListener(deviceListener);
362 packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800363
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700364 loadDevices();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700365
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700366 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/link", "discovery-%d", log));
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700367 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(),
368 DEVICE_SYNC_DELAY, DEVICE_SYNC_DELAY, SECONDS);
369 executor.scheduleAtFixedRate(new LinkPrunerTask(),
370 LINK_PRUNER_DELAY, LINK_PRUNER_DELAY, SECONDS);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700371
Thomas Vachuska05453c92015-09-09 14:40:49 -0700372 requestIntercepts();
373 }
374
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700375 /**
376 * Disables link discovery processing.
377 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700378 private void disable() {
379 withdrawIntercepts();
380
381 providerRegistry.unregister(this);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700382 masterService.removeListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700383 deviceService.removeListener(deviceListener);
384 packetService.removeProcessor(packetProcessor);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700385
386 if (executor != null) {
387 executor.shutdownNow();
388 }
389 discoverers.values().forEach(LinkDiscovery::stop);
390 discoverers.clear();
Jon Hall125c3f22017-08-09 13:46:28 -0700391 linkTimes.clear();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700392
393 providerService = null;
394 }
395
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700396 /**
397 * Loads available devices and registers their ports to be probed.
398 */
399 private void loadDevices() {
Ray Milkey0f87d482016-07-06 11:49:19 -0700400 if (!enabled || deviceService == null) {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600401 return;
402 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700403 deviceService.getAvailableDevices()
404 .forEach(d -> updateDevice(d)
405 .ifPresent(ld -> updatePorts(ld, d.id())));
406 }
407
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800408 private boolean isBlacklisted(DeviceId did) {
409 LinkDiscoveryFromDevice cfg = cfgRegistry.getConfig(did, LinkDiscoveryFromDevice.class);
410 if (cfg == null) {
411 return false;
412 }
413 return !cfg.enabled();
414 }
415
416 private boolean isBlacklisted(ConnectPoint cp) {
417 // if parent device is blacklisted, so is the port
418 if (isBlacklisted(cp.deviceId())) {
419 return true;
420 }
421 LinkDiscoveryFromPort cfg = cfgRegistry.getConfig(cp, LinkDiscoveryFromPort.class);
422 if (cfg == null) {
423 return false;
424 }
425 return !cfg.enabled();
426 }
427
428 private boolean isBlacklisted(Port port) {
429 return isBlacklisted(new ConnectPoint(port.element().id(), port.number()));
430 }
431
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700432 /**
433 * Updates discovery helper for specified device.
434 *
435 * Adds and starts a discovery helper for specified device if enabled,
436 * calls {@link #removeDevice(DeviceId)} otherwise.
437 *
438 * @param device device to add
439 * @return discovery helper if discovery is enabled for the device
440 */
441 private Optional<LinkDiscovery> updateDevice(Device device) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800442 if (device == null) {
443 return Optional.empty();
444 }
Jon Hall125c3f22017-08-09 13:46:28 -0700445 if (!masterService.isLocalMaster(device.id())) {
446 // Reset the last seen time for all links to this device
447 // then stop discovery for this device
448 List<LinkKey> updateLinks = new LinkedList<>();
449 linkTimes.forEach((link, time) -> {
450 if (link.dst().deviceId().equals(device.id())) {
451 updateLinks.add(link);
452 }
453 });
454 updateLinks.forEach(link -> linkTimes.remove(link));
455 removeDevice(device.id());
456 return Optional.empty();
457 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800458 if (rules.isSuppressed(device) || isBlacklisted(device.id())) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700459 log.trace("LinkDiscovery from {} disabled by configuration", device.id());
460 removeDevice(device.id());
461 return Optional.empty();
462 }
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800463
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700464 LinkDiscovery ld = discoverers.computeIfAbsent(device.id(),
DongRyeol Chac80570b2018-11-07 11:55:32 +0900465 did -> new LinkDiscovery(device.id(), context));
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700466 if (ld.isStopped()) {
467 ld.start();
468 }
469 return Optional.of(ld);
470 }
471
472 /**
473 * Removes after stopping discovery helper for specified device.
474 * @param deviceId device to remove
475 */
476 private void removeDevice(final DeviceId deviceId) {
477 discoverers.computeIfPresent(deviceId, (did, ld) -> {
478 ld.stop();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700479 return null;
480 });
481
482 }
483
484 /**
485 * Updates ports of the specified device to the specified discovery helper.
486 */
487 private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) {
488 deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p));
489 }
490
491 /**
492 * Updates discovery helper state of the specified port.
493 *
494 * Adds a port to the discovery helper if up and discovery is enabled,
495 * or calls {@link #removePort(Port)} otherwise.
496 */
497 private void updatePort(LinkDiscovery discoverer, Port port) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800498 if (port == null) {
499 return;
500 }
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800501 if (port.number().isLogical()) {
502 // silently ignore logical ports
503 return;
504 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600505
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800506 if (rules.isSuppressed(port) || isBlacklisted(port)) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700507 log.trace("LinkDiscovery from {} disabled by configuration", port);
508 removePort(port);
509 return;
510 }
511
512 // check if enabled and turn off discovery?
513 if (!port.isEnabled()) {
514 removePort(port);
515 return;
516 }
517
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800518 discoverer.addPort(port);
alshabib7911a052014-10-16 17:49:37 -0700519 }
520
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700521 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700522 * Removes a port from the specified discovery helper.
523 * @param port the port
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700524 */
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700525 private void removePort(Port port) {
526 if (port.element() instanceof Device) {
527 Device d = (Device) port.element();
528 LinkDiscovery ld = discoverers.get(d.id());
529 if (ld != null) {
530 ld.removePort(port.number());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700531 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700532 } else {
533 log.warn("Attempted to remove non-Device port", port);
Jonathan Hart45066bc2015-07-28 11:18:34 -0700534 }
535 }
536
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700537 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700538 * Requests packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800539 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700540 private void requestIntercepts() {
541 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700542 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700543 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800544
Thomas Vachuska347cc872015-09-23 10:25:29 -0700545 selector.matchEthType(TYPE_BSN);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800546 if (useBddp) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700547 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
548 } else {
549 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800550 }
551 }
552
Thomas Vachuska27bee092015-06-23 19:03:10 -0700553 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700554 * Withdraws packet intercepts.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700555 */
556 private void withdrawIntercepts() {
557 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700558 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700559 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska347cc872015-09-23 10:25:29 -0700560 selector.matchEthType(TYPE_BSN);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700561 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
562 }
563
Naoki Shiota399a0b32015-11-15 20:36:13 -0600564 protected SuppressionRules rules() {
565 return rules;
566 }
567
568 protected void updateRules(SuppressionRules newRules) {
569 if (!rules.equals(newRules)) {
570 rules = newRules;
571 loadDevices();
572 }
573 }
574
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700575 /**
576 * Processes device mastership role changes.
577 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800578 private class InternalRoleListener implements MastershipListener {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800579 @Override
580 public void event(MastershipEvent event) {
Jordan Halterman9d982a52017-12-11 15:27:37 -0800581 if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800582 // only need new master events
Jon Hall7a8bfc62016-05-26 17:59:04 -0700583 eventExecutor.execute(() -> {
584 DeviceId deviceId = event.subject();
585 Device device = deviceService.getDevice(deviceId);
586 if (device == null) {
587 log.debug("Device {} doesn't exist, or isn't there yet", deviceId);
588 return;
589 }
Jordan Halterman9d982a52017-12-11 15:27:37 -0800590 updateDevice(device).ifPresent(ld -> updatePorts(ld, device.id()));
Jon Hall7a8bfc62016-05-26 17:59:04 -0700591 });
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800592 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800593 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800594 }
alshabib7911a052014-10-16 17:49:37 -0700595
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700596 private class DeviceEventProcessor implements Runnable {
597
598 DeviceEvent event;
599
600 DeviceEventProcessor(DeviceEvent event) {
601 this.event = event;
602 }
603
alshabib7911a052014-10-16 17:49:37 -0700604 @Override
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700605 public void run() {
alshabib7911a052014-10-16 17:49:37 -0700606 Device device = event.subject();
alshabibacd91832014-10-17 14:38:41 -0700607 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700608 if (device == null) {
609 log.error("Device is null.");
610 return;
611 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700612 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700613 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700614 switch (event.type()) {
615 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700616 case DEVICE_UPDATED:
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700617 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700618 break;
619 case PORT_ADDED:
620 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700621 if (port.isEnabled()) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700622 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
alshabib7911a052014-10-16 17:49:37 -0700623 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700624 log.debug("Port down {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700625 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600626 providerService.linksVanished(new ConnectPoint(port.element().id(),
627 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700628 }
629 break;
630 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700631 log.debug("Port removed {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700632 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600633 providerService.linksVanished(new ConnectPoint(port.element().id(),
634 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700635 break;
636 case DEVICE_REMOVED:
637 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700638 log.debug("Device removed {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700639 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600640 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700641 break;
642 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700643 if (deviceService.isAvailable(deviceId)) {
644 log.debug("Device up {}", deviceId);
alshabibe3af2652015-12-01 23:05:34 -0800645 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700646 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700647 log.debug("Device down {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700648 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600649 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700650 }
651 break;
Jonathan Hart9de692c2015-04-23 11:45:47 -0700652 case PORT_STATS_UPDATED:
653 break;
alshabib7911a052014-10-16 17:49:37 -0700654 default:
655 log.debug("Unknown event {}", event);
656 }
657 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700658 }
alshabib7911a052014-10-16 17:49:37 -0700659
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700660 /**
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700661 * Processes device events.
662 */
663 private class InternalDeviceListener implements DeviceListener {
664 @Override
665 public void event(DeviceEvent event) {
666 if (event.type() == Type.PORT_STATS_UPDATED) {
667 return;
668 }
669
670 Runnable deviceEventProcessor = new DeviceEventProcessor(event);
671
672 eventExecutor.execute(deviceEventProcessor);
673 }
674 }
675
676 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700677 * Processes incoming packets.
678 */
679 private class InternalPacketProcessor implements PacketProcessor {
alshabib7911a052014-10-16 17:49:37 -0700680 @Override
681 public void process(PacketContext context) {
Thomas Vachuska347cc872015-09-23 10:25:29 -0700682 if (context == null || context.isHandled()) {
alshabib4a179dc2014-10-17 17:17:01 -0700683 return;
684 }
Thomas Vachuska347cc872015-09-23 10:25:29 -0700685
686 Ethernet eth = context.inPacket().parsed();
687 if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) {
688 return;
689 }
690
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700691 LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId());
alshabib7911a052014-10-16 17:49:37 -0700692 if (ld == null) {
693 return;
694 }
695
Jonathan Hartb35540a2015-11-17 09:30:56 -0800696 if (ld.handleLldp(context)) {
alshabib7911a052014-10-16 17:49:37 -0700697 context.block();
698 }
699 }
700 }
701
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700702 /**
703 * Auxiliary task to keep device ports up to date.
704 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800705 private final class SyncDeviceInfoTask implements Runnable {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800706 @Override
707 public void run() {
708 if (Thread.currentThread().isInterrupted()) {
709 log.info("Interrupted, quitting");
710 return;
711 }
712 // check what deviceService sees, to see if we are missing anything
713 try {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700714 loadDevices();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800715 } catch (Exception e) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700716 // Catch all exceptions to avoid task being suppressed
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800717 log.error("Exception thrown during synchronization process", e);
718 }
719 }
720 }
721
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700722 /**
723 * Auxiliary task for pruning stale links.
724 */
725 private class LinkPrunerTask implements Runnable {
726 @Override
727 public void run() {
728 if (Thread.currentThread().isInterrupted()) {
729 log.info("Interrupted, quitting");
730 return;
731 }
732
733 try {
734 // TODO: There is still a slight possibility of mastership
735 // change occurring right with link going stale. This will
736 // result in the stale link not being pruned.
737 Maps.filterEntries(linkTimes, e -> {
738 if (!masterService.isLocalMaster(e.getKey().dst().deviceId())) {
739 return true;
740 }
741 if (isStale(e.getValue())) {
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000742 if (useStaleLinkAge) {
743 providerService.linkVanished(new DefaultLinkDescription(e.getKey().src(),
744
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700745 e.getKey().dst(),
746 DIRECT));
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000747 return true;
748 }
749 log.warn("VanishStaleLinkAge feature is disabled, " +
750 "not bringing down link src {} dst {} with expired StaleLinkAge",
751 e.getKey().src(), e.getKey().dst());
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700752 }
753 return false;
754 }).clear();
755
756 } catch (Exception e) {
757 // Catch all exceptions to avoid task being suppressed
Ray Milkeye80e18f2016-06-02 16:44:14 -0700758 if (!shuttingDown) {
759 // Error condition
760 log.error("Exception thrown during link pruning process", e);
761 } else {
762 // Provider is shutting down, the error can be ignored
763 log.trace("Shutting down, ignoring error", e);
764 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700765 }
766 }
767
768 private boolean isStale(long lastSeen) {
769 return lastSeen < System.currentTimeMillis() - staleLinkAge;
770 }
771 }
772
773 /**
774 * Provides processing context for the device link discovery helpers.
775 */
Ray Milkey957390e2016-02-09 10:02:46 -0800776 private class InternalDiscoveryContext implements LinkDiscoveryContext {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700777 @Override
778 public MastershipService mastershipService() {
779 return masterService;
780 }
781
782 @Override
783 public LinkProviderService providerService() {
784 return providerService;
785 }
786
787 @Override
788 public PacketService packetService() {
789 return packetService;
790 }
791
792 @Override
793 public long probeRate() {
794 return probeRate;
795 }
796
797 @Override
Jonathan Hartb35540a2015-11-17 09:30:56 -0800798 public boolean useBddp() {
799 return useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700800 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700801
802 @Override
803 public void touchLink(LinkKey key) {
804 linkTimes.put(key, System.currentTimeMillis());
805 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800806
807 @Override
DongRyeol Chace65cc02018-07-23 15:02:28 +0900808 public void setTtl(LinkKey key, short ttl) {
809 linkTimes.put(key, System.currentTimeMillis() - staleLinkAge + SECONDS.toMillis(ttl));
810 }
811
812 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700813 public DeviceService deviceService() {
814 return deviceService;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800815 }
816
817 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700818 public String fingerprint() {
819 return buildSrcMac();
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800820 }
Samuel Jero31e16f52018-09-21 10:34:28 -0400821
822 @Override
823 public String lldpSecret() {
824 return clusterMetadataService.getClusterMetadata().getClusterSecret();
825 }
826
827 @Override
828 public long maxDiscoveryDelay() {
829 return maxDiscoveryDelayMs;
830 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700831 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700832
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800833 static final EnumSet<NetworkConfigEvent.Type> CONFIG_CHANGED
834 = EnumSet.of(NetworkConfigEvent.Type.CONFIG_ADDED,
835 NetworkConfigEvent.Type.CONFIG_UPDATED,
836 NetworkConfigEvent.Type.CONFIG_REMOVED);
837
Naoki Shiota399a0b32015-11-15 20:36:13 -0600838 private class InternalConfigListener implements NetworkConfigListener {
839
840 private synchronized void reconfigureSuppressionRules(SuppressionConfig cfg) {
841 if (cfg == null) {
Ray Milkey0a8ee912016-06-13 09:58:12 -0700842 log.debug("Suppression Config is null.");
Naoki Shiota399a0b32015-11-15 20:36:13 -0600843 return;
844 }
845
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800846 SuppressionRules newRules = new SuppressionRules(cfg.deviceTypes(),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600847 cfg.annotation());
848
849 updateRules(newRules);
850 }
851
Jon Hall125c3f22017-08-09 13:46:28 -0700852 private boolean isRelevantDeviceEvent(NetworkConfigEvent event) {
853 return event.configClass() == LinkDiscoveryFromDevice.class &&
854 CONFIG_CHANGED.contains(event.type());
855 }
856
857 private boolean isRelevantPortEvent(NetworkConfigEvent event) {
858 return event.configClass() == LinkDiscoveryFromPort.class &&
859 CONFIG_CHANGED.contains(event.type());
860 }
861
862 private boolean isRelevantSuppressionEvent(NetworkConfigEvent event) {
863 return (event.configClass().equals(SuppressionConfig.class) &&
864 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
865 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED));
866 }
867
Naoki Shiota399a0b32015-11-15 20:36:13 -0600868 @Override
869 public void event(NetworkConfigEvent event) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700870 eventExecutor.execute(() -> {
Jon Hall125c3f22017-08-09 13:46:28 -0700871 if (isRelevantDeviceEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800872 if (event.subject() instanceof DeviceId) {
873 final DeviceId did = (DeviceId) event.subject();
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800874 Device device = deviceService.getDevice(did);
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800875 updateDevice(device).ifPresent(ld -> updatePorts(ld, did));
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800876 }
Jon Hall125c3f22017-08-09 13:46:28 -0700877 } else if (isRelevantPortEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800878 if (event.subject() instanceof ConnectPoint) {
879 ConnectPoint cp = (ConnectPoint) event.subject();
880 if (cp.elementId() instanceof DeviceId) {
881 final DeviceId did = (DeviceId) cp.elementId();
882 Device device = deviceService.getDevice(did);
883 Port port = deviceService.getPort(did, cp.port());
884 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
885 }
886 }
Jon Hall125c3f22017-08-09 13:46:28 -0700887 } else if (isRelevantSuppressionEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800888 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
889 reconfigureSuppressionRules(cfg);
890 log.trace("Network config reconfigured");
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800891 }
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800892 });
Naoki Shiota399a0b32015-11-15 20:36:13 -0600893 }
894 }
alshabib7911a052014-10-16 17:49:37 -0700895}