Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.host.impl; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 18 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.Dictionary; |
| 22 | import java.util.Set; |
| 23 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Modified; |
| 28 | import org.apache.felix.scr.annotations.Property; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 31 | import org.onlab.packet.ARP; |
| 32 | import org.onlab.packet.Ethernet; |
| 33 | import org.onlab.packet.IPacket; |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 34 | import org.onlab.packet.ICMP6; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 35 | import org.onlab.packet.IPv6; |
| 36 | import org.onlab.packet.IpAddress; |
| 37 | import org.onlab.packet.VlanId; |
| 38 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 39 | import org.onlab.packet.ndp.NeighborSolicitation; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 40 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 43 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.Host; |
| 45 | import org.onosproject.net.HostId; |
| 46 | import org.onosproject.net.HostLocation; |
| 47 | import org.onosproject.net.device.DeviceEvent; |
| 48 | import org.onosproject.net.device.DeviceListener; |
| 49 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.host.DefaultHostDescription; |
| 53 | import org.onosproject.net.host.HostDescription; |
| 54 | import org.onosproject.net.host.HostProvider; |
| 55 | import org.onosproject.net.host.HostProviderRegistry; |
| 56 | import org.onosproject.net.host.HostProviderService; |
| 57 | import org.onosproject.net.host.HostService; |
| 58 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 59 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 60 | import org.onosproject.net.packet.PacketProcessor; |
| 61 | import org.onosproject.net.packet.PacketService; |
| 62 | import org.onosproject.net.provider.AbstractProvider; |
| 63 | import org.onosproject.net.provider.ProviderId; |
| 64 | import org.onosproject.net.topology.Topology; |
| 65 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 66 | import org.osgi.service.component.ComponentContext; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Provider which uses an OpenFlow controller to detect network |
| 71 | * end-station hosts. |
| 72 | */ |
| 73 | @Component(immediate = true) |
| 74 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
| 75 | |
| 76 | private final Logger log = getLogger(getClass()); |
| 77 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected CoreService coreService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 82 | protected HostProviderRegistry providerRegistry; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 85 | protected PacketService packetService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected TopologyService topologyService; |
| 89 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected HostService hostService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected DeviceService deviceService; |
| 95 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 96 | private HostProviderService providerService; |
| 97 | |
| 98 | private final InternalHostProvider processor = new InternalHostProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 99 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 100 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 101 | private ApplicationId appId; |
| 102 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 103 | @Property(name = "hostRemovalEnabled", boolValue = true, |
| 104 | label = "Enable host removal on port/device down events") |
| 105 | private boolean hostRemovalEnabled = true; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 106 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 107 | @Property(name = "ipv6NeighborDiscovery", boolValue = false, |
| 108 | label = "Enable using IPv6 Neighbor Discovery by the " + |
| 109 | "Host Location Provider; default is false") |
| 110 | private boolean ipv6NeighborDiscovery = false; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Creates an OpenFlow host provider. |
| 114 | */ |
| 115 | public HostLocationProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 116 | super(new ProviderId("of", "org.onosproject.provider.host")); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Activate |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 120 | public void activate(ComponentContext context) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 121 | appId = |
| 122 | coreService.registerApplication("org.onosproject.provider.host"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 123 | readComponentConfiguration(context); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 124 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 125 | providerService = providerRegistry.register(this); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 126 | packetService.addProcessor(processor, 1); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 127 | deviceService.addListener(deviceListener); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 128 | |
| 129 | TrafficSelector.Builder selectorBuilder = |
| 130 | DefaultTrafficSelector.builder(); |
| 131 | selectorBuilder.matchEthType(Ethernet.TYPE_ARP); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 132 | packetService.requestPackets(selectorBuilder.build(), |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 133 | PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 134 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 135 | if (ipv6NeighborDiscovery) { |
| 136 | // IPv6 Neighbor Solicitation packet. |
| 137 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 138 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 139 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 140 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 141 | packetService.requestPackets(selectorBuilder.build(), |
| 142 | PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 143 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 144 | // IPv6 Neighbor Advertisement packet. |
| 145 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 146 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 147 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 148 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 149 | packetService.requestPackets(selectorBuilder.build(), |
| 150 | PacketPriority.CONTROL, appId); |
| 151 | } |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 152 | |
| 153 | log.info("Started with Application ID {}", appId.id()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Deactivate |
| 157 | public void deactivate() { |
| 158 | providerRegistry.unregister(this); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 159 | packetService.removeProcessor(processor); |
alshabib | bfc6b72 | 2014-11-29 12:53:48 -0800 | [diff] [blame] | 160 | deviceService.removeListener(deviceListener); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 161 | providerService = null; |
| 162 | log.info("Stopped"); |
| 163 | } |
| 164 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 165 | @Modified |
| 166 | public void modified(ComponentContext context) { |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 167 | readComponentConfiguration(context); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Extracts properties from the component configuration context. |
| 172 | * |
| 173 | * @param context the component context |
| 174 | */ |
| 175 | private void readComponentConfiguration(ComponentContext context) { |
| 176 | Dictionary<?, ?> properties = context.getProperties(); |
| 177 | Boolean flag; |
| 178 | |
| 179 | flag = isPropertyEnabled(properties, "hostRemovalEnabled"); |
| 180 | if (flag == null) { |
| 181 | log.info("Host removal on port/device down events is not configured, " + |
| 182 | "using current value of {}", hostRemovalEnabled); |
| 183 | } else { |
| 184 | hostRemovalEnabled = flag; |
| 185 | log.info("Configured. Host removal on port/device down events is {}", |
| 186 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 187 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 188 | |
| 189 | flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery"); |
| 190 | if (flag == null) { |
| 191 | log.info("Using IPv6 Neighbor Discovery is not configured, " + |
| 192 | "using current value of {}", ipv6NeighborDiscovery); |
| 193 | } else { |
| 194 | ipv6NeighborDiscovery = flag; |
| 195 | log.info("Configured. Using IPv6 Neighbor Discovery is {}", |
| 196 | ipv6NeighborDiscovery ? "enabled" : "disabled"); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Check property name is defined and set to true. |
| 202 | * |
| 203 | * @param properties properties to be looked up |
| 204 | * @param propertyName the name of the property to look up |
| 205 | * @return value when the propertyName is defined or return null |
| 206 | */ |
| 207 | private static Boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 208 | String propertyName) { |
| 209 | Boolean value = null; |
| 210 | try { |
| 211 | String s = (String) properties.get(propertyName); |
| 212 | value = isNullOrEmpty(s) ? null : s.trim().equals("true"); |
| 213 | } catch (ClassCastException e) { |
| 214 | // No propertyName defined. |
| 215 | value = null; |
| 216 | } |
| 217 | return value; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 218 | } |
| 219 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 220 | @Override |
| 221 | public void triggerProbe(Host host) { |
| 222 | log.info("Triggering probe on device {}", host); |
| 223 | } |
| 224 | |
| 225 | private class InternalHostProvider implements PacketProcessor { |
| 226 | |
| 227 | @Override |
| 228 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 229 | if (context == null) { |
| 230 | return; |
| 231 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 232 | Ethernet eth = context.inPacket().parsed(); |
| 233 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 234 | if (eth == null) { |
| 235 | return; |
| 236 | } |
| 237 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 238 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 239 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 240 | |
| 241 | // If this is not an edge port, bail out. |
| 242 | Topology topology = topologyService.currentTopology(); |
| 243 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 244 | return; |
| 245 | } |
| 246 | |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 247 | HostLocation hloc = |
| 248 | new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 249 | |
| 250 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 251 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 252 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 253 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 254 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 255 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 256 | arp.getSenderProtocolAddress()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 257 | HostDescription hdescr = |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 258 | new DefaultHostDescription(eth.getSourceMAC(), vlan, |
| 259 | hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 260 | providerService.hostDetected(hid, hdescr); |
| 261 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 262 | // IPv4: update location only |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 263 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 264 | HostDescription hdescr = |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 265 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 266 | providerService.hostDetected(hid, hdescr); |
| 267 | |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 268 | // |
| 269 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 270 | // new hosts, update both location and IP. |
| 271 | // |
| 272 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 273 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
| 274 | IpAddress ip = null; |
| 275 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 276 | |
| 277 | IPacket iPkt = ipv6; |
| 278 | while (iPkt != null) { |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 279 | if (iPkt instanceof NeighborAdvertisement || |
| 280 | iPkt instanceof NeighborSolicitation) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 281 | IpAddress sourceAddress = |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 282 | IpAddress.valueOf(IpAddress.Version.INET6, |
| 283 | ipv6.getSourceAddress()); |
| 284 | // Ignore DAD packets, in which source address is zero |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 285 | if (!sourceAddress.isZero()) { |
| 286 | ip = sourceAddress; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | iPkt = iPkt.getPayload(); |
| 291 | } |
| 292 | HostDescription hdescr = (ip == null) ? |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 293 | new DefaultHostDescription(eth.getSourceMAC(), vlan, |
| 294 | hloc) : |
| 295 | new DefaultHostDescription(eth.getSourceMAC(), vlan, |
| 296 | hloc, ip); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 297 | providerService.hostDetected(hid, hdescr); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 301 | |
| 302 | // Auxiliary listener to device events. |
| 303 | private class InternalDeviceListener implements DeviceListener { |
| 304 | @Override |
| 305 | public void event(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 306 | Device device = event.subject(); |
| 307 | switch (event.type()) { |
| 308 | case DEVICE_ADDED: |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 309 | break; |
| 310 | case DEVICE_AVAILABILITY_CHANGED: |
| 311 | if (hostRemovalEnabled && |
| 312 | !deviceService.isAvailable(device.id())) { |
| 313 | removeHosts(hostService.getConnectedHosts(device.id())); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 314 | } |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 315 | break; |
| 316 | case DEVICE_SUSPENDED: |
| 317 | case DEVICE_UPDATED: |
| 318 | // Nothing to do? |
| 319 | break; |
| 320 | case DEVICE_REMOVED: |
| 321 | if (hostRemovalEnabled) { |
| 322 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 323 | } |
| 324 | break; |
| 325 | case PORT_ADDED: |
| 326 | break; |
| 327 | case PORT_UPDATED: |
| 328 | if (hostRemovalEnabled) { |
| 329 | ConnectPoint point = |
| 330 | new ConnectPoint(device.id(), event.port().number()); |
| 331 | removeHosts(hostService.getConnectedHosts(point)); |
| 332 | } |
| 333 | break; |
| 334 | case PORT_REMOVED: |
| 335 | // Nothing to do? |
| 336 | break; |
| 337 | default: |
| 338 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Signals host vanish for all specified hosts. |
| 344 | private void removeHosts(Set<Host> hosts) { |
| 345 | for (Host host : hosts) { |
| 346 | providerService.hostVanished(host.id()); |
| 347 | } |
| 348 | } |
| 349 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 350 | } |