blob: 29149653cc32a82087a0fbb66de0b5bcd576783a [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,
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700103 })
Ayaka Koshibe48229222016-05-16 18:04:26 -0700104public class LldpLinkProvider extends AbstractProvider implements ProbedLinkProvider {
alshabib7911a052014-10-16 17:49:37 -0700105
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700106 private static final String PROVIDER_NAME = "org.onosproject.provider.lldp";
107
Thomas Vachuska05453c92015-09-09 14:40:49 -0700108 private static final String FORMAT =
109 "Settings: enabled={}, useBDDP={}, probeRate={}, " +
Samuel Jero31e16f52018-09-21 10:34:28 -0400110 "staleLinkAge={}, maxLLDPage={}";
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800111
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700112 // When a Device/Port has this annotation, do not send out LLDP/BDDP
113 public static final String NO_LLDP = "no-lldp";
114
alshabib7911a052014-10-16 17:49:37 -0700115 private final Logger log = getLogger(getClass());
116
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800118 protected CoreService coreService;
119
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700120 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib7911a052014-10-16 17:49:37 -0700121 protected LinkProviderRegistry providerRegistry;
122
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700123 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib7911a052014-10-16 17:49:37 -0700124 protected DeviceService deviceService;
125
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700126 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700127 protected LinkService linkService;
128
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800130 protected PacketService packetService;
alshabib7911a052014-10-16 17:49:37 -0700131
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib875d6262014-10-17 16:19:40 -0700133 protected MastershipService masterService;
134
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700136 protected ComponentConfigService cfgService;
137
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700139 protected ClusterService clusterService;
140
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700142 protected NetworkConfigRegistry cfgRegistry;
143
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800145 protected ClusterMetadataService clusterMetadataService;
146
alshabib7911a052014-10-16 17:49:37 -0700147 private LinkProviderService providerService;
148
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800149 private ScheduledExecutorService executor;
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700150 protected ExecutorService eventExecutor;
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800151
Ray Milkeye80e18f2016-06-02 16:44:14 -0700152 private boolean shuttingDown = false;
153
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700154 // TODO: Add sanity checking for the configurable params based on the delays
155 private static final long DEVICE_SYNC_DELAY = 5;
156 private static final long LINK_PRUNER_DELAY = 3;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700157
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700158 //@Property(name = PROP_ENABLED, boolValue = true,
159 // label = "If false, link discovery is disabled")
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700160 /** If false, link discovery is disabled. */
Ray Milkey7e7bd862018-10-18 08:53:34 -0700161 protected boolean enabled = false;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700162
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700163 //@Property(name = PROP_USE_BDDP, boolValue = true,
164 // label = "Use BDDP for link discovery")
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700165 /** Use BDDP for link discovery. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700166 protected boolean useBddp = USE_BDDP_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700167
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700168 //@Property(name = PROP_PROBE_RATE, intValue = DEFAULT_PROBE_RATE,
169 // label = "LLDP and BDDP probe rate specified in millis")
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700170 /** LLDP and BDDP probe rate specified in millis. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700171 protected int probeRate = PROBE_RATE_DEFAULT;
Saurav Dasc313c402015-02-27 10:09:47 -0800172
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700173 //@Property(name = PROP_STALE_LINK_AGE, intValue = DEFAULT_STALE_LINK_AGE,
174 // label = "Number of millis beyond which links will be considered stale")
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700175 /** Number of millis beyond which links will be considered stale. */
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700176 protected int staleLinkAge = STALE_LINK_AGE_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700177
Ray Milkeyd17309c2018-10-18 09:34:54 -0700178 //@Property(name = PROP_DISCOVERY_DELAY, intValue = DEFAULT_DISCOVERY_DELAY,
179 // label = "Number of millis beyond which an LLDP packet will not be accepted")
180 private int maxDiscoveryDelayMs = DISCOVERY_DELAY_DEFAULT;
Samuel Jero31e16f52018-09-21 10:34:28 -0400181
Ray Milkey957390e2016-02-09 10:02:46 -0800182 private final LinkDiscoveryContext context = new InternalDiscoveryContext();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800183 private final InternalRoleListener roleListener = new InternalRoleListener();
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700184 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
185 private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800186
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700187 // Device link discovery helpers.
alshabib7911a052014-10-16 17:49:37 -0700188 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
189
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700190 // Most recent time a tracked link was seen; links are tracked if their
191 // destination connection point is mastered by this controller instance.
192 private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap();
193
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800194 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800195
Naoki Shiota399a0b32015-11-15 20:36:13 -0600196 static final SuppressionRules DEFAULT_RULES
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800197 = new SuppressionRules(EnumSet.of(Device.Type.ROADM,
198 Device.Type.FIBER_SWITCH,
199 Device.Type.OPTICAL_AMPLIFIER,
200 Device.Type.OTN),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600201 ImmutableMap.of(NO_LLDP, SuppressionRules.ANY_VALUE));
202
203 private SuppressionRules rules = LldpLinkProvider.DEFAULT_RULES;
204
205 public static final String CONFIG_KEY = "suppression";
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800206 public static final String FEATURE_NAME = "linkDiscovery";
Naoki Shiota399a0b32015-11-15 20:36:13 -0600207
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800208 private final Set<ConfigFactory<?, ?>> factories = ImmutableSet.of(
Naoki Shiota399a0b32015-11-15 20:36:13 -0600209 new ConfigFactory<ApplicationId, SuppressionConfig>(APP_SUBJECT_FACTORY,
210 SuppressionConfig.class,
211 CONFIG_KEY) {
212 @Override
213 public SuppressionConfig createConfig() {
214 return new SuppressionConfig();
215 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800216 },
217 new ConfigFactory<DeviceId, LinkDiscoveryFromDevice>(DEVICE_SUBJECT_FACTORY,
218 LinkDiscoveryFromDevice.class, FEATURE_NAME) {
219 @Override
220 public LinkDiscoveryFromDevice createConfig() {
221 return new LinkDiscoveryFromDevice();
222 }
223 },
224 new ConfigFactory<ConnectPoint, LinkDiscoveryFromPort>(CONNECT_POINT_SUBJECT_FACTORY,
225 LinkDiscoveryFromPort.class, FEATURE_NAME) {
226 @Override
227 public LinkDiscoveryFromPort createConfig() {
228 return new LinkDiscoveryFromPort();
229 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600230 }
231 );
232
233 private final InternalConfigListener cfgListener = new InternalConfigListener();
234
alshabib7911a052014-10-16 17:49:37 -0700235 /**
236 * Creates an OpenFlow link provider.
237 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800238 public LldpLinkProvider() {
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700239 super(new ProviderId("lldp", PROVIDER_NAME));
alshabib7911a052014-10-16 17:49:37 -0700240 }
241
Sho SHIMIZU9efeb812016-08-18 09:29:20 -0700242 private String buildSrcMac() {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700243 String defMac = ProbedLinkProvider.defaultMac();
Ray Milkey55f513f2017-12-01 15:58:21 -0800244 if (clusterMetadataService == null) {
245 log.debug("No cluster metadata service is available. Using default value {}", defMac);
246 return defMac;
247 }
248
249 String srcMac = ProbedLinkProvider.fingerprintMac(clusterMetadataService.getClusterMetadata());
Ayaka Koshibe48229222016-05-16 18:04:26 -0700250 if (srcMac.equals(defMac)) {
251 log.warn("Couldn't generate fingerprint. Using default value {}", defMac);
252 return defMac;
253 }
254 log.trace("Generated MAC address {}", srcMac);
255 return srcMac;
256 }
257
alshabib7911a052014-10-16 17:49:37 -0700258 @Activate
Saurav Dasc313c402015-02-27 10:09:47 -0800259 public void activate(ComponentContext context) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700260 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/linkevents", "events-%d", log));
Ray Milkeye80e18f2016-06-02 16:44:14 -0700261 shuttingDown = false;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700262 cfgService.registerProperties(getClass());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700263 appId = coreService.registerApplication(PROVIDER_NAME);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600264
265 cfgRegistry.addListener(cfgListener);
266 factories.forEach(cfgRegistry::registerConfigFactory);
267
Madan Jampanic6371882016-06-03 21:30:17 -0700268 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600269 if (cfg == null) {
270 // If no configuration is found, register default.
Madan Jampanic6371882016-06-03 21:30:17 -0700271 cfg = this.setDefaultSuppressionConfig();
Naoki Shiota399a0b32015-11-15 20:36:13 -0600272 }
273 cfgListener.reconfigureSuppressionRules(cfg);
274
Saurav Dasc313c402015-02-27 10:09:47 -0800275 modified(context);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700276 log.info("Started");
277 }
278
Thomas Vachuskaaad8b1d2015-12-11 10:36:53 -0800279 private SuppressionConfig setDefaultSuppressionConfig() {
280 SuppressionConfig cfg = cfgRegistry.addConfig(appId, SuppressionConfig.class);
281 cfg.deviceTypes(DEFAULT_RULES.getSuppressedDeviceType())
282 .annotation(DEFAULT_RULES.getSuppressedAnnotation())
283 .apply();
284 return cfg;
285 }
286
Thomas Vachuska05453c92015-09-09 14:40:49 -0700287 @Deactivate
288 public void deactivate() {
Ray Milkeye80e18f2016-06-02 16:44:14 -0700289 shuttingDown = true;
Naoki Shiota399a0b32015-11-15 20:36:13 -0600290 cfgRegistry.removeListener(cfgListener);
291 factories.forEach(cfgRegistry::unregisterConfigFactory);
292
Thomas Vachuska05453c92015-09-09 14:40:49 -0700293 cfgService.unregisterProperties(getClass(), false);
294 disable();
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700295 eventExecutor.shutdownNow();
296 eventExecutor = null;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700297 log.info("Stopped");
298 }
299
300 @Modified
301 public void modified(ComponentContext context) {
302 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
303
304 boolean newEnabled, newUseBddp;
Samuel Jero31e16f52018-09-21 10:34:28 -0400305 int newProbeRate, newStaleLinkAge, newDiscoveryDelay;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700306 try {
307 String s = get(properties, PROP_ENABLED);
308 newEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
309
310 s = get(properties, PROP_USE_BDDP);
311 newUseBddp = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
312
313 s = get(properties, PROP_PROBE_RATE);
314 newProbeRate = isNullOrEmpty(s) ? probeRate : Integer.parseInt(s.trim());
315
316 s = get(properties, PROP_STALE_LINK_AGE);
317 newStaleLinkAge = isNullOrEmpty(s) ? staleLinkAge : Integer.parseInt(s.trim());
318
Samuel Jero31e16f52018-09-21 10:34:28 -0400319 s = get(properties, PROP_DISCOVERY_DELAY);
320 newDiscoveryDelay = isNullOrEmpty(s) ? maxDiscoveryDelayMs : Integer.parseInt(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;
Saurav Dasc313c402015-02-27 10:09:47 -0800329 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800330
Thomas Vachuska05453c92015-09-09 14:40:49 -0700331 boolean wasEnabled = enabled;
332
333 enabled = newEnabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800334 useBddp = newUseBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700335 probeRate = newProbeRate;
336 staleLinkAge = newStaleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400337 maxDiscoveryDelayMs = newDiscoveryDelay;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700338
339 if (!wasEnabled && enabled) {
340 enable();
341 } else if (wasEnabled && !enabled) {
342 disable();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700343 } else {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700344 if (enabled) {
345 // update all discovery helper state
346 loadDevices();
347 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700348 }
349
Samuel Jero31e16f52018-09-21 10:34:28 -0400350 log.info(FORMAT, enabled, useBddp, probeRate, staleLinkAge, maxDiscoveryDelayMs);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700351 }
352
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700353 /**
354 * Enables link discovery processing.
355 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700356 private void enable() {
alshabib7911a052014-10-16 17:49:37 -0700357 providerService = providerRegistry.register(this);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800358 masterService.addListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700359 deviceService.addListener(deviceListener);
360 packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800361
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700362 loadDevices();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700363
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700364 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/link", "discovery-%d", log));
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700365 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(),
366 DEVICE_SYNC_DELAY, DEVICE_SYNC_DELAY, SECONDS);
367 executor.scheduleAtFixedRate(new LinkPrunerTask(),
368 LINK_PRUNER_DELAY, LINK_PRUNER_DELAY, SECONDS);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700369
Thomas Vachuska05453c92015-09-09 14:40:49 -0700370 requestIntercepts();
371 }
372
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700373 /**
374 * Disables link discovery processing.
375 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700376 private void disable() {
377 withdrawIntercepts();
378
379 providerRegistry.unregister(this);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700380 masterService.removeListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700381 deviceService.removeListener(deviceListener);
382 packetService.removeProcessor(packetProcessor);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700383
384 if (executor != null) {
385 executor.shutdownNow();
386 }
387 discoverers.values().forEach(LinkDiscovery::stop);
388 discoverers.clear();
Jon Hall125c3f22017-08-09 13:46:28 -0700389 linkTimes.clear();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700390
391 providerService = null;
392 }
393
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700394 /**
395 * Loads available devices and registers their ports to be probed.
396 */
397 private void loadDevices() {
Ray Milkey0f87d482016-07-06 11:49:19 -0700398 if (!enabled || deviceService == null) {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600399 return;
400 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700401 deviceService.getAvailableDevices()
402 .forEach(d -> updateDevice(d)
403 .ifPresent(ld -> updatePorts(ld, d.id())));
404 }
405
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800406 private boolean isBlacklisted(DeviceId did) {
407 LinkDiscoveryFromDevice cfg = cfgRegistry.getConfig(did, LinkDiscoveryFromDevice.class);
408 if (cfg == null) {
409 return false;
410 }
411 return !cfg.enabled();
412 }
413
414 private boolean isBlacklisted(ConnectPoint cp) {
415 // if parent device is blacklisted, so is the port
416 if (isBlacklisted(cp.deviceId())) {
417 return true;
418 }
419 LinkDiscoveryFromPort cfg = cfgRegistry.getConfig(cp, LinkDiscoveryFromPort.class);
420 if (cfg == null) {
421 return false;
422 }
423 return !cfg.enabled();
424 }
425
426 private boolean isBlacklisted(Port port) {
427 return isBlacklisted(new ConnectPoint(port.element().id(), port.number()));
428 }
429
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700430 /**
431 * Updates discovery helper for specified device.
432 *
433 * Adds and starts a discovery helper for specified device if enabled,
434 * calls {@link #removeDevice(DeviceId)} otherwise.
435 *
436 * @param device device to add
437 * @return discovery helper if discovery is enabled for the device
438 */
439 private Optional<LinkDiscovery> updateDevice(Device device) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800440 if (device == null) {
441 return Optional.empty();
442 }
Jon Hall125c3f22017-08-09 13:46:28 -0700443 if (!masterService.isLocalMaster(device.id())) {
444 // Reset the last seen time for all links to this device
445 // then stop discovery for this device
446 List<LinkKey> updateLinks = new LinkedList<>();
447 linkTimes.forEach((link, time) -> {
448 if (link.dst().deviceId().equals(device.id())) {
449 updateLinks.add(link);
450 }
451 });
452 updateLinks.forEach(link -> linkTimes.remove(link));
453 removeDevice(device.id());
454 return Optional.empty();
455 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800456 if (rules.isSuppressed(device) || isBlacklisted(device.id())) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700457 log.trace("LinkDiscovery from {} disabled by configuration", device.id());
458 removeDevice(device.id());
459 return Optional.empty();
460 }
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800461
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700462 LinkDiscovery ld = discoverers.computeIfAbsent(device.id(),
463 did -> new LinkDiscovery(device, context));
464 if (ld.isStopped()) {
465 ld.start();
466 }
467 return Optional.of(ld);
468 }
469
470 /**
471 * Removes after stopping discovery helper for specified device.
472 * @param deviceId device to remove
473 */
474 private void removeDevice(final DeviceId deviceId) {
475 discoverers.computeIfPresent(deviceId, (did, ld) -> {
476 ld.stop();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700477 return null;
478 });
479
480 }
481
482 /**
483 * Updates ports of the specified device to the specified discovery helper.
484 */
485 private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) {
486 deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p));
487 }
488
489 /**
490 * Updates discovery helper state of the specified port.
491 *
492 * Adds a port to the discovery helper if up and discovery is enabled,
493 * or calls {@link #removePort(Port)} otherwise.
494 */
495 private void updatePort(LinkDiscovery discoverer, Port port) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800496 if (port == null) {
497 return;
498 }
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800499 if (port.number().isLogical()) {
500 // silently ignore logical ports
501 return;
502 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600503
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800504 if (rules.isSuppressed(port) || isBlacklisted(port)) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700505 log.trace("LinkDiscovery from {} disabled by configuration", port);
506 removePort(port);
507 return;
508 }
509
510 // check if enabled and turn off discovery?
511 if (!port.isEnabled()) {
512 removePort(port);
513 return;
514 }
515
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800516 discoverer.addPort(port);
alshabib7911a052014-10-16 17:49:37 -0700517 }
518
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700519 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700520 * Removes a port from the specified discovery helper.
521 * @param port the port
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700522 */
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700523 private void removePort(Port port) {
524 if (port.element() instanceof Device) {
525 Device d = (Device) port.element();
526 LinkDiscovery ld = discoverers.get(d.id());
527 if (ld != null) {
528 ld.removePort(port.number());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700529 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700530 } else {
531 log.warn("Attempted to remove non-Device port", port);
Jonathan Hart45066bc2015-07-28 11:18:34 -0700532 }
533 }
534
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700535 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700536 * Requests packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800537 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700538 private void requestIntercepts() {
539 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700540 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700541 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800542
Thomas Vachuska347cc872015-09-23 10:25:29 -0700543 selector.matchEthType(TYPE_BSN);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800544 if (useBddp) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700545 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
546 } else {
547 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800548 }
549 }
550
Thomas Vachuska27bee092015-06-23 19:03:10 -0700551 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700552 * Withdraws packet intercepts.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700553 */
554 private void withdrawIntercepts() {
555 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700556 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700557 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska347cc872015-09-23 10:25:29 -0700558 selector.matchEthType(TYPE_BSN);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700559 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
560 }
561
Naoki Shiota399a0b32015-11-15 20:36:13 -0600562 protected SuppressionRules rules() {
563 return rules;
564 }
565
566 protected void updateRules(SuppressionRules newRules) {
567 if (!rules.equals(newRules)) {
568 rules = newRules;
569 loadDevices();
570 }
571 }
572
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700573 /**
574 * Processes device mastership role changes.
575 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800576 private class InternalRoleListener implements MastershipListener {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800577 @Override
578 public void event(MastershipEvent event) {
Jordan Halterman9d982a52017-12-11 15:27:37 -0800579 if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800580 // only need new master events
Jon Hall7a8bfc62016-05-26 17:59:04 -0700581 eventExecutor.execute(() -> {
582 DeviceId deviceId = event.subject();
583 Device device = deviceService.getDevice(deviceId);
584 if (device == null) {
585 log.debug("Device {} doesn't exist, or isn't there yet", deviceId);
586 return;
587 }
Jordan Halterman9d982a52017-12-11 15:27:37 -0800588 updateDevice(device).ifPresent(ld -> updatePorts(ld, device.id()));
Jon Hall7a8bfc62016-05-26 17:59:04 -0700589 });
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800590 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800591 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800592 }
alshabib7911a052014-10-16 17:49:37 -0700593
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700594 private class DeviceEventProcessor implements Runnable {
595
596 DeviceEvent event;
597
598 DeviceEventProcessor(DeviceEvent event) {
599 this.event = event;
600 }
601
alshabib7911a052014-10-16 17:49:37 -0700602 @Override
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700603 public void run() {
alshabib7911a052014-10-16 17:49:37 -0700604 Device device = event.subject();
alshabibacd91832014-10-17 14:38:41 -0700605 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700606 if (device == null) {
607 log.error("Device is null.");
608 return;
609 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700610 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700611 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700612 switch (event.type()) {
613 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700614 case DEVICE_UPDATED:
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700615 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700616 break;
617 case PORT_ADDED:
618 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700619 if (port.isEnabled()) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700620 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
alshabib7911a052014-10-16 17:49:37 -0700621 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700622 log.debug("Port down {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700623 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600624 providerService.linksVanished(new ConnectPoint(port.element().id(),
625 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700626 }
627 break;
628 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700629 log.debug("Port removed {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700630 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600631 providerService.linksVanished(new ConnectPoint(port.element().id(),
632 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700633 break;
634 case DEVICE_REMOVED:
635 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700636 log.debug("Device removed {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700637 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600638 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700639 break;
640 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700641 if (deviceService.isAvailable(deviceId)) {
642 log.debug("Device up {}", deviceId);
alshabibe3af2652015-12-01 23:05:34 -0800643 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700644 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700645 log.debug("Device down {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700646 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600647 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700648 }
649 break;
Jonathan Hart9de692c2015-04-23 11:45:47 -0700650 case PORT_STATS_UPDATED:
651 break;
alshabib7911a052014-10-16 17:49:37 -0700652 default:
653 log.debug("Unknown event {}", event);
654 }
655 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700656 }
alshabib7911a052014-10-16 17:49:37 -0700657
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700658 /**
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700659 * Processes device events.
660 */
661 private class InternalDeviceListener implements DeviceListener {
662 @Override
663 public void event(DeviceEvent event) {
664 if (event.type() == Type.PORT_STATS_UPDATED) {
665 return;
666 }
667
668 Runnable deviceEventProcessor = new DeviceEventProcessor(event);
669
670 eventExecutor.execute(deviceEventProcessor);
671 }
672 }
673
674 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700675 * Processes incoming packets.
676 */
677 private class InternalPacketProcessor implements PacketProcessor {
alshabib7911a052014-10-16 17:49:37 -0700678 @Override
679 public void process(PacketContext context) {
Thomas Vachuska347cc872015-09-23 10:25:29 -0700680 if (context == null || context.isHandled()) {
alshabib4a179dc2014-10-17 17:17:01 -0700681 return;
682 }
Thomas Vachuska347cc872015-09-23 10:25:29 -0700683
684 Ethernet eth = context.inPacket().parsed();
685 if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) {
686 return;
687 }
688
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700689 LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId());
alshabib7911a052014-10-16 17:49:37 -0700690 if (ld == null) {
691 return;
692 }
693
Jonathan Hartb35540a2015-11-17 09:30:56 -0800694 if (ld.handleLldp(context)) {
alshabib7911a052014-10-16 17:49:37 -0700695 context.block();
696 }
697 }
698 }
699
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700700 /**
701 * Auxiliary task to keep device ports up to date.
702 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800703 private final class SyncDeviceInfoTask implements Runnable {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800704 @Override
705 public void run() {
706 if (Thread.currentThread().isInterrupted()) {
707 log.info("Interrupted, quitting");
708 return;
709 }
710 // check what deviceService sees, to see if we are missing anything
711 try {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700712 loadDevices();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800713 } catch (Exception e) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700714 // Catch all exceptions to avoid task being suppressed
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800715 log.error("Exception thrown during synchronization process", e);
716 }
717 }
718 }
719
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700720 /**
721 * Auxiliary task for pruning stale links.
722 */
723 private class LinkPrunerTask implements Runnable {
724 @Override
725 public void run() {
726 if (Thread.currentThread().isInterrupted()) {
727 log.info("Interrupted, quitting");
728 return;
729 }
730
731 try {
732 // TODO: There is still a slight possibility of mastership
733 // change occurring right with link going stale. This will
734 // result in the stale link not being pruned.
735 Maps.filterEntries(linkTimes, e -> {
736 if (!masterService.isLocalMaster(e.getKey().dst().deviceId())) {
737 return true;
738 }
739 if (isStale(e.getValue())) {
740 providerService.linkVanished(new DefaultLinkDescription(e.getKey().src(),
741 e.getKey().dst(),
742 DIRECT));
743 return true;
744 }
745 return false;
746 }).clear();
747
748 } catch (Exception e) {
749 // Catch all exceptions to avoid task being suppressed
Ray Milkeye80e18f2016-06-02 16:44:14 -0700750 if (!shuttingDown) {
751 // Error condition
752 log.error("Exception thrown during link pruning process", e);
753 } else {
754 // Provider is shutting down, the error can be ignored
755 log.trace("Shutting down, ignoring error", e);
756 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700757 }
758 }
759
760 private boolean isStale(long lastSeen) {
761 return lastSeen < System.currentTimeMillis() - staleLinkAge;
762 }
763 }
764
765 /**
766 * Provides processing context for the device link discovery helpers.
767 */
Ray Milkey957390e2016-02-09 10:02:46 -0800768 private class InternalDiscoveryContext implements LinkDiscoveryContext {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700769 @Override
770 public MastershipService mastershipService() {
771 return masterService;
772 }
773
774 @Override
775 public LinkProviderService providerService() {
776 return providerService;
777 }
778
779 @Override
780 public PacketService packetService() {
781 return packetService;
782 }
783
784 @Override
785 public long probeRate() {
786 return probeRate;
787 }
788
789 @Override
Jonathan Hartb35540a2015-11-17 09:30:56 -0800790 public boolean useBddp() {
791 return useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700792 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700793
794 @Override
795 public void touchLink(LinkKey key) {
796 linkTimes.put(key, System.currentTimeMillis());
797 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800798
799 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700800 public DeviceService deviceService() {
801 return deviceService;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800802 }
803
804 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700805 public String fingerprint() {
806 return buildSrcMac();
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800807 }
Samuel Jero31e16f52018-09-21 10:34:28 -0400808
809 @Override
810 public String lldpSecret() {
811 return clusterMetadataService.getClusterMetadata().getClusterSecret();
812 }
813
814 @Override
815 public long maxDiscoveryDelay() {
816 return maxDiscoveryDelayMs;
817 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700818 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700819
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800820 static final EnumSet<NetworkConfigEvent.Type> CONFIG_CHANGED
821 = EnumSet.of(NetworkConfigEvent.Type.CONFIG_ADDED,
822 NetworkConfigEvent.Type.CONFIG_UPDATED,
823 NetworkConfigEvent.Type.CONFIG_REMOVED);
824
Naoki Shiota399a0b32015-11-15 20:36:13 -0600825 private class InternalConfigListener implements NetworkConfigListener {
826
827 private synchronized void reconfigureSuppressionRules(SuppressionConfig cfg) {
828 if (cfg == null) {
Ray Milkey0a8ee912016-06-13 09:58:12 -0700829 log.debug("Suppression Config is null.");
Naoki Shiota399a0b32015-11-15 20:36:13 -0600830 return;
831 }
832
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800833 SuppressionRules newRules = new SuppressionRules(cfg.deviceTypes(),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600834 cfg.annotation());
835
836 updateRules(newRules);
837 }
838
Jon Hall125c3f22017-08-09 13:46:28 -0700839 private boolean isRelevantDeviceEvent(NetworkConfigEvent event) {
840 return event.configClass() == LinkDiscoveryFromDevice.class &&
841 CONFIG_CHANGED.contains(event.type());
842 }
843
844 private boolean isRelevantPortEvent(NetworkConfigEvent event) {
845 return event.configClass() == LinkDiscoveryFromPort.class &&
846 CONFIG_CHANGED.contains(event.type());
847 }
848
849 private boolean isRelevantSuppressionEvent(NetworkConfigEvent event) {
850 return (event.configClass().equals(SuppressionConfig.class) &&
851 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
852 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED));
853 }
854
Naoki Shiota399a0b32015-11-15 20:36:13 -0600855 @Override
856 public void event(NetworkConfigEvent event) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700857 eventExecutor.execute(() -> {
Jon Hall125c3f22017-08-09 13:46:28 -0700858 if (isRelevantDeviceEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800859 if (event.subject() instanceof DeviceId) {
860 final DeviceId did = (DeviceId) event.subject();
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800861 Device device = deviceService.getDevice(did);
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800862 updateDevice(device).ifPresent(ld -> updatePorts(ld, did));
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800863 }
Jon Hall125c3f22017-08-09 13:46:28 -0700864 } else if (isRelevantPortEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800865 if (event.subject() instanceof ConnectPoint) {
866 ConnectPoint cp = (ConnectPoint) event.subject();
867 if (cp.elementId() instanceof DeviceId) {
868 final DeviceId did = (DeviceId) cp.elementId();
869 Device device = deviceService.getDevice(did);
870 Port port = deviceService.getPort(did, cp.port());
871 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
872 }
873 }
Jon Hall125c3f22017-08-09 13:46:28 -0700874 } else if (isRelevantSuppressionEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800875 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
876 reconfigureSuppressionRules(cfg);
877 log.trace("Network config reconfigured");
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800878 }
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800879 });
Naoki Shiota399a0b32015-11-15 20:36:13 -0600880 }
881 }
alshabib7911a052014-10-16 17:49:37 -0700882}