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