summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/net/src/test/java/org/onosproject/net/host
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/core/net/src/test/java/org/onosproject/net/host')
-rw-r--r--framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java71
-rw-r--r--framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostManagerTest.java272
-rw-r--r--framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java505
3 files changed, 0 insertions, 848 deletions
diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
deleted file mode 100644
index 940ed919..00000000
--- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.net.host.impl;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.onlab.packet.IpAddress;
-import org.onlab.packet.MacAddress;
-import org.onlab.packet.VlanId;
-import org.onosproject.net.config.ConfigApplyDelegate;
-import org.onosproject.net.config.basics.BasicHostConfig;
-import org.onosproject.net.AnnotationKeys;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.HostId;
-import org.onosproject.net.HostLocation;
-import org.onosproject.net.PortNumber;
-import org.onosproject.net.host.DefaultHostDescription;
-import org.onosproject.net.host.HostDescription;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
-
-public class BasicHostOperatorTest {
- private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
- private static final VlanId VLAN = VlanId.vlanId((short) 10);
- private static final IpAddress IP = IpAddress.valueOf("10.0.0.1");
-
- private static final HostId ID = HostId.hostId(MAC);
- private static final HostLocation LOC = new HostLocation(
- DeviceId.deviceId("of:foo"),
- PortNumber.portNumber(100),
- 123L
- );
- private static final HostDescription HOST = new DefaultHostDescription(MAC, VLAN, LOC, IP);
-
- private final ConfigApplyDelegate delegate = config -> { };
- private final ObjectMapper mapper = new ObjectMapper();
-
- private static final BasicHostConfig BHC = new BasicHostConfig();
- private static final String NAME = "testhost";
- private static final double LAT = 40.96;
-
- @Before
- public void setUp() {
- BHC.init(ID, "test", JsonNodeFactory.instance.objectNode(), mapper, delegate);
- BHC.name(NAME).latitude(40.96);
- }
-
- @Test
- public void testDescOps() {
- HostDescription desc = BasicHostOperator.combine(BHC, HOST);
- assertEquals(NAME, desc.annotations().value(AnnotationKeys.NAME));
- assertEquals(null, desc.annotations().value(AnnotationKeys.LONGITUDE));
- assertEquals(String.valueOf(LAT), desc.annotations().value(AnnotationKeys.LATITUDE));
- }
-}
diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostManagerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostManagerTest.java
deleted file mode 100644
index c5a6cabd..00000000
--- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostManagerTest.java
+++ /dev/null
@@ -1,272 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.net.host.impl;
-
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.onlab.junit.TestTools;
-import org.onlab.packet.IpAddress;
-import org.onlab.packet.MacAddress;
-import org.onlab.packet.VlanId;
-import org.onosproject.common.event.impl.TestEventDispatcher;
-import org.onosproject.event.Event;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.Host;
-import org.onosproject.net.HostId;
-import org.onosproject.net.HostLocation;
-import org.onosproject.net.PortNumber;
-import org.onosproject.net.config.NetworkConfigServiceAdapter;
-import org.onosproject.net.host.DefaultHostDescription;
-import org.onosproject.net.host.HostDescription;
-import org.onosproject.net.host.HostEvent;
-import org.onosproject.net.host.HostListener;
-import org.onosproject.net.host.HostProvider;
-import org.onosproject.net.host.HostProviderRegistry;
-import org.onosproject.net.host.HostProviderService;
-import org.onosproject.net.provider.AbstractProvider;
-import org.onosproject.net.provider.ProviderId;
-import org.onosproject.store.trivial.SimpleHostStore;
-
-import java.util.List;
-import java.util.Set;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.onosproject.net.NetTestTools.injectEventDispatcher;
-import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
-import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED;
-import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
-import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
-
-/**
- * Test codifying the host service & host provider service contracts.
- */
-public class HostManagerTest {
-
- private static final ProviderId PID = new ProviderId("of", "foo");
-
- private static final VlanId VLAN1 = VlanId.vlanId((short) 1);
- private static final VlanId VLAN2 = VlanId.vlanId((short) 2);
- private static final MacAddress MAC1 = MacAddress.valueOf("00:00:11:00:00:01");
- private static final MacAddress MAC2 = MacAddress.valueOf("00:00:22:00:00:02");
- private static final MacAddress MAC3 = MacAddress.valueOf("00:00:33:00:00:03");
- private static final MacAddress MAC4 = MacAddress.valueOf("00:00:44:00:00:04");
- private static final HostId HID1 = HostId.hostId(MAC1, VLAN1);
- private static final HostId HID2 = HostId.hostId(MAC2, VLAN1);
- private static final HostId HID3 = HostId.hostId(MAC3, VLAN1);
- private static final HostId HID4 = HostId.hostId(MAC4, VLAN1);
-
- private static final IpAddress IP1 = IpAddress.valueOf("10.0.0.1");
- private static final IpAddress IP2 = IpAddress.valueOf("10.0.0.2");
- private static final IpAddress IP3 = IpAddress.valueOf("2001::1");
- private static final IpAddress IP4 = IpAddress.valueOf("2001::2");
-
- private static final DeviceId DID1 = DeviceId.deviceId("of:001");
- private static final DeviceId DID2 = DeviceId.deviceId("of:002");
- private static final PortNumber P1 = PortNumber.portNumber(100);
- private static final PortNumber P2 = PortNumber.portNumber(200);
- private static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L);
- private static final HostLocation LOC2 = new HostLocation(DID1, P2, 123L);
-
- private HostManager mgr;
-
- protected TestListener listener = new TestListener();
- protected HostProviderRegistry registry;
- protected TestHostProvider provider;
- protected HostProviderService providerService;
-
- @Before
- public void setUp() {
- mgr = new HostManager();
- mgr.store = new SimpleHostStore();
- injectEventDispatcher(mgr, new TestEventDispatcher());
- registry = mgr;
- mgr.networkConfigService = new TestNetworkConfigService();
- mgr.activate();
-
- mgr.addListener(listener);
-
- provider = new TestHostProvider();
- providerService = registry.register(provider);
- assertTrue("provider should be registered",
- registry.getProviders().contains(provider.id()));
- }
-
- @After
- public void tearDown() {
- registry.unregister(provider);
- assertFalse("provider should not be registered",
- registry.getProviders().contains(provider.id()));
-
- mgr.removeListener(listener);
- mgr.deactivate();
- injectEventDispatcher(mgr, null);
- }
-
- private void detect(HostId hid, MacAddress mac, VlanId vlan,
- HostLocation loc, IpAddress ip) {
- HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ip);
- providerService.hostDetected(hid, descr);
- assertNotNull("host should be found", mgr.getHost(hid));
- }
-
- private void validateEvents(Enum... types) {
- TestTools.assertAfter(100, () -> {
- int i = 0;
- assertEquals("wrong events received", types.length, listener.events.size());
- for (Event event : listener.events) {
- assertEquals("incorrect event type", types[i], event.type());
- i++;
- }
- listener.events.clear();
- });
- }
-
- @Test
- public void hostDetected() {
- assertNull("host shouldn't be found", mgr.getHost(HID1));
-
- // host addition
- detect(HID1, MAC1, VLAN1, LOC1, IP1);
- assertEquals("exactly one should be found", 1, mgr.getHostCount());
- detect(HID2, MAC2, VLAN2, LOC2, IP1);
- assertEquals("two hosts should be found", 2, mgr.getHostCount());
- validateEvents(HOST_ADDED, HOST_ADDED);
-
- // host motion
- detect(HID1, MAC1, VLAN1, LOC2, IP1);
- validateEvents(HOST_MOVED);
- assertEquals("only two hosts should be found", 2, mgr.getHostCount());
-
- // host update
- detect(HID1, MAC1, VLAN1, LOC2, IP2);
- validateEvents(HOST_UPDATED);
- assertEquals("only two hosts should be found", 2, mgr.getHostCount());
- }
-
- @Test
- public void hostDetectedIPv6() {
- assertNull("host shouldn't be found", mgr.getHost(HID3));
-
- // host addition
- detect(HID3, MAC3, VLAN1, LOC1, IP3);
- assertEquals("exactly one should be found", 1, mgr.getHostCount());
- detect(HID4, MAC4, VLAN2, LOC2, IP3);
- assertEquals("two hosts should be found", 2, mgr.getHostCount());
- validateEvents(HOST_ADDED, HOST_ADDED);
-
- // host motion
- detect(HID3, MAC3, VLAN1, LOC2, IP3);
- validateEvents(HOST_MOVED);
- assertEquals("only two hosts should be found", 2, mgr.getHostCount());
-
- // host update
- detect(HID3, MAC3, VLAN1, LOC2, IP4);
- validateEvents(HOST_UPDATED);
- assertEquals("only two hosts should be found", 2, mgr.getHostCount());
- }
-
- @Test
- public void hostVanished() {
- detect(HID1, MAC1, VLAN1, LOC1, IP1);
- providerService.hostVanished(HID1);
- validateEvents(HOST_ADDED, HOST_REMOVED);
-
- assertNull("host should have been removed", mgr.getHost(HID1));
- }
-
- @Test
- public void hostVanishedIPv6() {
- detect(HID3, MAC3, VLAN1, LOC1, IP3);
- providerService.hostVanished(HID3);
- validateEvents(HOST_ADDED, HOST_REMOVED);
-
- assertNull("host should have been removed", mgr.getHost(HID3));
- }
-
- private void validateHosts(
- String msg, Iterable<Host> hosts, HostId... ids) {
- Set<HostId> hids = Sets.newHashSet(ids);
- for (Host h : hosts) {
- assertTrue(msg, hids.remove(h.id()));
- }
- assertTrue("expected hosts not fetched from store", hids.isEmpty());
- }
-
- @Test
- public void getHosts() {
- detect(HID1, MAC1, VLAN1, LOC1, IP1);
- detect(HID2, MAC2, VLAN1, LOC2, IP2);
-
- validateHosts("host not properly stored", mgr.getHosts(), HID1, HID2);
- validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID1, HID2);
- validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC1), HID1);
- validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP1), HID1);
- validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID1);
- assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty());
- }
-
- @Test
- public void getHostsIPv6() {
- detect(HID3, MAC3, VLAN1, LOC1, IP3);
- detect(HID4, MAC4, VLAN1, LOC2, IP4);
-
- validateHosts("host not properly stored", mgr.getHosts(), HID3, HID4);
- validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID3, HID4);
- validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC3), HID3);
- validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP3), HID3);
- validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID3);
- assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty());
- }
-
- private static class TestHostProvider extends AbstractProvider
- implements HostProvider {
-
- protected TestHostProvider() {
- super(PID);
- }
-
- @Override
- public ProviderId id() {
- return PID;
- }
-
- @Override
- public void triggerProbe(Host host) {
- }
-
- }
-
- private static class TestListener implements HostListener {
-
- protected List<HostEvent> events = Lists.newArrayList();
-
- @Override
- public void event(HostEvent event) {
- events.add(event);
- }
-
- }
-
- private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
- }
-}
diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java
deleted file mode 100644
index 3cd2ca2b..00000000
--- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java
+++ /dev/null
@@ -1,505 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.net.host.impl;
-
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Multimap;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.onlab.packet.ARP;
-import org.onlab.packet.Ethernet;
-import org.onlab.packet.IPv6;
-import org.onlab.packet.IpAddress;
-import org.onlab.packet.IpPrefix;
-import org.onlab.packet.MacAddress;
-import org.onlab.packet.VlanId;
-import org.onlab.packet.ndp.NeighborSolicitation;
-import org.onosproject.incubator.net.intf.Interface;
-import org.onosproject.incubator.net.intf.InterfaceService;
-import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.Device;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.Host;
-import org.onosproject.net.MastershipRole;
-import org.onosproject.net.Port;
-import org.onosproject.net.PortNumber;
-import org.onosproject.net.device.DeviceListener;
-import org.onosproject.net.device.DeviceServiceAdapter;
-import org.onosproject.net.edge.EdgePortService;
-import org.onosproject.net.flow.instructions.Instruction;
-import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
-import org.onosproject.net.host.HostProvider;
-import org.onosproject.net.host.InterfaceIpAddress;
-import org.onosproject.net.packet.OutboundPacket;
-import org.onosproject.net.packet.PacketServiceAdapter;
-import org.onosproject.net.provider.ProviderId;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
-
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-public class HostMonitorTest {
-
- private static final IpAddress TARGET_IPV4_ADDR =
- IpAddress.valueOf("10.0.0.1");
- private static final IpAddress SOURCE_IPV4_ADDR =
- IpAddress.valueOf("10.0.0.99");
- private static final InterfaceIpAddress IA1 =
- new InterfaceIpAddress(SOURCE_IPV4_ADDR, IpPrefix.valueOf("10.0.0.0/24"));
- private MacAddress sourceMac = MacAddress.valueOf(1L);
-
- private static final IpAddress TARGET_IPV6_ADDR =
- IpAddress.valueOf("1000::1");
- private static final IpAddress SOURCE_IPV6_ADDR =
- IpAddress.valueOf("1000::f");
- private static final InterfaceIpAddress IA2 =
- new InterfaceIpAddress(SOURCE_IPV6_ADDR, IpPrefix.valueOf("1000::/64"));
- private MacAddress sourceMac2 = MacAddress.valueOf(2L);
-
- private EdgePortService edgePortService;
-
- private HostMonitor hostMonitor;
-
- @Before
- public void setUp() {
- edgePortService = createMock(EdgePortService.class);
- expect(edgePortService.isEdgePoint(anyObject(ConnectPoint.class)))
- .andReturn(true).anyTimes();
- replay(edgePortService);
- }
-
- @After
- public void shutdown() {
- hostMonitor.shutdown();
- }
-
- @Test
- public void testMonitorIpv4HostExists() throws Exception {
- ProviderId id = new ProviderId("fake://", "id");
-
- Host host = createMock(Host.class);
- expect(host.providerId()).andReturn(id);
- replay(host);
-
- HostManager hostManager = createMock(HostManager.class);
- expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR))
- .andReturn(Collections.singleton(host));
- replay(hostManager);
-
- HostProvider hostProvider = createMock(HostProvider.class);
- expect(hostProvider.id()).andReturn(id).anyTimes();
- hostProvider.triggerProbe(host);
- expectLastCall().once();
- replay(hostProvider);
-
- hostMonitor = new HostMonitor(null, hostManager, null, edgePortService);
-
- hostMonitor.registerHostProvider(hostProvider);
- hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR);
-
- hostMonitor.run(null);
-
- verify(hostProvider);
- }
-
- @Test
- public void testMonitorIpv6HostExists() throws Exception {
- ProviderId id = new ProviderId("fake://", "id");
-
- Host host = createMock(Host.class);
- expect(host.providerId()).andReturn(id);
- replay(host);
-
- HostManager hostManager = createMock(HostManager.class);
- expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR))
- .andReturn(Collections.singleton(host));
- replay(hostManager);
-
- HostProvider hostProvider = createMock(HostProvider.class);
- expect(hostProvider.id()).andReturn(id).anyTimes();
- hostProvider.triggerProbe(host);
- expectLastCall().once();
- replay(hostProvider);
-
- hostMonitor = new HostMonitor(null, hostManager, null, edgePortService);
-
- hostMonitor.registerHostProvider(hostProvider);
- hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
-
- hostMonitor.run(null);
-
- verify(hostProvider);
- }
-
- @Test
- public void testMonitorIpv4HostDoesNotExist() throws Exception {
-
- HostManager hostManager = createMock(HostManager.class);
-
- DeviceId devId = DeviceId.deviceId("fake");
-
- Device device = createMock(Device.class);
- expect(device.id()).andReturn(devId).anyTimes();
- replay(device);
-
- PortNumber portNum = PortNumber.portNumber(1L);
-
- Port port = createMock(Port.class);
- expect(port.number()).andReturn(portNum).anyTimes();
- replay(port);
-
- TestDeviceService deviceService = new TestDeviceService();
- deviceService.addDevice(device, Collections.singleton(port));
-
- ConnectPoint cp = new ConnectPoint(devId, portNum);
-
- expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR))
- .andReturn(Collections.emptySet()).anyTimes();
- replay(hostManager);
-
- InterfaceService interfaceService = createMock(InterfaceService.class);
- expect(interfaceService.getMatchingInterface(TARGET_IPV4_ADDR))
- .andReturn(new Interface(cp, Collections.singleton(IA1), sourceMac, VlanId.NONE))
- .anyTimes();
- replay(interfaceService);
-
- TestPacketService packetService = new TestPacketService();
-
-
- // Run the test
- hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService);
-
- hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR);
- hostMonitor.run(null);
-
-
- // Check that a packet was sent to our PacketService and that it has
- // the properties we expect
- assertEquals(1, packetService.packets.size());
- OutboundPacket packet = packetService.packets.get(0);
-
- // Check the output port is correct
- assertEquals(1, packet.treatment().immediate().size());
- Instruction instruction = packet.treatment().immediate().get(0);
- assertTrue(instruction instanceof OutputInstruction);
- OutputInstruction oi = (OutputInstruction) instruction;
- assertEquals(portNum, oi.port());
-
- // Check the output packet is correct (well the important bits anyway)
- final byte[] pktData = new byte[packet.data().remaining()];
- packet.data().get(pktData);
- Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
- assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
- ARP arp = (ARP) eth.getPayload();
- assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(),
- arp.getSenderProtocolAddress());
- assertArrayEquals(sourceMac.toBytes(),
- arp.getSenderHardwareAddress());
- assertArrayEquals(TARGET_IPV4_ADDR.toOctets(),
- arp.getTargetProtocolAddress());
- }
-
- @Test
- public void testMonitorIpv6HostDoesNotExist() throws Exception {
-
- HostManager hostManager = createMock(HostManager.class);
-
- DeviceId devId = DeviceId.deviceId("fake");
-
- Device device = createMock(Device.class);
- expect(device.id()).andReturn(devId).anyTimes();
- replay(device);
-
- PortNumber portNum = PortNumber.portNumber(2L);
-
- Port port = createMock(Port.class);
- expect(port.number()).andReturn(portNum).anyTimes();
- replay(port);
-
- TestDeviceService deviceService = new TestDeviceService();
- deviceService.addDevice(device, Collections.singleton(port));
-
- ConnectPoint cp = new ConnectPoint(devId, portNum);
-
- expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR))
- .andReturn(Collections.emptySet()).anyTimes();
- replay(hostManager);
-
- InterfaceService interfaceService = createMock(InterfaceService.class);
- expect(interfaceService.getMatchingInterface(TARGET_IPV6_ADDR))
- .andReturn(new Interface(cp, Collections.singleton(IA2), sourceMac2, VlanId.NONE))
- .anyTimes();
- replay(interfaceService);
-
- TestPacketService packetService = new TestPacketService();
-
-
- // Run the test
- hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService);
-
- hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
- hostMonitor.run(null);
-
-
- // Check that a packet was sent to our PacketService and that it has
- // the properties we expect
- assertEquals(1, packetService.packets.size());
- OutboundPacket packet = packetService.packets.get(0);
-
- // Check the output port is correct
- assertEquals(1, packet.treatment().immediate().size());
- Instruction instruction = packet.treatment().immediate().get(0);
- assertTrue(instruction instanceof OutputInstruction);
- OutputInstruction oi = (OutputInstruction) instruction;
- assertEquals(portNum, oi.port());
-
- // Check the output packet is correct (well the important bits anyway)
- final byte[] pktData = new byte[packet.data().remaining()];
- packet.data().get(pktData);
- Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
- assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
- IPv6 ipv6 = (IPv6) eth.getPayload();
- assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress());
-
- NeighborSolicitation ns =
- (NeighborSolicitation) ipv6.getPayload().getPayload();
- assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data());
-
- assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress());
- }
-
- @Test
- public void testMonitorIpv4HostDoesNotExistWithVlan() throws Exception {
-
- HostManager hostManager = createMock(HostManager.class);
-
- DeviceId devId = DeviceId.deviceId("fake");
- short vlan = 5;
-
- Device device = createMock(Device.class);
- expect(device.id()).andReturn(devId).anyTimes();
- replay(device);
-
- PortNumber portNum = PortNumber.portNumber(1L);
-
- Port port = createMock(Port.class);
- expect(port.number()).andReturn(portNum).anyTimes();
- replay(port);
-
- TestDeviceService deviceService = new TestDeviceService();
- deviceService.addDevice(device, Collections.singleton(port));
-
- ConnectPoint cp = new ConnectPoint(devId, portNum);
-
- expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR))
- .andReturn(Collections.emptySet()).anyTimes();
- replay(hostManager);
-
- InterfaceService interfaceService = createMock(InterfaceService.class);
- expect(interfaceService.getMatchingInterface(TARGET_IPV4_ADDR))
- .andReturn(new Interface(cp, Collections.singleton(IA1), sourceMac, VlanId.vlanId(vlan)))
- .anyTimes();
- replay(interfaceService);
-
- TestPacketService packetService = new TestPacketService();
-
-
- // Run the test
- hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService);
-
- hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR);
- hostMonitor.run(null);
-
-
- // Check that a packet was sent to our PacketService and that it has
- // the properties we expect
- assertEquals(1, packetService.packets.size());
- OutboundPacket packet = packetService.packets.get(0);
-
- // Check the output port is correct
- assertEquals(1, packet.treatment().immediate().size());
- Instruction instruction = packet.treatment().immediate().get(0);
- assertTrue(instruction instanceof OutputInstruction);
- OutputInstruction oi = (OutputInstruction) instruction;
- assertEquals(portNum, oi.port());
-
- // Check the output packet is correct (well the important bits anyway)
- final byte[] pktData = new byte[packet.data().remaining()];
- packet.data().get(pktData);
- Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
- assertEquals(vlan, eth.getVlanID());
- ARP arp = (ARP) eth.getPayload();
- assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(),
- arp.getSenderProtocolAddress());
- assertArrayEquals(sourceMac.toBytes(),
- arp.getSenderHardwareAddress());
- assertArrayEquals(TARGET_IPV4_ADDR.toOctets(),
- arp.getTargetProtocolAddress());
- }
-
- @Test
- public void testMonitorIpv6HostDoesNotExistWithVlan() throws Exception {
-
- HostManager hostManager = createMock(HostManager.class);
-
- DeviceId devId = DeviceId.deviceId("fake");
- short vlan = 5;
-
- Device device = createMock(Device.class);
- expect(device.id()).andReturn(devId).anyTimes();
- replay(device);
-
- PortNumber portNum = PortNumber.portNumber(1L);
-
- Port port = createMock(Port.class);
- expect(port.number()).andReturn(portNum).anyTimes();
- replay(port);
-
- TestDeviceService deviceService = new TestDeviceService();
- deviceService.addDevice(device, Collections.singleton(port));
-
- ConnectPoint cp = new ConnectPoint(devId, portNum);
-
- expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR))
- .andReturn(Collections.emptySet()).anyTimes();
- replay(hostManager);
-
- InterfaceService interfaceService = createMock(InterfaceService.class);
- expect(interfaceService.getMatchingInterface(TARGET_IPV6_ADDR))
- .andReturn(new Interface(cp, Collections.singleton(IA2), sourceMac2, VlanId.vlanId(vlan)))
- .anyTimes();
- replay(interfaceService);
-
- TestPacketService packetService = new TestPacketService();
-
-
- // Run the test
- hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService);
-
- hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
- hostMonitor.run(null);
-
-
- // Check that a packet was sent to our PacketService and that it has
- // the properties we expect
- assertEquals(1, packetService.packets.size());
- OutboundPacket packet = packetService.packets.get(0);
-
- // Check the output port is correct
- assertEquals(1, packet.treatment().immediate().size());
- Instruction instruction = packet.treatment().immediate().get(0);
- assertTrue(instruction instanceof OutputInstruction);
- OutputInstruction oi = (OutputInstruction) instruction;
- assertEquals(portNum, oi.port());
-
- // Check the output packet is correct (well the important bits anyway)
- final byte[] pktData = new byte[packet.data().remaining()];
- packet.data().get(pktData);
- Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
- assertEquals(vlan, eth.getVlanID());
- IPv6 ipv6 = (IPv6) eth.getPayload();
- assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress());
-
- NeighborSolicitation ns =
- (NeighborSolicitation) ipv6.getPayload().getPayload();
- assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data());
-
- assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress());
- }
-
- class TestPacketService extends PacketServiceAdapter {
-
- List<OutboundPacket> packets = new ArrayList<>();
-
- @Override
- public void emit(OutboundPacket packet) {
- packets.add(packet);
- }
- }
-
- class TestDeviceService extends DeviceServiceAdapter {
-
- List<Device> devices = Lists.newArrayList();
- Multimap<DeviceId, Port> devicePorts = HashMultimap.create();
-
- void addDevice(Device device, Set<Port> ports) {
- devices.add(device);
- for (Port p : ports) {
- devicePorts.put(device.id(), p);
- }
- }
-
- @Override
- public int getDeviceCount() {
- return 0;
- }
-
- @Override
- public Iterable<Device> getDevices() {
- return devices;
- }
-
- @Override
- public Device getDevice(DeviceId deviceId) {
- return null;
- }
-
- @Override
- public MastershipRole getRole(DeviceId deviceId) {
- return null;
- }
-
- @Override
- public List<Port> getPorts(DeviceId deviceId) {
- List<Port> ports = Lists.newArrayList();
- for (Port p : devicePorts.get(deviceId)) {
- ports.add(p);
- }
- return ports;
- }
-
- @Override
- public Port getPort(DeviceId deviceId, PortNumber portNumber) {
- return null;
- }
-
- @Override
- public boolean isAvailable(DeviceId deviceId) {
- return false;
- }
-
- @Override
- public void addListener(DeviceListener listener) {
- }
-
- @Override
- public void removeListener(DeviceListener listener) {
- }
- }
-}