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