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