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