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 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableSet; |
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 | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.mastership.MastershipEvent; |
| 33 | import org.onosproject.mastership.MastershipListener; |
| 34 | import org.onosproject.mastership.MastershipService; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.device.DeviceEvent; |
| 40 | import org.onosproject.net.device.DeviceListener; |
| 41 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.link.LinkProvider; |
| 45 | import org.onosproject.net.link.LinkProviderRegistry; |
| 46 | import org.onosproject.net.link.LinkProviderService; |
| 47 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 48 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.packet.PacketProcessor; |
| 50 | import org.onosproject.net.packet.PacketService; |
| 51 | import org.onosproject.net.provider.AbstractProvider; |
| 52 | import org.onosproject.net.provider.ProviderId; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 53 | import org.osgi.service.component.ComponentContext; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 56 | import java.io.IOException; |
| 57 | import java.util.Dictionary; |
| 58 | import java.util.EnumSet; |
| 59 | import java.util.Map; |
| 60 | import java.util.concurrent.ConcurrentHashMap; |
| 61 | import java.util.concurrent.ScheduledExecutorService; |
| 62 | |
| 63 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 64 | import static java.util.concurrent.TimeUnit.SECONDS; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 65 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 66 | import static org.onlab.util.Tools.groupedThreads; |
| 67 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 68 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Provider which uses an OpenFlow controller to detect network |
| 71 | * infrastructure links. |
| 72 | */ |
| 73 | @Component(immediate = true) |
| 74 | public class LLDPLinkProvider extends AbstractProvider implements LinkProvider { |
| 75 | |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 76 | private static final String PROVIDER_NAME = "org.onosproject.provider.lldp"; |
| 77 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 78 | private static final String PROP_USE_BDDP = "useBDDP"; |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 79 | private static final String PROP_DISABLE_LD = "disableLinkDiscovery"; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 80 | private static final String PROP_LLDP_SUPPRESSION = "lldpSuppression"; |
| 81 | |
| 82 | private static final String DEFAULT_LLDP_SUPPRESSION_CONFIG = "../config/lldp_suppression.json"; |
| 83 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 84 | private final Logger log = getLogger(getClass()); |
| 85 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected CoreService coreService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 90 | protected LinkProviderRegistry providerRegistry; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected DeviceService deviceService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 96 | protected PacketService packetService; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 97 | |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected MastershipService masterService; |
| 100 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected ComponentConfigService cfgService; |
| 103 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 104 | private LinkProviderService providerService; |
| 105 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 106 | private ScheduledExecutorService executor; |
| 107 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 108 | @Property(name = PROP_USE_BDDP, boolValue = true, |
| 109 | label = "Use BDDP for link discovery") |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 110 | private boolean useBDDP = true; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 111 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 112 | @Property(name = PROP_DISABLE_LD, boolValue = false, |
| 113 | label = "Permanently disable link discovery") |
| 114 | private boolean disableLinkDiscovery = false; |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 115 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 116 | private static final long INIT_DELAY = 5; |
| 117 | private static final long DELAY = 5; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 118 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 119 | @Property(name = PROP_LLDP_SUPPRESSION, value = DEFAULT_LLDP_SUPPRESSION_CONFIG, |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 120 | label = "Path to LLDP suppression configuration file") |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 121 | private String lldpSuppression = DEFAULT_LLDP_SUPPRESSION_CONFIG; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 122 | |
| 123 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 124 | private final InternalLinkProvider listener = new InternalLinkProvider(); |
| 125 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 126 | private final InternalRoleListener roleListener = new InternalRoleListener(); |
| 127 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 128 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 129 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 130 | private SuppressionRules rules; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 131 | private ApplicationId appId; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 132 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Creates an OpenFlow link provider. |
| 135 | */ |
| 136 | public LLDPLinkProvider() { |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 137 | super(new ProviderId("lldp", PROVIDER_NAME)); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Activate |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 141 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 142 | cfgService.registerProperties(getClass()); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 143 | appId = coreService.registerApplication(PROVIDER_NAME); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 144 | |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 145 | // to load configuration at startup |
| 146 | modified(context); |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 147 | if (disableLinkDiscovery) { |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 148 | log.info("LinkDiscovery has been permanently disabled by configuration"); |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 149 | return; |
| 150 | } |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 151 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 152 | providerService = providerRegistry.register(this); |
| 153 | deviceService.addListener(listener); |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 154 | packetService.addProcessor(listener, PacketProcessor.advisor(0)); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 155 | masterService.addListener(roleListener); |
| 156 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 157 | LinkDiscovery ld; |
alshabib | 5dc5a34 | 2014-12-03 14:11:16 -0800 | [diff] [blame] | 158 | for (Device device : deviceService.getAvailableDevices()) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 159 | if (rules.isSuppressed(device)) { |
| 160 | log.debug("LinkDiscovery from {} disabled by configuration", device.id()); |
| 161 | continue; |
| 162 | } |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 163 | ld = new LinkDiscovery(device, packetService, masterService, |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 164 | providerService, useBDDP); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 165 | discoverers.put(device.id(), ld); |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 166 | addPorts(ld, device.id()); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 167 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 168 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 169 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "sync-%d")); |
| 170 | executor.scheduleAtFixedRate(new SyncDeviceInfoTask(), INIT_DELAY, DELAY, SECONDS); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 171 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 172 | requestIntercepts(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 173 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 174 | log.info("Started"); |
| 175 | } |
| 176 | |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 177 | private void addPorts(LinkDiscovery discoverer, DeviceId deviceId) { |
| 178 | for (Port p : deviceService.getPorts(deviceId)) { |
| 179 | if (rules.isSuppressed(p)) { |
| 180 | continue; |
| 181 | } |
| 182 | if (!p.number().isLogical()) { |
| 183 | discoverer.addPort(p); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 188 | @Deactivate |
| 189 | public void deactivate() { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 190 | cfgService.unregisterProperties(getClass(), false); |
| 191 | if (disableLinkDiscovery) { |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 192 | return; |
| 193 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 194 | |
| 195 | withdrawIntercepts(); |
| 196 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 197 | providerRegistry.unregister(this); |
| 198 | deviceService.removeListener(listener); |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 199 | packetService.removeProcessor(listener); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 200 | masterService.removeListener(roleListener); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 201 | |
| 202 | executor.shutdownNow(); |
| 203 | discoverers.values().forEach(LinkDiscovery::stop); |
| 204 | discoverers.clear(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 205 | providerService = null; |
| 206 | |
| 207 | log.info("Stopped"); |
| 208 | } |
| 209 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 210 | @Modified |
| 211 | public void modified(ComponentContext context) { |
| 212 | if (context == null) { |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 213 | loadSuppressionRules(); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | @SuppressWarnings("rawtypes") |
| 217 | Dictionary properties = context.getProperties(); |
| 218 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 219 | String s = get(properties, PROP_DISABLE_LD); |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 220 | if (!Strings.isNullOrEmpty(s)) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 221 | disableLinkDiscovery = Boolean.valueOf(s); |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 222 | } |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 223 | s = get(properties, PROP_USE_BDDP); |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 224 | if (!Strings.isNullOrEmpty(s)) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 225 | useBDDP = Boolean.valueOf(s); |
| 226 | } |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 227 | s = get(properties, PROP_LLDP_SUPPRESSION); |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 228 | if (!Strings.isNullOrEmpty(s)) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 229 | lldpSuppression = s; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 230 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 231 | requestIntercepts(); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 232 | loadSuppressionRules(); |
| 233 | } |
| 234 | |
| 235 | private void loadSuppressionRules() { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 236 | SuppressionRulesStore store = new SuppressionRulesStore(lldpSuppression); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 237 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 238 | log.info("Reading suppression rules from {}", lldpSuppression); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 239 | rules = store.read(); |
| 240 | } catch (IOException e) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 241 | log.info("Failed to load {}, using built-in rules", lldpSuppression); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 242 | // default rule to suppress ROADM to maintain compatibility |
| 243 | rules = new SuppressionRules(ImmutableSet.of(), |
| 244 | EnumSet.of(Device.Type.ROADM), |
| 245 | ImmutableMap.of()); |
| 246 | } |
| 247 | |
| 248 | // should refresh discoverers when we need dynamic reconfiguration |
| 249 | } |
| 250 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 251 | /** |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 252 | * Request packet intercepts. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 253 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 254 | private void requestIntercepts() { |
| 255 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 256 | selector.matchEthType(Ethernet.TYPE_LLDP); |
| 257 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 258 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 259 | selector.matchEthType(Ethernet.TYPE_BSN); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 260 | if (useBDDP) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 261 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 262 | } else { |
| 263 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Withdraw packet intercepts. |
| 269 | */ |
| 270 | private void withdrawIntercepts() { |
| 271 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 272 | selector.matchEthType(Ethernet.TYPE_LLDP); |
| 273 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 274 | selector.matchEthType(Ethernet.TYPE_BSN); |
| 275 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 276 | } |
| 277 | |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 278 | private LinkDiscovery createLinkDiscovery(Device device) { |
| 279 | return new LinkDiscovery(device, packetService, masterService, |
| 280 | providerService, useBDDP); |
| 281 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 282 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 283 | private class InternalRoleListener implements MastershipListener { |
| 284 | |
| 285 | @Override |
| 286 | public void event(MastershipEvent event) { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 287 | if (MastershipEvent.Type.BACKUPS_CHANGED.equals(event.type())) { |
| 288 | // only need new master events |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | DeviceId deviceId = event.subject(); |
| 293 | Device device = deviceService.getDevice(deviceId); |
| 294 | if (device == null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 295 | log.debug("Device {} doesn't exist, or isn't there yet", deviceId); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 296 | return; |
| 297 | } |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 298 | if (rules.isSuppressed(device)) { |
| 299 | return; |
| 300 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 301 | synchronized (discoverers) { |
| 302 | if (!discoverers.containsKey(deviceId)) { |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 303 | // ideally, should never reach here |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 304 | log.debug("Device mastership changed ({}) {}", event.type(), deviceId); |
| 305 | discoverers.put(deviceId, createLinkDiscovery(device)); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 311 | |
| 312 | private class InternalLinkProvider implements PacketProcessor, DeviceListener { |
| 313 | |
| 314 | @Override |
| 315 | public void event(DeviceEvent event) { |
| 316 | LinkDiscovery ld = null; |
| 317 | Device device = event.subject(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 318 | Port port = event.port(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 319 | if (device == null) { |
| 320 | log.error("Device is null."); |
| 321 | return; |
| 322 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 323 | log.trace("{} {} {}", event.type(), event.subject(), event); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 324 | final DeviceId deviceId = device.id(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 325 | switch (event.type()) { |
| 326 | case DEVICE_ADDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 327 | case DEVICE_UPDATED: |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 328 | synchronized (discoverers) { |
| 329 | ld = discoverers.get(deviceId); |
| 330 | if (ld == null) { |
| 331 | if (rules.isSuppressed(device)) { |
| 332 | log.debug("LinkDiscovery from {} disabled by configuration", device.id()); |
| 333 | return; |
| 334 | } |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 335 | log.debug("Device added ({}) {}", event.type(), deviceId); |
| 336 | discoverers.put(deviceId, createLinkDiscovery(device)); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 337 | } else { |
| 338 | if (ld.isStopped()) { |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 339 | log.debug("Device restarted ({}) {}", event.type(), deviceId); |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 340 | ld.start(); |
| 341 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 342 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 343 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 344 | break; |
| 345 | case PORT_ADDED: |
| 346 | case PORT_UPDATED: |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 347 | if (port.isEnabled()) { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 348 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 349 | if (ld == null) { |
| 350 | return; |
| 351 | } |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 352 | if (rules.isSuppressed(port)) { |
| 353 | log.debug("LinkDiscovery from {}@{} disabled by configuration", |
| 354 | port.number(), device.id()); |
| 355 | return; |
| 356 | } |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 357 | if (!port.number().isLogical()) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 358 | log.debug("Port added {}", port); |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 359 | ld.addPort(port); |
| 360 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 361 | } else { |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 362 | log.debug("Port down {}", port); |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 363 | ConnectPoint point = new ConnectPoint(deviceId, port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 364 | providerService.linksVanished(point); |
| 365 | } |
| 366 | break; |
| 367 | case PORT_REMOVED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 368 | log.debug("Port removed {}", port); |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 369 | ConnectPoint point = new ConnectPoint(deviceId, port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 370 | providerService.linksVanished(point); |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 371 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 372 | break; |
| 373 | case DEVICE_REMOVED: |
| 374 | case DEVICE_SUSPENDED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 375 | log.debug("Device removed {}", deviceId); |
| 376 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 377 | if (ld == null) { |
| 378 | return; |
| 379 | } |
| 380 | ld.stop(); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 381 | providerService.linksVanished(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 382 | break; |
| 383 | case DEVICE_AVAILABILITY_CHANGED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 384 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 385 | if (ld == null) { |
| 386 | return; |
| 387 | } |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 388 | if (deviceService.isAvailable(deviceId)) { |
| 389 | log.debug("Device up {}", deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 390 | ld.start(); |
| 391 | } else { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 392 | providerService.linksVanished(deviceId); |
| 393 | log.debug("Device down {}", deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 394 | ld.stop(); |
| 395 | } |
| 396 | break; |
Jonathan Hart | 9de692c | 2015-04-23 11:45:47 -0700 | [diff] [blame] | 397 | case PORT_STATS_UPDATED: |
| 398 | break; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 399 | default: |
| 400 | log.debug("Unknown event {}", event); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 406 | if (context == null) { |
| 407 | return; |
| 408 | } |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 409 | LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 410 | if (ld == null) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | if (ld.handleLLDP(context)) { |
| 415 | context.block(); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 420 | private final class SyncDeviceInfoTask implements Runnable { |
| 421 | |
| 422 | @Override |
| 423 | public void run() { |
| 424 | if (Thread.currentThread().isInterrupted()) { |
| 425 | log.info("Interrupted, quitting"); |
| 426 | return; |
| 427 | } |
| 428 | // check what deviceService sees, to see if we are missing anything |
| 429 | try { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 430 | for (Device dev : deviceService.getDevices()) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 431 | if (rules.isSuppressed(dev)) { |
| 432 | continue; |
| 433 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 434 | DeviceId did = dev.id(); |
| 435 | synchronized (discoverers) { |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 436 | LinkDiscovery discoverer = discoverers.get(did); |
| 437 | if (discoverer == null) { |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame^] | 438 | discoverer = createLinkDiscovery(dev); |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 439 | discoverers.put(did, discoverer); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 440 | } |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 441 | |
| 442 | addPorts(discoverer, did); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | } catch (Exception e) { |
| 446 | // catch all Exception to avoid Scheduled task being suppressed. |
| 447 | log.error("Exception thrown during synchronization process", e); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 452 | } |