blob: 963bac6021fabd1b2eee5ec3c5ea28ea841e9c12 [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")
Ray Milkey7e7bd862018-10-18 08:53:34 -0700160 protected boolean enabled = false;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700161
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700162 //@Property(name = PROP_USE_BDDP, boolValue = true,
163 // label = "Use BDDP for link discovery")
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700164 protected boolean useBddp = USE_BDDP_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700165
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700166 //@Property(name = PROP_PROBE_RATE, intValue = DEFAULT_PROBE_RATE,
167 // label = "LLDP and BDDP probe rate specified in millis")
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700168 protected int probeRate = PROBE_RATE_DEFAULT;
Saurav Dasc313c402015-02-27 10:09:47 -0800169
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700170 //@Property(name = PROP_STALE_LINK_AGE, intValue = DEFAULT_STALE_LINK_AGE,
171 // label = "Number of millis beyond which links will be considered stale")
Thomas Vachuska1c23d042018-10-17 10:05:19 -0700172 protected int staleLinkAge = STALE_LINK_AGE_DEFAULT;
alshabib7911a052014-10-16 17:49:37 -0700173
Ray Milkeyd17309c2018-10-18 09:34:54 -0700174 //@Property(name = PROP_DISCOVERY_DELAY, intValue = DEFAULT_DISCOVERY_DELAY,
175 // label = "Number of millis beyond which an LLDP packet will not be accepted")
176 private int maxDiscoveryDelayMs = DISCOVERY_DELAY_DEFAULT;
Samuel Jero31e16f52018-09-21 10:34:28 -0400177
Ray Milkey957390e2016-02-09 10:02:46 -0800178 private final LinkDiscoveryContext context = new InternalDiscoveryContext();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800179 private final InternalRoleListener roleListener = new InternalRoleListener();
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700180 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
181 private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800182
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700183 // Device link discovery helpers.
alshabib7911a052014-10-16 17:49:37 -0700184 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
185
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700186 // Most recent time a tracked link was seen; links are tracked if their
187 // destination connection point is mastered by this controller instance.
188 private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap();
189
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800190 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800191
Naoki Shiota399a0b32015-11-15 20:36:13 -0600192 static final SuppressionRules DEFAULT_RULES
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800193 = new SuppressionRules(EnumSet.of(Device.Type.ROADM,
194 Device.Type.FIBER_SWITCH,
195 Device.Type.OPTICAL_AMPLIFIER,
196 Device.Type.OTN),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600197 ImmutableMap.of(NO_LLDP, SuppressionRules.ANY_VALUE));
198
199 private SuppressionRules rules = LldpLinkProvider.DEFAULT_RULES;
200
201 public static final String CONFIG_KEY = "suppression";
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800202 public static final String FEATURE_NAME = "linkDiscovery";
Naoki Shiota399a0b32015-11-15 20:36:13 -0600203
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800204 private final Set<ConfigFactory<?, ?>> factories = ImmutableSet.of(
Naoki Shiota399a0b32015-11-15 20:36:13 -0600205 new ConfigFactory<ApplicationId, SuppressionConfig>(APP_SUBJECT_FACTORY,
206 SuppressionConfig.class,
207 CONFIG_KEY) {
208 @Override
209 public SuppressionConfig createConfig() {
210 return new SuppressionConfig();
211 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800212 },
213 new ConfigFactory<DeviceId, LinkDiscoveryFromDevice>(DEVICE_SUBJECT_FACTORY,
214 LinkDiscoveryFromDevice.class, FEATURE_NAME) {
215 @Override
216 public LinkDiscoveryFromDevice createConfig() {
217 return new LinkDiscoveryFromDevice();
218 }
219 },
220 new ConfigFactory<ConnectPoint, LinkDiscoveryFromPort>(CONNECT_POINT_SUBJECT_FACTORY,
221 LinkDiscoveryFromPort.class, FEATURE_NAME) {
222 @Override
223 public LinkDiscoveryFromPort createConfig() {
224 return new LinkDiscoveryFromPort();
225 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600226 }
227 );
228
229 private final InternalConfigListener cfgListener = new InternalConfigListener();
230
alshabib7911a052014-10-16 17:49:37 -0700231 /**
232 * Creates an OpenFlow link provider.
233 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800234 public LldpLinkProvider() {
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700235 super(new ProviderId("lldp", PROVIDER_NAME));
alshabib7911a052014-10-16 17:49:37 -0700236 }
237
Sho SHIMIZU9efeb812016-08-18 09:29:20 -0700238 private String buildSrcMac() {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700239 String defMac = ProbedLinkProvider.defaultMac();
Ray Milkey55f513f2017-12-01 15:58:21 -0800240 if (clusterMetadataService == null) {
241 log.debug("No cluster metadata service is available. Using default value {}", defMac);
242 return defMac;
243 }
244
245 String srcMac = ProbedLinkProvider.fingerprintMac(clusterMetadataService.getClusterMetadata());
Ayaka Koshibe48229222016-05-16 18:04:26 -0700246 if (srcMac.equals(defMac)) {
247 log.warn("Couldn't generate fingerprint. Using default value {}", defMac);
248 return defMac;
249 }
250 log.trace("Generated MAC address {}", srcMac);
251 return srcMac;
252 }
253
alshabib7911a052014-10-16 17:49:37 -0700254 @Activate
Saurav Dasc313c402015-02-27 10:09:47 -0800255 public void activate(ComponentContext context) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700256 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/linkevents", "events-%d", log));
Ray Milkeye80e18f2016-06-02 16:44:14 -0700257 shuttingDown = false;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700258 cfgService.registerProperties(getClass());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700259 appId = coreService.registerApplication(PROVIDER_NAME);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600260
261 cfgRegistry.addListener(cfgListener);
262 factories.forEach(cfgRegistry::registerConfigFactory);
263
Madan Jampanic6371882016-06-03 21:30:17 -0700264 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600265 if (cfg == null) {
266 // If no configuration is found, register default.
Madan Jampanic6371882016-06-03 21:30:17 -0700267 cfg = this.setDefaultSuppressionConfig();
Naoki Shiota399a0b32015-11-15 20:36:13 -0600268 }
269 cfgListener.reconfigureSuppressionRules(cfg);
270
Saurav Dasc313c402015-02-27 10:09:47 -0800271 modified(context);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700272 log.info("Started");
273 }
274
Thomas Vachuskaaad8b1d2015-12-11 10:36:53 -0800275 private SuppressionConfig setDefaultSuppressionConfig() {
276 SuppressionConfig cfg = cfgRegistry.addConfig(appId, SuppressionConfig.class);
277 cfg.deviceTypes(DEFAULT_RULES.getSuppressedDeviceType())
278 .annotation(DEFAULT_RULES.getSuppressedAnnotation())
279 .apply();
280 return cfg;
281 }
282
Thomas Vachuska05453c92015-09-09 14:40:49 -0700283 @Deactivate
284 public void deactivate() {
Ray Milkeye80e18f2016-06-02 16:44:14 -0700285 shuttingDown = true;
Naoki Shiota399a0b32015-11-15 20:36:13 -0600286 cfgRegistry.removeListener(cfgListener);
287 factories.forEach(cfgRegistry::unregisterConfigFactory);
288
Thomas Vachuska05453c92015-09-09 14:40:49 -0700289 cfgService.unregisterProperties(getClass(), false);
290 disable();
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700291 eventExecutor.shutdownNow();
292 eventExecutor = null;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700293 log.info("Stopped");
294 }
295
296 @Modified
297 public void modified(ComponentContext context) {
298 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
299
300 boolean newEnabled, newUseBddp;
Samuel Jero31e16f52018-09-21 10:34:28 -0400301 int newProbeRate, newStaleLinkAge, newDiscoveryDelay;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700302 try {
303 String s = get(properties, PROP_ENABLED);
304 newEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
305
306 s = get(properties, PROP_USE_BDDP);
307 newUseBddp = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
308
309 s = get(properties, PROP_PROBE_RATE);
310 newProbeRate = isNullOrEmpty(s) ? probeRate : Integer.parseInt(s.trim());
311
312 s = get(properties, PROP_STALE_LINK_AGE);
313 newStaleLinkAge = isNullOrEmpty(s) ? staleLinkAge : Integer.parseInt(s.trim());
314
Samuel Jero31e16f52018-09-21 10:34:28 -0400315 s = get(properties, PROP_DISCOVERY_DELAY);
316 newDiscoveryDelay = isNullOrEmpty(s) ? maxDiscoveryDelayMs : Integer.parseInt(s.trim());
317
Thomas Vachuska05453c92015-09-09 14:40:49 -0700318 } catch (NumberFormatException e) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700319 log.warn("Component configuration had invalid values", e);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700320 newEnabled = enabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800321 newUseBddp = useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700322 newProbeRate = probeRate;
323 newStaleLinkAge = staleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400324 newDiscoveryDelay = maxDiscoveryDelayMs;
Saurav Dasc313c402015-02-27 10:09:47 -0800325 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800326
Thomas Vachuska05453c92015-09-09 14:40:49 -0700327 boolean wasEnabled = enabled;
328
329 enabled = newEnabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800330 useBddp = newUseBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700331 probeRate = newProbeRate;
332 staleLinkAge = newStaleLinkAge;
Samuel Jero31e16f52018-09-21 10:34:28 -0400333 maxDiscoveryDelayMs = newDiscoveryDelay;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700334
335 if (!wasEnabled && enabled) {
336 enable();
337 } else if (wasEnabled && !enabled) {
338 disable();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700339 } else {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700340 if (enabled) {
341 // update all discovery helper state
342 loadDevices();
343 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700344 }
345
Samuel Jero31e16f52018-09-21 10:34:28 -0400346 log.info(FORMAT, enabled, useBddp, probeRate, staleLinkAge, maxDiscoveryDelayMs);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700347 }
348
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700349 /**
350 * Enables link discovery processing.
351 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700352 private void enable() {
alshabib7911a052014-10-16 17:49:37 -0700353 providerService = providerRegistry.register(this);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800354 masterService.addListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700355 deviceService.addListener(deviceListener);
356 packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800357
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700358 loadDevices();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700359
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700360 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/link", "discovery-%d", log));
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700361 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(),
362 DEVICE_SYNC_DELAY, DEVICE_SYNC_DELAY, SECONDS);
363 executor.scheduleAtFixedRate(new LinkPrunerTask(),
364 LINK_PRUNER_DELAY, LINK_PRUNER_DELAY, SECONDS);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700365
Thomas Vachuska05453c92015-09-09 14:40:49 -0700366 requestIntercepts();
367 }
368
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700369 /**
370 * Disables link discovery processing.
371 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700372 private void disable() {
373 withdrawIntercepts();
374
375 providerRegistry.unregister(this);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700376 masterService.removeListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700377 deviceService.removeListener(deviceListener);
378 packetService.removeProcessor(packetProcessor);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700379
380 if (executor != null) {
381 executor.shutdownNow();
382 }
383 discoverers.values().forEach(LinkDiscovery::stop);
384 discoverers.clear();
Jon Hall125c3f22017-08-09 13:46:28 -0700385 linkTimes.clear();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700386
387 providerService = null;
388 }
389
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700390 /**
391 * Loads available devices and registers their ports to be probed.
392 */
393 private void loadDevices() {
Ray Milkey0f87d482016-07-06 11:49:19 -0700394 if (!enabled || deviceService == null) {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600395 return;
396 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700397 deviceService.getAvailableDevices()
398 .forEach(d -> updateDevice(d)
399 .ifPresent(ld -> updatePorts(ld, d.id())));
400 }
401
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800402 private boolean isBlacklisted(DeviceId did) {
403 LinkDiscoveryFromDevice cfg = cfgRegistry.getConfig(did, LinkDiscoveryFromDevice.class);
404 if (cfg == null) {
405 return false;
406 }
407 return !cfg.enabled();
408 }
409
410 private boolean isBlacklisted(ConnectPoint cp) {
411 // if parent device is blacklisted, so is the port
412 if (isBlacklisted(cp.deviceId())) {
413 return true;
414 }
415 LinkDiscoveryFromPort cfg = cfgRegistry.getConfig(cp, LinkDiscoveryFromPort.class);
416 if (cfg == null) {
417 return false;
418 }
419 return !cfg.enabled();
420 }
421
422 private boolean isBlacklisted(Port port) {
423 return isBlacklisted(new ConnectPoint(port.element().id(), port.number()));
424 }
425
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700426 /**
427 * Updates discovery helper for specified device.
428 *
429 * Adds and starts a discovery helper for specified device if enabled,
430 * calls {@link #removeDevice(DeviceId)} otherwise.
431 *
432 * @param device device to add
433 * @return discovery helper if discovery is enabled for the device
434 */
435 private Optional<LinkDiscovery> updateDevice(Device device) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800436 if (device == null) {
437 return Optional.empty();
438 }
Jon Hall125c3f22017-08-09 13:46:28 -0700439 if (!masterService.isLocalMaster(device.id())) {
440 // Reset the last seen time for all links to this device
441 // then stop discovery for this device
442 List<LinkKey> updateLinks = new LinkedList<>();
443 linkTimes.forEach((link, time) -> {
444 if (link.dst().deviceId().equals(device.id())) {
445 updateLinks.add(link);
446 }
447 });
448 updateLinks.forEach(link -> linkTimes.remove(link));
449 removeDevice(device.id());
450 return Optional.empty();
451 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800452 if (rules.isSuppressed(device) || isBlacklisted(device.id())) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700453 log.trace("LinkDiscovery from {} disabled by configuration", device.id());
454 removeDevice(device.id());
455 return Optional.empty();
456 }
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800457
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700458 LinkDiscovery ld = discoverers.computeIfAbsent(device.id(),
459 did -> new LinkDiscovery(device, context));
460 if (ld.isStopped()) {
461 ld.start();
462 }
463 return Optional.of(ld);
464 }
465
466 /**
467 * Removes after stopping discovery helper for specified device.
468 * @param deviceId device to remove
469 */
470 private void removeDevice(final DeviceId deviceId) {
471 discoverers.computeIfPresent(deviceId, (did, ld) -> {
472 ld.stop();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700473 return null;
474 });
475
476 }
477
478 /**
479 * Updates ports of the specified device to the specified discovery helper.
480 */
481 private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) {
482 deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p));
483 }
484
485 /**
486 * Updates discovery helper state of the specified port.
487 *
488 * Adds a port to the discovery helper if up and discovery is enabled,
489 * or calls {@link #removePort(Port)} otherwise.
490 */
491 private void updatePort(LinkDiscovery discoverer, Port port) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800492 if (port == null) {
493 return;
494 }
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800495 if (port.number().isLogical()) {
496 // silently ignore logical ports
497 return;
498 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600499
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800500 if (rules.isSuppressed(port) || isBlacklisted(port)) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700501 log.trace("LinkDiscovery from {} disabled by configuration", port);
502 removePort(port);
503 return;
504 }
505
506 // check if enabled and turn off discovery?
507 if (!port.isEnabled()) {
508 removePort(port);
509 return;
510 }
511
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800512 discoverer.addPort(port);
alshabib7911a052014-10-16 17:49:37 -0700513 }
514
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700515 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700516 * Removes a port from the specified discovery helper.
517 * @param port the port
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700518 */
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700519 private void removePort(Port port) {
520 if (port.element() instanceof Device) {
521 Device d = (Device) port.element();
522 LinkDiscovery ld = discoverers.get(d.id());
523 if (ld != null) {
524 ld.removePort(port.number());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700525 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700526 } else {
527 log.warn("Attempted to remove non-Device port", port);
Jonathan Hart45066bc2015-07-28 11:18:34 -0700528 }
529 }
530
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700531 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700532 * Requests packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800533 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700534 private void requestIntercepts() {
535 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700536 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700537 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800538
Thomas Vachuska347cc872015-09-23 10:25:29 -0700539 selector.matchEthType(TYPE_BSN);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800540 if (useBddp) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700541 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
542 } else {
543 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800544 }
545 }
546
Thomas Vachuska27bee092015-06-23 19:03:10 -0700547 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700548 * Withdraws packet intercepts.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700549 */
550 private void withdrawIntercepts() {
551 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700552 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700553 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska347cc872015-09-23 10:25:29 -0700554 selector.matchEthType(TYPE_BSN);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700555 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
556 }
557
Naoki Shiota399a0b32015-11-15 20:36:13 -0600558 protected SuppressionRules rules() {
559 return rules;
560 }
561
562 protected void updateRules(SuppressionRules newRules) {
563 if (!rules.equals(newRules)) {
564 rules = newRules;
565 loadDevices();
566 }
567 }
568
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700569 /**
570 * Processes device mastership role changes.
571 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800572 private class InternalRoleListener implements MastershipListener {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800573 @Override
574 public void event(MastershipEvent event) {
Jordan Halterman9d982a52017-12-11 15:27:37 -0800575 if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800576 // only need new master events
Jon Hall7a8bfc62016-05-26 17:59:04 -0700577 eventExecutor.execute(() -> {
578 DeviceId deviceId = event.subject();
579 Device device = deviceService.getDevice(deviceId);
580 if (device == null) {
581 log.debug("Device {} doesn't exist, or isn't there yet", deviceId);
582 return;
583 }
Jordan Halterman9d982a52017-12-11 15:27:37 -0800584 updateDevice(device).ifPresent(ld -> updatePorts(ld, device.id()));
Jon Hall7a8bfc62016-05-26 17:59:04 -0700585 });
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800586 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800587 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800588 }
alshabib7911a052014-10-16 17:49:37 -0700589
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700590 private class DeviceEventProcessor implements Runnable {
591
592 DeviceEvent event;
593
594 DeviceEventProcessor(DeviceEvent event) {
595 this.event = event;
596 }
597
alshabib7911a052014-10-16 17:49:37 -0700598 @Override
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700599 public void run() {
alshabib7911a052014-10-16 17:49:37 -0700600 Device device = event.subject();
alshabibacd91832014-10-17 14:38:41 -0700601 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700602 if (device == null) {
603 log.error("Device is null.");
604 return;
605 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700606 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700607 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700608 switch (event.type()) {
609 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700610 case DEVICE_UPDATED:
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700611 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700612 break;
613 case PORT_ADDED:
614 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700615 if (port.isEnabled()) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700616 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
alshabib7911a052014-10-16 17:49:37 -0700617 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700618 log.debug("Port down {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700619 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600620 providerService.linksVanished(new ConnectPoint(port.element().id(),
621 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700622 }
623 break;
624 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700625 log.debug("Port removed {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700626 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600627 providerService.linksVanished(new ConnectPoint(port.element().id(),
628 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700629 break;
630 case DEVICE_REMOVED:
631 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700632 log.debug("Device removed {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700633 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600634 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700635 break;
636 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700637 if (deviceService.isAvailable(deviceId)) {
638 log.debug("Device up {}", deviceId);
alshabibe3af2652015-12-01 23:05:34 -0800639 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700640 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700641 log.debug("Device down {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700642 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600643 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700644 }
645 break;
Jonathan Hart9de692c2015-04-23 11:45:47 -0700646 case PORT_STATS_UPDATED:
647 break;
alshabib7911a052014-10-16 17:49:37 -0700648 default:
649 log.debug("Unknown event {}", event);
650 }
651 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700652 }
alshabib7911a052014-10-16 17:49:37 -0700653
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700654 /**
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700655 * Processes device events.
656 */
657 private class InternalDeviceListener implements DeviceListener {
658 @Override
659 public void event(DeviceEvent event) {
660 if (event.type() == Type.PORT_STATS_UPDATED) {
661 return;
662 }
663
664 Runnable deviceEventProcessor = new DeviceEventProcessor(event);
665
666 eventExecutor.execute(deviceEventProcessor);
667 }
668 }
669
670 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700671 * Processes incoming packets.
672 */
673 private class InternalPacketProcessor implements PacketProcessor {
alshabib7911a052014-10-16 17:49:37 -0700674 @Override
675 public void process(PacketContext context) {
Thomas Vachuska347cc872015-09-23 10:25:29 -0700676 if (context == null || context.isHandled()) {
alshabib4a179dc2014-10-17 17:17:01 -0700677 return;
678 }
Thomas Vachuska347cc872015-09-23 10:25:29 -0700679
680 Ethernet eth = context.inPacket().parsed();
681 if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) {
682 return;
683 }
684
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700685 LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId());
alshabib7911a052014-10-16 17:49:37 -0700686 if (ld == null) {
687 return;
688 }
689
Jonathan Hartb35540a2015-11-17 09:30:56 -0800690 if (ld.handleLldp(context)) {
alshabib7911a052014-10-16 17:49:37 -0700691 context.block();
692 }
693 }
694 }
695
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700696 /**
697 * Auxiliary task to keep device ports up to date.
698 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800699 private final class SyncDeviceInfoTask implements Runnable {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800700 @Override
701 public void run() {
702 if (Thread.currentThread().isInterrupted()) {
703 log.info("Interrupted, quitting");
704 return;
705 }
706 // check what deviceService sees, to see if we are missing anything
707 try {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700708 loadDevices();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800709 } catch (Exception e) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700710 // Catch all exceptions to avoid task being suppressed
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800711 log.error("Exception thrown during synchronization process", e);
712 }
713 }
714 }
715
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700716 /**
717 * Auxiliary task for pruning stale links.
718 */
719 private class LinkPrunerTask implements Runnable {
720 @Override
721 public void run() {
722 if (Thread.currentThread().isInterrupted()) {
723 log.info("Interrupted, quitting");
724 return;
725 }
726
727 try {
728 // TODO: There is still a slight possibility of mastership
729 // change occurring right with link going stale. This will
730 // result in the stale link not being pruned.
731 Maps.filterEntries(linkTimes, e -> {
732 if (!masterService.isLocalMaster(e.getKey().dst().deviceId())) {
733 return true;
734 }
735 if (isStale(e.getValue())) {
736 providerService.linkVanished(new DefaultLinkDescription(e.getKey().src(),
737 e.getKey().dst(),
738 DIRECT));
739 return true;
740 }
741 return false;
742 }).clear();
743
744 } catch (Exception e) {
745 // Catch all exceptions to avoid task being suppressed
Ray Milkeye80e18f2016-06-02 16:44:14 -0700746 if (!shuttingDown) {
747 // Error condition
748 log.error("Exception thrown during link pruning process", e);
749 } else {
750 // Provider is shutting down, the error can be ignored
751 log.trace("Shutting down, ignoring error", e);
752 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700753 }
754 }
755
756 private boolean isStale(long lastSeen) {
757 return lastSeen < System.currentTimeMillis() - staleLinkAge;
758 }
759 }
760
761 /**
762 * Provides processing context for the device link discovery helpers.
763 */
Ray Milkey957390e2016-02-09 10:02:46 -0800764 private class InternalDiscoveryContext implements LinkDiscoveryContext {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700765 @Override
766 public MastershipService mastershipService() {
767 return masterService;
768 }
769
770 @Override
771 public LinkProviderService providerService() {
772 return providerService;
773 }
774
775 @Override
776 public PacketService packetService() {
777 return packetService;
778 }
779
780 @Override
781 public long probeRate() {
782 return probeRate;
783 }
784
785 @Override
Jonathan Hartb35540a2015-11-17 09:30:56 -0800786 public boolean useBddp() {
787 return useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700788 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700789
790 @Override
791 public void touchLink(LinkKey key) {
792 linkTimes.put(key, System.currentTimeMillis());
793 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800794
795 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700796 public DeviceService deviceService() {
797 return deviceService;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800798 }
799
800 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700801 public String fingerprint() {
802 return buildSrcMac();
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800803 }
Samuel Jero31e16f52018-09-21 10:34:28 -0400804
805 @Override
806 public String lldpSecret() {
807 return clusterMetadataService.getClusterMetadata().getClusterSecret();
808 }
809
810 @Override
811 public long maxDiscoveryDelay() {
812 return maxDiscoveryDelayMs;
813 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700814 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700815
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800816 static final EnumSet<NetworkConfigEvent.Type> CONFIG_CHANGED
817 = EnumSet.of(NetworkConfigEvent.Type.CONFIG_ADDED,
818 NetworkConfigEvent.Type.CONFIG_UPDATED,
819 NetworkConfigEvent.Type.CONFIG_REMOVED);
820
Naoki Shiota399a0b32015-11-15 20:36:13 -0600821 private class InternalConfigListener implements NetworkConfigListener {
822
823 private synchronized void reconfigureSuppressionRules(SuppressionConfig cfg) {
824 if (cfg == null) {
Ray Milkey0a8ee912016-06-13 09:58:12 -0700825 log.debug("Suppression Config is null.");
Naoki Shiota399a0b32015-11-15 20:36:13 -0600826 return;
827 }
828
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800829 SuppressionRules newRules = new SuppressionRules(cfg.deviceTypes(),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600830 cfg.annotation());
831
832 updateRules(newRules);
833 }
834
Jon Hall125c3f22017-08-09 13:46:28 -0700835 private boolean isRelevantDeviceEvent(NetworkConfigEvent event) {
836 return event.configClass() == LinkDiscoveryFromDevice.class &&
837 CONFIG_CHANGED.contains(event.type());
838 }
839
840 private boolean isRelevantPortEvent(NetworkConfigEvent event) {
841 return event.configClass() == LinkDiscoveryFromPort.class &&
842 CONFIG_CHANGED.contains(event.type());
843 }
844
845 private boolean isRelevantSuppressionEvent(NetworkConfigEvent event) {
846 return (event.configClass().equals(SuppressionConfig.class) &&
847 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
848 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED));
849 }
850
Naoki Shiota399a0b32015-11-15 20:36:13 -0600851 @Override
852 public void event(NetworkConfigEvent event) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700853 eventExecutor.execute(() -> {
Jon Hall125c3f22017-08-09 13:46:28 -0700854 if (isRelevantDeviceEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800855 if (event.subject() instanceof DeviceId) {
856 final DeviceId did = (DeviceId) event.subject();
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800857 Device device = deviceService.getDevice(did);
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800858 updateDevice(device).ifPresent(ld -> updatePorts(ld, did));
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800859 }
Jon Hall125c3f22017-08-09 13:46:28 -0700860 } else if (isRelevantPortEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800861 if (event.subject() instanceof ConnectPoint) {
862 ConnectPoint cp = (ConnectPoint) event.subject();
863 if (cp.elementId() instanceof DeviceId) {
864 final DeviceId did = (DeviceId) cp.elementId();
865 Device device = deviceService.getDevice(did);
866 Port port = deviceService.getPort(did, cp.port());
867 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
868 }
869 }
Jon Hall125c3f22017-08-09 13:46:28 -0700870 } else if (isRelevantSuppressionEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800871 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
872 reconfigureSuppressionRules(cfg);
873 log.trace("Network config reconfigured");
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800874 }
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800875 });
Naoki Shiota399a0b32015-11-15 20:36:13 -0600876 }
877 }
alshabib7911a052014-10-16 17:49:37 -0700878}