Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.host.impl; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 17 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 19 | import org.onlab.packet.ARP; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 20 | import org.onlab.packet.BasePacket; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 21 | import org.onlab.packet.DHCP; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 22 | import org.onlab.packet.DHCP6; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 23 | import org.onlab.packet.EthType; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 25 | import org.onlab.packet.ICMP6; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IPacket; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IPv4; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 28 | import org.onlab.packet.IPv6; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 29 | import org.onlab.packet.Ip4Address; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 30 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 31 | import org.onlab.packet.IpAddress; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 32 | import org.onlab.packet.MacAddress; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 33 | import org.onlab.packet.UDP; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 34 | import org.onlab.packet.VlanId; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 35 | import org.onlab.packet.dhcp.Dhcp6ClientIdOption; |
| 36 | import org.onlab.packet.dhcp.Dhcp6IaAddressOption; |
| 37 | import org.onlab.packet.dhcp.Dhcp6IaNaOption; |
| 38 | import org.onlab.packet.dhcp.Dhcp6IaTaOption; |
| 39 | import org.onlab.packet.dhcp.Dhcp6RelayOption; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 40 | import org.onlab.packet.ipv6.IExtensionHeader; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 41 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 42 | import org.onlab.packet.ndp.NeighborSolicitation; |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 43 | import org.onlab.packet.ndp.RouterAdvertisement; |
| 44 | import org.onlab.packet.ndp.RouterSolicitation; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 45 | import org.onlab.util.Tools; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 46 | import org.onosproject.cfg.ComponentConfigService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 47 | import org.onosproject.core.ApplicationId; |
| 48 | import org.onosproject.core.CoreService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 49 | import org.onosproject.net.ConnectPoint; |
| 50 | import org.onosproject.net.Device; |
| 51 | import org.onosproject.net.DeviceId; |
| 52 | import org.onosproject.net.Host; |
| 53 | import org.onosproject.net.HostId; |
| 54 | import org.onosproject.net.HostLocation; |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 55 | import org.onosproject.net.config.ConfigFactory; |
| 56 | import org.onosproject.net.config.NetworkConfigEvent; |
| 57 | import org.onosproject.net.config.NetworkConfigListener; |
| 58 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 59 | import org.onosproject.net.config.NetworkConfigService; |
| 60 | import org.onosproject.net.config.basics.BasicHostConfig; |
| 61 | import org.onosproject.net.config.basics.HostLearningConfig; |
| 62 | import org.onosproject.net.config.basics.SubjectFactories; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import org.onosproject.net.device.DeviceEvent; |
| 64 | import org.onosproject.net.device.DeviceListener; |
| 65 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 66 | import org.onosproject.net.flow.DefaultTrafficSelector; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 67 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 68 | import org.onosproject.net.flow.TrafficSelector; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 69 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 70 | import org.onosproject.net.host.DefaultHostDescription; |
| 71 | import org.onosproject.net.host.HostDescription; |
| 72 | import org.onosproject.net.host.HostProvider; |
| 73 | import org.onosproject.net.host.HostProviderRegistry; |
| 74 | import org.onosproject.net.host.HostProviderService; |
| 75 | import org.onosproject.net.host.HostService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 76 | import org.onosproject.net.intf.InterfaceService; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 77 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 78 | import org.onosproject.net.packet.OutboundPacket; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 79 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 80 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 81 | import org.onosproject.net.packet.PacketProcessor; |
| 82 | import org.onosproject.net.packet.PacketService; |
| 83 | import org.onosproject.net.provider.AbstractProvider; |
| 84 | import org.onosproject.net.provider.ProviderId; |
| 85 | import org.onosproject.net.topology.Topology; |
| 86 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 87 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 88 | import org.osgi.service.component.annotations.Activate; |
| 89 | import org.osgi.service.component.annotations.Component; |
| 90 | import org.osgi.service.component.annotations.Deactivate; |
| 91 | import org.osgi.service.component.annotations.Modified; |
| 92 | import org.osgi.service.component.annotations.Reference; |
| 93 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 94 | import org.slf4j.Logger; |
| 95 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 96 | import java.nio.ByteBuffer; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 97 | import java.util.Dictionary; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 98 | import java.util.Objects; |
| 99 | import java.util.Optional; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 100 | import java.util.Set; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 101 | import java.util.concurrent.ExecutorService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 102 | import java.util.stream.Stream; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 103 | |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 104 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 105 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 106 | import static org.slf4j.LoggerFactory.getLogger; |
| 107 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 108 | /** |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 109 | * Provider which uses an OpenFlow controller to detect network end-station |
| 110 | * hosts. |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 111 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 112 | @Component(immediate = true, service = HostProvider.class) |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 113 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 114 | private final Logger log = getLogger(getClass()); |
| 115 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 117 | protected CoreService coreService; |
| 118 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 120 | protected HostProviderRegistry providerRegistry; |
| 121 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 123 | protected PacketService packetService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 124 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 126 | protected TopologyService topologyService; |
| 127 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 129 | protected HostService hostService; |
| 130 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 132 | protected DeviceService deviceService; |
| 133 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 135 | protected ComponentConfigService cfgService; |
| 136 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 138 | protected NetworkConfigRegistry registry; |
| 139 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 141 | protected InterfaceService interfaceService; |
| 142 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 143 | private final InternalHostProvider processor = new InternalHostProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 144 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 145 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 146 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 147 | private ApplicationId appId; |
| 148 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 149 | //@Property(name = "hostRemovalEnabled", boolValue = true, |
| 150 | // label = "Enable host removal on port/device down events") |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 151 | private boolean hostRemovalEnabled = true; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 152 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 153 | //@Property(name = "requestArp", boolValue = true, |
| 154 | // label = "Request ARP packets for neighbor discovery by the " + |
| 155 | // "Host Location Provider; default is true") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 156 | private boolean requestArp = true; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 157 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 158 | //@Property(name = "requestIpv6ND", boolValue = false, |
| 159 | // label = "Requests IPv6 Neighbor Discovery by the " + |
| 160 | // "Host Location Provider; default is false") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 161 | private boolean requestIpv6ND = false; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 162 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 163 | //@Property(name = "useDhcp", boolValue = false, |
| 164 | // label = "Use DHCP to update IP address of the host; default is false") |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 165 | private boolean useDhcp = false; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 166 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 167 | //@Property(name = "useDhcp6", boolValue = false, |
| 168 | // label = "Use DHCPv6 to update IP address of the host; default is false") |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 169 | private boolean useDhcp6 = false; |
| 170 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 171 | //@Property(name = "requestInterceptsEnabled", boolValue = true, |
| 172 | // label = "Enable requesting packet intercepts") |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 173 | private boolean requestInterceptsEnabled = true; |
| 174 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 175 | //@Property(name = "multihomingEnabled", boolValue = false, |
| 176 | // label = "Allow hosts to be multihomed") |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 177 | private boolean multihomingEnabled = false; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 178 | |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 179 | private HostProviderService providerService; |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 180 | |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 181 | ExecutorService deviceEventHandler; |
| 182 | private ExecutorService probeEventHandler; |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 183 | private ExecutorService packetHandler; |
Charles Chan | 8277b6b | 2017-12-03 13:48:46 -0800 | [diff] [blame] | 184 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 185 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 186 | protected NetworkConfigService netcfgService; |
| 187 | |
| 188 | private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig = |
| 189 | new ConfigFactory<ConnectPoint, HostLearningConfig>( |
| 190 | SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY, |
| 191 | HostLearningConfig.class, "hostLearning") { |
| 192 | @Override |
| 193 | public HostLearningConfig createConfig() { |
| 194 | return new HostLearningConfig(); |
| 195 | } |
| 196 | }; |
| 197 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Creates an OpenFlow host provider. |
| 200 | */ |
| 201 | public HostLocationProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 202 | super(new ProviderId("of", "org.onosproject.provider.host")); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Activate |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 206 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 207 | cfgService.registerProperties(getClass()); |
| 208 | appId = coreService.registerApplication("org.onosproject.provider.host"); |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 209 | deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider", |
| 210 | "device-event-handler", log)); |
| 211 | probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider", |
| 212 | "probe-event-handler", log)); |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 213 | packetHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider", |
| 214 | "packet-handler", log)); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 215 | providerService = providerRegistry.register(this); |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 216 | packetService.addProcessor(processor, PacketProcessor.advisor(1)); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 217 | deviceService.addListener(deviceListener); |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 218 | registry.registerConfigFactory(hostLearningConfig); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 219 | modified(context); |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 220 | netcfgService.addListener(cfgListener); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 221 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 222 | log.info("Started with Application ID {}", appId.id()); |
| 223 | } |
| 224 | |
| 225 | @Deactivate |
| 226 | public void deactivate() { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 227 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 228 | |
| 229 | withdrawIntercepts(); |
| 230 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 231 | providerRegistry.unregister(this); |
| 232 | packetService.removeProcessor(processor); |
| 233 | deviceService.removeListener(deviceListener); |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 234 | deviceEventHandler.shutdown(); |
| 235 | probeEventHandler.shutdown(); |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 236 | packetHandler.shutdown(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 237 | providerService = null; |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 238 | registry.unregisterConfigFactory(hostLearningConfig); |
| 239 | netcfgService.removeListener(cfgListener); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 240 | log.info("Stopped"); |
| 241 | } |
| 242 | |
| 243 | @Modified |
| 244 | public void modified(ComponentContext context) { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 245 | readComponentConfiguration(context); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 246 | |
| 247 | if (requestInterceptsEnabled) { |
| 248 | requestIntercepts(); |
| 249 | } else { |
| 250 | withdrawIntercepts(); |
| 251 | } |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 255 | * Request packet intercepts. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 256 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 257 | private void requestIntercepts() { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 258 | // Use ARP |
| 259 | TrafficSelector arpSelector = DefaultTrafficSelector.builder() |
| 260 | .matchEthType(Ethernet.TYPE_ARP) |
| 261 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 262 | if (requestArp) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 263 | packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 264 | } else { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 265 | packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 266 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 267 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 268 | // Use IPv6 Neighbor Discovery |
| 269 | TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder() |
| 270 | .matchEthType(Ethernet.TYPE_IPV6) |
| 271 | .matchIPProtocol(IPv6.PROTOCOL_ICMP6) |
| 272 | .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION) |
| 273 | .build(); |
| 274 | TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder() |
| 275 | .matchEthType(Ethernet.TYPE_IPV6) |
| 276 | .matchIPProtocol(IPv6.PROTOCOL_ICMP6) |
| 277 | .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT) |
| 278 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 279 | if (requestIpv6ND) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 280 | packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId); |
| 281 | packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 282 | } else { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 283 | packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId); |
| 284 | packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId); |
| 285 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Withdraw packet intercepts. |
| 290 | */ |
| 291 | private void withdrawIntercepts() { |
| 292 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 293 | selector.matchEthType(Ethernet.TYPE_ARP); |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 294 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 295 | |
| 296 | // IPv6 Neighbor Solicitation packet. |
| 297 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 298 | selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 299 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 300 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 301 | |
| 302 | // IPv6 Neighbor Advertisement packet. |
| 303 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 304 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Extracts properties from the component configuration context. |
| 309 | * |
| 310 | * @param context the component context |
| 311 | */ |
| 312 | private void readComponentConfiguration(ComponentContext context) { |
| 313 | Dictionary<?, ?> properties = context.getProperties(); |
| 314 | Boolean flag; |
| 315 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 316 | flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 317 | if (flag == null) { |
| 318 | log.info("Host removal on port/device down events is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 319 | "using current value of {}", hostRemovalEnabled); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 320 | } else { |
| 321 | hostRemovalEnabled = flag; |
| 322 | log.info("Configured. Host removal on port/device down events is {}", |
| 323 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 324 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 325 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 326 | flag = Tools.isPropertyEnabled(properties, "requestArp"); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 327 | if (flag == null) { |
| 328 | log.info("Using ARP is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 329 | "using current value of {}", requestArp); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 330 | } else { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 331 | requestArp = flag; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 332 | log.info("Configured. Using ARP is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 333 | requestArp ? "enabled" : "disabled"); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 336 | flag = Tools.isPropertyEnabled(properties, "requestIpv6ND"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 337 | if (flag == null) { |
| 338 | log.info("Using IPv6 Neighbor Discovery is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 339 | "using current value of {}", requestIpv6ND); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 340 | } else { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 341 | requestIpv6ND = flag; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 342 | log.info("Configured. Using IPv6 Neighbor Discovery is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 343 | requestIpv6ND ? "enabled" : "disabled"); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | flag = Tools.isPropertyEnabled(properties, "useDhcp"); |
| 347 | if (flag == null) { |
| 348 | log.info("Using DHCP is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 349 | "using current value of {}", useDhcp); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 350 | } else { |
| 351 | useDhcp = flag; |
| 352 | log.info("Configured. Using DHCP is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 353 | useDhcp ? "enabled" : "disabled"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 354 | } |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 355 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 356 | flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 357 | if (flag == null) { |
| 358 | log.info("Request intercepts is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 359 | "using current value of {}", requestInterceptsEnabled); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 360 | } else { |
| 361 | requestInterceptsEnabled = flag; |
| 362 | log.info("Configured. Request intercepts is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 363 | requestInterceptsEnabled ? "enabled" : "disabled"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 364 | } |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 365 | |
| 366 | flag = Tools.isPropertyEnabled(properties, "multihomingEnabled"); |
| 367 | if (flag == null) { |
| 368 | log.info("Multihoming is not configured, " + |
| 369 | "using current value of {}", multihomingEnabled); |
| 370 | } else { |
| 371 | multihomingEnabled = flag; |
| 372 | log.info("Configured. Multihoming is {}", |
| 373 | multihomingEnabled ? "enabled" : "disabled"); |
| 374 | } |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 375 | } |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 376 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 377 | @Override |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 378 | public void triggerProbe(Host host) { |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 379 | //log.info("Triggering probe on device {} ", host); |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 380 | |
| 381 | // FIXME Disabling host probing for now, because sending packets from a |
| 382 | // broadcast MAC address caused problems when two ONOS networks were |
| 383 | // interconnected. Host probing should take into account the interface |
| 384 | // configuration when determining which source address to use. |
| 385 | |
| 386 | //MastershipRole role = deviceService.getRole(host.location().deviceId()); |
| 387 | //if (role.equals(MastershipRole.MASTER)) { |
| 388 | // host.ipAddresses().forEach(ip -> { |
| 389 | // sendProbe(host, ip); |
| 390 | // }); |
| 391 | //} else { |
| 392 | // log.info("not the master, master will probe {}"); |
| 393 | //} |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | private void sendProbe(Host host, IpAddress targetIp) { |
| 397 | Ethernet probePacket = null; |
| 398 | if (targetIp.isIp4()) { |
| 399 | // IPv4: Use ARP |
| 400 | probePacket = buildArpRequest(targetIp, host); |
| 401 | } else { |
| 402 | // IPv6: Use Neighbor Discovery |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 403 | //TODO need to implement ndp probe |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 404 | log.info("Triggering probe on device {} ", host); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 405 | return; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build(); |
| 409 | |
| 410 | OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment, |
| 411 | ByteBuffer.wrap(probePacket.serialize())); |
| 412 | |
| 413 | packetService.emit(outboundPacket); |
| 414 | } |
| 415 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 416 | // 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] | 417 | private Ethernet buildArpRequest(IpAddress targetIp, Host host) { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 418 | return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(), |
| 419 | MacAddress.BROADCAST.toBytes(), targetIp.toOctets(), |
| 420 | MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | private class InternalHostProvider implements PacketProcessor { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 424 | /** |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 425 | * Create or update host information. |
| 426 | * 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] | 427 | * |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 428 | * @param hid host ID |
| 429 | * @param mac source Mac address |
| 430 | * @param vlan VLAN ID |
| 431 | * @param innerVlan inner VLAN ID |
| 432 | * @param outerTpid outer TPID |
| 433 | * @param hloc host location |
| 434 | * @param ip source IP address or null if not updating |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 435 | */ |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 436 | private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan, |
| 437 | VlanId innerVlan, EthType outerTpid, |
| 438 | HostLocation hloc, IpAddress ip) { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 439 | Set<HostLocation> newLocations = Sets.newHashSet(hloc); |
| 440 | |
| 441 | if (multihomingEnabled) { |
| 442 | Host existingHost = hostService.getHost(hid); |
| 443 | if (existingHost != null) { |
| 444 | Set<HostLocation> prevLocations = existingHost.locations(); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 445 | |
Charles Chan | 653e2ac | 2017-11-22 14:49:55 -0800 | [diff] [blame] | 446 | if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) { |
| 447 | // New location is on a device that we haven't seen before |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 448 | // Could be a dual-home host. |
Charles Chan | 653e2ac | 2017-11-22 14:49:55 -0800 | [diff] [blame] | 449 | newLocations.addAll(prevLocations); |
Charles Chan | 653e2ac | 2017-11-22 14:49:55 -0800 | [diff] [blame] | 450 | } else { |
| 451 | // Move within the same switch |
| 452 | // Simply replace old location that is on the same device |
| 453 | prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId())) |
| 454 | .forEach(newLocations::add); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 459 | HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ? |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 460 | new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(), |
| 461 | innerVlan, outerTpid, false) : |
| 462 | new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip), |
| 463 | innerVlan, outerTpid, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 464 | try { |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 465 | providerService.hostDetected(hid, desc, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 466 | } catch (IllegalStateException e) { |
| 467 | log.debug("Host {} suppressed", hid); |
| 468 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 469 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 470 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 471 | /** |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 472 | * Updates IP address for an existing host. |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 473 | * |
| 474 | * @param hid host ID |
| 475 | * @param ip IP address |
| 476 | */ |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 477 | private void updateHostIp(HostId hid, IpAddress ip) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 478 | Host host = hostService.getHost(hid); |
| 479 | if (host == null) { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 480 | log.warn("Fail to update IP for {}. Host does not exist", hid); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 484 | HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(), |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 485 | host.locations(), Sets.newHashSet(ip), false); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 486 | try { |
| 487 | providerService.hostDetected(hid, desc, false); |
| 488 | } catch (IllegalStateException e) { |
| 489 | log.debug("Host {} suppressed", hid); |
| 490 | } |
| 491 | } |
| 492 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 493 | @Override |
| 494 | public void process(PacketContext context) { |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 495 | packetHandler.execute(() -> processPacketInternal(context)); |
| 496 | } |
| 497 | |
| 498 | private void processPacketInternal(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 499 | if (context == null) { |
| 500 | return; |
| 501 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 502 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 503 | Ethernet eth = context.inPacket().parsed(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 504 | if (eth == null) { |
| 505 | return; |
| 506 | } |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 507 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 508 | MacAddress srcMac = eth.getSourceMAC(); |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 509 | if (srcMac.isBroadcast() || srcMac.isMulticast()) { |
| 510 | return; |
| 511 | } |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 512 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 513 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 514 | VlanId outerVlan = VlanId.vlanId(eth.getQinQVID()); |
| 515 | VlanId innerVlan = VlanId.NONE; |
| 516 | EthType outerTpid = EthType.EtherType.UNKNOWN.ethType(); |
| 517 | // Set up values for double-tagged hosts |
| 518 | if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) { |
| 519 | innerVlan = vlan; |
| 520 | vlan = outerVlan; |
| 521 | outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType(); |
| 522 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 523 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 524 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 525 | // If this arrived on control port, bail out. |
| 526 | if (heardOn.port().isLogical()) { |
| 527 | return; |
| 528 | } |
| 529 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 530 | // If this is not an edge port, bail out. |
| 531 | Topology topology = topologyService.currentTopology(); |
| 532 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 533 | return; |
| 534 | } |
| 535 | |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 536 | HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 537 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 538 | MacAddress destMac = eth.getDestinationMAC(); |
| 539 | |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 540 | // Ignore location probes |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 541 | if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 542 | return; |
| 543 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 544 | |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 545 | HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class); |
| 546 | // if learning is disabled bail out. |
| 547 | if ((cfg != null) && (!cfg.hostLearningEnabled())) { |
| 548 | log.debug("Learning disabled for {}, abort.", heardOn); |
| 549 | return; |
| 550 | } |
| 551 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 552 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 553 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 554 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 555 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 556 | arp.getSenderProtocolAddress()); |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 557 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 558 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 559 | // IPv4: update location only |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 560 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 561 | // Update host location |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 562 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 563 | if (useDhcp) { |
| 564 | DHCP dhcp = findDhcp(eth).orElse(null); |
| 565 | // DHCP ACK: additionally update IP of DHCP client |
| 566 | if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) { |
| 567 | MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress()); |
| 568 | VlanId hostVlan = VlanId.vlanId(eth.getVlanID()); |
| 569 | HostId hostId = HostId.hostId(hostMac, hostVlan); |
| 570 | updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress())); |
Yi Tseng | cfa637a | 2017-07-26 17:24:10 -0700 | [diff] [blame] | 571 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 572 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 573 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 574 | // new hosts, update both location and IP. |
| 575 | // |
| 576 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 577 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 578 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 579 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 580 | ipv6.getSourceAddress()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 581 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 582 | // skip extension headers |
| 583 | IPacket pkt = ipv6; |
| 584 | while (pkt.getPayload() != null && |
| 585 | pkt.getPayload() instanceof IExtensionHeader) { |
| 586 | pkt = pkt.getPayload(); |
| 587 | } |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 588 | pkt = pkt.getPayload(); |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 589 | |
| 590 | // DHCPv6 protocol |
| 591 | DHCP6 dhcp6 = findDhcp6(pkt).orElse(null); |
| 592 | if (dhcp6 != null && useDhcp6) { |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 593 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null); |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 594 | handleDhcp6(dhcp6, vlan); |
| 595 | return; |
| 596 | } |
| 597 | |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 598 | if (pkt != null && pkt instanceof ICMP6) { |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 599 | // Neighbor Discovery Protocol |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 600 | pkt = pkt.getPayload(); |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 601 | if (pkt != null) { |
| 602 | // RouterSolicitation, RouterAdvertisement |
| 603 | if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 604 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 605 | } |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 606 | if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) { |
| 607 | // Duplicate Address Detection |
| 608 | if (ip.isZero()) { |
| 609 | return; |
| 610 | } |
| 611 | // NeighborSolicitation, NeighborAdvertisement |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 612 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip); |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 613 | |
| 614 | // Also learn from the target address of NeighborAdvertisement |
| 615 | if (pkt instanceof NeighborAdvertisement) { |
| 616 | NeighborAdvertisement na = (NeighborAdvertisement) pkt; |
| 617 | Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress()); |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 618 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr); |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 619 | } |
| 620 | return; |
| 621 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 622 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 623 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 624 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 625 | // multicast, exclude DHCPv6 |
| 626 | if (eth.isMulticast() && dhcp6 == null) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
| 630 | // normal IPv6 packets |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 631 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 634 | |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 635 | /** |
| 636 | * Handles DHCPv6 packet, if message type is ACK, update IP address |
| 637 | * according to DHCPv6 payload (IA Address option). |
| 638 | * |
| 639 | * @param dhcp6 the DHCPv6 payload |
| 640 | * @param vlanId the vlan of this packet |
| 641 | */ |
| 642 | private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) { |
| 643 | // extract the relay message if exist |
| 644 | while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) { |
| 645 | dhcp6 = dhcp6.getOptions().stream() |
| 646 | .filter(opt -> opt instanceof Dhcp6RelayOption) |
| 647 | .map(BasePacket::getPayload) |
| 648 | .map(pld -> (DHCP6) pld) |
| 649 | .findFirst() |
| 650 | .orElse(null); |
| 651 | } |
| 652 | |
| 653 | if (dhcp6 == null) { |
| 654 | // Can't find dhcp payload |
| 655 | log.warn("Can't find dhcp payload from relay message"); |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) { |
| 660 | // Update IP address only when we received REPLY message |
| 661 | return; |
| 662 | } |
| 663 | Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions() |
| 664 | .stream() |
| 665 | .filter(opt -> opt instanceof Dhcp6ClientIdOption) |
| 666 | .map(opt -> (Dhcp6ClientIdOption) opt) |
| 667 | .findFirst(); |
| 668 | |
| 669 | if (!clientIdOption.isPresent()) { |
| 670 | // invalid DHCPv6 option |
| 671 | log.warn("Can't find client ID from DHCPv6 {}", dhcp6); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress(); |
| 676 | if (linkLayerAddr == null || linkLayerAddr.length != 6) { |
| 677 | // No any mac address found |
| 678 | log.warn("Can't find client mac from option {}", clientIdOption); |
| 679 | return; |
| 680 | } |
| 681 | MacAddress clientMac = MacAddress.valueOf(linkLayerAddr); |
| 682 | |
| 683 | // Extract IPv6 address from IA NA ot IA TA option |
| 684 | Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions() |
| 685 | .stream() |
| 686 | .filter(opt -> opt instanceof Dhcp6IaNaOption) |
| 687 | .map(opt -> (Dhcp6IaNaOption) opt) |
| 688 | .findFirst(); |
| 689 | Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions() |
| 690 | .stream() |
| 691 | .filter(opt -> opt instanceof Dhcp6IaTaOption) |
| 692 | .map(opt -> (Dhcp6IaTaOption) opt) |
| 693 | .findFirst(); |
| 694 | Optional<Dhcp6IaAddressOption> iaAddressOption; |
| 695 | if (iaNaOption.isPresent()) { |
| 696 | iaAddressOption = iaNaOption.get().getOptions().stream() |
| 697 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 698 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 699 | .findFirst(); |
| 700 | } else if (iaTaOption.isPresent()) { |
| 701 | iaAddressOption = iaTaOption.get().getOptions().stream() |
| 702 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 703 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 704 | .findFirst(); |
| 705 | } else { |
| 706 | iaAddressOption = Optional.empty(); |
| 707 | } |
| 708 | if (iaAddressOption.isPresent()) { |
| 709 | Ip6Address ip = iaAddressOption.get().getIp6Address(); |
| 710 | HostId hostId = HostId.hostId(clientMac, vlanId); |
| 711 | updateHostIp(hostId, ip); |
| 712 | } else { |
| 713 | log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6); |
| 714 | } |
| 715 | } |
| 716 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 717 | private Optional<DHCP> findDhcp(Ethernet eth) { |
| 718 | IPacket pkt = eth.getPayload(); |
| 719 | return Stream.of(pkt) |
| 720 | .filter(Objects::nonNull) |
| 721 | .filter(p -> p instanceof IPv4) |
| 722 | .map(IPacket::getPayload) |
| 723 | .filter(Objects::nonNull) |
| 724 | .filter(p -> p instanceof UDP) |
| 725 | .map(IPacket::getPayload) |
| 726 | .filter(Objects::nonNull) |
| 727 | .filter(p -> p instanceof DHCP) |
| 728 | .map(p -> (DHCP) p) |
| 729 | .findFirst(); |
| 730 | } |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 731 | |
| 732 | private Optional<DHCP6> findDhcp6(IPacket pkt) { |
| 733 | return Stream.of(pkt) |
| 734 | .filter(Objects::nonNull) |
| 735 | .filter(p -> p instanceof UDP) |
| 736 | .map(IPacket::getPayload) |
| 737 | .filter(Objects::nonNull) |
| 738 | .filter(p -> p instanceof DHCP6) |
| 739 | .map(p -> (DHCP6) p) |
| 740 | .findFirst(); |
| 741 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 742 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 743 | |
| 744 | // Auxiliary listener to device events. |
| 745 | private class InternalDeviceListener implements DeviceListener { |
| 746 | @Override |
| 747 | public void event(DeviceEvent event) { |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 748 | deviceEventHandler.execute(() -> handleEvent(event)); |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | private void handleEvent(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 752 | Device device = event.subject(); |
| 753 | switch (event.type()) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 754 | case DEVICE_ADDED: |
| 755 | break; |
| 756 | case DEVICE_AVAILABILITY_CHANGED: |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 757 | if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) { |
| 758 | processDeviceDown(device.id()); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 759 | } |
| 760 | break; |
| 761 | case DEVICE_SUSPENDED: |
| 762 | case DEVICE_UPDATED: |
| 763 | // Nothing to do? |
| 764 | break; |
| 765 | case DEVICE_REMOVED: |
| 766 | if (hostRemovalEnabled) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 767 | processDeviceDown(device.id()); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 768 | } |
| 769 | break; |
| 770 | case PORT_ADDED: |
| 771 | break; |
| 772 | case PORT_UPDATED: |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 773 | if (hostRemovalEnabled && !event.port().isEnabled()) { |
| 774 | processPortDown(new ConnectPoint(device.id(), event.port().number())); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 775 | } |
| 776 | break; |
| 777 | case PORT_REMOVED: |
enen92 | 3305efe | 2018-07-03 23:16:53 +0100 | [diff] [blame] | 778 | if (hostRemovalEnabled) { |
| 779 | processPortDown(new ConnectPoint(device.id(), event.port().number())); |
| 780 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 781 | break; |
| 782 | default: |
| 783 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 788 | /** |
| 789 | * When a device goes down, update the location of affected hosts. |
| 790 | * |
| 791 | * @param deviceId the device that goes down |
| 792 | */ |
| 793 | private void processDeviceDown(DeviceId deviceId) { |
| 794 | hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream() |
| 795 | .filter(hostLocation -> hostLocation.deviceId().equals(deviceId)) |
| 796 | .forEach(affectedLocation -> |
| 797 | providerService.removeLocationFromHost(affectedHost.id(), affectedLocation)) |
| 798 | ); |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * When a port goes down, update the location of affected hosts. |
| 803 | * |
| 804 | * @param connectPoint the port that goes down |
| 805 | */ |
| 806 | private void processPortDown(ConnectPoint connectPoint) { |
| 807 | hostService.getConnectedHosts(connectPoint).forEach(affectedHost -> |
| 808 | providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L)) |
| 809 | ); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 812 | |
| 813 | private class InternalConfigListener implements NetworkConfigListener { |
| 814 | |
| 815 | @Override |
| 816 | public void event(NetworkConfigEvent event) { |
| 817 | switch (event.type()) { |
| 818 | case CONFIG_ADDED: |
| 819 | case CONFIG_UPDATED: |
| 820 | log.debug("HostLearningConfig event of type {}", event.type()); |
| 821 | // if learning enabled do nothing |
| 822 | HostLearningConfig learningConfig = (HostLearningConfig) event.config().get(); |
| 823 | if (learningConfig.hostLearningEnabled()) { |
| 824 | return; |
| 825 | } |
| 826 | |
| 827 | // if host learning is disable remove this location from existing, learnt hosts |
| 828 | ConnectPoint connectPoint = learningConfig.subject(); |
| 829 | Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint); |
| 830 | for (Host host : connectedHosts) { |
| 831 | BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class); |
| 832 | |
| 833 | if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) { |
| 834 | // timestamp shoud not matter for comparing HostLocation and ConnectPoint |
| 835 | providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1)); |
| 836 | } |
| 837 | } |
| 838 | break; |
| 839 | case CONFIG_REMOVED: |
| 840 | default: |
| 841 | break; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | @Override |
| 846 | public boolean isRelevant(NetworkConfigEvent event) { |
| 847 | if (!event.configClass().equals(HostLearningConfig.class)) { |
| 848 | return false; |
| 849 | } |
| 850 | return true; |
| 851 | } |
| 852 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 853 | } |