Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.host.impl; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 25 | import org.onlab.packet.ARP; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 26 | import org.onlab.packet.DHCP; |
| 27 | import org.onlab.packet.DHCPPacketType; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ethernet; |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 29 | import org.onlab.packet.ICMP6; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 30 | import org.onlab.packet.IPacket; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 31 | import org.onlab.packet.IPv4; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 32 | import org.onlab.packet.IPv6; |
| 33 | import org.onlab.packet.IpAddress; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 34 | import org.onlab.packet.MacAddress; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 35 | import org.onlab.packet.TpPort; |
| 36 | import org.onlab.packet.UDP; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 37 | import org.onlab.packet.VlanId; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 38 | import org.onlab.packet.ipv6.IExtensionHeader; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 39 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 40 | import org.onlab.packet.ndp.NeighborSolicitation; |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 41 | import org.onlab.packet.ndp.RouterAdvertisement; |
| 42 | import org.onlab.packet.ndp.RouterSolicitation; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 43 | import org.onlab.util.Tools; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 44 | import org.onosproject.cfg.ComponentConfigService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 45 | import org.onosproject.core.ApplicationId; |
| 46 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.net.ConnectPoint; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 48 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.Host; |
| 50 | import org.onosproject.net.HostId; |
| 51 | import org.onosproject.net.HostLocation; |
| 52 | import org.onosproject.net.device.DeviceEvent; |
| 53 | import org.onosproject.net.device.DeviceListener; |
| 54 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 55 | import org.onosproject.net.flow.DefaultTrafficSelector; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 56 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 57 | import org.onosproject.net.flow.TrafficSelector; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.host.DefaultHostDescription; |
| 60 | import org.onosproject.net.host.HostDescription; |
| 61 | import org.onosproject.net.host.HostProvider; |
| 62 | import org.onosproject.net.host.HostProviderRegistry; |
| 63 | import org.onosproject.net.host.HostProviderService; |
| 64 | import org.onosproject.net.host.HostService; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 65 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 66 | import org.onosproject.net.packet.OutboundPacket; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 67 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 68 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 69 | import org.onosproject.net.packet.PacketProcessor; |
| 70 | import org.onosproject.net.packet.PacketService; |
| 71 | import org.onosproject.net.provider.AbstractProvider; |
| 72 | import org.onosproject.net.provider.ProviderId; |
| 73 | import org.onosproject.net.topology.Topology; |
| 74 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 75 | import org.osgi.service.component.ComponentContext; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 78 | import java.nio.ByteBuffer; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 79 | import java.util.Dictionary; |
| 80 | import java.util.Set; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 81 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 82 | |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 83 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 84 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 85 | import static org.slf4j.LoggerFactory.getLogger; |
| 86 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 87 | /** |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 88 | * Provider which uses an OpenFlow controller to detect network end-station |
| 89 | * hosts. |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 90 | */ |
| 91 | @Component(immediate = true) |
| 92 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
| 93 | |
| 94 | private final Logger log = getLogger(getClass()); |
| 95 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected CoreService coreService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 100 | protected HostProviderRegistry providerRegistry; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 103 | protected PacketService packetService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected TopologyService topologyService; |
| 107 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected HostService hostService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected DeviceService deviceService; |
| 113 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected ComponentConfigService cfgService; |
| 116 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 117 | private HostProviderService providerService; |
| 118 | |
| 119 | private final InternalHostProvider processor = new InternalHostProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 120 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 121 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 122 | private ApplicationId appId; |
| 123 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 124 | @Property(name = "hostRemovalEnabled", boolValue = true, |
| 125 | label = "Enable host removal on port/device down events") |
| 126 | private boolean hostRemovalEnabled = true; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 127 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 128 | @Property(name = "useArp", boolValue = true, |
| 129 | label = "Enable using ARP for neighbor discovery by the " + |
| 130 | "Host Location Provider; default is true") |
| 131 | private boolean useArp = true; |
| 132 | |
| 133 | @Property(name = "useIpv6ND", boolValue = false, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 134 | label = "Enable using IPv6 Neighbor Discovery by the " + |
| 135 | "Host Location Provider; default is false") |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 136 | private boolean useIpv6ND = false; |
| 137 | |
| 138 | @Property(name = "useDhcp", boolValue = false, |
| 139 | label = "Enable using DHCP for neighbor discovery by the " + |
| 140 | "Host Location Provider; default is false") |
| 141 | private boolean useDhcp = false; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 142 | |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 143 | @Property(name = "requestInterceptsEnabled", boolValue = true, |
| 144 | label = "Enable requesting packet intercepts") |
| 145 | private boolean requestInterceptsEnabled = true; |
| 146 | |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 147 | protected ExecutorService eventHandler; |
| 148 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 149 | private static final byte[] SENDER_ADDRESS = IpAddress.valueOf("0.0.0.0").toOctets(); |
| 150 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Creates an OpenFlow host provider. |
| 153 | */ |
| 154 | public HostLocationProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 155 | super(new ProviderId("of", "org.onosproject.provider.host")); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Activate |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 159 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 160 | cfgService.registerProperties(getClass()); |
| 161 | appId = coreService.registerApplication("org.onosproject.provider.host"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 162 | eventHandler = newSingleThreadScheduledExecutor( |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 163 | groupedThreads("onos/host-loc-provider", "event-handler", log)); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 164 | providerService = providerRegistry.register(this); |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 165 | packetService.addProcessor(processor, PacketProcessor.advisor(1)); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 166 | deviceService.addListener(deviceListener); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 167 | |
| 168 | modified(context); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 169 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 170 | log.info("Started with Application ID {}", appId.id()); |
| 171 | } |
| 172 | |
| 173 | @Deactivate |
| 174 | public void deactivate() { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 175 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 176 | |
| 177 | withdrawIntercepts(); |
| 178 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 179 | providerRegistry.unregister(this); |
| 180 | packetService.removeProcessor(processor); |
| 181 | deviceService.removeListener(deviceListener); |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 182 | eventHandler.shutdown(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 183 | providerService = null; |
| 184 | log.info("Stopped"); |
| 185 | } |
| 186 | |
| 187 | @Modified |
| 188 | public void modified(ComponentContext context) { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 189 | readComponentConfiguration(context); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 190 | |
| 191 | if (requestInterceptsEnabled) { |
| 192 | requestIntercepts(); |
| 193 | } else { |
| 194 | withdrawIntercepts(); |
| 195 | } |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 199 | * Request packet intercepts. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 200 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 201 | private void requestIntercepts() { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 202 | // Use ARP |
| 203 | TrafficSelector arpSelector = DefaultTrafficSelector.builder() |
| 204 | .matchEthType(Ethernet.TYPE_ARP) |
| 205 | .build(); |
| 206 | if (useArp) { |
| 207 | packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 208 | } else { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 209 | packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 210 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 211 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 212 | // Use IPv6 Neighbor Discovery |
| 213 | TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder() |
| 214 | .matchEthType(Ethernet.TYPE_IPV6) |
| 215 | .matchIPProtocol(IPv6.PROTOCOL_ICMP6) |
| 216 | .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION) |
| 217 | .build(); |
| 218 | TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder() |
| 219 | .matchEthType(Ethernet.TYPE_IPV6) |
| 220 | .matchIPProtocol(IPv6.PROTOCOL_ICMP6) |
| 221 | .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT) |
| 222 | .build(); |
| 223 | if (useIpv6ND) { |
| 224 | packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId); |
| 225 | packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 226 | } else { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 227 | packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId); |
| 228 | packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId); |
| 229 | } |
| 230 | |
| 231 | // Use DHCP |
| 232 | TrafficSelector dhcpServerSelector = DefaultTrafficSelector.builder() |
| 233 | .matchEthType(Ethernet.TYPE_IPV4) |
| 234 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 235 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 236 | .build(); |
| 237 | TrafficSelector dhcpClientSelector = DefaultTrafficSelector.builder() |
| 238 | .matchEthType(Ethernet.TYPE_IPV4) |
| 239 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 240 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)) |
| 241 | .build(); |
| 242 | if (useDhcp) { |
| 243 | packetService.requestPackets(dhcpServerSelector, PacketPriority.CONTROL, appId); |
| 244 | packetService.requestPackets(dhcpClientSelector, PacketPriority.CONTROL, appId); |
| 245 | } else { |
| 246 | packetService.cancelPackets(dhcpServerSelector, PacketPriority.CONTROL, appId); |
| 247 | packetService.cancelPackets(dhcpClientSelector, PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Withdraw packet intercepts. |
| 253 | */ |
| 254 | private void withdrawIntercepts() { |
| 255 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 256 | selector.matchEthType(Ethernet.TYPE_ARP); |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 257 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 258 | |
| 259 | // IPv6 Neighbor Solicitation packet. |
| 260 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 261 | selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 262 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 263 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 264 | |
| 265 | // IPv6 Neighbor Advertisement packet. |
| 266 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 267 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Extracts properties from the component configuration context. |
| 272 | * |
| 273 | * @param context the component context |
| 274 | */ |
| 275 | private void readComponentConfiguration(ComponentContext context) { |
| 276 | Dictionary<?, ?> properties = context.getProperties(); |
| 277 | Boolean flag; |
| 278 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 279 | flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 280 | if (flag == null) { |
| 281 | log.info("Host removal on port/device down events is not configured, " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 282 | "using current value of {}", hostRemovalEnabled); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 283 | } else { |
| 284 | hostRemovalEnabled = flag; |
| 285 | log.info("Configured. Host removal on port/device down events is {}", |
| 286 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 287 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 288 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 289 | flag = Tools.isPropertyEnabled(properties, "useArp"); |
| 290 | if (flag == null) { |
| 291 | log.info("Using ARP is not configured, " + |
| 292 | "using current value of {}", useArp); |
| 293 | } else { |
| 294 | useArp = flag; |
| 295 | log.info("Configured. Using ARP is {}", |
| 296 | useArp ? "enabled" : "disabled"); |
| 297 | } |
| 298 | |
| 299 | flag = Tools.isPropertyEnabled(properties, "useIpv6ND"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 300 | if (flag == null) { |
| 301 | log.info("Using IPv6 Neighbor Discovery is not configured, " + |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 302 | "using current value of {}", useIpv6ND); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 303 | } else { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 304 | useIpv6ND = flag; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 305 | log.info("Configured. Using IPv6 Neighbor Discovery is {}", |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 306 | useIpv6ND ? "enabled" : "disabled"); |
| 307 | } |
| 308 | |
| 309 | flag = Tools.isPropertyEnabled(properties, "useDhcp"); |
| 310 | if (flag == null) { |
| 311 | log.info("Using DHCP is not configured, " + |
| 312 | "using current value of {}", useDhcp); |
| 313 | } else { |
| 314 | useDhcp = flag; |
| 315 | log.info("Configured. Using DHCP is {}", |
| 316 | useDhcp ? "enabled" : "disabled"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 317 | } |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 318 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 319 | flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 320 | if (flag == null) { |
| 321 | log.info("Request intercepts is not configured, " + |
| 322 | "using current value of {}", requestInterceptsEnabled); |
| 323 | } else { |
| 324 | requestInterceptsEnabled = flag; |
| 325 | log.info("Configured. Request intercepts is {}", |
| 326 | requestInterceptsEnabled ? "enabled" : "disabled"); |
| 327 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 328 | } |
| 329 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 330 | @Override |
| 331 | public void triggerProbe(Host host) { |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 332 | //log.info("Triggering probe on device {} ", host); |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 333 | |
| 334 | // FIXME Disabling host probing for now, because sending packets from a |
| 335 | // broadcast MAC address caused problems when two ONOS networks were |
| 336 | // interconnected. Host probing should take into account the interface |
| 337 | // configuration when determining which source address to use. |
| 338 | |
| 339 | //MastershipRole role = deviceService.getRole(host.location().deviceId()); |
| 340 | //if (role.equals(MastershipRole.MASTER)) { |
| 341 | // host.ipAddresses().forEach(ip -> { |
| 342 | // sendProbe(host, ip); |
| 343 | // }); |
| 344 | //} else { |
| 345 | // log.info("not the master, master will probe {}"); |
| 346 | //} |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private void sendProbe(Host host, IpAddress targetIp) { |
| 350 | Ethernet probePacket = null; |
| 351 | if (targetIp.isIp4()) { |
| 352 | // IPv4: Use ARP |
| 353 | probePacket = buildArpRequest(targetIp, host); |
| 354 | } else { |
| 355 | // IPv6: Use Neighbor Discovery |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 356 | //TODO need to implement ndp probe |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 357 | log.info("Triggering probe on device {} ", host); |
| 358 | } |
| 359 | |
| 360 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build(); |
| 361 | |
| 362 | OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment, |
| 363 | ByteBuffer.wrap(probePacket.serialize())); |
| 364 | |
| 365 | packetService.emit(outboundPacket); |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * This method is using source ip as 0.0.0.0 , to receive the reply even from the sub net hosts. |
| 370 | */ |
| 371 | private Ethernet buildArpRequest(IpAddress targetIp, Host host) { |
| 372 | |
| 373 | ARP arp = new ARP(); |
| 374 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 375 | .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 376 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 377 | .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH) |
| 378 | .setOpCode(ARP.OP_REQUEST); |
| 379 | |
| 380 | arp.setSenderHardwareAddress(MacAddress.BROADCAST.toBytes()) |
| 381 | .setSenderProtocolAddress(SENDER_ADDRESS) |
| 382 | .setTargetHardwareAddress(MacAddress.BROADCAST.toBytes()) |
| 383 | .setTargetProtocolAddress(targetIp.toOctets()); |
| 384 | |
| 385 | Ethernet ethernet = new Ethernet(); |
| 386 | ethernet.setEtherType(Ethernet.TYPE_ARP) |
| 387 | .setDestinationMACAddress(MacAddress.BROADCAST) |
| 388 | .setSourceMACAddress(MacAddress.BROADCAST).setPayload(arp); |
| 389 | |
| 390 | ethernet.setPad(true); |
| 391 | return ethernet; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | private class InternalHostProvider implements PacketProcessor { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 395 | /** |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 396 | * Updates host location only. |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 397 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 398 | * @param hid host ID |
| 399 | * @param mac source Mac address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 400 | * @param vlan VLAN ID |
| 401 | * @param hloc host location |
| 402 | */ |
| 403 | private void updateLocation(HostId hid, MacAddress mac, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 404 | VlanId vlan, HostLocation hloc) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 405 | HostDescription desc = new DefaultHostDescription(mac, vlan, hloc); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 406 | try { |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 407 | providerService.hostDetected(hid, desc, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 408 | } catch (IllegalStateException e) { |
| 409 | log.debug("Host {} suppressed", hid); |
| 410 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 411 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 412 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 413 | /** |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 414 | * Updates host location and IP address. |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 415 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 416 | * @param hid host ID |
| 417 | * @param mac source Mac address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 418 | * @param vlan VLAN ID |
| 419 | * @param hloc host location |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 420 | * @param ip source IP address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 421 | */ |
| 422 | private void updateLocationIP(HostId hid, MacAddress mac, |
| 423 | VlanId vlan, HostLocation hloc, |
| 424 | IpAddress ip) { |
Thomas Vachuska | e796610 | 2015-09-09 17:33:33 -0700 | [diff] [blame] | 425 | HostDescription desc = ip.isZero() || ip.isSelfAssigned() ? |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 426 | new DefaultHostDescription(mac, vlan, hloc) : |
| 427 | new DefaultHostDescription(mac, vlan, hloc, ip); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 428 | try { |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 429 | providerService.hostDetected(hid, desc, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 430 | } catch (IllegalStateException e) { |
| 431 | log.debug("Host {} suppressed", hid); |
| 432 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 433 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 434 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 435 | /** |
| 436 | * Updates host IP address for an existing host. |
| 437 | * |
| 438 | * @param hid host ID |
| 439 | * @param ip IP address |
| 440 | */ |
| 441 | private void updateIp(HostId hid, IpAddress ip) { |
| 442 | Host host = hostService.getHost(hid); |
| 443 | if (host == null) { |
| 444 | log.debug("Fail to update IP for {}. Host does not exist"); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | HostDescription desc = |
| 449 | new DefaultHostDescription(hid.mac(), hid.vlanId(), host.location(), ip); |
| 450 | try { |
| 451 | providerService.hostDetected(hid, desc, false); |
| 452 | } catch (IllegalStateException e) { |
| 453 | log.debug("Host {} suppressed", hid); |
| 454 | } |
| 455 | } |
| 456 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 459 | if (context == null) { |
| 460 | return; |
| 461 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 462 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 463 | Ethernet eth = context.inPacket().parsed(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 464 | if (eth == null) { |
| 465 | return; |
| 466 | } |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 467 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 468 | MacAddress srcMac = eth.getSourceMAC(); |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 469 | if (srcMac.isBroadcast() || srcMac.isMulticast()) { |
| 470 | return; |
| 471 | } |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 472 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 473 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 474 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 475 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 476 | // If this arrived on control port, bail out. |
| 477 | if (heardOn.port().isLogical()) { |
| 478 | return; |
| 479 | } |
| 480 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 481 | // If this is not an edge port, bail out. |
| 482 | Topology topology = topologyService.currentTopology(); |
| 483 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 484 | return; |
| 485 | } |
| 486 | |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 487 | HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 488 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 489 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 490 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 491 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 492 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 493 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 494 | arp.getSenderProtocolAddress()); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 495 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 496 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 497 | // IPv4: update location only |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 498 | // DHCP ACK: additionally update IP of DHCP client |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 499 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 500 | IPacket pkt = eth.getPayload(); |
| 501 | if (pkt != null && pkt instanceof IPv4) { |
| 502 | pkt = pkt.getPayload(); |
| 503 | if (pkt != null && pkt instanceof UDP) { |
| 504 | pkt = pkt.getPayload(); |
| 505 | if (pkt != null && pkt instanceof DHCP) { |
| 506 | DHCP dhcp = (DHCP) pkt; |
| 507 | if (dhcp.getOptions().stream() |
| 508 | .anyMatch(dhcpOption -> dhcpOption.getCode() == |
| 509 | DHCP.DHCPOptionCode.OptionCode_MessageType.getValue() && |
| 510 | dhcpOption.getLength() == 1 && |
| 511 | dhcpOption.getData()[0] == DHCPPacketType.DHCPACK.getValue())) { |
| 512 | MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress()); |
| 513 | VlanId hostVlan = VlanId.vlanId(eth.getVlanID()); |
| 514 | HostId hostId = HostId.hostId(hostMac, hostVlan); |
| 515 | updateIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress())); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 520 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 521 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 522 | // |
| 523 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 524 | // new hosts, update both location and IP. |
| 525 | // |
| 526 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 527 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 528 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 529 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 530 | ipv6.getSourceAddress()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 531 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 532 | // skip extension headers |
| 533 | IPacket pkt = ipv6; |
| 534 | while (pkt.getPayload() != null && |
| 535 | pkt.getPayload() instanceof IExtensionHeader) { |
| 536 | pkt = pkt.getPayload(); |
| 537 | } |
| 538 | |
| 539 | // Neighbor Discovery Protocol |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 540 | pkt = pkt.getPayload(); |
| 541 | if (pkt != null && pkt instanceof ICMP6) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 542 | pkt = pkt.getPayload(); |
| 543 | // RouterSolicitation, RouterAdvertisement |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 544 | if (pkt != null && (pkt instanceof RouterAdvertisement || |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 545 | pkt instanceof RouterSolicitation)) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 546 | return; |
| 547 | } |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 548 | if (pkt != null && (pkt instanceof NeighborSolicitation || |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 549 | pkt instanceof NeighborAdvertisement)) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 550 | // Duplicate Address Detection |
| 551 | if (ip.isZero()) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 552 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 553 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 554 | // NeighborSolicitation, NeighborAdvertisement |
| 555 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
| 556 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 557 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 558 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 559 | |
| 560 | // multicast |
| 561 | if (eth.isMulticast()) { |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | // normal IPv6 packets |
| 566 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 570 | |
| 571 | // Auxiliary listener to device events. |
| 572 | private class InternalDeviceListener implements DeviceListener { |
| 573 | @Override |
| 574 | public void event(DeviceEvent event) { |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 575 | eventHandler.execute(() -> handleEvent(event)); |
| 576 | } |
| 577 | |
| 578 | private void handleEvent(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 579 | Device device = event.subject(); |
| 580 | switch (event.type()) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 581 | case DEVICE_ADDED: |
| 582 | break; |
| 583 | case DEVICE_AVAILABILITY_CHANGED: |
| 584 | if (hostRemovalEnabled && |
| 585 | !deviceService.isAvailable(device.id())) { |
| 586 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 587 | } |
| 588 | break; |
| 589 | case DEVICE_SUSPENDED: |
| 590 | case DEVICE_UPDATED: |
| 591 | // Nothing to do? |
| 592 | break; |
| 593 | case DEVICE_REMOVED: |
| 594 | if (hostRemovalEnabled) { |
| 595 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 596 | } |
| 597 | break; |
| 598 | case PORT_ADDED: |
| 599 | break; |
| 600 | case PORT_UPDATED: |
| 601 | if (hostRemovalEnabled) { |
| 602 | ConnectPoint point = |
| 603 | new ConnectPoint(device.id(), event.port().number()); |
| 604 | removeHosts(hostService.getConnectedHosts(point)); |
| 605 | } |
| 606 | break; |
| 607 | case PORT_REMOVED: |
| 608 | // Nothing to do? |
| 609 | break; |
| 610 | default: |
| 611 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // Signals host vanish for all specified hosts. |
| 617 | private void removeHosts(Set<Host> hosts) { |
| 618 | for (Host host : hosts) { |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 619 | providerService.hostVanished(host.id()); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 623 | } |