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