Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.host.impl; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Multimap; |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 21 | import org.junit.After; |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 22 | import org.junit.Before; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 24 | import org.onlab.packet.ARP; |
| 25 | import org.onlab.packet.Ethernet; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IPv6; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 30 | import org.onlab.packet.VlanId; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 31 | import org.onlab.packet.ndp.NeighborSolicitation; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 32 | import org.onosproject.incubator.net.intf.Interface; |
| 33 | import org.onosproject.incubator.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.Host; |
| 38 | import org.onosproject.net.MastershipRole; |
| 39 | import org.onosproject.net.Port; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.device.DeviceListener; |
| 42 | import org.onosproject.net.device.DeviceServiceAdapter; |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 43 | import org.onosproject.net.edge.EdgePortService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.instructions.Instruction; |
| 45 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 46 | import org.onosproject.net.host.HostProvider; |
| 47 | import org.onosproject.net.host.InterfaceIpAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.packet.OutboundPacket; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 49 | import org.onosproject.net.packet.PacketServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 51 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.Collections; |
| 54 | import java.util.List; |
| 55 | import java.util.Set; |
| 56 | |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 57 | import static org.easymock.EasyMock.anyObject; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 58 | import static org.easymock.EasyMock.createMock; |
| 59 | import static org.easymock.EasyMock.expect; |
| 60 | import static org.easymock.EasyMock.expectLastCall; |
| 61 | import static org.easymock.EasyMock.replay; |
| 62 | import static org.easymock.EasyMock.verify; |
| 63 | import static org.junit.Assert.assertArrayEquals; |
| 64 | import static org.junit.Assert.assertEquals; |
| 65 | import static org.junit.Assert.assertTrue; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 66 | |
| 67 | public class HostMonitorTest { |
| 68 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 69 | private static final IpAddress TARGET_IPV4_ADDR = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 70 | IpAddress.valueOf("10.0.0.1"); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 71 | private static final IpAddress SOURCE_IPV4_ADDR = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 72 | IpAddress.valueOf("10.0.0.99"); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 73 | private static final InterfaceIpAddress IA1 = |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 74 | new InterfaceIpAddress(SOURCE_IPV4_ADDR, IpPrefix.valueOf("10.0.0.0/24")); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 75 | private MacAddress sourceMac = MacAddress.valueOf(1L); |
| 76 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 77 | private static final IpAddress TARGET_IPV6_ADDR = |
| 78 | IpAddress.valueOf("1000::1"); |
| 79 | private static final IpAddress SOURCE_IPV6_ADDR = |
| 80 | IpAddress.valueOf("1000::f"); |
| 81 | private static final InterfaceIpAddress IA2 = |
| 82 | new InterfaceIpAddress(SOURCE_IPV6_ADDR, IpPrefix.valueOf("1000::/64")); |
| 83 | private MacAddress sourceMac2 = MacAddress.valueOf(2L); |
| 84 | |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 85 | private EdgePortService edgePortService; |
| 86 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 87 | private HostMonitor hostMonitor; |
| 88 | |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 89 | @Before |
| 90 | public void setUp() { |
| 91 | edgePortService = createMock(EdgePortService.class); |
| 92 | expect(edgePortService.isEdgePoint(anyObject(ConnectPoint.class))) |
| 93 | .andReturn(true).anyTimes(); |
| 94 | replay(edgePortService); |
| 95 | } |
| 96 | |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 97 | @After |
| 98 | public void shutdown() { |
| 99 | hostMonitor.shutdown(); |
| 100 | } |
| 101 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 102 | @Test |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 103 | public void testMonitorIpv4HostExists() throws Exception { |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 104 | ProviderId id = new ProviderId("fake://", "id"); |
| 105 | |
| 106 | Host host = createMock(Host.class); |
| 107 | expect(host.providerId()).andReturn(id); |
| 108 | replay(host); |
| 109 | |
| 110 | HostManager hostManager = createMock(HostManager.class); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 111 | expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR)) |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 112 | .andReturn(Collections.singleton(host)); |
| 113 | replay(hostManager); |
| 114 | |
| 115 | HostProvider hostProvider = createMock(HostProvider.class); |
| 116 | expect(hostProvider.id()).andReturn(id).anyTimes(); |
| 117 | hostProvider.triggerProbe(host); |
| 118 | expectLastCall().once(); |
| 119 | replay(hostProvider); |
| 120 | |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 121 | hostMonitor = new HostMonitor(null, hostManager, null, edgePortService); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 122 | |
| 123 | hostMonitor.registerHostProvider(hostProvider); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 124 | hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 125 | |
| 126 | hostMonitor.run(null); |
| 127 | |
| 128 | verify(hostProvider); |
| 129 | } |
| 130 | |
| 131 | @Test |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 132 | public void testMonitorIpv6HostExists() throws Exception { |
| 133 | ProviderId id = new ProviderId("fake://", "id"); |
| 134 | |
| 135 | Host host = createMock(Host.class); |
| 136 | expect(host.providerId()).andReturn(id); |
| 137 | replay(host); |
| 138 | |
| 139 | HostManager hostManager = createMock(HostManager.class); |
| 140 | expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR)) |
| 141 | .andReturn(Collections.singleton(host)); |
| 142 | replay(hostManager); |
| 143 | |
| 144 | HostProvider hostProvider = createMock(HostProvider.class); |
| 145 | expect(hostProvider.id()).andReturn(id).anyTimes(); |
| 146 | hostProvider.triggerProbe(host); |
| 147 | expectLastCall().once(); |
| 148 | replay(hostProvider); |
| 149 | |
| 150 | hostMonitor = new HostMonitor(null, hostManager, null, edgePortService); |
| 151 | |
| 152 | hostMonitor.registerHostProvider(hostProvider); |
| 153 | hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR); |
| 154 | |
| 155 | hostMonitor.run(null); |
| 156 | |
| 157 | verify(hostProvider); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testMonitorIpv4HostDoesNotExist() throws Exception { |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 162 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 163 | HostManager hostManager = createMock(HostManager.class); |
| 164 | |
| 165 | DeviceId devId = DeviceId.deviceId("fake"); |
| 166 | |
| 167 | Device device = createMock(Device.class); |
| 168 | expect(device.id()).andReturn(devId).anyTimes(); |
| 169 | replay(device); |
| 170 | |
| 171 | PortNumber portNum = PortNumber.portNumber(1L); |
| 172 | |
| 173 | Port port = createMock(Port.class); |
| 174 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 175 | replay(port); |
| 176 | |
| 177 | TestDeviceService deviceService = new TestDeviceService(); |
| 178 | deviceService.addDevice(device, Collections.singleton(port)); |
| 179 | |
| 180 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 181 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 182 | expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 183 | .andReturn(Collections.emptySet()).anyTimes(); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 184 | replay(hostManager); |
| 185 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 186 | InterfaceService interfaceService = createMock(InterfaceService.class); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 187 | expect(interfaceService.getMatchingInterface(TARGET_IPV4_ADDR)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 188 | .andReturn(new Interface(cp, Collections.singleton(IA1), sourceMac, VlanId.NONE)) |
| 189 | .anyTimes(); |
| 190 | replay(interfaceService); |
| 191 | |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 192 | TestPacketService packetService = new TestPacketService(); |
| 193 | |
| 194 | |
| 195 | // Run the test |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 196 | hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 197 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 198 | hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 199 | hostMonitor.run(null); |
| 200 | |
| 201 | |
| 202 | // Check that a packet was sent to our PacketService and that it has |
| 203 | // the properties we expect |
Jonathan Hart | dc09a3b | 2014-10-27 11:34:26 -0700 | [diff] [blame] | 204 | assertEquals(1, packetService.packets.size()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 205 | OutboundPacket packet = packetService.packets.get(0); |
| 206 | |
| 207 | // Check the output port is correct |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 208 | assertEquals(1, packet.treatment().immediate().size()); |
| 209 | Instruction instruction = packet.treatment().immediate().get(0); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 210 | assertTrue(instruction instanceof OutputInstruction); |
| 211 | OutputInstruction oi = (OutputInstruction) instruction; |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 212 | assertEquals(portNum, oi.port()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 213 | |
| 214 | // Check the output packet is correct (well the important bits anyway) |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 215 | final byte[] pktData = new byte[packet.data().remaining()]; |
| 216 | packet.data().get(pktData); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 217 | Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 218 | assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID()); |
| 219 | ARP arp = (ARP) eth.getPayload(); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 220 | assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(), |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 221 | arp.getSenderProtocolAddress()); |
| 222 | assertArrayEquals(sourceMac.toBytes(), |
| 223 | arp.getSenderHardwareAddress()); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 224 | assertArrayEquals(TARGET_IPV4_ADDR.toOctets(), |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 225 | arp.getTargetProtocolAddress()); |
| 226 | } |
| 227 | |
| 228 | @Test |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 229 | public void testMonitorIpv6HostDoesNotExist() throws Exception { |
| 230 | |
| 231 | HostManager hostManager = createMock(HostManager.class); |
| 232 | |
| 233 | DeviceId devId = DeviceId.deviceId("fake"); |
| 234 | |
| 235 | Device device = createMock(Device.class); |
| 236 | expect(device.id()).andReturn(devId).anyTimes(); |
| 237 | replay(device); |
| 238 | |
| 239 | PortNumber portNum = PortNumber.portNumber(2L); |
| 240 | |
| 241 | Port port = createMock(Port.class); |
| 242 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 243 | replay(port); |
| 244 | |
| 245 | TestDeviceService deviceService = new TestDeviceService(); |
| 246 | deviceService.addDevice(device, Collections.singleton(port)); |
| 247 | |
| 248 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
| 249 | |
| 250 | expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR)) |
| 251 | .andReturn(Collections.emptySet()).anyTimes(); |
| 252 | replay(hostManager); |
| 253 | |
| 254 | InterfaceService interfaceService = createMock(InterfaceService.class); |
| 255 | expect(interfaceService.getMatchingInterface(TARGET_IPV6_ADDR)) |
| 256 | .andReturn(new Interface(cp, Collections.singleton(IA2), sourceMac2, VlanId.NONE)) |
| 257 | .anyTimes(); |
| 258 | replay(interfaceService); |
| 259 | |
| 260 | TestPacketService packetService = new TestPacketService(); |
| 261 | |
| 262 | |
| 263 | // Run the test |
| 264 | hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService); |
| 265 | |
| 266 | hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR); |
| 267 | hostMonitor.run(null); |
| 268 | |
| 269 | |
| 270 | // Check that a packet was sent to our PacketService and that it has |
| 271 | // the properties we expect |
| 272 | assertEquals(1, packetService.packets.size()); |
| 273 | OutboundPacket packet = packetService.packets.get(0); |
| 274 | |
| 275 | // Check the output port is correct |
| 276 | assertEquals(1, packet.treatment().immediate().size()); |
| 277 | Instruction instruction = packet.treatment().immediate().get(0); |
| 278 | assertTrue(instruction instanceof OutputInstruction); |
| 279 | OutputInstruction oi = (OutputInstruction) instruction; |
| 280 | assertEquals(portNum, oi.port()); |
| 281 | |
| 282 | // Check the output packet is correct (well the important bits anyway) |
| 283 | final byte[] pktData = new byte[packet.data().remaining()]; |
| 284 | packet.data().get(pktData); |
| 285 | Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length); |
| 286 | assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID()); |
| 287 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 288 | assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress()); |
| 289 | |
| 290 | NeighborSolicitation ns = |
| 291 | (NeighborSolicitation) ipv6.getPayload().getPayload(); |
| 292 | assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data()); |
| 293 | |
| 294 | assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress()); |
| 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void testMonitorIpv4HostDoesNotExistWithVlan() throws Exception { |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 299 | |
| 300 | HostManager hostManager = createMock(HostManager.class); |
| 301 | |
| 302 | DeviceId devId = DeviceId.deviceId("fake"); |
| 303 | short vlan = 5; |
| 304 | |
| 305 | Device device = createMock(Device.class); |
| 306 | expect(device.id()).andReturn(devId).anyTimes(); |
| 307 | replay(device); |
| 308 | |
| 309 | PortNumber portNum = PortNumber.portNumber(1L); |
| 310 | |
| 311 | Port port = createMock(Port.class); |
| 312 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 313 | replay(port); |
| 314 | |
| 315 | TestDeviceService deviceService = new TestDeviceService(); |
| 316 | deviceService.addDevice(device, Collections.singleton(port)); |
| 317 | |
| 318 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 319 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 320 | expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 321 | .andReturn(Collections.emptySet()).anyTimes(); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 322 | replay(hostManager); |
| 323 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 324 | InterfaceService interfaceService = createMock(InterfaceService.class); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 325 | expect(interfaceService.getMatchingInterface(TARGET_IPV4_ADDR)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 326 | .andReturn(new Interface(cp, Collections.singleton(IA1), sourceMac, VlanId.vlanId(vlan))) |
| 327 | .anyTimes(); |
| 328 | replay(interfaceService); |
| 329 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 330 | TestPacketService packetService = new TestPacketService(); |
| 331 | |
| 332 | |
| 333 | // Run the test |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 334 | hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 335 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 336 | hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 337 | hostMonitor.run(null); |
| 338 | |
| 339 | |
| 340 | // Check that a packet was sent to our PacketService and that it has |
| 341 | // the properties we expect |
| 342 | assertEquals(1, packetService.packets.size()); |
| 343 | OutboundPacket packet = packetService.packets.get(0); |
| 344 | |
| 345 | // Check the output port is correct |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 346 | assertEquals(1, packet.treatment().immediate().size()); |
| 347 | Instruction instruction = packet.treatment().immediate().get(0); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 348 | assertTrue(instruction instanceof OutputInstruction); |
| 349 | OutputInstruction oi = (OutputInstruction) instruction; |
| 350 | assertEquals(portNum, oi.port()); |
| 351 | |
| 352 | // Check the output packet is correct (well the important bits anyway) |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 353 | final byte[] pktData = new byte[packet.data().remaining()]; |
| 354 | packet.data().get(pktData); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 355 | Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 356 | assertEquals(vlan, eth.getVlanID()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 357 | ARP arp = (ARP) eth.getPayload(); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 358 | assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(), |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 359 | arp.getSenderProtocolAddress()); |
| 360 | assertArrayEquals(sourceMac.toBytes(), |
| 361 | arp.getSenderHardwareAddress()); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 362 | assertArrayEquals(TARGET_IPV4_ADDR.toOctets(), |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 363 | arp.getTargetProtocolAddress()); |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 366 | @Test |
| 367 | public void testMonitorIpv6HostDoesNotExistWithVlan() throws Exception { |
| 368 | |
| 369 | HostManager hostManager = createMock(HostManager.class); |
| 370 | |
| 371 | DeviceId devId = DeviceId.deviceId("fake"); |
| 372 | short vlan = 5; |
| 373 | |
| 374 | Device device = createMock(Device.class); |
| 375 | expect(device.id()).andReturn(devId).anyTimes(); |
| 376 | replay(device); |
| 377 | |
| 378 | PortNumber portNum = PortNumber.portNumber(1L); |
| 379 | |
| 380 | Port port = createMock(Port.class); |
| 381 | expect(port.number()).andReturn(portNum).anyTimes(); |
| 382 | replay(port); |
| 383 | |
| 384 | TestDeviceService deviceService = new TestDeviceService(); |
| 385 | deviceService.addDevice(device, Collections.singleton(port)); |
| 386 | |
| 387 | ConnectPoint cp = new ConnectPoint(devId, portNum); |
| 388 | |
| 389 | expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR)) |
| 390 | .andReturn(Collections.emptySet()).anyTimes(); |
| 391 | replay(hostManager); |
| 392 | |
| 393 | InterfaceService interfaceService = createMock(InterfaceService.class); |
| 394 | expect(interfaceService.getMatchingInterface(TARGET_IPV6_ADDR)) |
| 395 | .andReturn(new Interface(cp, Collections.singleton(IA2), sourceMac2, VlanId.vlanId(vlan))) |
| 396 | .anyTimes(); |
| 397 | replay(interfaceService); |
| 398 | |
| 399 | TestPacketService packetService = new TestPacketService(); |
| 400 | |
| 401 | |
| 402 | // Run the test |
| 403 | hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService); |
| 404 | |
| 405 | hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR); |
| 406 | hostMonitor.run(null); |
| 407 | |
| 408 | |
| 409 | // Check that a packet was sent to our PacketService and that it has |
| 410 | // the properties we expect |
| 411 | assertEquals(1, packetService.packets.size()); |
| 412 | OutboundPacket packet = packetService.packets.get(0); |
| 413 | |
| 414 | // Check the output port is correct |
| 415 | assertEquals(1, packet.treatment().immediate().size()); |
| 416 | Instruction instruction = packet.treatment().immediate().get(0); |
| 417 | assertTrue(instruction instanceof OutputInstruction); |
| 418 | OutputInstruction oi = (OutputInstruction) instruction; |
| 419 | assertEquals(portNum, oi.port()); |
| 420 | |
| 421 | // Check the output packet is correct (well the important bits anyway) |
| 422 | final byte[] pktData = new byte[packet.data().remaining()]; |
| 423 | packet.data().get(pktData); |
| 424 | Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length); |
| 425 | assertEquals(vlan, eth.getVlanID()); |
| 426 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 427 | assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress()); |
| 428 | |
| 429 | NeighborSolicitation ns = |
| 430 | (NeighborSolicitation) ipv6.getPayload().getPayload(); |
| 431 | assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data()); |
| 432 | |
| 433 | assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress()); |
| 434 | } |
| 435 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 436 | class TestPacketService extends PacketServiceAdapter { |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 437 | |
| 438 | List<OutboundPacket> packets = new ArrayList<>(); |
| 439 | |
| 440 | @Override |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 441 | public void emit(OutboundPacket packet) { |
| 442 | packets.add(packet); |
| 443 | } |
| 444 | } |
| 445 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 446 | class TestDeviceService extends DeviceServiceAdapter { |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 447 | |
| 448 | List<Device> devices = Lists.newArrayList(); |
| 449 | Multimap<DeviceId, Port> devicePorts = HashMultimap.create(); |
| 450 | |
| 451 | void addDevice(Device device, Set<Port> ports) { |
| 452 | devices.add(device); |
| 453 | for (Port p : ports) { |
| 454 | devicePorts.put(device.id(), p); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public int getDeviceCount() { |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public Iterable<Device> getDevices() { |
| 465 | return devices; |
| 466 | } |
| 467 | |
| 468 | @Override |
| 469 | public Device getDevice(DeviceId deviceId) { |
| 470 | return null; |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public MastershipRole getRole(DeviceId deviceId) { |
| 475 | return null; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public List<Port> getPorts(DeviceId deviceId) { |
| 480 | List<Port> ports = Lists.newArrayList(); |
| 481 | for (Port p : devicePorts.get(deviceId)) { |
| 482 | ports.add(p); |
| 483 | } |
| 484 | return ports; |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 489 | return null; |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public boolean isAvailable(DeviceId deviceId) { |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public void addListener(DeviceListener listener) { |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public void removeListener(DeviceListener listener) { |
| 503 | } |
| 504 | } |
| 505 | } |