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 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 49 | private static final IpAddress TARGET_IP_ADDR = |
| 50 | IpAddress.valueOf("10.0.0.1"); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 51 | private static final IpAddress SOURCE_ADDR = |
| 52 | IpAddress.valueOf("10.0.0.99"); |
| 53 | private static final InterfaceIpAddress IA1 = |
| 54 | new InterfaceIpAddress(SOURCE_ADDR, IpPrefix.valueOf("10.0.0.0/24")); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 55 | private MacAddress sourceMac = MacAddress.valueOf(1L); |
| 56 | |
| 57 | private HostMonitor hostMonitor; |
| 58 | |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 59 | @After |
| 60 | public void shutdown() { |
| 61 | hostMonitor.shutdown(); |
| 62 | } |
| 63 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 64 | @Test |
| 65 | public void testMonitorHostExists() throws Exception { |
| 66 | ProviderId id = new ProviderId("fake://", "id"); |
| 67 | |
| 68 | Host host = createMock(Host.class); |
| 69 | expect(host.providerId()).andReturn(id); |
| 70 | replay(host); |
| 71 | |
| 72 | HostManager hostManager = createMock(HostManager.class); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 73 | expect(hostManager.getHostsByIp(TARGET_IP_ADDR)) |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 74 | .andReturn(Collections.singleton(host)); |
| 75 | replay(hostManager); |
| 76 | |
| 77 | HostProvider hostProvider = createMock(HostProvider.class); |
| 78 | expect(hostProvider.id()).andReturn(id).anyTimes(); |
| 79 | hostProvider.triggerProbe(host); |
| 80 | expectLastCall().once(); |
| 81 | replay(hostProvider); |
| 82 | |
| 83 | hostMonitor = new HostMonitor(null, null, hostManager); |
| 84 | |
| 85 | hostMonitor.registerHostProvider(hostProvider); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 86 | hostMonitor.addMonitoringFor(TARGET_IP_ADDR); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 87 | |
| 88 | hostMonitor.run(null); |
| 89 | |
| 90 | verify(hostProvider); |
| 91 | } |
| 92 | |
| 93 | @Test |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 94 | public void testMonitorHostDoesNotExist() throws Exception { |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 95 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 96 | HostManager hostManager = createMock(HostManager.class); |
| 97 | |
| 98 | DeviceId devId = DeviceId.deviceId("fake"); |
| 99 | |
| 100 | Device device = createMock(Device.class); |
| 101 | expect(device.id()).andReturn(devId).anyTimes(); |
| 102 | replay(device); |
| 103 | |
| 104 | PortNumber portNum = PortNumber.portNumber(1L); |
| 105 | |
| 106 | Port port = createMock(Port.class); |
| 107 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 108 | replay(port); |
| 109 | |
| 110 | TestDeviceService deviceService = new TestDeviceService(); |
| 111 | deviceService.addDevice(device, Collections.singleton(port)); |
| 112 | |
| 113 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 114 | PortAddresses pa = |
| 115 | new PortAddresses(cp, Collections.singleton(IA1), sourceMac); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 116 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 117 | expect(hostManager.getHostsByIp(TARGET_IP_ADDR)) |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 118 | .andReturn(Collections.<Host>emptySet()).anyTimes(); |
| 119 | expect(hostManager.getAddressBindingsForPort(cp)) |
| 120 | .andReturn(pa).anyTimes(); |
| 121 | replay(hostManager); |
| 122 | |
| 123 | TestPacketService packetService = new TestPacketService(); |
| 124 | |
| 125 | |
| 126 | // Run the test |
| 127 | hostMonitor = new HostMonitor(deviceService, packetService, hostManager); |
| 128 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 129 | hostMonitor.addMonitoringFor(TARGET_IP_ADDR); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 130 | hostMonitor.run(null); |
| 131 | |
| 132 | |
| 133 | // Check that a packet was sent to our PacketService and that it has |
| 134 | // the properties we expect |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 135 | assertEquals(1, packetService.packets.size()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 136 | OutboundPacket packet = packetService.packets.get(0); |
| 137 | |
| 138 | // Check the output port is correct |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 139 | assertEquals(1, packet.treatment().instructions().size()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 140 | Instruction instruction = packet.treatment().instructions().get(0); |
| 141 | assertTrue(instruction instanceof OutputInstruction); |
| 142 | OutputInstruction oi = (OutputInstruction) instruction; |
| 143 | assertTrue(oi.port().equals(portNum)); |
| 144 | |
| 145 | // Check the output packet is correct (well the important bits anyway) |
| 146 | Ethernet eth = new Ethernet(); |
| 147 | eth.deserialize(packet.data().array(), 0, packet.data().array().length); |
| 148 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 149 | assertTrue(Arrays.equals(arp.getSenderProtocolAddress(), |
| 150 | SOURCE_ADDR.toOctets())); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 151 | assertTrue(Arrays.equals(arp.getSenderHardwareAddress(), sourceMac.toBytes())); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 152 | assertTrue(Arrays.equals(arp.getTargetProtocolAddress(), |
| 153 | TARGET_IP_ADDR.toOctets())); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 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 | } |