blob: 40e67b5c87c7aaedc94882beb8ec62f2513307c9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.host.impl;
alshabibe1cf87d2014-10-17 09:23:50 -070017
Charles Chan35a32322017-08-14 11:42:11 -070018import com.google.common.collect.Sets;
alshabibe1cf87d2014-10-17 09:23:50 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska33601602014-11-19 03:32:15 -080022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
alshabibe1cf87d2014-10-17 09:23:50 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Charles Chan47933752017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Service;
Jonathan Harte8600eb2015-01-12 10:30:45 -080027import org.onlab.packet.ARP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070028import org.onlab.packet.BasePacket;
Charles Chane6067892016-11-17 10:23:46 -080029import org.onlab.packet.DHCP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070030import org.onlab.packet.DHCP6;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070031import org.onlab.packet.EthType;
Jonathan Harte8600eb2015-01-12 10:30:45 -080032import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080033import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070034import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080035import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080036import org.onlab.packet.IPv6;
Charles Chan35a32322017-08-14 11:42:11 -070037import org.onlab.packet.Ip4Address;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070038import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080039import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080040import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080041import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080042import org.onlab.packet.VlanId;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070043import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
44import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
45import org.onlab.packet.dhcp.Dhcp6IaNaOption;
46import org.onlab.packet.dhcp.Dhcp6IaTaOption;
47import org.onlab.packet.dhcp.Dhcp6RelayOption;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080048import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080049import org.onlab.packet.ndp.NeighborAdvertisement;
50import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080051import org.onlab.packet.ndp.RouterAdvertisement;
52import org.onlab.packet.ndp.RouterSolicitation;
Jian Lid9b5f552016-03-11 18:15:31 -080053import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070054import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080055import org.onosproject.core.ApplicationId;
56import org.onosproject.core.CoreService;
Ray Milkeyfacf2862017-08-03 11:58:29 -070057import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080058import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080059import org.onosproject.net.Device;
Charles Chan888e20a2017-05-01 15:44:23 -070060import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.net.Host;
62import org.onosproject.net.HostId;
63import org.onosproject.net.HostLocation;
64import org.onosproject.net.device.DeviceEvent;
65import org.onosproject.net.device.DeviceListener;
66import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080067import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070068import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080069import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070070import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080071import org.onosproject.net.host.DefaultHostDescription;
72import org.onosproject.net.host.HostDescription;
73import org.onosproject.net.host.HostProvider;
74import org.onosproject.net.host.HostProviderRegistry;
75import org.onosproject.net.host.HostProviderService;
76import org.onosproject.net.host.HostService;
sdn94b00152016-08-30 02:12:32 -070077import org.onosproject.net.packet.DefaultOutboundPacket;
78import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080079import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080080import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080081import org.onosproject.net.packet.PacketProcessor;
82import org.onosproject.net.packet.PacketService;
83import org.onosproject.net.provider.AbstractProvider;
84import org.onosproject.net.provider.ProviderId;
85import org.onosproject.net.topology.Topology;
86import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080087import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070088import org.slf4j.Logger;
89
sdn94b00152016-08-30 02:12:32 -070090import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070091import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070092import java.util.Objects;
93import java.util.Optional;
Madan Jampania3770c32015-12-11 12:07:41 -080094import java.util.concurrent.ExecutorService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070095import java.util.stream.Stream;
Charles Chan35a32322017-08-14 11:42:11 -070096import java.util.Set;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070097
Madan Jampania3770c32015-12-11 12:07:41 -080098import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
99import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700100import static org.slf4j.LoggerFactory.getLogger;
101
alshabibe1cf87d2014-10-17 09:23:50 -0700102/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700103 * Provider which uses an OpenFlow controller to detect network end-station
104 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -0700105 */
106@Component(immediate = true)
Charles Chan47933752017-11-30 15:37:50 -0800107@Service
Charles Chanff79dd92018-06-01 16:33:48 -0700108public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -0700109 private final Logger log = getLogger(getClass());
110
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected CoreService coreService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -0700115 protected HostProviderRegistry providerRegistry;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800118 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected TopologyService topologyService;
122
Thomas Vachuska33601602014-11-19 03:32:15 -0800123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected HostService hostService;
125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected DeviceService deviceService;
128
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected ComponentConfigService cfgService;
131
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected InterfaceService interfaceService;
134
alshabibe1cf87d2014-10-17 09:23:50 -0700135 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800136 private final DeviceListener deviceListener = new InternalDeviceListener();
137
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800138 private ApplicationId appId;
139
Thomas Vachuska33601602014-11-19 03:32:15 -0800140 @Property(name = "hostRemovalEnabled", boolValue = true,
141 label = "Enable host removal on port/device down events")
142 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700143
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700144 @Property(name = "requestArp", boolValue = true,
145 label = "Request ARP packets for neighbor discovery by the " +
Charles Chane6067892016-11-17 10:23:46 -0800146 "Host Location Provider; default is true")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700147 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800148
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700149 @Property(name = "requestIpv6ND", boolValue = false,
150 label = "Requests IPv6 Neighbor Discovery by the " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700151 "Host Location Provider; default is false")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700152 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800153
154 @Property(name = "useDhcp", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700155 label = "Use DHCP to update IP address of the host; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800156 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700157
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700158 @Property(name = "useDhcp6", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700159 label = "Use DHCPv6 to update IP address of the host; default is false")
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700160 private boolean useDhcp6 = false;
161
Jonathan Hart9af322d2016-01-06 17:42:04 -0800162 @Property(name = "requestInterceptsEnabled", boolValue = true,
163 label = "Enable requesting packet intercepts")
164 private boolean requestInterceptsEnabled = true;
165
Charles Chan35a32322017-08-14 11:42:11 -0700166 @Property(name = "multihomingEnabled", boolValue = false,
167 label = "Allow hosts to be multihomed")
168 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800169
Charles Chanff79dd92018-06-01 16:33:48 -0700170 private HostProviderService providerService;
Charles Chan47933752017-11-30 15:37:50 -0800171
Charles Chanff79dd92018-06-01 16:33:48 -0700172 ExecutorService deviceEventHandler;
173 private ExecutorService probeEventHandler;
Charles Chan07f15f22018-05-08 21:35:50 -0700174 private ExecutorService packetHandler;
Charles Chan8277b6b2017-12-03 13:48:46 -0800175
alshabibe1cf87d2014-10-17 09:23:50 -0700176 /**
177 * Creates an OpenFlow host provider.
178 */
179 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800180 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700181 }
182
183 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800184 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700185 cfgService.registerProperties(getClass());
186 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chanff79dd92018-06-01 16:33:48 -0700187 deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
188 "device-event-handler", log));
189 probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
190 "probe-event-handler", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700191 packetHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
192 "packet-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700193 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700194 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800195 deviceService.addListener(deviceListener);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800196
197 modified(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800198
Charles M.C. Chane148de82015-05-06 12:38:21 +0800199 log.info("Started with Application ID {}", appId.id());
200 }
201
202 @Deactivate
203 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800204 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700205
206 withdrawIntercepts();
207
Charles M.C. Chane148de82015-05-06 12:38:21 +0800208 providerRegistry.unregister(this);
209 packetService.removeProcessor(processor);
210 deviceService.removeListener(deviceListener);
Charles Chanff79dd92018-06-01 16:33:48 -0700211 deviceEventHandler.shutdown();
212 probeEventHandler.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700213 packetHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800214 providerService = null;
215 log.info("Stopped");
216 }
217
218 @Modified
219 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800220 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800221
222 if (requestInterceptsEnabled) {
223 requestIntercepts();
224 } else {
225 withdrawIntercepts();
226 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800227 }
228
229 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700230 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800231 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700232 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800233 // Use ARP
234 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
235 .matchEthType(Ethernet.TYPE_ARP)
236 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700237 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800238 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700239 } else {
Charles Chane6067892016-11-17 10:23:46 -0800240 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800241 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700242
Charles Chane6067892016-11-17 10:23:46 -0800243 // Use IPv6 Neighbor Discovery
244 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
245 .matchEthType(Ethernet.TYPE_IPV6)
246 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
247 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
248 .build();
249 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
250 .matchEthType(Ethernet.TYPE_IPV6)
251 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
252 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
253 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700254 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800255 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
256 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700257 } else {
Charles Chane6067892016-11-17 10:23:46 -0800258 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
259 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
260 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700261 }
262
263 /**
264 * Withdraw packet intercepts.
265 */
266 private void withdrawIntercepts() {
267 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
268 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700269 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700270
271 // IPv6 Neighbor Solicitation packet.
272 selector.matchEthType(Ethernet.TYPE_IPV6);
273 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
274 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
275 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
276
277 // IPv6 Neighbor Advertisement packet.
278 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
279 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800280 }
281
282 /**
283 * Extracts properties from the component configuration context.
284 *
285 * @param context the component context
286 */
287 private void readComponentConfiguration(ComponentContext context) {
288 Dictionary<?, ?> properties = context.getProperties();
289 Boolean flag;
290
Jian Lid9b5f552016-03-11 18:15:31 -0800291 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800292 if (flag == null) {
293 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700294 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800295 } else {
296 hostRemovalEnabled = flag;
297 log.info("Configured. Host removal on port/device down events is {}",
298 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800299 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800300
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700301 flag = Tools.isPropertyEnabled(properties, "requestArp");
Charles Chane6067892016-11-17 10:23:46 -0800302 if (flag == null) {
303 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700304 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800305 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700306 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800307 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700308 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800309 }
310
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700311 flag = Tools.isPropertyEnabled(properties, "requestIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800312 if (flag == null) {
313 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700314 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800315 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700316 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800317 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700318 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800319 }
320
321 flag = Tools.isPropertyEnabled(properties, "useDhcp");
322 if (flag == null) {
323 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700324 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800325 } else {
326 useDhcp = flag;
327 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700328 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800329 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800330
Jian Lid9b5f552016-03-11 18:15:31 -0800331 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800332 if (flag == null) {
333 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700334 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800335 } else {
336 requestInterceptsEnabled = flag;
337 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700338 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800339 }
Charles Chan35a32322017-08-14 11:42:11 -0700340
341 flag = Tools.isPropertyEnabled(properties, "multihomingEnabled");
342 if (flag == null) {
343 log.info("Multihoming is not configured, " +
344 "using current value of {}", multihomingEnabled);
345 } else {
346 multihomingEnabled = flag;
347 log.info("Configured. Multihoming is {}",
348 multihomingEnabled ? "enabled" : "disabled");
349 }
Charles Chan47933752017-11-30 15:37:50 -0800350 }
Charles Chan35a32322017-08-14 11:42:11 -0700351
Charles Chan47933752017-11-30 15:37:50 -0800352 @Override
alshabibe1cf87d2014-10-17 09:23:50 -0700353 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800354 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700355
356 // FIXME Disabling host probing for now, because sending packets from a
357 // broadcast MAC address caused problems when two ONOS networks were
358 // interconnected. Host probing should take into account the interface
359 // configuration when determining which source address to use.
360
361 //MastershipRole role = deviceService.getRole(host.location().deviceId());
362 //if (role.equals(MastershipRole.MASTER)) {
363 // host.ipAddresses().forEach(ip -> {
364 // sendProbe(host, ip);
365 // });
366 //} else {
367 // log.info("not the master, master will probe {}");
368 //}
sdn94b00152016-08-30 02:12:32 -0700369 }
370
371 private void sendProbe(Host host, IpAddress targetIp) {
372 Ethernet probePacket = null;
373 if (targetIp.isIp4()) {
374 // IPv4: Use ARP
375 probePacket = buildArpRequest(targetIp, host);
376 } else {
377 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700378 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700379 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800380 return;
sdn94b00152016-08-30 02:12:32 -0700381 }
382
383 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
384
385 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
386 ByteBuffer.wrap(probePacket.serialize()));
387
388 packetService.emit(outboundPacket);
389 }
390
Charles Chan888e20a2017-05-01 15:44:23 -0700391 // This method is using source ip as 0.0.0.0 , to receive the reply even from the sub net hosts.
sdn94b00152016-08-30 02:12:32 -0700392 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700393 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
394 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
395 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700396 }
397
398 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800399 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700400 * Create or update host information.
401 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800402 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700403 * @param hid host ID
404 * @param mac source Mac address
405 * @param vlan VLAN ID
406 * @param innerVlan inner VLAN ID
407 * @param outerTpid outer TPID
408 * @param hloc host location
409 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800410 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700411 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
412 VlanId innerVlan, EthType outerTpid,
413 HostLocation hloc, IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700414 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
415
416 if (multihomingEnabled) {
417 Host existingHost = hostService.getHost(hid);
418 if (existingHost != null) {
419 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700420
Charles Chan653e2ac2017-11-22 14:49:55 -0800421 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
422 // New location is on a device that we haven't seen before
Charles Chanff79dd92018-06-01 16:33:48 -0700423 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800424 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800425 } else {
426 // Move within the same switch
427 // Simply replace old location that is on the same device
428 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
429 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700430 }
431 }
432 }
433
Charles Chan888e20a2017-05-01 15:44:23 -0700434 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700435 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
436 innerVlan, outerTpid, false) :
437 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
438 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700439 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800440 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700441 } catch (IllegalStateException e) {
442 log.debug("Host {} suppressed", hid);
443 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800444 }
alshabibe1cf87d2014-10-17 09:23:50 -0700445
Charles Chane6067892016-11-17 10:23:46 -0800446 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700447 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800448 *
449 * @param hid host ID
450 * @param ip IP address
451 */
Charles Chan888e20a2017-05-01 15:44:23 -0700452 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800453 Host host = hostService.getHost(hid);
454 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700455 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800456 return;
457 }
458
Charles Chan888e20a2017-05-01 15:44:23 -0700459 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700460 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800461 try {
462 providerService.hostDetected(hid, desc, false);
463 } catch (IllegalStateException e) {
464 log.debug("Host {} suppressed", hid);
465 }
466 }
467
alshabibe1cf87d2014-10-17 09:23:50 -0700468 @Override
469 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -0700470 packetHandler.execute(() -> processPacketInternal(context));
471 }
472
473 private void processPacketInternal(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700474 if (context == null) {
475 return;
476 }
alshabibe1cf87d2014-10-17 09:23:50 -0700477
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700478 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800479 if (eth == null) {
480 return;
481 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700482
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800483 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700484 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
485 return;
486 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800487
alshabibe1cf87d2014-10-17 09:23:50 -0700488 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700489 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
490 VlanId innerVlan = VlanId.NONE;
491 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
492 // Set up values for double-tagged hosts
493 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
494 innerVlan = vlan;
495 vlan = outerVlan;
496 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
497 }
alshabibe1cf87d2014-10-17 09:23:50 -0700498 ConnectPoint heardOn = context.inPacket().receivedFrom();
499
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700500 // If this arrived on control port, bail out.
501 if (heardOn.port().isLogical()) {
502 return;
503 }
504
alshabibe1cf87d2014-10-17 09:23:50 -0700505 // If this is not an edge port, bail out.
506 Topology topology = topologyService.currentTopology();
507 if (topologyService.isInfrastructure(topology, heardOn)) {
508 return;
509 }
510
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700511 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700512 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700513 MacAddress destMac = eth.getDestinationMAC();
514
Charles Chanff79dd92018-06-01 16:33:48 -0700515 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700516 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700517 return;
518 }
alshabibe1cf87d2014-10-17 09:23:50 -0700519
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800520 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700521 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
522 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800523 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
524 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700525 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700526
Charles Chan5d5e0622015-09-25 13:00:06 -0700527 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700528 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700529 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700530 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700531 if (useDhcp) {
532 DHCP dhcp = findDhcp(eth).orElse(null);
533 // DHCP ACK: additionally update IP of DHCP client
534 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
535 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
536 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
537 HostId hostId = HostId.hostId(hostMac, hostVlan);
538 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700539 }
Charles Chane6067892016-11-17 10:23:46 -0800540 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700541 // NeighborAdvertisement and NeighborSolicitation: possible
542 // new hosts, update both location and IP.
543 //
544 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800545 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800546 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800547 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700548 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800549
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800550 // skip extension headers
551 IPacket pkt = ipv6;
552 while (pkt.getPayload() != null &&
553 pkt.getPayload() instanceof IExtensionHeader) {
554 pkt = pkt.getPayload();
555 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800556 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700557
558 // DHCPv6 protocol
559 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
560 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700561 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700562 handleDhcp6(dhcp6, vlan);
563 return;
564 }
565
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800566 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700567 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800568 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400569 if (pkt != null) {
570 // RouterSolicitation, RouterAdvertisement
571 if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800572 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800573 }
Charles Chan178b0f52017-10-08 23:51:22 -0400574 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
575 // Duplicate Address Detection
576 if (ip.isZero()) {
577 return;
578 }
579 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700580 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400581
582 // Also learn from the target address of NeighborAdvertisement
583 if (pkt instanceof NeighborAdvertisement) {
584 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
585 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700586 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400587 }
588 return;
589 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800590 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800591 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800592
Yi Tsengaa417a62017-09-08 17:22:51 -0700593 // multicast, exclude DHCPv6
594 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800595 return;
596 }
597
598 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700599 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700600 }
601 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700602
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700603 /**
604 * Handles DHCPv6 packet, if message type is ACK, update IP address
605 * according to DHCPv6 payload (IA Address option).
606 *
607 * @param dhcp6 the DHCPv6 payload
608 * @param vlanId the vlan of this packet
609 */
610 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
611 // extract the relay message if exist
612 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
613 dhcp6 = dhcp6.getOptions().stream()
614 .filter(opt -> opt instanceof Dhcp6RelayOption)
615 .map(BasePacket::getPayload)
616 .map(pld -> (DHCP6) pld)
617 .findFirst()
618 .orElse(null);
619 }
620
621 if (dhcp6 == null) {
622 // Can't find dhcp payload
623 log.warn("Can't find dhcp payload from relay message");
624 return;
625 }
626
627 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
628 // Update IP address only when we received REPLY message
629 return;
630 }
631 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
632 .stream()
633 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
634 .map(opt -> (Dhcp6ClientIdOption) opt)
635 .findFirst();
636
637 if (!clientIdOption.isPresent()) {
638 // invalid DHCPv6 option
639 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
640 return;
641 }
642
643 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
644 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
645 // No any mac address found
646 log.warn("Can't find client mac from option {}", clientIdOption);
647 return;
648 }
649 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
650
651 // Extract IPv6 address from IA NA ot IA TA option
652 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
653 .stream()
654 .filter(opt -> opt instanceof Dhcp6IaNaOption)
655 .map(opt -> (Dhcp6IaNaOption) opt)
656 .findFirst();
657 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
658 .stream()
659 .filter(opt -> opt instanceof Dhcp6IaTaOption)
660 .map(opt -> (Dhcp6IaTaOption) opt)
661 .findFirst();
662 Optional<Dhcp6IaAddressOption> iaAddressOption;
663 if (iaNaOption.isPresent()) {
664 iaAddressOption = iaNaOption.get().getOptions().stream()
665 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
666 .map(opt -> (Dhcp6IaAddressOption) opt)
667 .findFirst();
668 } else if (iaTaOption.isPresent()) {
669 iaAddressOption = iaTaOption.get().getOptions().stream()
670 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
671 .map(opt -> (Dhcp6IaAddressOption) opt)
672 .findFirst();
673 } else {
674 iaAddressOption = Optional.empty();
675 }
676 if (iaAddressOption.isPresent()) {
677 Ip6Address ip = iaAddressOption.get().getIp6Address();
678 HostId hostId = HostId.hostId(clientMac, vlanId);
679 updateHostIp(hostId, ip);
680 } else {
681 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
682 }
683 }
684
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700685 private Optional<DHCP> findDhcp(Ethernet eth) {
686 IPacket pkt = eth.getPayload();
687 return Stream.of(pkt)
688 .filter(Objects::nonNull)
689 .filter(p -> p instanceof IPv4)
690 .map(IPacket::getPayload)
691 .filter(Objects::nonNull)
692 .filter(p -> p instanceof UDP)
693 .map(IPacket::getPayload)
694 .filter(Objects::nonNull)
695 .filter(p -> p instanceof DHCP)
696 .map(p -> (DHCP) p)
697 .findFirst();
698 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700699
700 private Optional<DHCP6> findDhcp6(IPacket pkt) {
701 return Stream.of(pkt)
702 .filter(Objects::nonNull)
703 .filter(p -> p instanceof UDP)
704 .map(IPacket::getPayload)
705 .filter(Objects::nonNull)
706 .filter(p -> p instanceof DHCP6)
707 .map(p -> (DHCP6) p)
708 .findFirst();
709 }
alshabibe1cf87d2014-10-17 09:23:50 -0700710 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800711
712 // Auxiliary listener to device events.
713 private class InternalDeviceListener implements DeviceListener {
714 @Override
715 public void event(DeviceEvent event) {
Charles Chanff79dd92018-06-01 16:33:48 -0700716 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800717 }
718
719 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800720 Device device = event.subject();
721 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700722 case DEVICE_ADDED:
723 break;
724 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700725 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
726 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700727 }
728 break;
729 case DEVICE_SUSPENDED:
730 case DEVICE_UPDATED:
731 // Nothing to do?
732 break;
733 case DEVICE_REMOVED:
734 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700735 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700736 }
737 break;
738 case PORT_ADDED:
739 break;
740 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700741 if (hostRemovalEnabled && !event.port().isEnabled()) {
742 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700743 }
744 break;
745 case PORT_REMOVED:
746 // Nothing to do?
747 break;
748 default:
749 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800750 }
751 }
752 }
753
Charles Chan888e20a2017-05-01 15:44:23 -0700754 /**
755 * When a device goes down, update the location of affected hosts.
756 *
757 * @param deviceId the device that goes down
758 */
759 private void processDeviceDown(DeviceId deviceId) {
760 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
761 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
762 .forEach(affectedLocation ->
763 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
764 );
765 }
766
767 /**
768 * When a port goes down, update the location of affected hosts.
769 *
770 * @param connectPoint the port that goes down
771 */
772 private void processPortDown(ConnectPoint connectPoint) {
773 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
774 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
775 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800776 }
777
alshabibe1cf87d2014-10-17 09:23:50 -0700778}