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