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