Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.lldp.impl; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Property; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ethernet; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipEvent; |
| 34 | import org.onosproject.mastership.MastershipListener; |
| 35 | import org.onosproject.mastership.MastershipService; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 39 | import org.onosproject.net.LinkKey; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.Port; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 41 | import org.onosproject.net.config.ConfigFactory; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
| 43 | import org.onosproject.net.config.NetworkConfigListener; |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceEvent; |
| 46 | import org.onosproject.net.device.DeviceListener; |
| 47 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 48 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.TrafficSelector; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 50 | import org.onosproject.net.link.DefaultLinkDescription; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.link.LinkProvider; |
| 52 | import org.onosproject.net.link.LinkProviderRegistry; |
| 53 | import org.onosproject.net.link.LinkProviderService; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 54 | import org.onosproject.net.link.LinkService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 55 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 56 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.net.packet.PacketProcessor; |
| 58 | import org.onosproject.net.packet.PacketService; |
| 59 | import org.onosproject.net.provider.AbstractProvider; |
| 60 | import org.onosproject.net.provider.ProviderId; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 61 | import org.osgi.service.component.ComponentContext; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 64 | import java.util.Dictionary; |
| 65 | import java.util.EnumSet; |
| 66 | import java.util.Map; |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 67 | import java.util.Optional; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 68 | import java.util.Properties; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 69 | import java.util.Set; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 70 | import java.util.concurrent.ConcurrentHashMap; |
| 71 | import java.util.concurrent.ScheduledExecutorService; |
| 72 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 73 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 74 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 75 | import static java.util.concurrent.TimeUnit.SECONDS; |
Thomas Vachuska | 347cc87 | 2015-09-23 10:25:29 -0700 | [diff] [blame] | 76 | import static org.onlab.packet.Ethernet.TYPE_BSN; |
| 77 | import static org.onlab.packet.Ethernet.TYPE_LLDP; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 78 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 79 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 80 | import static org.onosproject.net.Link.Type.DIRECT; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 81 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 82 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 83 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 84 | /** |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 85 | * Provider which uses LLDP and BDDP packets to detect network infrastructure links. |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 86 | */ |
| 87 | @Component(immediate = true) |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 88 | public class LldpLinkProvider extends AbstractProvider implements LinkProvider { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 89 | |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 90 | private static final String PROVIDER_NAME = "org.onosproject.provider.lldp"; |
| 91 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 92 | private static final String FORMAT = |
| 93 | "Settings: enabled={}, useBDDP={}, probeRate={}, " + |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 94 | "staleLinkAge={}"; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 95 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 96 | // When a Device/Port has this annotation, do not send out LLDP/BDDP |
| 97 | public static final String NO_LLDP = "no-lldp"; |
| 98 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 99 | private final Logger log = getLogger(getClass()); |
| 100 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected CoreService coreService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 105 | protected LinkProviderRegistry providerRegistry; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected DeviceService deviceService; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 111 | protected LinkService linkService; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 114 | protected PacketService packetService; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 115 | |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected MastershipService masterService; |
| 118 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected ComponentConfigService cfgService; |
| 121 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | protected ClusterService clusterService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 126 | protected NetworkConfigRegistry cfgRegistry; |
| 127 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 128 | private LinkProviderService providerService; |
| 129 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 130 | private ScheduledExecutorService executor; |
| 131 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 132 | // TODO: Add sanity checking for the configurable params based on the delays |
| 133 | private static final long DEVICE_SYNC_DELAY = 5; |
| 134 | private static final long LINK_PRUNER_DELAY = 3; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 135 | |
| 136 | private static final String PROP_ENABLED = "enabled"; |
| 137 | @Property(name = PROP_ENABLED, boolValue = true, |
| 138 | label = "If false, link discovery is disabled") |
| 139 | private boolean enabled = false; |
| 140 | |
| 141 | private static final String PROP_USE_BDDP = "useBDDP"; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 142 | @Property(name = PROP_USE_BDDP, boolValue = true, |
| 143 | label = "Use BDDP for link discovery") |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 144 | private boolean useBddp = true; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 145 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 146 | private static final String PROP_PROBE_RATE = "probeRate"; |
| 147 | private static final int DEFAULT_PROBE_RATE = 3_000; |
| 148 | @Property(name = PROP_PROBE_RATE, intValue = DEFAULT_PROBE_RATE, |
| 149 | label = "LLDP and BDDP probe rate specified in millis") |
| 150 | private int probeRate = DEFAULT_PROBE_RATE; |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 151 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 152 | private static final String PROP_STALE_LINK_AGE = "staleLinkAge"; |
| 153 | private static final int DEFAULT_STALE_LINK_AGE = 10_000; |
| 154 | @Property(name = PROP_STALE_LINK_AGE, intValue = DEFAULT_STALE_LINK_AGE, |
| 155 | label = "Number of millis beyond which links will be considered stale") |
| 156 | private int staleLinkAge = DEFAULT_STALE_LINK_AGE; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 157 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 158 | private final DiscoveryContext context = new InternalDiscoveryContext(); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 159 | private final InternalRoleListener roleListener = new InternalRoleListener(); |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 160 | private final InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 161 | private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor(); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 162 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 163 | // Device link discovery helpers. |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 164 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 165 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 166 | // Most recent time a tracked link was seen; links are tracked if their |
| 167 | // destination connection point is mastered by this controller instance. |
| 168 | private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap(); |
| 169 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 170 | private ApplicationId appId; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 171 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 172 | static final SuppressionRules DEFAULT_RULES |
| 173 | = new SuppressionRules(ImmutableSet.of(), |
| 174 | EnumSet.of(Device.Type.ROADM), |
| 175 | ImmutableMap.of(NO_LLDP, SuppressionRules.ANY_VALUE)); |
| 176 | |
| 177 | private SuppressionRules rules = LldpLinkProvider.DEFAULT_RULES; |
| 178 | |
| 179 | public static final String CONFIG_KEY = "suppression"; |
| 180 | |
| 181 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 182 | new ConfigFactory<ApplicationId, SuppressionConfig>(APP_SUBJECT_FACTORY, |
| 183 | SuppressionConfig.class, |
| 184 | CONFIG_KEY) { |
| 185 | @Override |
| 186 | public SuppressionConfig createConfig() { |
| 187 | return new SuppressionConfig(); |
| 188 | } |
| 189 | } |
| 190 | ); |
| 191 | |
| 192 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 193 | |
| 194 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Creates an OpenFlow link provider. |
| 197 | */ |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 198 | public LldpLinkProvider() { |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 199 | super(new ProviderId("lldp", PROVIDER_NAME)); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Activate |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 203 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 204 | cfgService.registerProperties(getClass()); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 205 | appId = coreService.registerApplication(PROVIDER_NAME); |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 206 | |
| 207 | cfgRegistry.addListener(cfgListener); |
| 208 | factories.forEach(cfgRegistry::registerConfigFactory); |
| 209 | |
| 210 | SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class); |
| 211 | if (cfg == null) { |
| 212 | // If no configuration is found, register default. |
| 213 | cfg = cfgRegistry.addConfig(appId, SuppressionConfig.class); |
| 214 | cfg.deviceIds(DEFAULT_RULES.getSuppressedDevice()) |
| 215 | .deviceTypes(DEFAULT_RULES.getSuppressedDeviceType()) |
| 216 | .annotation(DEFAULT_RULES.getSuppressedAnnotation()) |
| 217 | .apply(); |
| 218 | } |
| 219 | cfgListener.reconfigureSuppressionRules(cfg); |
| 220 | |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 221 | modified(context); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 222 | log.info("Started"); |
| 223 | } |
| 224 | |
| 225 | @Deactivate |
| 226 | public void deactivate() { |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 227 | cfgRegistry.removeListener(cfgListener); |
| 228 | factories.forEach(cfgRegistry::unregisterConfigFactory); |
| 229 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 230 | cfgService.unregisterProperties(getClass(), false); |
| 231 | disable(); |
| 232 | log.info("Stopped"); |
| 233 | } |
| 234 | |
| 235 | @Modified |
| 236 | public void modified(ComponentContext context) { |
| 237 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 238 | |
| 239 | boolean newEnabled, newUseBddp; |
| 240 | int newProbeRate, newStaleLinkAge; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 241 | try { |
| 242 | String s = get(properties, PROP_ENABLED); |
| 243 | newEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim()); |
| 244 | |
| 245 | s = get(properties, PROP_USE_BDDP); |
| 246 | newUseBddp = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim()); |
| 247 | |
| 248 | s = get(properties, PROP_PROBE_RATE); |
| 249 | newProbeRate = isNullOrEmpty(s) ? probeRate : Integer.parseInt(s.trim()); |
| 250 | |
| 251 | s = get(properties, PROP_STALE_LINK_AGE); |
| 252 | newStaleLinkAge = isNullOrEmpty(s) ? staleLinkAge : Integer.parseInt(s.trim()); |
| 253 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 254 | } catch (NumberFormatException e) { |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 255 | log.warn("Component configuration had invalid values", e); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 256 | newEnabled = enabled; |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 257 | newUseBddp = useBddp; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 258 | newProbeRate = probeRate; |
| 259 | newStaleLinkAge = staleLinkAge; |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 260 | } |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 261 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 262 | boolean wasEnabled = enabled; |
| 263 | |
| 264 | enabled = newEnabled; |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 265 | useBddp = newUseBddp; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 266 | probeRate = newProbeRate; |
| 267 | staleLinkAge = newStaleLinkAge; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 268 | |
| 269 | if (!wasEnabled && enabled) { |
| 270 | enable(); |
| 271 | } else if (wasEnabled && !enabled) { |
| 272 | disable(); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 273 | } else { |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 274 | if (enabled) { |
| 275 | // update all discovery helper state |
| 276 | loadDevices(); |
| 277 | } |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 280 | log.info(FORMAT, enabled, useBddp, probeRate, staleLinkAge); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Enables link discovery processing. |
| 285 | */ |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 286 | private void enable() { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 287 | providerService = providerRegistry.register(this); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 288 | masterService.addListener(roleListener); |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 289 | deviceService.addListener(deviceListener); |
| 290 | packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0)); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 291 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 292 | loadDevices(); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 293 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 294 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/link", "discovery-%d")); |
| 295 | executor.scheduleAtFixedRate(new SyncDeviceInfoTask(), |
| 296 | DEVICE_SYNC_DELAY, DEVICE_SYNC_DELAY, SECONDS); |
| 297 | executor.scheduleAtFixedRate(new LinkPrunerTask(), |
| 298 | LINK_PRUNER_DELAY, LINK_PRUNER_DELAY, SECONDS); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 299 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 300 | requestIntercepts(); |
| 301 | } |
| 302 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Disables link discovery processing. |
| 305 | */ |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 306 | private void disable() { |
| 307 | withdrawIntercepts(); |
| 308 | |
| 309 | providerRegistry.unregister(this); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 310 | masterService.removeListener(roleListener); |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 311 | deviceService.removeListener(deviceListener); |
| 312 | packetService.removeProcessor(packetProcessor); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 313 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 314 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 315 | if (executor != null) { |
| 316 | executor.shutdownNow(); |
| 317 | } |
| 318 | discoverers.values().forEach(LinkDiscovery::stop); |
| 319 | discoverers.clear(); |
| 320 | |
| 321 | providerService = null; |
| 322 | } |
| 323 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 324 | /** |
| 325 | * Loads available devices and registers their ports to be probed. |
| 326 | */ |
| 327 | private void loadDevices() { |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 328 | if (!enabled) { |
| 329 | return; |
| 330 | } |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 331 | deviceService.getAvailableDevices() |
| 332 | .forEach(d -> updateDevice(d) |
| 333 | .ifPresent(ld -> updatePorts(ld, d.id()))); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Updates discovery helper for specified device. |
| 338 | * |
| 339 | * Adds and starts a discovery helper for specified device if enabled, |
| 340 | * calls {@link #removeDevice(DeviceId)} otherwise. |
| 341 | * |
| 342 | * @param device device to add |
| 343 | * @return discovery helper if discovery is enabled for the device |
| 344 | */ |
| 345 | private Optional<LinkDiscovery> updateDevice(Device device) { |
| 346 | if (rules.isSuppressed(device)) { |
| 347 | log.trace("LinkDiscovery from {} disabled by configuration", device.id()); |
| 348 | removeDevice(device.id()); |
| 349 | return Optional.empty(); |
| 350 | } |
| 351 | LinkDiscovery ld = discoverers.computeIfAbsent(device.id(), |
| 352 | did -> new LinkDiscovery(device, context)); |
| 353 | if (ld.isStopped()) { |
| 354 | ld.start(); |
| 355 | } |
| 356 | return Optional.of(ld); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Removes after stopping discovery helper for specified device. |
| 361 | * @param deviceId device to remove |
| 362 | */ |
| 363 | private void removeDevice(final DeviceId deviceId) { |
| 364 | discoverers.computeIfPresent(deviceId, (did, ld) -> { |
| 365 | ld.stop(); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 366 | return null; |
| 367 | }); |
| 368 | |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Updates ports of the specified device to the specified discovery helper. |
| 373 | */ |
| 374 | private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) { |
| 375 | deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p)); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Updates discovery helper state of the specified port. |
| 380 | * |
| 381 | * Adds a port to the discovery helper if up and discovery is enabled, |
| 382 | * or calls {@link #removePort(Port)} otherwise. |
| 383 | */ |
| 384 | private void updatePort(LinkDiscovery discoverer, Port port) { |
HIGUCHI Yuta | b853b3f | 2015-11-17 18:43:20 -0800 | [diff] [blame] | 385 | if (port.number().isLogical()) { |
| 386 | // silently ignore logical ports |
| 387 | return; |
| 388 | } |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 389 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 390 | if (rules.isSuppressed(port)) { |
| 391 | log.trace("LinkDiscovery from {} disabled by configuration", port); |
| 392 | removePort(port); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | // check if enabled and turn off discovery? |
| 397 | if (!port.isEnabled()) { |
| 398 | removePort(port); |
| 399 | return; |
| 400 | } |
| 401 | |
HIGUCHI Yuta | b853b3f | 2015-11-17 18:43:20 -0800 | [diff] [blame] | 402 | discoverer.addPort(port); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 405 | /** |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 406 | * Removes a port from the specified discovery helper. |
| 407 | * @param port the port |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 408 | */ |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 409 | private void removePort(Port port) { |
| 410 | if (port.element() instanceof Device) { |
| 411 | Device d = (Device) port.element(); |
| 412 | LinkDiscovery ld = discoverers.get(d.id()); |
| 413 | if (ld != null) { |
| 414 | ld.removePort(port.number()); |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 415 | } |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 416 | } else { |
| 417 | log.warn("Attempted to remove non-Device port", port); |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 421 | /** |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 422 | * Requests packet intercepts. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 423 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 424 | private void requestIntercepts() { |
| 425 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Thomas Vachuska | 347cc87 | 2015-09-23 10:25:29 -0700 | [diff] [blame] | 426 | selector.matchEthType(TYPE_LLDP); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 427 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 428 | |
Thomas Vachuska | 347cc87 | 2015-09-23 10:25:29 -0700 | [diff] [blame] | 429 | selector.matchEthType(TYPE_BSN); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 430 | if (useBddp) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 431 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 432 | } else { |
| 433 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 437 | /** |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 438 | * Withdraws packet intercepts. |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 439 | */ |
| 440 | private void withdrawIntercepts() { |
| 441 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Thomas Vachuska | 347cc87 | 2015-09-23 10:25:29 -0700 | [diff] [blame] | 442 | selector.matchEthType(TYPE_LLDP); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 443 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Thomas Vachuska | 347cc87 | 2015-09-23 10:25:29 -0700 | [diff] [blame] | 444 | selector.matchEthType(TYPE_BSN); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 445 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 446 | } |
| 447 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 448 | protected SuppressionRules rules() { |
| 449 | return rules; |
| 450 | } |
| 451 | |
| 452 | protected void updateRules(SuppressionRules newRules) { |
| 453 | if (!rules.equals(newRules)) { |
| 454 | rules = newRules; |
| 455 | loadDevices(); |
| 456 | } |
| 457 | } |
| 458 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 459 | /** |
| 460 | * Processes device mastership role changes. |
| 461 | */ |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 462 | private class InternalRoleListener implements MastershipListener { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 463 | @Override |
| 464 | public void event(MastershipEvent event) { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 465 | if (MastershipEvent.Type.BACKUPS_CHANGED.equals(event.type())) { |
| 466 | // only need new master events |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | DeviceId deviceId = event.subject(); |
| 471 | Device device = deviceService.getDevice(deviceId); |
| 472 | if (device == null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 473 | log.debug("Device {} doesn't exist, or isn't there yet", deviceId); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 474 | return; |
| 475 | } |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 476 | if (clusterService.getLocalNode().id().equals(event.roleInfo().master())) { |
| 477 | updateDevice(device).ifPresent(ld -> updatePorts(ld, device.id())); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 478 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 479 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 480 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 481 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Processes device events. |
| 484 | */ |
| 485 | private class InternalDeviceListener implements DeviceListener { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 486 | @Override |
| 487 | public void event(DeviceEvent event) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 488 | Device device = event.subject(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 489 | Port port = event.port(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 490 | if (device == null) { |
| 491 | log.error("Device is null."); |
| 492 | return; |
| 493 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 494 | log.trace("{} {} {}", event.type(), event.subject(), event); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 495 | final DeviceId deviceId = device.id(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 496 | switch (event.type()) { |
| 497 | case DEVICE_ADDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 498 | case DEVICE_UPDATED: |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 499 | updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId)); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 500 | break; |
| 501 | case PORT_ADDED: |
| 502 | case PORT_UPDATED: |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 503 | if (port.isEnabled()) { |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 504 | updateDevice(device).ifPresent(ld -> updatePort(ld, port)); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 505 | } else { |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 506 | log.debug("Port down {}", port); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 507 | removePort(port); |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 508 | providerService.linksVanished(new ConnectPoint(port.element().id(), |
| 509 | port.number())); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 510 | } |
| 511 | break; |
| 512 | case PORT_REMOVED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 513 | log.debug("Port removed {}", port); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 514 | removePort(port); |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 515 | providerService.linksVanished(new ConnectPoint(port.element().id(), |
| 516 | port.number())); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 517 | break; |
| 518 | case DEVICE_REMOVED: |
| 519 | case DEVICE_SUSPENDED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 520 | log.debug("Device removed {}", deviceId); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 521 | removeDevice(deviceId); |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 522 | providerService.linksVanished(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 523 | break; |
| 524 | case DEVICE_AVAILABILITY_CHANGED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 525 | if (deviceService.isAvailable(deviceId)) { |
| 526 | log.debug("Device up {}", deviceId); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 527 | updateDevice(device); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 528 | } else { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 529 | log.debug("Device down {}", deviceId); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 530 | removeDevice(deviceId); |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 531 | providerService.linksVanished(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 532 | } |
| 533 | break; |
Jonathan Hart | 9de692c | 2015-04-23 11:45:47 -0700 | [diff] [blame] | 534 | case PORT_STATS_UPDATED: |
| 535 | break; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 536 | default: |
| 537 | log.debug("Unknown event {}", event); |
| 538 | } |
| 539 | } |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 540 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 541 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 542 | /** |
| 543 | * Processes incoming packets. |
| 544 | */ |
| 545 | private class InternalPacketProcessor implements PacketProcessor { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 546 | @Override |
| 547 | public void process(PacketContext context) { |
Thomas Vachuska | 347cc87 | 2015-09-23 10:25:29 -0700 | [diff] [blame] | 548 | if (context == null || context.isHandled()) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
Thomas Vachuska | 347cc87 | 2015-09-23 10:25:29 -0700 | [diff] [blame] | 551 | |
| 552 | Ethernet eth = context.inPacket().parsed(); |
| 553 | if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) { |
| 554 | return; |
| 555 | } |
| 556 | |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 557 | LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 558 | if (ld == null) { |
| 559 | return; |
| 560 | } |
| 561 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 562 | if (ld.handleLldp(context)) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 563 | context.block(); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 568 | /** |
| 569 | * Auxiliary task to keep device ports up to date. |
| 570 | */ |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 571 | private final class SyncDeviceInfoTask implements Runnable { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 572 | @Override |
| 573 | public void run() { |
| 574 | if (Thread.currentThread().isInterrupted()) { |
| 575 | log.info("Interrupted, quitting"); |
| 576 | return; |
| 577 | } |
| 578 | // check what deviceService sees, to see if we are missing anything |
| 579 | try { |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 580 | loadDevices(); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 581 | } catch (Exception e) { |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 582 | // Catch all exceptions to avoid task being suppressed |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 583 | log.error("Exception thrown during synchronization process", e); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 588 | /** |
| 589 | * Auxiliary task for pruning stale links. |
| 590 | */ |
| 591 | private class LinkPrunerTask implements Runnable { |
| 592 | @Override |
| 593 | public void run() { |
| 594 | if (Thread.currentThread().isInterrupted()) { |
| 595 | log.info("Interrupted, quitting"); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | try { |
| 600 | // TODO: There is still a slight possibility of mastership |
| 601 | // change occurring right with link going stale. This will |
| 602 | // result in the stale link not being pruned. |
| 603 | Maps.filterEntries(linkTimes, e -> { |
| 604 | if (!masterService.isLocalMaster(e.getKey().dst().deviceId())) { |
| 605 | return true; |
| 606 | } |
| 607 | if (isStale(e.getValue())) { |
| 608 | providerService.linkVanished(new DefaultLinkDescription(e.getKey().src(), |
| 609 | e.getKey().dst(), |
| 610 | DIRECT)); |
| 611 | return true; |
| 612 | } |
| 613 | return false; |
| 614 | }).clear(); |
| 615 | |
| 616 | } catch (Exception e) { |
| 617 | // Catch all exceptions to avoid task being suppressed |
| 618 | log.error("Exception thrown during link pruning process", e); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | private boolean isStale(long lastSeen) { |
| 623 | return lastSeen < System.currentTimeMillis() - staleLinkAge; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Provides processing context for the device link discovery helpers. |
| 629 | */ |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 630 | private class InternalDiscoveryContext implements DiscoveryContext { |
| 631 | @Override |
| 632 | public MastershipService mastershipService() { |
| 633 | return masterService; |
| 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public LinkProviderService providerService() { |
| 638 | return providerService; |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public PacketService packetService() { |
| 643 | return packetService; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public long probeRate() { |
| 648 | return probeRate; |
| 649 | } |
| 650 | |
| 651 | @Override |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 652 | public boolean useBddp() { |
| 653 | return useBddp; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 654 | } |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 655 | |
| 656 | @Override |
| 657 | public void touchLink(LinkKey key) { |
| 658 | linkTimes.put(key, System.currentTimeMillis()); |
| 659 | } |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 660 | } |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 661 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 662 | private class InternalConfigListener implements NetworkConfigListener { |
| 663 | |
| 664 | private synchronized void reconfigureSuppressionRules(SuppressionConfig cfg) { |
| 665 | if (cfg == null) { |
| 666 | log.error("Suppression Config is null."); |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | SuppressionRules newRules = new SuppressionRules(cfg.deviceIds(), |
| 671 | cfg.deviceTypes(), |
| 672 | cfg.annotation()); |
| 673 | |
| 674 | updateRules(newRules); |
| 675 | } |
| 676 | |
| 677 | @Override |
| 678 | public void event(NetworkConfigEvent event) { |
| 679 | if (event.configClass().equals(SuppressionConfig.class) && |
| 680 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 681 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) { |
| 682 | SuppressionConfig cfg = cfgRegistry.getConfig(appId, SuppressionConfig.class); |
| 683 | reconfigureSuppressionRules(cfg); |
| 684 | log.trace("Network config reconfigured"); |
| 685 | } |
| 686 | } |
| 687 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 688 | } |