Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.host.impl; |
| 2 | |
| 3 | import java.util.HashSet; |
| 4 | import java.util.Set; |
| 5 | import java.util.concurrent.TimeUnit; |
| 6 | |
| 7 | import org.jboss.netty.util.Timeout; |
| 8 | import org.jboss.netty.util.TimerTask; |
| 9 | import org.onlab.onos.net.Device; |
| 10 | import org.onlab.onos.net.Host; |
| 11 | import org.onlab.onos.net.Port; |
| 12 | import org.onlab.onos.net.device.DeviceService; |
| 13 | import org.onlab.onos.net.host.HostProvider; |
| 14 | import org.onlab.onos.net.host.HostService; |
| 15 | import org.onlab.onos.net.packet.PacketProvider; |
| 16 | import org.onlab.onos.net.topology.TopologyService; |
| 17 | import org.onlab.packet.IpPrefix; |
| 18 | import org.onlab.util.Timer; |
| 19 | |
| 20 | public class SimpleHostMonitor implements TimerTask { |
| 21 | |
| 22 | private final HostService hostService; |
| 23 | private final TopologyService topologyService; |
| 24 | private final DeviceService deviceService; |
| 25 | private final HostProvider hostProvider; |
| 26 | private final PacketProvider packetProvider; |
| 27 | |
| 28 | private final Set<IpPrefix> monitoredAddresses; |
| 29 | |
| 30 | private final long probeRate; |
| 31 | |
| 32 | private Timeout timeout; |
| 33 | |
| 34 | public SimpleHostMonitor(HostService hostService, TopologyService topologyService, |
| 35 | DeviceService deviceService, |
| 36 | HostProvider hostProvider, PacketProvider packetProvider) { |
| 37 | this.hostService = hostService; |
| 38 | this.topologyService = topologyService; |
| 39 | this.deviceService = deviceService; |
| 40 | this.hostProvider = hostProvider; |
| 41 | this.packetProvider = packetProvider; |
| 42 | |
| 43 | monitoredAddresses = new HashSet<>(); |
| 44 | |
| 45 | probeRate = 30000; // milliseconds |
| 46 | |
| 47 | timeout = Timer.getTimer().newTimeout(this, 0, TimeUnit.MILLISECONDS); |
| 48 | } |
| 49 | |
| 50 | public void addMonitoringFor(IpPrefix ip) { |
| 51 | monitoredAddresses.add(ip); |
| 52 | } |
| 53 | |
| 54 | public void stopMonitoring(IpPrefix ip) { |
| 55 | monitoredAddresses.remove(ip); |
| 56 | } |
| 57 | |
| 58 | public void shutdown() { |
| 59 | timeout.cancel(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void run(Timeout timeout) throws Exception { |
| 64 | for (IpPrefix ip : monitoredAddresses) { |
| 65 | Set<Host> hosts = hostService.getHostsByIp(ip); |
| 66 | |
| 67 | if (hosts.isEmpty()) { |
| 68 | sendArpRequest(ip); |
| 69 | } else { |
| 70 | for (Host host : hosts) { |
| 71 | hostProvider.triggerProbe(host); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | timeout = Timer.getTimer().newTimeout(this, probeRate, TimeUnit.MILLISECONDS); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sends an ARP request for the given IP address. |
| 81 | * |
| 82 | * @param targetIp IP address to ARP for |
| 83 | */ |
| 84 | private void sendArpRequest(IpPrefix targetIp) { |
| 85 | // emit ARP packet out appropriate ports |
| 86 | |
| 87 | // if ip in one of the configured (external) subnets |
| 88 | // sent out that port |
| 89 | // else (ip isn't in any configured subnet) |
| 90 | // send out all non-external edge ports |
| 91 | |
| 92 | for (Device device : deviceService.getDevices()) { |
| 93 | for (Port port : deviceService.getPorts(device.id())) { |
| 94 | for (IpPrefix ip : port.ipAddresses()) { |
| 95 | if (ip.contains(targetIp)) { |
| 96 | sendProbe(port, targetIp); |
| 97 | continue; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | private void sendProbe(Port port, IpPrefix targetIp) { |
| 106 | |
| 107 | } |
| 108 | } |