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