Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.host.impl; |
| 2 | |
| 3 | import static org.easymock.EasyMock.createMock; |
| 4 | import static org.easymock.EasyMock.expect; |
| 5 | import static org.easymock.EasyMock.expectLastCall; |
| 6 | import static org.easymock.EasyMock.replay; |
| 7 | import static org.easymock.EasyMock.verify; |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 8 | import static org.junit.Assert.assertEquals; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 9 | import static org.junit.Assert.assertTrue; |
| 10 | |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.Arrays; |
| 13 | import java.util.Collections; |
| 14 | import java.util.List; |
| 15 | import java.util.Set; |
| 16 | |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 17 | import org.junit.After; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 18 | import org.junit.Test; |
| 19 | import org.onlab.onos.net.ConnectPoint; |
| 20 | import org.onlab.onos.net.Device; |
| 21 | import org.onlab.onos.net.DeviceId; |
| 22 | import org.onlab.onos.net.Host; |
| 23 | import org.onlab.onos.net.MastershipRole; |
| 24 | import org.onlab.onos.net.Port; |
| 25 | import org.onlab.onos.net.PortNumber; |
| 26 | import org.onlab.onos.net.device.DeviceListener; |
| 27 | import org.onlab.onos.net.device.DeviceService; |
| 28 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 29 | import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction; |
| 30 | import org.onlab.onos.net.host.HostProvider; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame^] | 31 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.host.PortAddresses; |
| 33 | import org.onlab.onos.net.packet.OutboundPacket; |
| 34 | import org.onlab.onos.net.packet.PacketProcessor; |
| 35 | import org.onlab.onos.net.packet.PacketService; |
| 36 | import org.onlab.onos.net.provider.ProviderId; |
| 37 | import org.onlab.packet.ARP; |
| 38 | import org.onlab.packet.Ethernet; |
| 39 | import org.onlab.packet.IpAddress; |
| 40 | import org.onlab.packet.IpPrefix; |
| 41 | import org.onlab.packet.MacAddress; |
| 42 | |
| 43 | import com.google.common.collect.HashMultimap; |
| 44 | import com.google.common.collect.Lists; |
| 45 | import com.google.common.collect.Multimap; |
| 46 | |
| 47 | public class HostMonitorTest { |
| 48 | |
| 49 | private IpAddress targetIpAddress = IpAddress.valueOf("10.0.0.1"); |
| 50 | private IpPrefix targetIpPrefix = IpPrefix.valueOf(targetIpAddress.toOctets()); |
| 51 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame^] | 52 | private static final IpAddress SOURCE_ADDR = |
| 53 | IpAddress.valueOf("10.0.0.99"); |
| 54 | private static final InterfaceIpAddress IA1 = |
| 55 | new InterfaceIpAddress(SOURCE_ADDR, IpPrefix.valueOf("10.0.0.0/24")); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 56 | private MacAddress sourceMac = MacAddress.valueOf(1L); |
| 57 | |
| 58 | private HostMonitor hostMonitor; |
| 59 | |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 60 | @After |
| 61 | public void shutdown() { |
| 62 | hostMonitor.shutdown(); |
| 63 | } |
| 64 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 65 | @Test |
| 66 | public void testMonitorHostExists() throws Exception { |
| 67 | ProviderId id = new ProviderId("fake://", "id"); |
| 68 | |
| 69 | Host host = createMock(Host.class); |
| 70 | expect(host.providerId()).andReturn(id); |
| 71 | replay(host); |
| 72 | |
| 73 | HostManager hostManager = createMock(HostManager.class); |
| 74 | expect(hostManager.getHostsByIp(targetIpPrefix)) |
| 75 | .andReturn(Collections.singleton(host)); |
| 76 | replay(hostManager); |
| 77 | |
| 78 | HostProvider hostProvider = createMock(HostProvider.class); |
| 79 | expect(hostProvider.id()).andReturn(id).anyTimes(); |
| 80 | hostProvider.triggerProbe(host); |
| 81 | expectLastCall().once(); |
| 82 | replay(hostProvider); |
| 83 | |
| 84 | hostMonitor = new HostMonitor(null, null, hostManager); |
| 85 | |
| 86 | hostMonitor.registerHostProvider(hostProvider); |
| 87 | hostMonitor.addMonitoringFor(targetIpAddress); |
| 88 | |
| 89 | hostMonitor.run(null); |
| 90 | |
| 91 | verify(hostProvider); |
| 92 | } |
| 93 | |
| 94 | @Test |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 95 | public void testMonitorHostDoesNotExist() throws Exception { |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 96 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 97 | HostManager hostManager = createMock(HostManager.class); |
| 98 | |
| 99 | DeviceId devId = DeviceId.deviceId("fake"); |
| 100 | |
| 101 | Device device = createMock(Device.class); |
| 102 | expect(device.id()).andReturn(devId).anyTimes(); |
| 103 | replay(device); |
| 104 | |
| 105 | PortNumber portNum = PortNumber.portNumber(1L); |
| 106 | |
| 107 | Port port = createMock(Port.class); |
| 108 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 109 | replay(port); |
| 110 | |
| 111 | TestDeviceService deviceService = new TestDeviceService(); |
| 112 | deviceService.addDevice(device, Collections.singleton(port)); |
| 113 | |
| 114 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame^] | 115 | PortAddresses pa = |
| 116 | new PortAddresses(cp, Collections.singleton(IA1), sourceMac); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 117 | |
| 118 | expect(hostManager.getHostsByIp(targetIpPrefix)) |
| 119 | .andReturn(Collections.<Host>emptySet()).anyTimes(); |
| 120 | expect(hostManager.getAddressBindingsForPort(cp)) |
| 121 | .andReturn(pa).anyTimes(); |
| 122 | replay(hostManager); |
| 123 | |
| 124 | TestPacketService packetService = new TestPacketService(); |
| 125 | |
| 126 | |
| 127 | // Run the test |
| 128 | hostMonitor = new HostMonitor(deviceService, packetService, hostManager); |
| 129 | |
| 130 | hostMonitor.addMonitoringFor(targetIpAddress); |
| 131 | hostMonitor.run(null); |
| 132 | |
| 133 | |
| 134 | // Check that a packet was sent to our PacketService and that it has |
| 135 | // the properties we expect |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 136 | assertEquals(1, packetService.packets.size()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 137 | OutboundPacket packet = packetService.packets.get(0); |
| 138 | |
| 139 | // Check the output port is correct |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 140 | assertEquals(1, packet.treatment().instructions().size()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 141 | Instruction instruction = packet.treatment().instructions().get(0); |
| 142 | assertTrue(instruction instanceof OutputInstruction); |
| 143 | OutputInstruction oi = (OutputInstruction) instruction; |
| 144 | assertTrue(oi.port().equals(portNum)); |
| 145 | |
| 146 | // Check the output packet is correct (well the important bits anyway) |
| 147 | Ethernet eth = new Ethernet(); |
| 148 | eth.deserialize(packet.data().array(), 0, packet.data().array().length); |
| 149 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame^] | 150 | assertTrue(Arrays.equals(arp.getSenderProtocolAddress(), |
| 151 | SOURCE_ADDR.toOctets())); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 152 | assertTrue(Arrays.equals(arp.getSenderHardwareAddress(), sourceMac.toBytes())); |
| 153 | assertTrue(Arrays.equals(arp.getTargetProtocolAddress(), targetIpPrefix.toOctets())); |
| 154 | } |
| 155 | |
| 156 | class TestPacketService implements PacketService { |
| 157 | |
| 158 | List<OutboundPacket> packets = new ArrayList<>(); |
| 159 | |
| 160 | @Override |
| 161 | public void addProcessor(PacketProcessor processor, int priority) { |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void removeProcessor(PacketProcessor processor) { |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void emit(OutboundPacket packet) { |
| 170 | packets.add(packet); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | class TestDeviceService implements DeviceService { |
| 175 | |
| 176 | List<Device> devices = Lists.newArrayList(); |
| 177 | Multimap<DeviceId, Port> devicePorts = HashMultimap.create(); |
| 178 | |
| 179 | void addDevice(Device device, Set<Port> ports) { |
| 180 | devices.add(device); |
| 181 | for (Port p : ports) { |
| 182 | devicePorts.put(device.id(), p); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public int getDeviceCount() { |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Iterable<Device> getDevices() { |
| 193 | return devices; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public Device getDevice(DeviceId deviceId) { |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public MastershipRole getRole(DeviceId deviceId) { |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public List<Port> getPorts(DeviceId deviceId) { |
| 208 | List<Port> ports = Lists.newArrayList(); |
| 209 | for (Port p : devicePorts.get(deviceId)) { |
| 210 | ports.add(p); |
| 211 | } |
| 212 | return ports; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public boolean isAvailable(DeviceId deviceId) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void addListener(DeviceListener listener) { |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void removeListener(DeviceListener listener) { |
| 231 | } |
| 232 | } |
| 233 | } |