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