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