blob: f9821ed83769fa26ffc600d3da005edcfe138eb7 [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;
Jonathan Harte8600eb2015-01-12 10:30:45 -080026import org.onlab.packet.ARP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070027import org.onlab.packet.BasePacket;
Charles Chane6067892016-11-17 10:23:46 -080028import org.onlab.packet.DHCP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070029import org.onlab.packet.DHCP6;
Jonathan Harte8600eb2015-01-12 10:30:45 -080030import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080031import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070032import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080033import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080034import org.onlab.packet.IPv6;
Charles Chan35a32322017-08-14 11:42:11 -070035import org.onlab.packet.Ip4Address;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070036import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080037import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080038import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080039import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080040import org.onlab.packet.VlanId;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070041import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
42import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
43import org.onlab.packet.dhcp.Dhcp6IaNaOption;
44import org.onlab.packet.dhcp.Dhcp6IaTaOption;
45import org.onlab.packet.dhcp.Dhcp6RelayOption;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080046import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080047import org.onlab.packet.ndp.NeighborAdvertisement;
48import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080049import org.onlab.packet.ndp.RouterAdvertisement;
50import org.onlab.packet.ndp.RouterSolicitation;
Jian Lid9b5f552016-03-11 18:15:31 -080051import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070052import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080053import org.onosproject.core.ApplicationId;
54import org.onosproject.core.CoreService;
Ray Milkeyfacf2862017-08-03 11:58:29 -070055import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080056import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080057import org.onosproject.net.Device;
Charles Chan888e20a2017-05-01 15:44:23 -070058import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.Host;
60import org.onosproject.net.HostId;
61import org.onosproject.net.HostLocation;
62import org.onosproject.net.device.DeviceEvent;
63import org.onosproject.net.device.DeviceListener;
64import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080065import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070066import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080067import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070068import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080069import org.onosproject.net.host.DefaultHostDescription;
70import org.onosproject.net.host.HostDescription;
71import org.onosproject.net.host.HostProvider;
72import org.onosproject.net.host.HostProviderRegistry;
73import org.onosproject.net.host.HostProviderService;
74import org.onosproject.net.host.HostService;
sdn94b00152016-08-30 02:12:32 -070075import org.onosproject.net.packet.DefaultOutboundPacket;
76import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080077import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080078import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080079import org.onosproject.net.packet.PacketProcessor;
80import org.onosproject.net.packet.PacketService;
81import org.onosproject.net.provider.AbstractProvider;
82import org.onosproject.net.provider.ProviderId;
83import org.onosproject.net.topology.Topology;
84import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080085import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070086import org.slf4j.Logger;
87
sdn94b00152016-08-30 02:12:32 -070088import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070089import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070090import java.util.Objects;
91import java.util.Optional;
Madan Jampania3770c32015-12-11 12:07:41 -080092import java.util.concurrent.ExecutorService;
Charles Chanbb86b782017-09-13 16:14:22 -070093import java.util.concurrent.Executors;
94import java.util.concurrent.ScheduledExecutorService;
95import java.util.concurrent.TimeUnit;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070096import java.util.stream.Stream;
Charles Chan35a32322017-08-14 11:42:11 -070097import java.util.Set;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070098
Madan Jampania3770c32015-12-11 12:07:41 -080099import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
100import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700101import static org.slf4j.LoggerFactory.getLogger;
102
alshabibe1cf87d2014-10-17 09:23:50 -0700103/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700104 * Provider which uses an OpenFlow controller to detect network end-station
105 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -0700106 */
107@Component(immediate = true)
108public 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 HostProviderService providerService;
136
137 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800138 private final DeviceListener deviceListener = new InternalDeviceListener();
139
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800140 private ApplicationId appId;
141
Thomas Vachuska33601602014-11-19 03:32:15 -0800142 @Property(name = "hostRemovalEnabled", boolValue = true,
143 label = "Enable host removal on port/device down events")
144 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700145
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700146 @Property(name = "requestArp", boolValue = true,
147 label = "Request ARP packets for neighbor discovery by the " +
Charles Chane6067892016-11-17 10:23:46 -0800148 "Host Location Provider; default is true")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700149 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800150
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700151 @Property(name = "requestIpv6ND", boolValue = false,
152 label = "Requests IPv6 Neighbor Discovery by the " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700153 "Host Location Provider; default is false")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700154 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800155
156 @Property(name = "useDhcp", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700157 label = "Use DHCP to update IP address of the host; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800158 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700159
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700160 @Property(name = "useDhcp6", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700161 label = "Use DHCPv6 to update IP address of the host; default is false")
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700162 private boolean useDhcp6 = false;
163
Jonathan Hart9af322d2016-01-06 17:42:04 -0800164 @Property(name = "requestInterceptsEnabled", boolValue = true,
165 label = "Enable requesting packet intercepts")
166 private boolean requestInterceptsEnabled = true;
167
Charles Chan35a32322017-08-14 11:42:11 -0700168 @Property(name = "multihomingEnabled", boolValue = false,
169 label = "Allow hosts to be multihomed")
170 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800171
Charles Chan35a32322017-08-14 11:42:11 -0700172 protected ExecutorService eventHandler;
sdn94b00152016-08-30 02:12:32 -0700173
alshabibe1cf87d2014-10-17 09:23:50 -0700174 /**
175 * Creates an OpenFlow host provider.
176 */
177 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800178 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700179 }
180
181 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800182 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700183 cfgService.registerProperties(getClass());
184 appId = coreService.registerApplication("org.onosproject.provider.host");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800185 eventHandler = newSingleThreadScheduledExecutor(
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700186 groupedThreads("onos/host-loc-provider", "event-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700187 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700188 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800189 deviceService.addListener(deviceListener);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800190
191 modified(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800192
Charles M.C. Chane148de82015-05-06 12:38:21 +0800193 log.info("Started with Application ID {}", appId.id());
194 }
195
196 @Deactivate
197 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800198 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700199
200 withdrawIntercepts();
201
Charles M.C. Chane148de82015-05-06 12:38:21 +0800202 providerRegistry.unregister(this);
203 packetService.removeProcessor(processor);
204 deviceService.removeListener(deviceListener);
Madan Jampania3770c32015-12-11 12:07:41 -0800205 eventHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800206 providerService = null;
207 log.info("Stopped");
208 }
209
210 @Modified
211 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800212 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800213
214 if (requestInterceptsEnabled) {
215 requestIntercepts();
216 } else {
217 withdrawIntercepts();
218 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800219 }
220
221 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700222 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800223 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700224 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800225 // Use ARP
226 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
227 .matchEthType(Ethernet.TYPE_ARP)
228 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700229 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800230 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700231 } else {
Charles Chane6067892016-11-17 10:23:46 -0800232 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800233 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700234
Charles Chane6067892016-11-17 10:23:46 -0800235 // Use IPv6 Neighbor Discovery
236 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
237 .matchEthType(Ethernet.TYPE_IPV6)
238 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
239 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
240 .build();
241 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
242 .matchEthType(Ethernet.TYPE_IPV6)
243 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
244 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
245 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700246 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800247 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
248 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700249 } else {
Charles Chane6067892016-11-17 10:23:46 -0800250 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
251 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
252 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700253 }
254
255 /**
256 * Withdraw packet intercepts.
257 */
258 private void withdrawIntercepts() {
259 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
260 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700261 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700262
263 // IPv6 Neighbor Solicitation packet.
264 selector.matchEthType(Ethernet.TYPE_IPV6);
265 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
266 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
267 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
268
269 // IPv6 Neighbor Advertisement packet.
270 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
271 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800272 }
273
274 /**
275 * Extracts properties from the component configuration context.
276 *
277 * @param context the component context
278 */
279 private void readComponentConfiguration(ComponentContext context) {
280 Dictionary<?, ?> properties = context.getProperties();
281 Boolean flag;
282
Jian Lid9b5f552016-03-11 18:15:31 -0800283 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800284 if (flag == null) {
285 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700286 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800287 } else {
288 hostRemovalEnabled = flag;
289 log.info("Configured. Host removal on port/device down events is {}",
290 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800291 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800292
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700293 flag = Tools.isPropertyEnabled(properties, "requestArp");
Charles Chane6067892016-11-17 10:23:46 -0800294 if (flag == null) {
295 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700296 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800297 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700298 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800299 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700300 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800301 }
302
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700303 flag = Tools.isPropertyEnabled(properties, "requestIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800304 if (flag == null) {
305 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700306 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800307 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700308 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800309 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700310 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800311 }
312
313 flag = Tools.isPropertyEnabled(properties, "useDhcp");
314 if (flag == null) {
315 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700316 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800317 } else {
318 useDhcp = flag;
319 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700320 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800321 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800322
Jian Lid9b5f552016-03-11 18:15:31 -0800323 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800324 if (flag == null) {
325 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700326 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800327 } else {
328 requestInterceptsEnabled = flag;
329 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700330 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800331 }
Charles Chan35a32322017-08-14 11:42:11 -0700332
333 flag = Tools.isPropertyEnabled(properties, "multihomingEnabled");
334 if (flag == null) {
335 log.info("Multihoming is not configured, " +
336 "using current value of {}", multihomingEnabled);
337 } else {
338 multihomingEnabled = flag;
339 log.info("Configured. Multihoming is {}",
340 multihomingEnabled ? "enabled" : "disabled");
341 }
342
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800343 }
344
alshabibe1cf87d2014-10-17 09:23:50 -0700345 @Override
346 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800347 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700348
349 // FIXME Disabling host probing for now, because sending packets from a
350 // broadcast MAC address caused problems when two ONOS networks were
351 // interconnected. Host probing should take into account the interface
352 // configuration when determining which source address to use.
353
354 //MastershipRole role = deviceService.getRole(host.location().deviceId());
355 //if (role.equals(MastershipRole.MASTER)) {
356 // host.ipAddresses().forEach(ip -> {
357 // sendProbe(host, ip);
358 // });
359 //} else {
360 // log.info("not the master, master will probe {}");
361 //}
sdn94b00152016-08-30 02:12:32 -0700362 }
363
364 private void sendProbe(Host host, IpAddress targetIp) {
365 Ethernet probePacket = null;
366 if (targetIp.isIp4()) {
367 // IPv4: Use ARP
368 probePacket = buildArpRequest(targetIp, host);
369 } else {
370 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700371 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700372 log.info("Triggering probe on device {} ", host);
373 }
374
375 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
376
377 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
378 ByteBuffer.wrap(probePacket.serialize()));
379
380 packetService.emit(outboundPacket);
381 }
382
Charles Chan888e20a2017-05-01 15:44:23 -0700383 // 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 -0700384 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700385 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
386 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
387 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700388 }
389
390 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800391 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700392 * Create or update host information.
393 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800394 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700395 * @param hid host ID
396 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800397 * @param vlan VLAN ID
398 * @param hloc host location
Charles Chan888e20a2017-05-01 15:44:23 -0700399 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800400 */
Charles Chan888e20a2017-05-01 15:44:23 -0700401 private void createOrUpdateHost(HostId hid, MacAddress mac,
402 VlanId vlan, HostLocation hloc,
403 IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700404 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
405
406 if (multihomingEnabled) {
407 Host existingHost = hostService.getHost(hid);
408 if (existingHost != null) {
409 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700410
Charles Chan653e2ac2017-11-22 14:49:55 -0800411 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
412 // New location is on a device that we haven't seen before
413 // Could be a dual-home host. Append new location and send out the probe
414 newLocations.addAll(prevLocations);
Charles Chan35a32322017-08-14 11:42:11 -0700415 probeLocations(existingHost);
Charles Chan653e2ac2017-11-22 14:49:55 -0800416 } else {
417 // Move within the same switch
418 // Simply replace old location that is on the same device
419 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
420 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700421 }
422 }
423 }
424
Charles Chan888e20a2017-05-01 15:44:23 -0700425 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Charles Chan35a32322017-08-14 11:42:11 -0700426 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(), false) :
427 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip), false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700428 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800429 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700430 } catch (IllegalStateException e) {
431 log.debug("Host {} suppressed", hid);
432 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800433 }
alshabibe1cf87d2014-10-17 09:23:50 -0700434
Charles Chane6067892016-11-17 10:23:46 -0800435 /**
Charles Chan35a32322017-08-14 11:42:11 -0700436 * Start verification procedure of all previous locations by sending probes.
437 *
438 * @param host Host to be probed
439 */
440 private void probeLocations(Host host) {
441 host.locations().forEach(location -> {
442 MacAddress probeMac = providerService.addPendingHostLocation(host.id(), location);
443
444 host.ipAddresses().stream().findFirst().ifPresent(ip -> {
Charles Chan2fde6d42017-08-23 14:46:43 -0700445 log.debug("Probing host {} with {}", host.id(), ip);
Charles Chan35a32322017-08-14 11:42:11 -0700446 Ethernet probe;
447 if (ip.isIp4()) {
448 probe = ARP.buildArpRequest(probeMac.toBytes(), Ip4Address.ZERO.toOctets(),
449 host.id().mac().toBytes(), ip.toOctets(),
450 host.id().mac().toBytes(), host.id().vlanId().toShort());
451 } else {
452 probe = NeighborSolicitation.buildNdpSolicit(
453 ip.getIp6Address().toOctets(),
454 IPv6.getLinkLocalAddress(probeMac.toBytes()),
455 IPv6.getSolicitNodeAddress(ip.getIp6Address().toOctets()),
456 probeMac.toBytes(),
457 IPv6.getMCastMacAddress(ip.getIp6Address().toOctets()),
458 host.id().vlanId());
459 }
Charles Chanbb86b782017-09-13 16:14:22 -0700460
461 // NOTE: delay the probe a little bit to wait for the store synchronization is done
462 ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
463 executorService.schedule(() -> sendProbe(probe, location), 1000, TimeUnit.MILLISECONDS);
Charles Chan35a32322017-08-14 11:42:11 -0700464 });
465 });
466 }
467
468 /**
469 * Send the probe packet on given port.
470 *
471 * @param probe the probe packet
472 * @param connectPoint the port we want to probe
473 */
474 private void sendProbe(Ethernet probe, ConnectPoint connectPoint) {
475 log.info("Probing host {} on location {} with probeMac {}",
476 probe.getDestinationMAC(), connectPoint, probe.getSourceMAC());
477 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
478 OutboundPacket outboundPacket = new DefaultOutboundPacket(connectPoint.deviceId(),
479 treatment, ByteBuffer.wrap(probe.serialize()));
480 packetService.emit(outboundPacket);
481 }
482
483 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700484 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800485 *
486 * @param hid host ID
487 * @param ip IP address
488 */
Charles Chan888e20a2017-05-01 15:44:23 -0700489 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800490 Host host = hostService.getHost(hid);
491 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700492 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800493 return;
494 }
495
Charles Chan888e20a2017-05-01 15:44:23 -0700496 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700497 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800498 try {
499 providerService.hostDetected(hid, desc, false);
500 } catch (IllegalStateException e) {
501 log.debug("Host {} suppressed", hid);
502 }
503 }
504
alshabibe1cf87d2014-10-17 09:23:50 -0700505 @Override
506 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700507 if (context == null) {
508 return;
509 }
alshabibe1cf87d2014-10-17 09:23:50 -0700510
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700511 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800512 if (eth == null) {
513 return;
514 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700515
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800516 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700517 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
518 return;
519 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800520
alshabibe1cf87d2014-10-17 09:23:50 -0700521 VlanId vlan = VlanId.vlanId(eth.getVlanID());
522 ConnectPoint heardOn = context.inPacket().receivedFrom();
523
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700524 // If this arrived on control port, bail out.
525 if (heardOn.port().isLogical()) {
526 return;
527 }
528
alshabibe1cf87d2014-10-17 09:23:50 -0700529 // If this is not an edge port, bail out.
530 Topology topology = topologyService.currentTopology();
531 if (topologyService.isInfrastructure(topology, heardOn)) {
532 return;
533 }
534
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700535 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700536 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700537 MacAddress destMac = eth.getDestinationMAC();
538
539 // Receives a location probe. Invalid entry from the cache
540 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
541 log.info("Receives probe for {}/{} on {}", srcMac, vlan, heardOn);
542 providerService.removePendingHostLocation(destMac);
543 return;
544 }
alshabibe1cf87d2014-10-17 09:23:50 -0700545
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800546 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700547 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
548 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800549 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
550 arp.getSenderProtocolAddress());
Charles Chan888e20a2017-05-01 15:44:23 -0700551 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700552
Charles Chan5d5e0622015-09-25 13:00:06 -0700553 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700554 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700555 // Update host location
556 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
557 if (useDhcp) {
558 DHCP dhcp = findDhcp(eth).orElse(null);
559 // DHCP ACK: additionally update IP of DHCP client
560 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
561 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
562 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
563 HostId hostId = HostId.hostId(hostMac, hostVlan);
564 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700565 }
Charles Chane6067892016-11-17 10:23:46 -0800566 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700567 // NeighborAdvertisement and NeighborSolicitation: possible
568 // new hosts, update both location and IP.
569 //
570 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800571 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800572 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800573 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700574 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800575
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800576 // skip extension headers
577 IPacket pkt = ipv6;
578 while (pkt.getPayload() != null &&
579 pkt.getPayload() instanceof IExtensionHeader) {
580 pkt = pkt.getPayload();
581 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800582 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700583
584 // DHCPv6 protocol
585 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
586 if (dhcp6 != null && useDhcp6) {
587 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
588 handleDhcp6(dhcp6, vlan);
589 return;
590 }
591
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800592 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700593 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800594 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400595 if (pkt != null) {
596 // RouterSolicitation, RouterAdvertisement
597 if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800598 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800599 }
Charles Chan178b0f52017-10-08 23:51:22 -0400600 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
601 // Duplicate Address Detection
602 if (ip.isZero()) {
603 return;
604 }
605 // NeighborSolicitation, NeighborAdvertisement
606 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
607
608 // Also learn from the target address of NeighborAdvertisement
609 if (pkt instanceof NeighborAdvertisement) {
610 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
611 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
612 createOrUpdateHost(hid, srcMac, vlan, hloc, targetAddr);
613 }
614 return;
615 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800616 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800617 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800618
Yi Tsengaa417a62017-09-08 17:22:51 -0700619 // multicast, exclude DHCPv6
620 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800621 return;
622 }
623
624 // normal IPv6 packets
Charles Chan888e20a2017-05-01 15:44:23 -0700625 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700626 }
627 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700628
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700629 /**
630 * Handles DHCPv6 packet, if message type is ACK, update IP address
631 * according to DHCPv6 payload (IA Address option).
632 *
633 * @param dhcp6 the DHCPv6 payload
634 * @param vlanId the vlan of this packet
635 */
636 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
637 // extract the relay message if exist
638 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
639 dhcp6 = dhcp6.getOptions().stream()
640 .filter(opt -> opt instanceof Dhcp6RelayOption)
641 .map(BasePacket::getPayload)
642 .map(pld -> (DHCP6) pld)
643 .findFirst()
644 .orElse(null);
645 }
646
647 if (dhcp6 == null) {
648 // Can't find dhcp payload
649 log.warn("Can't find dhcp payload from relay message");
650 return;
651 }
652
653 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
654 // Update IP address only when we received REPLY message
655 return;
656 }
657 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
658 .stream()
659 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
660 .map(opt -> (Dhcp6ClientIdOption) opt)
661 .findFirst();
662
663 if (!clientIdOption.isPresent()) {
664 // invalid DHCPv6 option
665 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
666 return;
667 }
668
669 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
670 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
671 // No any mac address found
672 log.warn("Can't find client mac from option {}", clientIdOption);
673 return;
674 }
675 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
676
677 // Extract IPv6 address from IA NA ot IA TA option
678 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
679 .stream()
680 .filter(opt -> opt instanceof Dhcp6IaNaOption)
681 .map(opt -> (Dhcp6IaNaOption) opt)
682 .findFirst();
683 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
684 .stream()
685 .filter(opt -> opt instanceof Dhcp6IaTaOption)
686 .map(opt -> (Dhcp6IaTaOption) opt)
687 .findFirst();
688 Optional<Dhcp6IaAddressOption> iaAddressOption;
689 if (iaNaOption.isPresent()) {
690 iaAddressOption = iaNaOption.get().getOptions().stream()
691 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
692 .map(opt -> (Dhcp6IaAddressOption) opt)
693 .findFirst();
694 } else if (iaTaOption.isPresent()) {
695 iaAddressOption = iaTaOption.get().getOptions().stream()
696 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
697 .map(opt -> (Dhcp6IaAddressOption) opt)
698 .findFirst();
699 } else {
700 iaAddressOption = Optional.empty();
701 }
702 if (iaAddressOption.isPresent()) {
703 Ip6Address ip = iaAddressOption.get().getIp6Address();
704 HostId hostId = HostId.hostId(clientMac, vlanId);
705 updateHostIp(hostId, ip);
706 } else {
707 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
708 }
709 }
710
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700711 private Optional<DHCP> findDhcp(Ethernet eth) {
712 IPacket pkt = eth.getPayload();
713 return Stream.of(pkt)
714 .filter(Objects::nonNull)
715 .filter(p -> p instanceof IPv4)
716 .map(IPacket::getPayload)
717 .filter(Objects::nonNull)
718 .filter(p -> p instanceof UDP)
719 .map(IPacket::getPayload)
720 .filter(Objects::nonNull)
721 .filter(p -> p instanceof DHCP)
722 .map(p -> (DHCP) p)
723 .findFirst();
724 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700725
726 private Optional<DHCP6> findDhcp6(IPacket pkt) {
727 return Stream.of(pkt)
728 .filter(Objects::nonNull)
729 .filter(p -> p instanceof UDP)
730 .map(IPacket::getPayload)
731 .filter(Objects::nonNull)
732 .filter(p -> p instanceof DHCP6)
733 .map(p -> (DHCP6) p)
734 .findFirst();
735 }
alshabibe1cf87d2014-10-17 09:23:50 -0700736 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800737
738 // Auxiliary listener to device events.
739 private class InternalDeviceListener implements DeviceListener {
740 @Override
741 public void event(DeviceEvent event) {
Madan Jampania3770c32015-12-11 12:07:41 -0800742 eventHandler.execute(() -> handleEvent(event));
743 }
744
745 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800746 Device device = event.subject();
747 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700748 case DEVICE_ADDED:
749 break;
750 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700751 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
752 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700753 }
754 break;
755 case DEVICE_SUSPENDED:
756 case DEVICE_UPDATED:
757 // Nothing to do?
758 break;
759 case DEVICE_REMOVED:
760 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700761 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700762 }
763 break;
764 case PORT_ADDED:
765 break;
766 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700767 if (hostRemovalEnabled && !event.port().isEnabled()) {
768 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700769 }
770 break;
771 case PORT_REMOVED:
772 // Nothing to do?
773 break;
774 default:
775 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800776 }
777 }
778 }
779
Charles Chan888e20a2017-05-01 15:44:23 -0700780 /**
781 * When a device goes down, update the location of affected hosts.
782 *
783 * @param deviceId the device that goes down
784 */
785 private void processDeviceDown(DeviceId deviceId) {
786 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
787 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
788 .forEach(affectedLocation ->
789 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
790 );
791 }
792
793 /**
794 * When a port goes down, update the location of affected hosts.
795 *
796 * @param connectPoint the port that goes down
797 */
798 private void processPortDown(ConnectPoint connectPoint) {
799 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
800 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
801 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800802 }
803
alshabibe1cf87d2014-10-17 09:23:50 -0700804}