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