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