blob: d492e39f569a0ea3446d1a97219997cf84e97a49 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host.impl;
Jonathan Hartb4758a92014-09-24 10:46:45 -070017
Thomas Vachuska27bee092015-06-23 19:03:10 -070018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Multimap;
Jonathan Hartdc09a3b2014-10-27 11:34:26 -070021import org.junit.After;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020022import org.junit.Before;
Jonathan Hartb4758a92014-09-24 10:46:45 -070023import org.junit.Test;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080024import org.onlab.packet.ARP;
25import org.onlab.packet.Ethernet;
Charles Chan5d5e0622015-09-25 13:00:06 -070026import org.onlab.packet.IPv6;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080027import org.onlab.packet.IpAddress;
28import org.onlab.packet.IpPrefix;
29import org.onlab.packet.MacAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080030import org.onlab.packet.VlanId;
Charles Chan5d5e0622015-09-25 13:00:06 -070031import org.onlab.packet.ndp.NeighborSolicitation;
Ray Milkeyfacf2862017-08-03 11:58:29 -070032import org.onosproject.net.intf.Interface;
33import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.Host;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.Port;
39import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.device.DeviceServiceAdapter;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020041import org.onosproject.net.edge.EdgePortService;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.flow.instructions.Instruction;
43import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
44import org.onosproject.net.host.HostProvider;
45import org.onosproject.net.host.InterfaceIpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.packet.OutboundPacket;
Thomas Vachuska27bee092015-06-23 19:03:10 -070047import org.onosproject.net.packet.PacketServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.provider.ProviderId;
Jonathan Hartb4758a92014-09-24 10:46:45 -070049
Thomas Vachuska27bee092015-06-23 19:03:10 -070050import java.util.ArrayList;
51import java.util.Collections;
52import java.util.List;
53import java.util.Set;
54
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020055import static org.easymock.EasyMock.anyObject;
Jonathan Hart2a655752015-04-07 16:46:33 -070056import static org.easymock.EasyMock.createMock;
57import static org.easymock.EasyMock.expect;
58import static org.easymock.EasyMock.expectLastCall;
59import static org.easymock.EasyMock.replay;
60import static org.easymock.EasyMock.verify;
61import static org.junit.Assert.assertArrayEquals;
62import static org.junit.Assert.assertEquals;
63import static org.junit.Assert.assertTrue;
Jonathan Hartb4758a92014-09-24 10:46:45 -070064
65public class HostMonitorTest {
66
Charles Chan5d5e0622015-09-25 13:00:06 -070067 private static final IpAddress TARGET_IPV4_ADDR =
Thomas Vachuska27bee092015-06-23 19:03:10 -070068 IpAddress.valueOf("10.0.0.1");
Charles Chan5d5e0622015-09-25 13:00:06 -070069 private static final IpAddress SOURCE_IPV4_ADDR =
Thomas Vachuska27bee092015-06-23 19:03:10 -070070 IpAddress.valueOf("10.0.0.99");
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070071 private static final InterfaceIpAddress IA1 =
Charles Chan5d5e0622015-09-25 13:00:06 -070072 new InterfaceIpAddress(SOURCE_IPV4_ADDR, IpPrefix.valueOf("10.0.0.0/24"));
Jonathan Hartb4758a92014-09-24 10:46:45 -070073 private MacAddress sourceMac = MacAddress.valueOf(1L);
74
Charles Chan5d5e0622015-09-25 13:00:06 -070075 private static final IpAddress TARGET_IPV6_ADDR =
76 IpAddress.valueOf("1000::1");
77 private static final IpAddress SOURCE_IPV6_ADDR =
78 IpAddress.valueOf("1000::f");
79 private static final InterfaceIpAddress IA2 =
80 new InterfaceIpAddress(SOURCE_IPV6_ADDR, IpPrefix.valueOf("1000::/64"));
81 private MacAddress sourceMac2 = MacAddress.valueOf(2L);
82
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020083 private EdgePortService edgePortService;
84
Jonathan Hartb4758a92014-09-24 10:46:45 -070085 private HostMonitor hostMonitor;
86
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020087 @Before
88 public void setUp() {
89 edgePortService = createMock(EdgePortService.class);
90 expect(edgePortService.isEdgePoint(anyObject(ConnectPoint.class)))
91 .andReturn(true).anyTimes();
92 replay(edgePortService);
93 }
94
Jonathan Hartdc09a3b2014-10-27 11:34:26 -070095 @After
96 public void shutdown() {
97 hostMonitor.shutdown();
98 }
99
Jonathan Hartb4758a92014-09-24 10:46:45 -0700100 @Test
Charles Chan5d5e0622015-09-25 13:00:06 -0700101 public void testMonitorIpv4HostExists() throws Exception {
Jonathan Hart78613d22016-07-27 11:25:29 -0700102 testMonitorHostExists(TARGET_IPV4_ADDR);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700103 }
104
105 @Test
Charles Chan5d5e0622015-09-25 13:00:06 -0700106 public void testMonitorIpv6HostExists() throws Exception {
Jonathan Hart78613d22016-07-27 11:25:29 -0700107 testMonitorHostExists(TARGET_IPV6_ADDR);
108 }
109
110 private void testMonitorHostExists(IpAddress hostIp) throws Exception {
Charles Chan5d5e0622015-09-25 13:00:06 -0700111 ProviderId id = new ProviderId("fake://", "id");
112
113 Host host = createMock(Host.class);
Jonathan Hart78613d22016-07-27 11:25:29 -0700114 expect(host.providerId()).andReturn(id).anyTimes();
Charles Chan5d5e0622015-09-25 13:00:06 -0700115 replay(host);
116
117 HostManager hostManager = createMock(HostManager.class);
Jonathan Hart78613d22016-07-27 11:25:29 -0700118 expect(hostManager.getHostsByIp(hostIp))
119 .andReturn(Collections.singleton(host))
120 .anyTimes();
Charles Chan5d5e0622015-09-25 13:00:06 -0700121 replay(hostManager);
122
123 HostProvider hostProvider = createMock(HostProvider.class);
124 expect(hostProvider.id()).andReturn(id).anyTimes();
125 hostProvider.triggerProbe(host);
Jonathan Hart78613d22016-07-27 11:25:29 -0700126 expectLastCall().times(2);
Charles Chan5d5e0622015-09-25 13:00:06 -0700127 replay(hostProvider);
128
Charles Chanf5c6f282022-01-04 16:39:44 -0800129 hostMonitor = new HostMonitor(null, hostManager, null, edgePortService, null);
Charles Chan5d5e0622015-09-25 13:00:06 -0700130
131 hostMonitor.registerHostProvider(hostProvider);
Jonathan Hart78613d22016-07-27 11:25:29 -0700132 hostMonitor.addMonitoringFor(hostIp);
Charles Chan5d5e0622015-09-25 13:00:06 -0700133
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700134 hostMonitor.run();
Charles Chan5d5e0622015-09-25 13:00:06 -0700135
136 verify(hostProvider);
137 }
138
139 @Test
140 public void testMonitorIpv4HostDoesNotExist() throws Exception {
Jonathan Hartdc09a3b2014-10-27 11:34:26 -0700141
Jonathan Hartb4758a92014-09-24 10:46:45 -0700142 HostManager hostManager = createMock(HostManager.class);
143
144 DeviceId devId = DeviceId.deviceId("fake");
145
146 Device device = createMock(Device.class);
147 expect(device.id()).andReturn(devId).anyTimes();
148 replay(device);
149
150 PortNumber portNum = PortNumber.portNumber(1L);
151
152 Port port = createMock(Port.class);
153 expect(port.number()).andReturn(portNum).anyTimes();
Charles Chanf5c6f282022-01-04 16:39:44 -0800154 expect(port.isEnabled()).andReturn(true).anyTimes();
Jonathan Hartb4758a92014-09-24 10:46:45 -0700155 replay(port);
156
157 TestDeviceService deviceService = new TestDeviceService();
158 deviceService.addDevice(device, Collections.singleton(port));
159
160 ConnectPoint cp = new ConnectPoint(devId, portNum);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700161
Charles Chan5d5e0622015-09-25 13:00:06 -0700162 expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR))
Jonathan Hart4cb39882015-08-12 23:50:55 -0400163 .andReturn(Collections.emptySet()).anyTimes();
Jonathan Hartb4758a92014-09-24 10:46:45 -0700164 replay(hostManager);
165
Jonathan Hart4cb39882015-08-12 23:50:55 -0400166 InterfaceService interfaceService = createMock(InterfaceService.class);
Charles Chan9238b382017-03-14 13:14:58 -0700167 expect(interfaceService.getMatchingInterfaces(TARGET_IPV4_ADDR))
168 .andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME,
169 cp, Collections.singletonList(IA1), sourceMac, VlanId.NONE)))
Jonathan Hart4cb39882015-08-12 23:50:55 -0400170 .anyTimes();
171 replay(interfaceService);
172
Jonathan Hartb4758a92014-09-24 10:46:45 -0700173 TestPacketService packetService = new TestPacketService();
174
175
176 // Run the test
Charles Chanf5c6f282022-01-04 16:39:44 -0800177 hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700178
Charles Chan5d5e0622015-09-25 13:00:06 -0700179 hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR);
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700180 hostMonitor.run();
Jonathan Hartb4758a92014-09-24 10:46:45 -0700181
182
183 // Check that a packet was sent to our PacketService and that it has
184 // the properties we expect
Jonathan Hart78613d22016-07-27 11:25:29 -0700185 assertEquals(2, packetService.packets.size());
Jonathan Hartb4758a92014-09-24 10:46:45 -0700186 OutboundPacket packet = packetService.packets.get(0);
187
188 // Check the output port is correct
Ray Milkey42507352015-03-20 15:16:10 -0700189 assertEquals(1, packet.treatment().immediate().size());
190 Instruction instruction = packet.treatment().immediate().get(0);
Jonathan Hartb4758a92014-09-24 10:46:45 -0700191 assertTrue(instruction instanceof OutputInstruction);
192 OutputInstruction oi = (OutputInstruction) instruction;
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800193 assertEquals(portNum, oi.port());
Jonathan Hartb4758a92014-09-24 10:46:45 -0700194
195 // Check the output packet is correct (well the important bits anyway)
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800196 final byte[] pktData = new byte[packet.data().remaining()];
197 packet.data().get(pktData);
Jonathan Hart2a655752015-04-07 16:46:33 -0700198 Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800199 assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
200 ARP arp = (ARP) eth.getPayload();
Charles Chan5d5e0622015-09-25 13:00:06 -0700201 assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800202 arp.getSenderProtocolAddress());
203 assertArrayEquals(sourceMac.toBytes(),
204 arp.getSenderHardwareAddress());
Charles Chan5d5e0622015-09-25 13:00:06 -0700205 assertArrayEquals(TARGET_IPV4_ADDR.toOctets(),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800206 arp.getTargetProtocolAddress());
207 }
208
209 @Test
Charles Chan5d5e0622015-09-25 13:00:06 -0700210 public void testMonitorIpv6HostDoesNotExist() throws Exception {
211
212 HostManager hostManager = createMock(HostManager.class);
213
214 DeviceId devId = DeviceId.deviceId("fake");
215
216 Device device = createMock(Device.class);
217 expect(device.id()).andReturn(devId).anyTimes();
218 replay(device);
219
220 PortNumber portNum = PortNumber.portNumber(2L);
221
222 Port port = createMock(Port.class);
223 expect(port.number()).andReturn(portNum).anyTimes();
Charles Chanf5c6f282022-01-04 16:39:44 -0800224 expect(port.isEnabled()).andReturn(true).anyTimes();
Charles Chan5d5e0622015-09-25 13:00:06 -0700225 replay(port);
226
227 TestDeviceService deviceService = new TestDeviceService();
228 deviceService.addDevice(device, Collections.singleton(port));
229
230 ConnectPoint cp = new ConnectPoint(devId, portNum);
231
232 expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR))
233 .andReturn(Collections.emptySet()).anyTimes();
234 replay(hostManager);
235
236 InterfaceService interfaceService = createMock(InterfaceService.class);
Charles Chan9238b382017-03-14 13:14:58 -0700237 expect(interfaceService.getMatchingInterfaces(TARGET_IPV6_ADDR))
238 .andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp,
239 Collections.singletonList(IA2), sourceMac2, VlanId.NONE)))
Charles Chan5d5e0622015-09-25 13:00:06 -0700240 .anyTimes();
241 replay(interfaceService);
242
243 TestPacketService packetService = new TestPacketService();
244
245
246 // Run the test
Charles Chanf5c6f282022-01-04 16:39:44 -0800247 hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
Charles Chan5d5e0622015-09-25 13:00:06 -0700248
249 hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700250 hostMonitor.run();
Charles Chan5d5e0622015-09-25 13:00:06 -0700251
252
253 // Check that a packet was sent to our PacketService and that it has
254 // the properties we expect
Jonathan Hart78613d22016-07-27 11:25:29 -0700255 assertEquals(2, packetService.packets.size());
Charles Chan5d5e0622015-09-25 13:00:06 -0700256 OutboundPacket packet = packetService.packets.get(0);
257
258 // Check the output port is correct
259 assertEquals(1, packet.treatment().immediate().size());
260 Instruction instruction = packet.treatment().immediate().get(0);
261 assertTrue(instruction instanceof OutputInstruction);
262 OutputInstruction oi = (OutputInstruction) instruction;
263 assertEquals(portNum, oi.port());
264
265 // Check the output packet is correct (well the important bits anyway)
266 final byte[] pktData = new byte[packet.data().remaining()];
267 packet.data().get(pktData);
268 Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
269 assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
270 IPv6 ipv6 = (IPv6) eth.getPayload();
271 assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress());
272
273 NeighborSolicitation ns =
274 (NeighborSolicitation) ipv6.getPayload().getPayload();
275 assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data());
276
277 assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress());
278 }
279
280 @Test
281 public void testMonitorIpv4HostDoesNotExistWithVlan() throws Exception {
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800282
283 HostManager hostManager = createMock(HostManager.class);
284
285 DeviceId devId = DeviceId.deviceId("fake");
286 short vlan = 5;
287
288 Device device = createMock(Device.class);
289 expect(device.id()).andReturn(devId).anyTimes();
290 replay(device);
291
292 PortNumber portNum = PortNumber.portNumber(1L);
293
294 Port port = createMock(Port.class);
295 expect(port.number()).andReturn(portNum).anyTimes();
Charles Chanf5c6f282022-01-04 16:39:44 -0800296 expect(port.isEnabled()).andReturn(true).anyTimes();
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800297 replay(port);
298
299 TestDeviceService deviceService = new TestDeviceService();
300 deviceService.addDevice(device, Collections.singleton(port));
301
302 ConnectPoint cp = new ConnectPoint(devId, portNum);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800303
Charles Chan5d5e0622015-09-25 13:00:06 -0700304 expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR))
Jonathan Hart4cb39882015-08-12 23:50:55 -0400305 .andReturn(Collections.emptySet()).anyTimes();
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800306 replay(hostManager);
307
Jonathan Hart4cb39882015-08-12 23:50:55 -0400308 InterfaceService interfaceService = createMock(InterfaceService.class);
Charles Chan9238b382017-03-14 13:14:58 -0700309 expect(interfaceService.getMatchingInterfaces(TARGET_IPV4_ADDR))
310 .andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp,
311 Collections.singletonList(IA1), sourceMac, VlanId.vlanId(vlan))))
Jonathan Hart4cb39882015-08-12 23:50:55 -0400312 .anyTimes();
313 replay(interfaceService);
314
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800315 TestPacketService packetService = new TestPacketService();
316
317
318 // Run the test
Charles Chanf5c6f282022-01-04 16:39:44 -0800319 hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800320
Charles Chan5d5e0622015-09-25 13:00:06 -0700321 hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR);
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700322 hostMonitor.run();
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800323
324
325 // Check that a packet was sent to our PacketService and that it has
326 // the properties we expect
Jonathan Hart78613d22016-07-27 11:25:29 -0700327 assertEquals(2, packetService.packets.size());
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800328 OutboundPacket packet = packetService.packets.get(0);
329
330 // Check the output port is correct
Ray Milkey42507352015-03-20 15:16:10 -0700331 assertEquals(1, packet.treatment().immediate().size());
332 Instruction instruction = packet.treatment().immediate().get(0);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800333 assertTrue(instruction instanceof OutputInstruction);
334 OutputInstruction oi = (OutputInstruction) instruction;
335 assertEquals(portNum, oi.port());
336
337 // Check the output packet is correct (well the important bits anyway)
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800338 final byte[] pktData = new byte[packet.data().remaining()];
339 packet.data().get(pktData);
Jonathan Hart2a655752015-04-07 16:46:33 -0700340 Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800341 assertEquals(vlan, eth.getVlanID());
Jonathan Hartb4758a92014-09-24 10:46:45 -0700342 ARP arp = (ARP) eth.getPayload();
Charles Chan5d5e0622015-09-25 13:00:06 -0700343 assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(),
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800344 arp.getSenderProtocolAddress());
345 assertArrayEquals(sourceMac.toBytes(),
346 arp.getSenderHardwareAddress());
Charles Chan5d5e0622015-09-25 13:00:06 -0700347 assertArrayEquals(TARGET_IPV4_ADDR.toOctets(),
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800348 arp.getTargetProtocolAddress());
Jonathan Hartb4758a92014-09-24 10:46:45 -0700349 }
350
Charles Chan5d5e0622015-09-25 13:00:06 -0700351 @Test
352 public void testMonitorIpv6HostDoesNotExistWithVlan() throws Exception {
353
354 HostManager hostManager = createMock(HostManager.class);
355
356 DeviceId devId = DeviceId.deviceId("fake");
357 short vlan = 5;
358
359 Device device = createMock(Device.class);
360 expect(device.id()).andReturn(devId).anyTimes();
361 replay(device);
362
363 PortNumber portNum = PortNumber.portNumber(1L);
364
365 Port port = createMock(Port.class);
366 expect(port.number()).andReturn(portNum).anyTimes();
Charles Chanf5c6f282022-01-04 16:39:44 -0800367 expect(port.isEnabled()).andReturn(true).anyTimes();
Charles Chan5d5e0622015-09-25 13:00:06 -0700368 replay(port);
369
370 TestDeviceService deviceService = new TestDeviceService();
371 deviceService.addDevice(device, Collections.singleton(port));
372
373 ConnectPoint cp = new ConnectPoint(devId, portNum);
374
375 expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR))
376 .andReturn(Collections.emptySet()).anyTimes();
377 replay(hostManager);
378
379 InterfaceService interfaceService = createMock(InterfaceService.class);
Charles Chan9238b382017-03-14 13:14:58 -0700380 expect(interfaceService.getMatchingInterfaces(TARGET_IPV6_ADDR))
381 .andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp,
382 Collections.singletonList(IA2), sourceMac2, VlanId.vlanId(vlan))))
Charles Chan5d5e0622015-09-25 13:00:06 -0700383 .anyTimes();
384 replay(interfaceService);
385
386 TestPacketService packetService = new TestPacketService();
387
388
389 // Run the test
Charles Chanf5c6f282022-01-04 16:39:44 -0800390 hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
Charles Chan5d5e0622015-09-25 13:00:06 -0700391
392 hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700393 hostMonitor.run();
Charles Chan5d5e0622015-09-25 13:00:06 -0700394
395
396 // Check that a packet was sent to our PacketService and that it has
397 // the properties we expect
Jonathan Hart78613d22016-07-27 11:25:29 -0700398 assertEquals(2, packetService.packets.size());
Charles Chan5d5e0622015-09-25 13:00:06 -0700399 OutboundPacket packet = packetService.packets.get(0);
400
401 // Check the output port is correct
402 assertEquals(1, packet.treatment().immediate().size());
403 Instruction instruction = packet.treatment().immediate().get(0);
404 assertTrue(instruction instanceof OutputInstruction);
405 OutputInstruction oi = (OutputInstruction) instruction;
406 assertEquals(portNum, oi.port());
407
408 // Check the output packet is correct (well the important bits anyway)
409 final byte[] pktData = new byte[packet.data().remaining()];
410 packet.data().get(pktData);
411 Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
412 assertEquals(vlan, eth.getVlanID());
413 IPv6 ipv6 = (IPv6) eth.getPayload();
414 assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress());
415
416 NeighborSolicitation ns =
417 (NeighborSolicitation) ipv6.getPayload().getPayload();
418 assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data());
419
420 assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress());
421 }
422
Thomas Vachuska27bee092015-06-23 19:03:10 -0700423 class TestPacketService extends PacketServiceAdapter {
Jonathan Hartb4758a92014-09-24 10:46:45 -0700424
425 List<OutboundPacket> packets = new ArrayList<>();
426
427 @Override
Jonathan Hartb4758a92014-09-24 10:46:45 -0700428 public void emit(OutboundPacket packet) {
429 packets.add(packet);
430 }
431 }
432
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800433 class TestDeviceService extends DeviceServiceAdapter {
Jonathan Hartb4758a92014-09-24 10:46:45 -0700434
435 List<Device> devices = Lists.newArrayList();
436 Multimap<DeviceId, Port> devicePorts = HashMultimap.create();
437
438 void addDevice(Device device, Set<Port> ports) {
439 devices.add(device);
440 for (Port p : ports) {
441 devicePorts.put(device.id(), p);
442 }
443 }
444
445 @Override
Jonathan Hartb4758a92014-09-24 10:46:45 -0700446 public Iterable<Device> getDevices() {
447 return devices;
448 }
449
450 @Override
Jonathan Hartb4758a92014-09-24 10:46:45 -0700451 public List<Port> getPorts(DeviceId deviceId) {
452 List<Port> ports = Lists.newArrayList();
453 for (Port p : devicePorts.get(deviceId)) {
454 ports.add(p);
455 }
456 return ports;
457 }
Charles Chanf5c6f282022-01-04 16:39:44 -0800458
459 @Override
460 public Port getPort(ConnectPoint cp) {
461 return devicePorts.get(cp.deviceId()).stream()
462 .filter(p -> p.number().equals(cp.port()))
463 .findAny().orElse(null);
464 }
Jonathan Hartb4758a92014-09-24 10:46:45 -0700465 }
466}