blob: 9ef85ced3f5fd013dba6a5df4cfef8e7d0bde1c8 [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;
pier7aa3a2d2020-01-29 16:19:22 +010023import org.onosproject.cluster.ClusterMetadata;
24import org.onosproject.cluster.ClusterMetadataEvent;
25import org.onosproject.cluster.ClusterMetadataEventListener;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080026import org.onosproject.cluster.ClusterMetadataService;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070027import org.onosproject.cluster.ClusterService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.mastership.MastershipEvent;
31import org.onosproject.mastership.MastershipListener;
32import org.onosproject.mastership.MastershipService;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.Device;
35import org.onosproject.net.DeviceId;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070036import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.Port;
Naoki Shiota399a0b32015-11-15 20:36:13 -060038import org.onosproject.net.config.ConfigFactory;
39import org.onosproject.net.config.NetworkConfigEvent;
40import org.onosproject.net.config.NetworkConfigListener;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070041import org.onosproject.net.config.NetworkConfigRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.device.DeviceEvent;
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080043import org.onosproject.net.device.DeviceEvent.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.device.DeviceListener;
45import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080046import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080047import org.onosproject.net.flow.TrafficSelector;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070048import org.onosproject.net.link.DefaultLinkDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.link.LinkProviderRegistry;
50import org.onosproject.net.link.LinkProviderService;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070051import org.onosproject.net.link.LinkService;
Ray Milkeyd9bbde82016-06-09 11:35:00 -070052import org.onosproject.net.link.ProbedLinkProvider;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080054import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.packet.PacketProcessor;
56import org.onosproject.net.packet.PacketService;
57import org.onosproject.net.provider.AbstractProvider;
58import org.onosproject.net.provider.ProviderId;
Ray Milkey957390e2016-02-09 10:02:46 -080059import org.onosproject.provider.lldpcommon.LinkDiscovery;
Ray Milkeyd9bbde82016-06-09 11:35:00 -070060import org.onosproject.provider.lldpcommon.LinkDiscoveryContext;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080061import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062import org.osgi.service.component.annotations.Activate;
63import org.osgi.service.component.annotations.Component;
64import org.osgi.service.component.annotations.Deactivate;
65import org.osgi.service.component.annotations.Modified;
66import org.osgi.service.component.annotations.Reference;
67import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib7911a052014-10-16 17:49:37 -070068import org.slf4j.Logger;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070import java.util.Dictionary;
71import java.util.EnumSet;
72import java.util.LinkedList;
73import java.util.List;
74import java.util.Map;
75import java.util.Optional;
76import java.util.Properties;
77import java.util.Set;
78import java.util.concurrent.ConcurrentHashMap;
79import java.util.concurrent.ExecutorService;
80import java.util.concurrent.ScheduledExecutorService;
pier7aa3a2d2020-01-29 16:19:22 +010081import java.util.concurrent.atomic.AtomicReference;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080082
83import static com.google.common.base.Strings.isNullOrEmpty;
84import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
85import static java.util.concurrent.TimeUnit.SECONDS;
86import static org.onlab.packet.Ethernet.TYPE_BSN;
87import static org.onlab.packet.Ethernet.TYPE_LLDP;
88import static org.onlab.util.Tools.get;
89import static org.onlab.util.Tools.groupedThreads;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080090import static org.onosproject.net.Link.Type.DIRECT;
91import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
92import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
93import static org.onosproject.net.config.basics.SubjectFactories.DEVICE_SUBJECT_FACTORY;
Thomas Vachuska4167c3f2018-10-16 07:16:31 -070094import static org.onosproject.provider.lldp.impl.OsgiPropertyConstants.*;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080095import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080096
alshabib7911a052014-10-16 17:49:37 -070097/**
Thomas Vachuska05453c92015-09-09 14:40:49 -070098 * Provider which uses LLDP and BDDP packets to detect network infrastructure links.
alshabib7911a052014-10-16 17:49:37 -070099 */
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700100@Component(immediate = true,
101 property = {
102 PROP_ENABLED + ":Boolean=" + ENABLED_DEFAULT,
103 PROP_USE_BDDP + ":Boolean=" + USE_BDDP_DEFAULT,
104 PROP_PROBE_RATE + ":Integer=" + PROBE_RATE_DEFAULT,
105 PROP_STALE_LINK_AGE + ":Integer=" + STALE_LINK_AGE_DEFAULT,
Ray Milkeyd17309c2018-10-18 09:34:54 -0700106 PROP_DISCOVERY_DELAY + ":Integer=" + DISCOVERY_DELAY_DEFAULT,
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000107 PROP_USE_STALE_LINK_AGE + ":Boolean=" + USE_STALE_LINK_AGE_DEFAULT,
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700108 })
Ayaka Koshibe48229222016-05-16 18:04:26 -0700109public class LldpLinkProvider extends AbstractProvider implements ProbedLinkProvider {
alshabib7911a052014-10-16 17:49:37 -0700110
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700111 private static final String PROVIDER_NAME = "org.onosproject.provider.lldp";
112
Thomas Vachuska05453c92015-09-09 14:40:49 -0700113 private static final String FORMAT =
114 "Settings: enabled={}, useBDDP={}, probeRate={}, " +
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000115 "staleLinkAge={}, maxLLDPage={}, useStaleLinkAge={}";
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800116
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700117 // When a Device/Port has this annotation, do not send out LLDP/BDDP
118 public static final String NO_LLDP = "no-lldp";
119
alshabib7911a052014-10-16 17:49:37 -0700120 private final Logger log = getLogger(getClass());
121
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800123 protected CoreService coreService;
124
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib7911a052014-10-16 17:49:37 -0700126 protected LinkProviderRegistry providerRegistry;
127
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib7911a052014-10-16 17:49:37 -0700129 protected DeviceService deviceService;
130
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700132 protected LinkService linkService;
133
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800135 protected PacketService packetService;
alshabib7911a052014-10-16 17:49:37 -0700136
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib875d6262014-10-17 16:19:40 -0700138 protected MastershipService masterService;
139
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700141 protected ComponentConfigService cfgService;
142
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700143 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700144 protected ClusterService clusterService;
145
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700146 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700147 protected NetworkConfigRegistry cfgRegistry;
148
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700149 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800150 protected ClusterMetadataService clusterMetadataService;
151
alshabib7911a052014-10-16 17:49:37 -0700152 private LinkProviderService providerService;
153
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800154 private ScheduledExecutorService executor;
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700155 protected ExecutorService eventExecutor;
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800156
Ray Milkeye80e18f2016-06-02 16:44:14 -0700157 private boolean shuttingDown = false;
158
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700159 // TODO: Add sanity checking for the configurable params based on the delays
160 private static final long DEVICE_SYNC_DELAY = 5;
161 private static final long LINK_PRUNER_DELAY = 3;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700162
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700163 /** If false, link discovery is disabled. */
Ray Milkey7e7bd862018-10-18 08:53:34 -0700164 protected boolean enabled = false;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700165
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700166 /** Use BDDP for link discovery. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700167 protected boolean useBddp = USE_BDDP_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700168
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700169 /** LLDP and BDDP probe rate specified in millis. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700170 protected int probeRate = PROBE_RATE_DEFAULT;
Saurav Dasc313c402015-02-27 10:09:47 -0800171
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700172 /** Number of millis beyond which links will be considered stale. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700173 protected int staleLinkAge = STALE_LINK_AGE_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700174
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700175 /** Number of millis beyond which an LLDP packet will not be accepted. */
Ray Milkeyd17309c2018-10-18 09:34:54 -0700176 private int maxDiscoveryDelayMs = DISCOVERY_DELAY_DEFAULT;
Samuel Jero31e16f52018-09-21 10:34:28 -0400177
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000178 /** If false, StaleLinkAge capability is disabled. */
179 private boolean useStaleLinkAge = USE_STALE_LINK_AGE_DEFAULT;
180
Ray Milkey957390e2016-02-09 10:02:46 -0800181 private final LinkDiscoveryContext context = new InternalDiscoveryContext();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800182 private final InternalRoleListener roleListener = new InternalRoleListener();
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700183 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
184 private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800185
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700186 // Device link discovery helpers.
alshabib7911a052014-10-16 17:49:37 -0700187 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
188
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700189 // Most recent time a tracked link was seen; links are tracked if their
190 // destination connection point is mastered by this controller instance.
191 private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap();
192
pier7aa3a2d2020-01-29 16:19:22 +0100193 // Cache for clustermetadata
194 private AtomicReference<ClusterMetadata> clusterMetadata = new AtomicReference<>();
195
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800196 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800197
Naoki Shiota399a0b32015-11-15 20:36:13 -0600198 static final SuppressionRules DEFAULT_RULES
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800199 = new SuppressionRules(EnumSet.of(Device.Type.ROADM,
200 Device.Type.FIBER_SWITCH,
201 Device.Type.OPTICAL_AMPLIFIER,
Andrea Campanellafa0f6cc2018-12-18 15:17:53 +0100202 Device.Type.OTN,
Andrea Campanella1c24fb92018-12-20 16:43:59 +0100203 Device.Type.OLS,
204 Device.Type.TERMINAL_DEVICE),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600205 ImmutableMap.of(NO_LLDP, SuppressionRules.ANY_VALUE));
206
207 private SuppressionRules rules = LldpLinkProvider.DEFAULT_RULES;
208
209 public static final String CONFIG_KEY = "suppression";
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800210 public static final String FEATURE_NAME = "linkDiscovery";
Naoki Shiota399a0b32015-11-15 20:36:13 -0600211
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800212 private final Set<ConfigFactory<?, ?>> factories = ImmutableSet.of(
Naoki Shiota399a0b32015-11-15 20:36:13 -0600213 new ConfigFactory<ApplicationId, SuppressionConfig>(APP_SUBJECT_FACTORY,
214 SuppressionConfig.class,
215 CONFIG_KEY) {
216 @Override
217 public SuppressionConfig createConfig() {
218 return new SuppressionConfig();
219 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800220 },
221 new ConfigFactory<DeviceId, LinkDiscoveryFromDevice>(DEVICE_SUBJECT_FACTORY,
222 LinkDiscoveryFromDevice.class, FEATURE_NAME) {
223 @Override
224 public LinkDiscoveryFromDevice createConfig() {
225 return new LinkDiscoveryFromDevice();
226 }
227 },
228 new ConfigFactory<ConnectPoint, LinkDiscoveryFromPort>(CONNECT_POINT_SUBJECT_FACTORY,
229 LinkDiscoveryFromPort.class, FEATURE_NAME) {
230 @Override
231 public LinkDiscoveryFromPort createConfig() {
232 return new LinkDiscoveryFromPort();
233 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600234 }
235 );
236
237 private final InternalConfigListener cfgListener = new InternalConfigListener();
pier7aa3a2d2020-01-29 16:19:22 +0100238 private final ClusterMetadataEventListener metadataListener = new InternalClusterMetadataListener();
Naoki Shiota399a0b32015-11-15 20:36:13 -0600239
alshabib7911a052014-10-16 17:49:37 -0700240 /**
241 * Creates an OpenFlow link provider.
242 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800243 public LldpLinkProvider() {
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700244 super(new ProviderId("lldp", PROVIDER_NAME));
alshabib7911a052014-10-16 17:49:37 -0700245 }
246
Sho SHIMIZU9efeb812016-08-18 09:29:20 -0700247 private String buildSrcMac() {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700248 String defMac = ProbedLinkProvider.defaultMac();
Ray Milkey55f513f2017-12-01 15:58:21 -0800249 if (clusterMetadataService == null) {
250 log.debug("No cluster metadata service is available. Using default value {}", defMac);
251 return defMac;
252 }
253
pier7aa3a2d2020-01-29 16:19:22 +0100254 String srcMac = ProbedLinkProvider.fingerprintMac(clusterMetadata.get());
Ayaka Koshibe48229222016-05-16 18:04:26 -0700255 if (srcMac.equals(defMac)) {
256 log.warn("Couldn't generate fingerprint. Using default value {}", defMac);
257 return defMac;
258 }
259 log.trace("Generated MAC address {}", srcMac);
260 return srcMac;
261 }
262
alshabib7911a052014-10-16 17:49:37 -0700263 @Activate
Saurav Dasc313c402015-02-27 10:09:47 -0800264 public void activate(ComponentContext context) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700265 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/linkevents", "events-%d", log));
Ray Milkeye80e18f2016-06-02 16:44:14 -0700266 shuttingDown = false;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700267 cfgService.registerProperties(getClass());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700268 appId = coreService.registerApplication(PROVIDER_NAME);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600269
270 cfgRegistry.addListener(cfgListener);
271 factories.forEach(cfgRegistry::registerConfigFactory);
272
Madan Jampanic6371882016-06-03 21:30:17 -0700273 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600274 if (cfg == null) {
275 // If no configuration is found, register default.
Madan Jampanic6371882016-06-03 21:30:17 -0700276 cfg = this.setDefaultSuppressionConfig();
Naoki Shiota399a0b32015-11-15 20:36:13 -0600277 }
278 cfgListener.reconfigureSuppressionRules(cfg);
pier7aa3a2d2020-01-29 16:19:22 +0100279 if (clusterMetadataService != null) {
280 clusterMetadataService.addListener(metadataListener);
281 clusterMetadata.set(clusterMetadataService.getClusterMetadata());
282 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600283
Saurav Dasc313c402015-02-27 10:09:47 -0800284 modified(context);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700285 log.info("Started");
286 }
287
Thomas Vachuskaaad8b1d2015-12-11 10:36:53 -0800288 private SuppressionConfig setDefaultSuppressionConfig() {
289 SuppressionConfig cfg = cfgRegistry.addConfig(appId, SuppressionConfig.class);
290 cfg.deviceTypes(DEFAULT_RULES.getSuppressedDeviceType())
291 .annotation(DEFAULT_RULES.getSuppressedAnnotation())
292 .apply();
293 return cfg;
294 }
295
Thomas Vachuska05453c92015-09-09 14:40:49 -0700296 @Deactivate
297 public void deactivate() {
Ray Milkeye80e18f2016-06-02 16:44:14 -0700298 shuttingDown = true;
pier7aa3a2d2020-01-29 16:19:22 +0100299 if (clusterMetadataService != null) {
300 clusterMetadataService.removeListener(metadataListener);
301 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600302 cfgRegistry.removeListener(cfgListener);
303 factories.forEach(cfgRegistry::unregisterConfigFactory);
304
Thomas Vachuska05453c92015-09-09 14:40:49 -0700305 cfgService.unregisterProperties(getClass(), false);
306 disable();
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700307 eventExecutor.shutdownNow();
308 eventExecutor = null;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700309 log.info("Stopped");
310 }
311
312 @Modified
313 public void modified(ComponentContext context) {
314 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
315
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000316 boolean newEnabled, newUseBddp, newUseStaleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400317 int newProbeRate, newStaleLinkAge, newDiscoveryDelay;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700318 try {
319 String s = get(properties, PROP_ENABLED);
320 newEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
321
322 s = get(properties, PROP_USE_BDDP);
323 newUseBddp = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
324
325 s = get(properties, PROP_PROBE_RATE);
326 newProbeRate = isNullOrEmpty(s) ? probeRate : Integer.parseInt(s.trim());
327
328 s = get(properties, PROP_STALE_LINK_AGE);
329 newStaleLinkAge = isNullOrEmpty(s) ? staleLinkAge : Integer.parseInt(s.trim());
330
Samuel Jero31e16f52018-09-21 10:34:28 -0400331 s = get(properties, PROP_DISCOVERY_DELAY);
332 newDiscoveryDelay = isNullOrEmpty(s) ? maxDiscoveryDelayMs : Integer.parseInt(s.trim());
333
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000334 s = get(properties, PROP_USE_STALE_LINK_AGE);
335 newUseStaleLinkAge = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
336
Thomas Vachuska05453c92015-09-09 14:40:49 -0700337 } catch (NumberFormatException e) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700338 log.warn("Component configuration had invalid values", e);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700339 newEnabled = enabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800340 newUseBddp = useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700341 newProbeRate = probeRate;
342 newStaleLinkAge = staleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400343 newDiscoveryDelay = maxDiscoveryDelayMs;
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000344 newUseStaleLinkAge = useStaleLinkAge;
Saurav Dasc313c402015-02-27 10:09:47 -0800345 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800346
Thomas Vachuska05453c92015-09-09 14:40:49 -0700347 boolean wasEnabled = enabled;
348
349 enabled = newEnabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800350 useBddp = newUseBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700351 probeRate = newProbeRate;
352 staleLinkAge = newStaleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400353 maxDiscoveryDelayMs = newDiscoveryDelay;
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000354 useStaleLinkAge = newUseStaleLinkAge;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700355
356 if (!wasEnabled && enabled) {
357 enable();
358 } else if (wasEnabled && !enabled) {
359 disable();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700360 } else {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700361 if (enabled) {
362 // update all discovery helper state
363 loadDevices();
364 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700365 }
366
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000367 log.info(FORMAT, enabled, useBddp, probeRate, staleLinkAge, maxDiscoveryDelayMs, useStaleLinkAge);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700368 }
369
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700370 /**
371 * Enables link discovery processing.
372 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700373 private void enable() {
alshabib7911a052014-10-16 17:49:37 -0700374 providerService = providerRegistry.register(this);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800375 masterService.addListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700376 deviceService.addListener(deviceListener);
377 packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800378
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700379 loadDevices();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700380
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700381 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/link", "discovery-%d", log));
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700382 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(),
383 DEVICE_SYNC_DELAY, DEVICE_SYNC_DELAY, SECONDS);
384 executor.scheduleAtFixedRate(new LinkPrunerTask(),
385 LINK_PRUNER_DELAY, LINK_PRUNER_DELAY, SECONDS);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700386
Thomas Vachuska05453c92015-09-09 14:40:49 -0700387 requestIntercepts();
388 }
389
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700390 /**
391 * Disables link discovery processing.
392 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700393 private void disable() {
394 withdrawIntercepts();
395
396 providerRegistry.unregister(this);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700397 masterService.removeListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700398 deviceService.removeListener(deviceListener);
399 packetService.removeProcessor(packetProcessor);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700400
401 if (executor != null) {
402 executor.shutdownNow();
403 }
404 discoverers.values().forEach(LinkDiscovery::stop);
405 discoverers.clear();
Jon Hall125c3f22017-08-09 13:46:28 -0700406 linkTimes.clear();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700407
408 providerService = null;
409 }
410
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700411 /**
412 * Loads available devices and registers their ports to be probed.
413 */
414 private void loadDevices() {
Ray Milkey0f87d482016-07-06 11:49:19 -0700415 if (!enabled || deviceService == null) {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600416 return;
417 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700418 deviceService.getAvailableDevices()
419 .forEach(d -> updateDevice(d)
420 .ifPresent(ld -> updatePorts(ld, d.id())));
421 }
422
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800423 private boolean isBlacklisted(DeviceId did) {
424 LinkDiscoveryFromDevice cfg = cfgRegistry.getConfig(did, LinkDiscoveryFromDevice.class);
425 if (cfg == null) {
426 return false;
427 }
428 return !cfg.enabled();
429 }
430
431 private boolean isBlacklisted(ConnectPoint cp) {
432 // if parent device is blacklisted, so is the port
433 if (isBlacklisted(cp.deviceId())) {
434 return true;
435 }
436 LinkDiscoveryFromPort cfg = cfgRegistry.getConfig(cp, LinkDiscoveryFromPort.class);
437 if (cfg == null) {
438 return false;
439 }
440 return !cfg.enabled();
441 }
442
443 private boolean isBlacklisted(Port port) {
444 return isBlacklisted(new ConnectPoint(port.element().id(), port.number()));
445 }
446
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700447 /**
448 * Updates discovery helper for specified device.
449 *
450 * Adds and starts a discovery helper for specified device if enabled,
451 * calls {@link #removeDevice(DeviceId)} otherwise.
452 *
453 * @param device device to add
454 * @return discovery helper if discovery is enabled for the device
455 */
456 private Optional<LinkDiscovery> updateDevice(Device device) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800457 if (device == null) {
458 return Optional.empty();
459 }
Jon Hall125c3f22017-08-09 13:46:28 -0700460 if (!masterService.isLocalMaster(device.id())) {
461 // Reset the last seen time for all links to this device
462 // then stop discovery for this device
463 List<LinkKey> updateLinks = new LinkedList<>();
464 linkTimes.forEach((link, time) -> {
465 if (link.dst().deviceId().equals(device.id())) {
466 updateLinks.add(link);
467 }
468 });
469 updateLinks.forEach(link -> linkTimes.remove(link));
470 removeDevice(device.id());
471 return Optional.empty();
472 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800473 if (rules.isSuppressed(device) || isBlacklisted(device.id())) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700474 log.trace("LinkDiscovery from {} disabled by configuration", device.id());
475 removeDevice(device.id());
476 return Optional.empty();
477 }
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800478
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700479 LinkDiscovery ld = discoverers.computeIfAbsent(device.id(),
DongRyeol Chac80570b2018-11-07 11:55:32 +0900480 did -> new LinkDiscovery(device.id(), context));
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700481 if (ld.isStopped()) {
482 ld.start();
483 }
484 return Optional.of(ld);
485 }
486
487 /**
488 * Removes after stopping discovery helper for specified device.
489 * @param deviceId device to remove
490 */
491 private void removeDevice(final DeviceId deviceId) {
492 discoverers.computeIfPresent(deviceId, (did, ld) -> {
493 ld.stop();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700494 return null;
495 });
496
497 }
498
499 /**
500 * Updates ports of the specified device to the specified discovery helper.
501 */
502 private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) {
503 deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p));
504 }
505
506 /**
507 * Updates discovery helper state of the specified port.
508 *
509 * Adds a port to the discovery helper if up and discovery is enabled,
510 * or calls {@link #removePort(Port)} otherwise.
511 */
512 private void updatePort(LinkDiscovery discoverer, Port port) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800513 if (port == null) {
514 return;
515 }
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800516 if (port.number().isLogical()) {
517 // silently ignore logical ports
518 return;
519 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600520
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800521 if (rules.isSuppressed(port) || isBlacklisted(port)) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700522 log.trace("LinkDiscovery from {} disabled by configuration", port);
523 removePort(port);
524 return;
525 }
526
527 // check if enabled and turn off discovery?
528 if (!port.isEnabled()) {
529 removePort(port);
530 return;
531 }
532
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800533 discoverer.addPort(port);
alshabib7911a052014-10-16 17:49:37 -0700534 }
535
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700536 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700537 * Removes a port from the specified discovery helper.
538 * @param port the port
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700539 */
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700540 private void removePort(Port port) {
541 if (port.element() instanceof Device) {
542 Device d = (Device) port.element();
543 LinkDiscovery ld = discoverers.get(d.id());
544 if (ld != null) {
545 ld.removePort(port.number());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700546 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700547 } else {
548 log.warn("Attempted to remove non-Device port", port);
Jonathan Hart45066bc2015-07-28 11:18:34 -0700549 }
550 }
551
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700552 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700553 * Requests packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800554 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700555 private void requestIntercepts() {
556 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700557 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700558 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800559
Thomas Vachuska347cc872015-09-23 10:25:29 -0700560 selector.matchEthType(TYPE_BSN);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800561 if (useBddp) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700562 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
563 } else {
564 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800565 }
566 }
567
Thomas Vachuska27bee092015-06-23 19:03:10 -0700568 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700569 * Withdraws packet intercepts.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700570 */
571 private void withdrawIntercepts() {
572 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700573 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700574 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska347cc872015-09-23 10:25:29 -0700575 selector.matchEthType(TYPE_BSN);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700576 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
577 }
578
Naoki Shiota399a0b32015-11-15 20:36:13 -0600579 protected SuppressionRules rules() {
580 return rules;
581 }
582
583 protected void updateRules(SuppressionRules newRules) {
584 if (!rules.equals(newRules)) {
585 rules = newRules;
586 loadDevices();
587 }
588 }
589
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700590 /**
591 * Processes device mastership role changes.
592 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800593 private class InternalRoleListener implements MastershipListener {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800594 @Override
595 public void event(MastershipEvent event) {
Jordan Halterman9d982a52017-12-11 15:27:37 -0800596 if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800597 // only need new master events
Jon Hall7a8bfc62016-05-26 17:59:04 -0700598 eventExecutor.execute(() -> {
599 DeviceId deviceId = event.subject();
600 Device device = deviceService.getDevice(deviceId);
601 if (device == null) {
602 log.debug("Device {} doesn't exist, or isn't there yet", deviceId);
603 return;
604 }
Jordan Halterman9d982a52017-12-11 15:27:37 -0800605 updateDevice(device).ifPresent(ld -> updatePorts(ld, device.id()));
Jon Hall7a8bfc62016-05-26 17:59:04 -0700606 });
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800607 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800608 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800609 }
alshabib7911a052014-10-16 17:49:37 -0700610
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700611 private class DeviceEventProcessor implements Runnable {
612
613 DeviceEvent event;
614
615 DeviceEventProcessor(DeviceEvent event) {
616 this.event = event;
617 }
618
alshabib7911a052014-10-16 17:49:37 -0700619 @Override
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700620 public void run() {
alshabib7911a052014-10-16 17:49:37 -0700621 Device device = event.subject();
alshabibacd91832014-10-17 14:38:41 -0700622 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700623 if (device == null) {
624 log.error("Device is null.");
625 return;
626 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700627 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700628 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700629 switch (event.type()) {
630 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700631 case DEVICE_UPDATED:
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700632 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700633 break;
634 case PORT_ADDED:
635 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700636 if (port.isEnabled()) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700637 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
alshabib7911a052014-10-16 17:49:37 -0700638 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700639 log.debug("Port down {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700640 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600641 providerService.linksVanished(new ConnectPoint(port.element().id(),
642 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700643 }
644 break;
645 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700646 log.debug("Port removed {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700647 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600648 providerService.linksVanished(new ConnectPoint(port.element().id(),
649 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700650 break;
651 case DEVICE_REMOVED:
652 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700653 log.debug("Device removed {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700654 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600655 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700656 break;
657 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700658 if (deviceService.isAvailable(deviceId)) {
659 log.debug("Device up {}", deviceId);
alshabibe3af2652015-12-01 23:05:34 -0800660 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700661 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700662 log.debug("Device down {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700663 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600664 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700665 }
666 break;
Jonathan Hart9de692c2015-04-23 11:45:47 -0700667 case PORT_STATS_UPDATED:
668 break;
alshabib7911a052014-10-16 17:49:37 -0700669 default:
670 log.debug("Unknown event {}", event);
671 }
672 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700673 }
alshabib7911a052014-10-16 17:49:37 -0700674
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700675 /**
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700676 * Processes device events.
677 */
678 private class InternalDeviceListener implements DeviceListener {
679 @Override
680 public void event(DeviceEvent event) {
681 if (event.type() == Type.PORT_STATS_UPDATED) {
682 return;
683 }
684
685 Runnable deviceEventProcessor = new DeviceEventProcessor(event);
686
687 eventExecutor.execute(deviceEventProcessor);
688 }
689 }
690
691 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700692 * Processes incoming packets.
693 */
694 private class InternalPacketProcessor implements PacketProcessor {
alshabib7911a052014-10-16 17:49:37 -0700695 @Override
696 public void process(PacketContext context) {
Thomas Vachuska347cc872015-09-23 10:25:29 -0700697 if (context == null || context.isHandled()) {
alshabib4a179dc2014-10-17 17:17:01 -0700698 return;
699 }
Thomas Vachuska347cc872015-09-23 10:25:29 -0700700
701 Ethernet eth = context.inPacket().parsed();
702 if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) {
703 return;
704 }
705
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700706 LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId());
alshabib7911a052014-10-16 17:49:37 -0700707 if (ld == null) {
708 return;
709 }
710
Jonathan Hartb35540a2015-11-17 09:30:56 -0800711 if (ld.handleLldp(context)) {
alshabib7911a052014-10-16 17:49:37 -0700712 context.block();
713 }
714 }
715 }
716
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700717 /**
718 * Auxiliary task to keep device ports up to date.
719 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800720 private final class SyncDeviceInfoTask implements Runnable {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800721 @Override
722 public void run() {
723 if (Thread.currentThread().isInterrupted()) {
724 log.info("Interrupted, quitting");
725 return;
726 }
727 // check what deviceService sees, to see if we are missing anything
728 try {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700729 loadDevices();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800730 } catch (Exception e) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700731 // Catch all exceptions to avoid task being suppressed
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800732 log.error("Exception thrown during synchronization process", e);
733 }
734 }
735 }
736
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700737 /**
738 * Auxiliary task for pruning stale links.
739 */
740 private class LinkPrunerTask implements Runnable {
741 @Override
742 public void run() {
743 if (Thread.currentThread().isInterrupted()) {
744 log.info("Interrupted, quitting");
745 return;
746 }
747
748 try {
749 // TODO: There is still a slight possibility of mastership
750 // change occurring right with link going stale. This will
751 // result in the stale link not being pruned.
752 Maps.filterEntries(linkTimes, e -> {
753 if (!masterService.isLocalMaster(e.getKey().dst().deviceId())) {
754 return true;
755 }
756 if (isStale(e.getValue())) {
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000757 if (useStaleLinkAge) {
758 providerService.linkVanished(new DefaultLinkDescription(e.getKey().src(),
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700759 e.getKey().dst(),
760 DIRECT));
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000761 return true;
762 }
pierbd7ecf02019-08-27 10:55:02 -0700763 // if one of the device is not available - let's prune the link
764 if (!deviceService.isAvailable(e.getKey().src().deviceId()) ||
765 !deviceService.isAvailable(e.getKey().dst().deviceId())) {
766 return true;
767 }
768 // if one of the ports is not enable - let's prune the link
769 Port srcPort = deviceService.getPort(e.getKey().src());
770 Port dstPort = deviceService.getPort(e.getKey().dst());
771 if (!srcPort.isEnabled() || !dstPort.isEnabled()) {
772 return true;
773 }
774 log.trace("VanishStaleLinkAge feature is disabled, " +
775 "not bringing down link src {} dst {} with expired StaleLinkAge",
776 e.getKey().src(), e.getKey().dst());
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700777 }
778 return false;
779 }).clear();
780
781 } catch (Exception e) {
782 // Catch all exceptions to avoid task being suppressed
Ray Milkeye80e18f2016-06-02 16:44:14 -0700783 if (!shuttingDown) {
784 // Error condition
785 log.error("Exception thrown during link pruning process", e);
786 } else {
787 // Provider is shutting down, the error can be ignored
788 log.trace("Shutting down, ignoring error", e);
789 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700790 }
791 }
792
793 private boolean isStale(long lastSeen) {
794 return lastSeen < System.currentTimeMillis() - staleLinkAge;
795 }
796 }
797
798 /**
799 * Provides processing context for the device link discovery helpers.
800 */
Ray Milkey957390e2016-02-09 10:02:46 -0800801 private class InternalDiscoveryContext implements LinkDiscoveryContext {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700802 @Override
803 public MastershipService mastershipService() {
804 return masterService;
805 }
806
807 @Override
808 public LinkProviderService providerService() {
809 return providerService;
810 }
811
812 @Override
813 public PacketService packetService() {
814 return packetService;
815 }
816
817 @Override
818 public long probeRate() {
819 return probeRate;
820 }
821
822 @Override
Jonathan Hartb35540a2015-11-17 09:30:56 -0800823 public boolean useBddp() {
824 return useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700825 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700826
827 @Override
828 public void touchLink(LinkKey key) {
829 linkTimes.put(key, System.currentTimeMillis());
830 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800831
832 @Override
DongRyeol Chace65cc02018-07-23 15:02:28 +0900833 public void setTtl(LinkKey key, short ttl) {
834 linkTimes.put(key, System.currentTimeMillis() - staleLinkAge + SECONDS.toMillis(ttl));
835 }
836
837 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700838 public DeviceService deviceService() {
839 return deviceService;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800840 }
841
842 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700843 public String fingerprint() {
844 return buildSrcMac();
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800845 }
Samuel Jero31e16f52018-09-21 10:34:28 -0400846
847 @Override
848 public String lldpSecret() {
pier7aa3a2d2020-01-29 16:19:22 +0100849 return clusterMetadata.get() != null ?
850 clusterMetadata.get().getClusterSecret() : null;
Samuel Jero31e16f52018-09-21 10:34:28 -0400851 }
852
853 @Override
854 public long maxDiscoveryDelay() {
855 return maxDiscoveryDelayMs;
856 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700857 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700858
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800859 static final EnumSet<NetworkConfigEvent.Type> CONFIG_CHANGED
860 = EnumSet.of(NetworkConfigEvent.Type.CONFIG_ADDED,
861 NetworkConfigEvent.Type.CONFIG_UPDATED,
862 NetworkConfigEvent.Type.CONFIG_REMOVED);
863
Naoki Shiota399a0b32015-11-15 20:36:13 -0600864 private class InternalConfigListener implements NetworkConfigListener {
865
866 private synchronized void reconfigureSuppressionRules(SuppressionConfig cfg) {
867 if (cfg == null) {
Ray Milkey0a8ee912016-06-13 09:58:12 -0700868 log.debug("Suppression Config is null.");
Naoki Shiota399a0b32015-11-15 20:36:13 -0600869 return;
870 }
871
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800872 SuppressionRules newRules = new SuppressionRules(cfg.deviceTypes(),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600873 cfg.annotation());
874
875 updateRules(newRules);
876 }
877
Jon Hall125c3f22017-08-09 13:46:28 -0700878 private boolean isRelevantDeviceEvent(NetworkConfigEvent event) {
879 return event.configClass() == LinkDiscoveryFromDevice.class &&
880 CONFIG_CHANGED.contains(event.type());
881 }
882
883 private boolean isRelevantPortEvent(NetworkConfigEvent event) {
884 return event.configClass() == LinkDiscoveryFromPort.class &&
885 CONFIG_CHANGED.contains(event.type());
886 }
887
888 private boolean isRelevantSuppressionEvent(NetworkConfigEvent event) {
889 return (event.configClass().equals(SuppressionConfig.class) &&
890 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
891 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED));
892 }
893
Naoki Shiota399a0b32015-11-15 20:36:13 -0600894 @Override
895 public void event(NetworkConfigEvent event) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700896 eventExecutor.execute(() -> {
Jon Hall125c3f22017-08-09 13:46:28 -0700897 if (isRelevantDeviceEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800898 if (event.subject() instanceof DeviceId) {
899 final DeviceId did = (DeviceId) event.subject();
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800900 Device device = deviceService.getDevice(did);
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800901 updateDevice(device).ifPresent(ld -> updatePorts(ld, did));
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800902 }
Jon Hall125c3f22017-08-09 13:46:28 -0700903 } else if (isRelevantPortEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800904 if (event.subject() instanceof ConnectPoint) {
905 ConnectPoint cp = (ConnectPoint) event.subject();
906 if (cp.elementId() instanceof DeviceId) {
907 final DeviceId did = (DeviceId) cp.elementId();
908 Device device = deviceService.getDevice(did);
909 Port port = deviceService.getPort(did, cp.port());
910 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
911 }
912 }
Jon Hall125c3f22017-08-09 13:46:28 -0700913 } else if (isRelevantSuppressionEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800914 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
915 reconfigureSuppressionRules(cfg);
916 log.trace("Network config reconfigured");
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800917 }
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800918 });
Naoki Shiota399a0b32015-11-15 20:36:13 -0600919 }
920 }
pier7aa3a2d2020-01-29 16:19:22 +0100921
922 private class InternalClusterMetadataListener implements ClusterMetadataEventListener {
923 @Override
924 public void event(ClusterMetadataEvent event) {
925 clusterMetadata.set(event.subject());
926 }
927 }
alshabib7911a052014-10-16 17:49:37 -0700928}