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