blob: 87ca7d757cf492b6e5ea1810f4f4af068ee14605 [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 Milkeyd9bbde82016-06-09 11:35:00 -070018import java.util.Dictionary;
19import java.util.EnumSet;
Jon Hall125c3f22017-08-09 13:46:28 -070020import java.util.LinkedList;
21import java.util.List;
Ray Milkeyd9bbde82016-06-09 11:35:00 -070022import java.util.Map;
23import java.util.Optional;
24import java.util.Properties;
25import java.util.Set;
26import java.util.concurrent.ConcurrentHashMap;
27import java.util.concurrent.ExecutorService;
28import java.util.concurrent.ScheduledExecutorService;
29
alshabib7911a052014-10-16 17:49:37 -070030import org.apache.felix.scr.annotations.Activate;
31import org.apache.felix.scr.annotations.Component;
32import org.apache.felix.scr.annotations.Deactivate;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080033import org.apache.felix.scr.annotations.Modified;
34import org.apache.felix.scr.annotations.Property;
alshabib7911a052014-10-16 17:49:37 -070035import org.apache.felix.scr.annotations.Reference;
36import org.apache.felix.scr.annotations.ReferenceCardinality;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080037import org.onlab.packet.Ethernet;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070038import org.onosproject.cfg.ComponentConfigService;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080039import org.onosproject.cluster.ClusterMetadataService;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070040import org.onosproject.cluster.ClusterService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080041import org.onosproject.core.ApplicationId;
42import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.mastership.MastershipEvent;
44import org.onosproject.mastership.MastershipListener;
45import org.onosproject.mastership.MastershipService;
46import org.onosproject.net.ConnectPoint;
47import org.onosproject.net.Device;
48import org.onosproject.net.DeviceId;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070049import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.Port;
Naoki Shiota399a0b32015-11-15 20:36:13 -060051import org.onosproject.net.config.ConfigFactory;
52import org.onosproject.net.config.NetworkConfigEvent;
53import org.onosproject.net.config.NetworkConfigListener;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070054import org.onosproject.net.config.NetworkConfigRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.device.DeviceEvent;
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080056import org.onosproject.net.device.DeviceEvent.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.device.DeviceListener;
58import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080059import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080060import org.onosproject.net.flow.TrafficSelector;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070061import org.onosproject.net.link.DefaultLinkDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080062import org.onosproject.net.link.LinkProviderRegistry;
63import org.onosproject.net.link.LinkProviderService;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070064import org.onosproject.net.link.LinkService;
Ray Milkeyd9bbde82016-06-09 11:35:00 -070065import org.onosproject.net.link.ProbedLinkProvider;
Brian O'Connorabafb502014-12-02 22:26:20 -080066import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080067import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080068import org.onosproject.net.packet.PacketProcessor;
69import org.onosproject.net.packet.PacketService;
70import org.onosproject.net.provider.AbstractProvider;
71import org.onosproject.net.provider.ProviderId;
Ray Milkey957390e2016-02-09 10:02:46 -080072import org.onosproject.provider.lldpcommon.LinkDiscovery;
Ray Milkeyd9bbde82016-06-09 11:35:00 -070073import org.onosproject.provider.lldpcommon.LinkDiscoveryContext;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080074import org.osgi.service.component.ComponentContext;
alshabib7911a052014-10-16 17:49:37 -070075import org.slf4j.Logger;
76
Ray Milkeyd9bbde82016-06-09 11:35:00 -070077import com.google.common.collect.ImmutableMap;
78import com.google.common.collect.ImmutableSet;
79import com.google.common.collect.Maps;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080080
81import static com.google.common.base.Strings.isNullOrEmpty;
82import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
83import static java.util.concurrent.TimeUnit.SECONDS;
84import static org.onlab.packet.Ethernet.TYPE_BSN;
85import static org.onlab.packet.Ethernet.TYPE_LLDP;
86import static org.onlab.util.Tools.get;
87import static org.onlab.util.Tools.groupedThreads;
Marc De Leenheer0bfc2a12016-02-02 22:46:27 -080088import static org.onosproject.net.Link.Type.DIRECT;
89import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
90import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
91import static org.onosproject.net.config.basics.SubjectFactories.DEVICE_SUBJECT_FACTORY;
92import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080093
alshabib7911a052014-10-16 17:49:37 -070094/**
Thomas Vachuska05453c92015-09-09 14:40:49 -070095 * Provider which uses LLDP and BDDP packets to detect network infrastructure links.
alshabib7911a052014-10-16 17:49:37 -070096 */
97@Component(immediate = true)
Ayaka Koshibe48229222016-05-16 18:04:26 -070098public class LldpLinkProvider extends AbstractProvider implements ProbedLinkProvider {
alshabib7911a052014-10-16 17:49:37 -070099
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700100 private static final String PROVIDER_NAME = "org.onosproject.provider.lldp";
101
Thomas Vachuska05453c92015-09-09 14:40:49 -0700102 private static final String FORMAT =
103 "Settings: enabled={}, useBDDP={}, probeRate={}, " +
Naoki Shiota399a0b32015-11-15 20:36:13 -0600104 "staleLinkAge={}";
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800105
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700106 // When a Device/Port has this annotation, do not send out LLDP/BDDP
107 public static final String NO_LLDP = "no-lldp";
108
Thomas Vachuskaaad8b1d2015-12-11 10:36:53 -0800109 private static final int MAX_RETRIES = 5;
110 private static final int RETRY_DELAY = 1_000; // millis
111
alshabib7911a052014-10-16 17:49:37 -0700112 private final Logger log = getLogger(getClass());
113
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected CoreService coreService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib7911a052014-10-16 17:49:37 -0700118 protected LinkProviderRegistry providerRegistry;
119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected DeviceService deviceService;
122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700124 protected LinkService linkService;
125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800127 protected PacketService packetService;
alshabib7911a052014-10-16 17:49:37 -0700128
alshabib875d6262014-10-17 16:19:40 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected MastershipService masterService;
131
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected ComponentConfigService cfgService;
134
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected ClusterService clusterService;
137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected NetworkConfigRegistry cfgRegistry;
140
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected ClusterMetadataService clusterMetadataService;
143
alshabib7911a052014-10-16 17:49:37 -0700144 private LinkProviderService providerService;
145
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800146 private ScheduledExecutorService executor;
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700147 protected ExecutorService eventExecutor;
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800148
Ray Milkeye80e18f2016-06-02 16:44:14 -0700149 private boolean shuttingDown = false;
150
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700151 // TODO: Add sanity checking for the configurable params based on the delays
152 private static final long DEVICE_SYNC_DELAY = 5;
153 private static final long LINK_PRUNER_DELAY = 3;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700154
155 private static final String PROP_ENABLED = "enabled";
156 @Property(name = PROP_ENABLED, boolValue = true,
157 label = "If false, link discovery is disabled")
158 private boolean enabled = false;
159
160 private static final String PROP_USE_BDDP = "useBDDP";
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700161 @Property(name = PROP_USE_BDDP, boolValue = true,
162 label = "Use BDDP for link discovery")
Jonathan Hartb35540a2015-11-17 09:30:56 -0800163 private boolean useBddp = true;
alshabib7911a052014-10-16 17:49:37 -0700164
Thomas Vachuska05453c92015-09-09 14:40:49 -0700165 private static final String PROP_PROBE_RATE = "probeRate";
Thomas Vachuska9e51fd02016-01-04 16:51:28 -0800166 private static final int DEFAULT_PROBE_RATE = 3000;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700167 @Property(name = PROP_PROBE_RATE, intValue = DEFAULT_PROBE_RATE,
168 label = "LLDP and BDDP probe rate specified in millis")
169 private int probeRate = DEFAULT_PROBE_RATE;
Saurav Dasc313c402015-02-27 10:09:47 -0800170
Thomas Vachuska05453c92015-09-09 14:40:49 -0700171 private static final String PROP_STALE_LINK_AGE = "staleLinkAge";
Thomas Vachuska9e51fd02016-01-04 16:51:28 -0800172 private static final int DEFAULT_STALE_LINK_AGE = 10000;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700173 @Property(name = PROP_STALE_LINK_AGE, intValue = DEFAULT_STALE_LINK_AGE,
174 label = "Number of millis beyond which links will be considered stale")
175 private int staleLinkAge = DEFAULT_STALE_LINK_AGE;
alshabib7911a052014-10-16 17:49:37 -0700176
Ray Milkey957390e2016-02-09 10:02:46 -0800177 private final LinkDiscoveryContext context = new InternalDiscoveryContext();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800178 private final InternalRoleListener roleListener = new InternalRoleListener();
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700179 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
180 private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800181
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700182 // Device link discovery helpers.
alshabib7911a052014-10-16 17:49:37 -0700183 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
184
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700185 // Most recent time a tracked link was seen; links are tracked if their
186 // destination connection point is mastered by this controller instance.
187 private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap();
188
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800189 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800190
Naoki Shiota399a0b32015-11-15 20:36:13 -0600191 static final SuppressionRules DEFAULT_RULES
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800192 = new SuppressionRules(EnumSet.of(Device.Type.ROADM,
193 Device.Type.FIBER_SWITCH,
194 Device.Type.OPTICAL_AMPLIFIER,
195 Device.Type.OTN),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600196 ImmutableMap.of(NO_LLDP, SuppressionRules.ANY_VALUE));
197
198 private SuppressionRules rules = LldpLinkProvider.DEFAULT_RULES;
199
200 public static final String CONFIG_KEY = "suppression";
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800201 public static final String FEATURE_NAME = "linkDiscovery";
Naoki Shiota399a0b32015-11-15 20:36:13 -0600202
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800203 private final Set<ConfigFactory<?, ?>> factories = ImmutableSet.of(
Naoki Shiota399a0b32015-11-15 20:36:13 -0600204 new ConfigFactory<ApplicationId, SuppressionConfig>(APP_SUBJECT_FACTORY,
205 SuppressionConfig.class,
206 CONFIG_KEY) {
207 @Override
208 public SuppressionConfig createConfig() {
209 return new SuppressionConfig();
210 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800211 },
212 new ConfigFactory<DeviceId, LinkDiscoveryFromDevice>(DEVICE_SUBJECT_FACTORY,
213 LinkDiscoveryFromDevice.class, FEATURE_NAME) {
214 @Override
215 public LinkDiscoveryFromDevice createConfig() {
216 return new LinkDiscoveryFromDevice();
217 }
218 },
219 new ConfigFactory<ConnectPoint, LinkDiscoveryFromPort>(CONNECT_POINT_SUBJECT_FACTORY,
220 LinkDiscoveryFromPort.class, FEATURE_NAME) {
221 @Override
222 public LinkDiscoveryFromPort createConfig() {
223 return new LinkDiscoveryFromPort();
224 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600225 }
226 );
227
228 private final InternalConfigListener cfgListener = new InternalConfigListener();
229
alshabib7911a052014-10-16 17:49:37 -0700230 /**
231 * Creates an OpenFlow link provider.
232 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800233 public LldpLinkProvider() {
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700234 super(new ProviderId("lldp", PROVIDER_NAME));
alshabib7911a052014-10-16 17:49:37 -0700235 }
236
Sho SHIMIZU9efeb812016-08-18 09:29:20 -0700237 private String buildSrcMac() {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700238 String defMac = ProbedLinkProvider.defaultMac();
Ray Milkey55f513f2017-12-01 15:58:21 -0800239 if (clusterMetadataService == null) {
240 log.debug("No cluster metadata service is available. Using default value {}", defMac);
241 return defMac;
242 }
243
244 String srcMac = ProbedLinkProvider.fingerprintMac(clusterMetadataService.getClusterMetadata());
Ayaka Koshibe48229222016-05-16 18:04:26 -0700245 if (srcMac.equals(defMac)) {
246 log.warn("Couldn't generate fingerprint. Using default value {}", defMac);
247 return defMac;
248 }
249 log.trace("Generated MAC address {}", srcMac);
250 return srcMac;
251 }
252
alshabib7911a052014-10-16 17:49:37 -0700253 @Activate
Saurav Dasc313c402015-02-27 10:09:47 -0800254 public void activate(ComponentContext context) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700255 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/linkevents", "events-%d", log));
Ray Milkeye80e18f2016-06-02 16:44:14 -0700256 shuttingDown = false;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700257 cfgService.registerProperties(getClass());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700258 appId = coreService.registerApplication(PROVIDER_NAME);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600259
260 cfgRegistry.addListener(cfgListener);
261 factories.forEach(cfgRegistry::registerConfigFactory);
262
Madan Jampanic6371882016-06-03 21:30:17 -0700263 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600264 if (cfg == null) {
265 // If no configuration is found, register default.
Madan Jampanic6371882016-06-03 21:30:17 -0700266 cfg = this.setDefaultSuppressionConfig();
Naoki Shiota399a0b32015-11-15 20:36:13 -0600267 }
268 cfgListener.reconfigureSuppressionRules(cfg);
269
Saurav Dasc313c402015-02-27 10:09:47 -0800270 modified(context);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700271 log.info("Started");
272 }
273
Thomas Vachuskaaad8b1d2015-12-11 10:36:53 -0800274 private SuppressionConfig setDefaultSuppressionConfig() {
275 SuppressionConfig cfg = cfgRegistry.addConfig(appId, SuppressionConfig.class);
276 cfg.deviceTypes(DEFAULT_RULES.getSuppressedDeviceType())
277 .annotation(DEFAULT_RULES.getSuppressedAnnotation())
278 .apply();
279 return cfg;
280 }
281
Thomas Vachuska05453c92015-09-09 14:40:49 -0700282 @Deactivate
283 public void deactivate() {
Ray Milkeye80e18f2016-06-02 16:44:14 -0700284 shuttingDown = true;
Naoki Shiota399a0b32015-11-15 20:36:13 -0600285 cfgRegistry.removeListener(cfgListener);
286 factories.forEach(cfgRegistry::unregisterConfigFactory);
287
Thomas Vachuska05453c92015-09-09 14:40:49 -0700288 cfgService.unregisterProperties(getClass(), false);
289 disable();
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700290 eventExecutor.shutdownNow();
291 eventExecutor = null;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700292 log.info("Stopped");
293 }
294
295 @Modified
296 public void modified(ComponentContext context) {
297 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
298
299 boolean newEnabled, newUseBddp;
300 int newProbeRate, newStaleLinkAge;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700301 try {
302 String s = get(properties, PROP_ENABLED);
303 newEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
304
305 s = get(properties, PROP_USE_BDDP);
306 newUseBddp = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
307
308 s = get(properties, PROP_PROBE_RATE);
309 newProbeRate = isNullOrEmpty(s) ? probeRate : Integer.parseInt(s.trim());
310
311 s = get(properties, PROP_STALE_LINK_AGE);
312 newStaleLinkAge = isNullOrEmpty(s) ? staleLinkAge : Integer.parseInt(s.trim());
313
Thomas Vachuska05453c92015-09-09 14:40:49 -0700314 } catch (NumberFormatException e) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700315 log.warn("Component configuration had invalid values", e);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700316 newEnabled = enabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800317 newUseBddp = useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700318 newProbeRate = probeRate;
319 newStaleLinkAge = staleLinkAge;
Saurav Dasc313c402015-02-27 10:09:47 -0800320 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800321
Thomas Vachuska05453c92015-09-09 14:40:49 -0700322 boolean wasEnabled = enabled;
323
324 enabled = newEnabled;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800325 useBddp = newUseBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700326 probeRate = newProbeRate;
327 staleLinkAge = newStaleLinkAge;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700328
329 if (!wasEnabled && enabled) {
330 enable();
331 } else if (wasEnabled && !enabled) {
332 disable();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700333 } else {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700334 if (enabled) {
335 // update all discovery helper state
336 loadDevices();
337 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700338 }
339
Naoki Shiota399a0b32015-11-15 20:36:13 -0600340 log.info(FORMAT, enabled, useBddp, probeRate, staleLinkAge);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700341 }
342
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700343 /**
344 * Enables link discovery processing.
345 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700346 private void enable() {
alshabib7911a052014-10-16 17:49:37 -0700347 providerService = providerRegistry.register(this);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800348 masterService.addListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700349 deviceService.addListener(deviceListener);
350 packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0));
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800351
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700352 loadDevices();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700353
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700354 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/link", "discovery-%d", log));
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700355 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(),
356 DEVICE_SYNC_DELAY, DEVICE_SYNC_DELAY, SECONDS);
357 executor.scheduleAtFixedRate(new LinkPrunerTask(),
358 LINK_PRUNER_DELAY, LINK_PRUNER_DELAY, SECONDS);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700359
Thomas Vachuska05453c92015-09-09 14:40:49 -0700360 requestIntercepts();
361 }
362
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700363 /**
364 * Disables link discovery processing.
365 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700366 private void disable() {
367 withdrawIntercepts();
368
369 providerRegistry.unregister(this);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700370 masterService.removeListener(roleListener);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700371 deviceService.removeListener(deviceListener);
372 packetService.removeProcessor(packetProcessor);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700373
374 if (executor != null) {
375 executor.shutdownNow();
376 }
377 discoverers.values().forEach(LinkDiscovery::stop);
378 discoverers.clear();
Jon Hall125c3f22017-08-09 13:46:28 -0700379 linkTimes.clear();
Thomas Vachuska05453c92015-09-09 14:40:49 -0700380
381 providerService = null;
382 }
383
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700384 /**
385 * Loads available devices and registers their ports to be probed.
386 */
387 private void loadDevices() {
Ray Milkey0f87d482016-07-06 11:49:19 -0700388 if (!enabled || deviceService == null) {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600389 return;
390 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700391 deviceService.getAvailableDevices()
392 .forEach(d -> updateDevice(d)
393 .ifPresent(ld -> updatePorts(ld, d.id())));
394 }
395
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800396 private boolean isBlacklisted(DeviceId did) {
397 LinkDiscoveryFromDevice cfg = cfgRegistry.getConfig(did, LinkDiscoveryFromDevice.class);
398 if (cfg == null) {
399 return false;
400 }
401 return !cfg.enabled();
402 }
403
404 private boolean isBlacklisted(ConnectPoint cp) {
405 // if parent device is blacklisted, so is the port
406 if (isBlacklisted(cp.deviceId())) {
407 return true;
408 }
409 LinkDiscoveryFromPort cfg = cfgRegistry.getConfig(cp, LinkDiscoveryFromPort.class);
410 if (cfg == null) {
411 return false;
412 }
413 return !cfg.enabled();
414 }
415
416 private boolean isBlacklisted(Port port) {
417 return isBlacklisted(new ConnectPoint(port.element().id(), port.number()));
418 }
419
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700420 /**
421 * Updates discovery helper for specified device.
422 *
423 * Adds and starts a discovery helper for specified device if enabled,
424 * calls {@link #removeDevice(DeviceId)} otherwise.
425 *
426 * @param device device to add
427 * @return discovery helper if discovery is enabled for the device
428 */
429 private Optional<LinkDiscovery> updateDevice(Device device) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800430 if (device == null) {
431 return Optional.empty();
432 }
Jon Hall125c3f22017-08-09 13:46:28 -0700433 if (!masterService.isLocalMaster(device.id())) {
434 // Reset the last seen time for all links to this device
435 // then stop discovery for this device
436 List<LinkKey> updateLinks = new LinkedList<>();
437 linkTimes.forEach((link, time) -> {
438 if (link.dst().deviceId().equals(device.id())) {
439 updateLinks.add(link);
440 }
441 });
442 updateLinks.forEach(link -> linkTimes.remove(link));
443 removeDevice(device.id());
444 return Optional.empty();
445 }
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800446 if (rules.isSuppressed(device) || isBlacklisted(device.id())) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700447 log.trace("LinkDiscovery from {} disabled by configuration", device.id());
448 removeDevice(device.id());
449 return Optional.empty();
450 }
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800451
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700452 LinkDiscovery ld = discoverers.computeIfAbsent(device.id(),
453 did -> new LinkDiscovery(device, context));
454 if (ld.isStopped()) {
455 ld.start();
456 }
457 return Optional.of(ld);
458 }
459
460 /**
461 * Removes after stopping discovery helper for specified device.
462 * @param deviceId device to remove
463 */
464 private void removeDevice(final DeviceId deviceId) {
465 discoverers.computeIfPresent(deviceId, (did, ld) -> {
466 ld.stop();
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700467 return null;
468 });
469
470 }
471
472 /**
473 * Updates ports of the specified device to the specified discovery helper.
474 */
475 private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) {
476 deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p));
477 }
478
479 /**
480 * Updates discovery helper state of the specified port.
481 *
482 * Adds a port to the discovery helper if up and discovery is enabled,
483 * or calls {@link #removePort(Port)} otherwise.
484 */
485 private void updatePort(LinkDiscovery discoverer, Port port) {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800486 if (port == null) {
487 return;
488 }
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800489 if (port.number().isLogical()) {
490 // silently ignore logical ports
491 return;
492 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600493
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800494 if (rules.isSuppressed(port) || isBlacklisted(port)) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700495 log.trace("LinkDiscovery from {} disabled by configuration", port);
496 removePort(port);
497 return;
498 }
499
500 // check if enabled and turn off discovery?
501 if (!port.isEnabled()) {
502 removePort(port);
503 return;
504 }
505
HIGUCHI Yutab853b3f2015-11-17 18:43:20 -0800506 discoverer.addPort(port);
alshabib7911a052014-10-16 17:49:37 -0700507 }
508
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700509 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700510 * Removes a port from the specified discovery helper.
511 * @param port the port
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700512 */
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700513 private void removePort(Port port) {
514 if (port.element() instanceof Device) {
515 Device d = (Device) port.element();
516 LinkDiscovery ld = discoverers.get(d.id());
517 if (ld != null) {
518 ld.removePort(port.number());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700519 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700520 } else {
521 log.warn("Attempted to remove non-Device port", port);
Jonathan Hart45066bc2015-07-28 11:18:34 -0700522 }
523 }
524
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700525 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700526 * Requests packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800527 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700528 private void requestIntercepts() {
529 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700530 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700531 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800532
Thomas Vachuska347cc872015-09-23 10:25:29 -0700533 selector.matchEthType(TYPE_BSN);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800534 if (useBddp) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700535 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
536 } else {
537 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800538 }
539 }
540
Thomas Vachuska27bee092015-06-23 19:03:10 -0700541 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700542 * Withdraws packet intercepts.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700543 */
544 private void withdrawIntercepts() {
545 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Thomas Vachuska347cc872015-09-23 10:25:29 -0700546 selector.matchEthType(TYPE_LLDP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700547 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska347cc872015-09-23 10:25:29 -0700548 selector.matchEthType(TYPE_BSN);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700549 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
550 }
551
Naoki Shiota399a0b32015-11-15 20:36:13 -0600552 protected SuppressionRules rules() {
553 return rules;
554 }
555
556 protected void updateRules(SuppressionRules newRules) {
557 if (!rules.equals(newRules)) {
558 rules = newRules;
559 loadDevices();
560 }
561 }
562
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700563 /**
564 * Processes device mastership role changes.
565 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800566 private class InternalRoleListener implements MastershipListener {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800567 @Override
568 public void event(MastershipEvent event) {
Jordan Halterman9d982a52017-12-11 15:27:37 -0800569 if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800570 // only need new master events
Jon Hall7a8bfc62016-05-26 17:59:04 -0700571 eventExecutor.execute(() -> {
572 DeviceId deviceId = event.subject();
573 Device device = deviceService.getDevice(deviceId);
574 if (device == null) {
575 log.debug("Device {} doesn't exist, or isn't there yet", deviceId);
576 return;
577 }
Jordan Halterman9d982a52017-12-11 15:27:37 -0800578 updateDevice(device).ifPresent(ld -> updatePorts(ld, device.id()));
Jon Hall7a8bfc62016-05-26 17:59:04 -0700579 });
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800580 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800581 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800582 }
alshabib7911a052014-10-16 17:49:37 -0700583
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700584 private class DeviceEventProcessor implements Runnable {
585
586 DeviceEvent event;
587
588 DeviceEventProcessor(DeviceEvent event) {
589 this.event = event;
590 }
591
alshabib7911a052014-10-16 17:49:37 -0700592 @Override
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700593 public void run() {
alshabib7911a052014-10-16 17:49:37 -0700594 Device device = event.subject();
alshabibacd91832014-10-17 14:38:41 -0700595 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700596 if (device == null) {
597 log.error("Device is null.");
598 return;
599 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700600 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700601 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700602 switch (event.type()) {
603 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700604 case DEVICE_UPDATED:
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700605 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700606 break;
607 case PORT_ADDED:
608 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700609 if (port.isEnabled()) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700610 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
alshabib7911a052014-10-16 17:49:37 -0700611 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700612 log.debug("Port down {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700613 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600614 providerService.linksVanished(new ConnectPoint(port.element().id(),
615 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700616 }
617 break;
618 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700619 log.debug("Port removed {}", port);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700620 removePort(port);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600621 providerService.linksVanished(new ConnectPoint(port.element().id(),
622 port.number()));
alshabib7911a052014-10-16 17:49:37 -0700623 break;
624 case DEVICE_REMOVED:
625 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700626 log.debug("Device removed {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700627 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600628 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700629 break;
630 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700631 if (deviceService.isAvailable(deviceId)) {
632 log.debug("Device up {}", deviceId);
alshabibe3af2652015-12-01 23:05:34 -0800633 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
alshabib7911a052014-10-16 17:49:37 -0700634 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700635 log.debug("Device down {}", deviceId);
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700636 removeDevice(deviceId);
Naoki Shiota399a0b32015-11-15 20:36:13 -0600637 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700638 }
639 break;
Jonathan Hart9de692c2015-04-23 11:45:47 -0700640 case PORT_STATS_UPDATED:
641 break;
alshabib7911a052014-10-16 17:49:37 -0700642 default:
643 log.debug("Unknown event {}", event);
644 }
645 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700646 }
alshabib7911a052014-10-16 17:49:37 -0700647
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700648 /**
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700649 * Processes device events.
650 */
651 private class InternalDeviceListener implements DeviceListener {
652 @Override
653 public void event(DeviceEvent event) {
654 if (event.type() == Type.PORT_STATS_UPDATED) {
655 return;
656 }
657
658 Runnable deviceEventProcessor = new DeviceEventProcessor(event);
659
660 eventExecutor.execute(deviceEventProcessor);
661 }
662 }
663
664 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700665 * Processes incoming packets.
666 */
667 private class InternalPacketProcessor implements PacketProcessor {
alshabib7911a052014-10-16 17:49:37 -0700668 @Override
669 public void process(PacketContext context) {
Thomas Vachuska347cc872015-09-23 10:25:29 -0700670 if (context == null || context.isHandled()) {
alshabib4a179dc2014-10-17 17:17:01 -0700671 return;
672 }
Thomas Vachuska347cc872015-09-23 10:25:29 -0700673
674 Ethernet eth = context.inPacket().parsed();
675 if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) {
676 return;
677 }
678
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700679 LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId());
alshabib7911a052014-10-16 17:49:37 -0700680 if (ld == null) {
681 return;
682 }
683
Jonathan Hartb35540a2015-11-17 09:30:56 -0800684 if (ld.handleLldp(context)) {
alshabib7911a052014-10-16 17:49:37 -0700685 context.block();
686 }
687 }
688 }
689
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700690 /**
691 * Auxiliary task to keep device ports up to date.
692 */
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800693 private final class SyncDeviceInfoTask implements Runnable {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800694 @Override
695 public void run() {
696 if (Thread.currentThread().isInterrupted()) {
697 log.info("Interrupted, quitting");
698 return;
699 }
700 // check what deviceService sees, to see if we are missing anything
701 try {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700702 loadDevices();
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800703 } catch (Exception e) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700704 // Catch all exceptions to avoid task being suppressed
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800705 log.error("Exception thrown during synchronization process", e);
706 }
707 }
708 }
709
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700710 /**
711 * Auxiliary task for pruning stale links.
712 */
713 private class LinkPrunerTask implements Runnable {
714 @Override
715 public void run() {
716 if (Thread.currentThread().isInterrupted()) {
717 log.info("Interrupted, quitting");
718 return;
719 }
720
721 try {
722 // TODO: There is still a slight possibility of mastership
723 // change occurring right with link going stale. This will
724 // result in the stale link not being pruned.
725 Maps.filterEntries(linkTimes, e -> {
726 if (!masterService.isLocalMaster(e.getKey().dst().deviceId())) {
727 return true;
728 }
729 if (isStale(e.getValue())) {
730 providerService.linkVanished(new DefaultLinkDescription(e.getKey().src(),
731 e.getKey().dst(),
732 DIRECT));
733 return true;
734 }
735 return false;
736 }).clear();
737
738 } catch (Exception e) {
739 // Catch all exceptions to avoid task being suppressed
Ray Milkeye80e18f2016-06-02 16:44:14 -0700740 if (!shuttingDown) {
741 // Error condition
742 log.error("Exception thrown during link pruning process", e);
743 } else {
744 // Provider is shutting down, the error can be ignored
745 log.trace("Shutting down, ignoring error", e);
746 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700747 }
748 }
749
750 private boolean isStale(long lastSeen) {
751 return lastSeen < System.currentTimeMillis() - staleLinkAge;
752 }
753 }
754
755 /**
756 * Provides processing context for the device link discovery helpers.
757 */
Ray Milkey957390e2016-02-09 10:02:46 -0800758 private class InternalDiscoveryContext implements LinkDiscoveryContext {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700759 @Override
760 public MastershipService mastershipService() {
761 return masterService;
762 }
763
764 @Override
765 public LinkProviderService providerService() {
766 return providerService;
767 }
768
769 @Override
770 public PacketService packetService() {
771 return packetService;
772 }
773
774 @Override
775 public long probeRate() {
776 return probeRate;
777 }
778
779 @Override
Jonathan Hartb35540a2015-11-17 09:30:56 -0800780 public boolean useBddp() {
781 return useBddp;
Thomas Vachuska05453c92015-09-09 14:40:49 -0700782 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700783
784 @Override
785 public void touchLink(LinkKey key) {
786 linkTimes.put(key, System.currentTimeMillis());
787 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800788
789 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700790 public DeviceService deviceService() {
791 return deviceService;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800792 }
793
794 @Override
Ayaka Koshibe48229222016-05-16 18:04:26 -0700795 public String fingerprint() {
796 return buildSrcMac();
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800797 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700798 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700799
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800800 static final EnumSet<NetworkConfigEvent.Type> CONFIG_CHANGED
801 = EnumSet.of(NetworkConfigEvent.Type.CONFIG_ADDED,
802 NetworkConfigEvent.Type.CONFIG_UPDATED,
803 NetworkConfigEvent.Type.CONFIG_REMOVED);
804
Naoki Shiota399a0b32015-11-15 20:36:13 -0600805 private class InternalConfigListener implements NetworkConfigListener {
806
807 private synchronized void reconfigureSuppressionRules(SuppressionConfig cfg) {
808 if (cfg == null) {
Ray Milkey0a8ee912016-06-13 09:58:12 -0700809 log.debug("Suppression Config is null.");
Naoki Shiota399a0b32015-11-15 20:36:13 -0600810 return;
811 }
812
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800813 SuppressionRules newRules = new SuppressionRules(cfg.deviceTypes(),
Naoki Shiota399a0b32015-11-15 20:36:13 -0600814 cfg.annotation());
815
816 updateRules(newRules);
817 }
818
Jon Hall125c3f22017-08-09 13:46:28 -0700819 private boolean isRelevantDeviceEvent(NetworkConfigEvent event) {
820 return event.configClass() == LinkDiscoveryFromDevice.class &&
821 CONFIG_CHANGED.contains(event.type());
822 }
823
824 private boolean isRelevantPortEvent(NetworkConfigEvent event) {
825 return event.configClass() == LinkDiscoveryFromPort.class &&
826 CONFIG_CHANGED.contains(event.type());
827 }
828
829 private boolean isRelevantSuppressionEvent(NetworkConfigEvent event) {
830 return (event.configClass().equals(SuppressionConfig.class) &&
831 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
832 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED));
833 }
834
Naoki Shiota399a0b32015-11-15 20:36:13 -0600835 @Override
836 public void event(NetworkConfigEvent event) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700837 eventExecutor.execute(() -> {
Jon Hall125c3f22017-08-09 13:46:28 -0700838 if (isRelevantDeviceEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800839 if (event.subject() instanceof DeviceId) {
840 final DeviceId did = (DeviceId) event.subject();
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800841 Device device = deviceService.getDevice(did);
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800842 updateDevice(device).ifPresent(ld -> updatePorts(ld, did));
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800843 }
Jon Hall125c3f22017-08-09 13:46:28 -0700844 } else if (isRelevantPortEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800845 if (event.subject() instanceof ConnectPoint) {
846 ConnectPoint cp = (ConnectPoint) event.subject();
847 if (cp.elementId() instanceof DeviceId) {
848 final DeviceId did = (DeviceId) cp.elementId();
849 Device device = deviceService.getDevice(did);
850 Port port = deviceService.getPort(did, cp.port());
851 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
852 }
853 }
Jon Hall125c3f22017-08-09 13:46:28 -0700854 } else if (isRelevantSuppressionEvent(event)) {
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800855 SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class);
856 reconfigureSuppressionRules(cfg);
857 log.trace("Network config reconfigured");
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800858 }
Thomas Vachuskab5f6f522016-03-01 13:52:10 -0800859 });
Naoki Shiota399a0b32015-11-15 20:36:13 -0600860 }
861 }
alshabib7911a052014-10-16 17:49:37 -0700862}