Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.host.impl; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 17 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 21 | import com.google.common.util.concurrent.MoreExecutors; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 25 | import org.onlab.junit.TestUtils; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 26 | import org.onlab.osgi.ComponentContextAdapter; |
| 27 | import org.onlab.packet.ARP; |
| 28 | import org.onlab.packet.ChassisId; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 29 | import org.onlab.packet.DHCP; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 30 | import org.onlab.packet.dhcp.CircuitId; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 31 | import org.onlab.packet.dhcp.DhcpOption; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 32 | import org.onlab.packet.Ethernet; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 33 | import org.onlab.packet.ICMP6; |
| 34 | import org.onlab.packet.IPv4; |
| 35 | import org.onlab.packet.IPv6; |
| 36 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 37 | import org.onlab.packet.IpAddress; |
| 38 | import org.onlab.packet.MacAddress; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 39 | import org.onlab.packet.UDP; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 40 | import org.onlab.packet.VlanId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 41 | import org.onlab.packet.dhcp.DhcpRelayAgentOption; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 42 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 43 | import org.onlab.packet.ndp.NeighborSolicitation; |
| 44 | import org.onlab.packet.ndp.RouterAdvertisement; |
| 45 | import org.onlab.packet.ndp.RouterSolicitation; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 46 | import org.onosproject.cfg.ComponentConfigAdapter; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 47 | import org.onosproject.core.ApplicationId; |
| 48 | import org.onosproject.core.CoreService; |
| 49 | import org.onosproject.core.DefaultApplicationId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 50 | import org.onosproject.incubator.net.intf.Interface; |
| 51 | import org.onosproject.incubator.net.intf.InterfaceListener; |
| 52 | import org.onosproject.incubator.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.ConnectPoint; |
| 54 | import org.onosproject.net.DefaultDevice; |
| 55 | import org.onosproject.net.DefaultHost; |
| 56 | import org.onosproject.net.DefaultPort; |
| 57 | import org.onosproject.net.Device; |
| 58 | import org.onosproject.net.DeviceId; |
| 59 | import org.onosproject.net.Host; |
| 60 | import org.onosproject.net.HostId; |
| 61 | import org.onosproject.net.HostLocation; |
| 62 | import org.onosproject.net.device.DeviceEvent; |
| 63 | import org.onosproject.net.device.DeviceListener; |
| 64 | import org.onosproject.net.device.DeviceServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 65 | import org.onosproject.net.host.HostDescription; |
| 66 | import org.onosproject.net.host.HostProvider; |
| 67 | import org.onosproject.net.host.HostProviderRegistry; |
| 68 | import org.onosproject.net.host.HostProviderService; |
| 69 | import org.onosproject.net.host.HostServiceAdapter; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 70 | import org.onosproject.net.host.InterfaceIpAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 71 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 72 | import org.onosproject.net.packet.InboundPacket; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 73 | import org.onosproject.net.packet.PacketContextAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 74 | import org.onosproject.net.packet.PacketProcessor; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 75 | import org.onosproject.net.packet.PacketServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 76 | import org.onosproject.net.provider.AbstractProviderService; |
| 77 | import org.onosproject.net.provider.ProviderId; |
| 78 | import org.onosproject.net.topology.Topology; |
| 79 | import org.onosproject.net.topology.TopologyServiceAdapter; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 80 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 81 | import java.nio.ByteBuffer; |
| 82 | import java.util.Collections; |
| 83 | import java.util.Dictionary; |
| 84 | import java.util.Hashtable; |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 85 | import java.util.List; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 86 | import java.util.Set; |
| 87 | |
Michele Santuari | 72f6bee | 2016-11-18 12:56:30 +0100 | [diff] [blame] | 88 | import static org.easymock.EasyMock.createMock; |
| 89 | import static org.easymock.EasyMock.expect; |
| 90 | import static org.easymock.EasyMock.replay; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 91 | import static org.hamcrest.Matchers.is; |
Michele Santuari | 72f6bee | 2016-11-18 12:56:30 +0100 | [diff] [blame] | 92 | import static org.junit.Assert.assertEquals; |
| 93 | import static org.junit.Assert.assertNotEquals; |
| 94 | import static org.junit.Assert.assertNotNull; |
| 95 | import static org.junit.Assert.assertThat; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 96 | import static org.onlab.packet.VlanId.vlanId; |
| 97 | import static org.onosproject.net.Device.Type.SWITCH; |
| 98 | import static org.onosproject.net.DeviceId.deviceId; |
| 99 | import static org.onosproject.net.HostId.hostId; |
| 100 | import static org.onosproject.net.PortNumber.portNumber; |
Michele Santuari | 72f6bee | 2016-11-18 12:56:30 +0100 | [diff] [blame] | 101 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 102 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
| 103 | import static org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 104 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 105 | public class HostLocationProviderTest { |
Charles Chan | 8b0fdaa | 2016-02-23 18:35:27 -0800 | [diff] [blame] | 106 | private static final ProviderId PROVIDER_ID = |
| 107 | new ProviderId("of", "org.onosproject.provider.host"); |
| 108 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 109 | private static final Integer INPORT = 10; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 110 | private static final Integer INPORT2 = 11; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 111 | private static final String DEV1 = "of:1"; |
| 112 | private static final String DEV2 = "of:2"; |
| 113 | private static final String DEV3 = "of:3"; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 114 | private static final String DEV4 = "of:4"; |
| 115 | private static final String DEV5 = "of:5"; |
| 116 | private static final String DEV6 = "of:6"; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 117 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 118 | private static final VlanId VLAN = vlanId(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 119 | private static final VlanId VLAN_100 = VlanId.vlanId("100"); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 120 | |
| 121 | // IPv4 Host |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 122 | private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); |
| 123 | private static final MacAddress BCMAC = MacAddress.valueOf("ff:ff:ff:ff:ff:ff"); |
| 124 | private static final byte[] IP = new byte[]{10, 0, 0, 1}; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 125 | private static final IpAddress IP_ADDRESS = |
| 126 | IpAddress.valueOf(IpAddress.Version.INET, IP); |
| 127 | private static final HostLocation LOCATION = |
| 128 | new HostLocation(deviceId(DEV1), portNumber(INPORT), 0L); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 129 | private static final DefaultHost HOST = |
Charles Chan | 8b0fdaa | 2016-02-23 18:35:27 -0800 | [diff] [blame] | 130 | new DefaultHost(PROVIDER_ID, hostId(MAC), MAC, |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 131 | VLAN, LOCATION, |
| 132 | ImmutableSet.of(IP_ADDRESS)); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 133 | private static final DefaultHost HOST_VLAN_100 = |
| 134 | new DefaultHost(PROVIDER_ID, hostId(MAC, VLAN_100), MAC, |
| 135 | VLAN_100, LOCATION, |
| 136 | ImmutableSet.of(IP_ADDRESS)); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 137 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 138 | // IPv6 Host |
| 139 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:22:00:00:02"); |
| 140 | private static final MacAddress BCMAC2 = MacAddress.valueOf("33:33:00:00:00:01"); |
| 141 | private static final byte[] IP2 = Ip6Address.valueOf("1000::1").toOctets(); |
| 142 | private static final IpAddress IP_ADDRESS2 = |
| 143 | IpAddress.valueOf(IpAddress.Version.INET6, IP2); |
| 144 | private static final HostLocation LOCATION2 = |
| 145 | new HostLocation(deviceId(DEV4), portNumber(INPORT), 0L); |
| 146 | private static final DefaultHost HOST2 = |
Charles Chan | 8b0fdaa | 2016-02-23 18:35:27 -0800 | [diff] [blame] | 147 | new DefaultHost(PROVIDER_ID, hostId(MAC2), MAC2, |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 148 | VLAN, LOCATION2, |
| 149 | ImmutableSet.of(IP_ADDRESS2)); |
| 150 | |
| 151 | // DHCP Server |
| 152 | private static final MacAddress MAC3 = MacAddress.valueOf("00:00:33:00:00:03"); |
| 153 | private static final byte[] IP3 = new byte[]{10, 0, 0, 2}; |
| 154 | private static final IpAddress IP_ADDRESS3 = |
| 155 | IpAddress.valueOf(IpAddress.Version.INET, IP3); |
| 156 | |
| 157 | private static final HostLocation LOCATION3 = |
| 158 | new HostLocation(deviceId(DEV1), portNumber(INPORT2), 0L); |
| 159 | private static final DefaultHost HOST3 = |
| 160 | new DefaultHost(PROVIDER_ID, hostId(MAC3), MAC3, |
| 161 | VLAN, LOCATION3, |
| 162 | ImmutableSet.of(IP_ADDRESS3)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 163 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 164 | // Gateway information for relay agent |
| 165 | private static final InterfaceIpAddress GW_IFACE_ADDR = InterfaceIpAddress.valueOf("10.0.1.1/32"); |
| 166 | private static final Interface GW_IFACE = new Interface("gateway", |
| 167 | LOCATION, |
| 168 | ImmutableList.of(GW_IFACE_ADDR), |
| 169 | null, |
| 170 | VLAN_100); |
| 171 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 172 | private static final ComponentContextAdapter CTX_FOR_REMOVE = |
| 173 | new ComponentContextAdapter() { |
| 174 | @Override |
| 175 | public Dictionary getProperties() { |
| 176 | Hashtable<String, String> props = new Hashtable<>(); |
| 177 | props.put("hostRemovalEnabled", "true"); |
| 178 | return props; |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | public static final ComponentContextAdapter CTX_FOR_NO_REMOVE = |
| 183 | new ComponentContextAdapter() { |
| 184 | @Override |
| 185 | public Dictionary getProperties() { |
| 186 | return new Hashtable(); |
| 187 | } |
| 188 | }; |
| 189 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 190 | private final HostLocationProvider provider = new HostLocationProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 191 | private final TestHostRegistry hostRegistry = new TestHostRegistry(); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 192 | private final TestTopologyService topoService = new TestTopologyService(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 193 | private final TestDeviceService deviceService = new TestDeviceService(); |
| 194 | private final TestHostService hostService = new TestHostService(); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 195 | private final TestPacketService packetService = new TestPacketService(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 196 | private final TestInterfaceService interfaceService = new TestInterfaceService(); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 197 | |
| 198 | private PacketProcessor testProcessor; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 199 | private CoreService coreService; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 200 | private TestHostProviderService providerService; |
| 201 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 202 | private ApplicationId appId = |
| 203 | new DefaultApplicationId(100, "org.onosproject.provider.host"); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 204 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 205 | @Before |
| 206 | public void setUp() { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 207 | coreService = createMock(CoreService.class); |
| 208 | expect(coreService.registerApplication(appId.name())) |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 209 | .andReturn(appId).anyTimes(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 210 | replay(coreService); |
| 211 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 212 | provider.cfgService = new ComponentConfigAdapter(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 213 | provider.coreService = coreService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 214 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 215 | provider.providerRegistry = hostRegistry; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 216 | provider.topologyService = topoService; |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 217 | provider.packetService = packetService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 218 | provider.deviceService = deviceService; |
| 219 | provider.hostService = hostService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 220 | provider.interfaceService = interfaceService; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 221 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 222 | provider.activate(CTX_FOR_NO_REMOVE); |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 223 | |
| 224 | provider.eventHandler = MoreExecutors.newDirectExecutorService(); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Test |
| 228 | public void basics() { |
| 229 | assertNotNull("registration expected", providerService); |
| 230 | assertEquals("incorrect provider", provider, providerService.provider()); |
| 231 | } |
| 232 | |
| 233 | @Test |
| 234 | public void events() { |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 235 | // New host. Expect one additional host description. |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 236 | testProcessor.process(new TestArpPacketContext(DEV1)); |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 237 | assertThat("New host expected", providerService.descriptions.size(), is(1)); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 238 | |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 239 | // The host moved to new switch. Expect one additional host description. |
| 240 | // The second host description should have a different location. |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 241 | testProcessor.process(new TestArpPacketContext(DEV2)); |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 242 | assertThat("Host motion expected", providerService.descriptions.size(), is(2)); |
| 243 | HostLocation loc1 = providerService.descriptions.get(0).location(); |
| 244 | HostLocation loc2 = providerService.descriptions.get(1).location(); |
| 245 | assertNotEquals("Host location should be different", loc1, loc2); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 246 | |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 247 | // The host was misheard on a spine. Expect no additional host description. |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 248 | testProcessor.process(new TestArpPacketContext(DEV3)); |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 249 | assertThat("Host misheard on spine switch", providerService.descriptions.size(), is(2)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 250 | |
| 251 | providerService.clear(); |
| 252 | |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 253 | // New host. Expect one additional host description. |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 254 | testProcessor.process(new TestNaPacketContext(DEV4)); |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 255 | assertThat("New host expected", providerService.descriptions.size(), is(1)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 256 | |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 257 | // The host moved to new switch. Expect one additional host description. |
| 258 | // The second host description should have a different location. |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 259 | testProcessor.process(new TestNaPacketContext(DEV5)); |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 260 | assertThat("Host motion expected", providerService.descriptions.size(), is(2)); |
| 261 | loc1 = providerService.descriptions.get(0).location(); |
| 262 | loc2 = providerService.descriptions.get(1).location(); |
| 263 | assertNotEquals("Host location should be different", loc1, loc2); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 264 | |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 265 | // The host was misheard on a spine. Expect no additional host description. |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 266 | testProcessor.process(new TestNaPacketContext(DEV6)); |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 267 | assertThat("Host misheard on spine switch", providerService.descriptions.size(), is(2)); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 270 | @Test |
| 271 | public void removeHostByDeviceRemove() { |
| 272 | provider.modified(CTX_FOR_REMOVE); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 273 | testProcessor.process(new TestArpPacketContext(DEV1)); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 274 | testProcessor.process(new TestNaPacketContext(DEV4)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 275 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 276 | Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH, |
| 277 | "m", "h", "s", "n", new ChassisId(0L)); |
| 278 | deviceService.listener.event(new DeviceEvent(DEVICE_REMOVED, device)); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 279 | assertEquals("incorrect remove count", 2, providerService.locationRemoveCount); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 280 | |
| 281 | device = new DefaultDevice(ProviderId.NONE, deviceId(DEV4), SWITCH, |
| 282 | "m", "h", "s", "n", new ChassisId(0L)); |
| 283 | deviceService.listener.event(new DeviceEvent(DEVICE_REMOVED, device)); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 284 | assertEquals("incorrect remove count", 3, providerService.locationRemoveCount); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Test |
| 288 | public void removeHostByDeviceOffline() { |
| 289 | provider.modified(CTX_FOR_REMOVE); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 290 | testProcessor.process(new TestArpPacketContext(DEV1)); |
| 291 | testProcessor.process(new TestArpPacketContext(DEV4)); |
| 292 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 293 | Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH, |
| 294 | "m", "h", "s", "n", new ChassisId(0L)); |
| 295 | deviceService.listener.event(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device)); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 296 | assertEquals("incorrect remove count", 2, providerService.locationRemoveCount); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 297 | |
| 298 | device = new DefaultDevice(ProviderId.NONE, deviceId(DEV4), SWITCH, |
| 299 | "m", "h", "s", "n", new ChassisId(0L)); |
| 300 | deviceService.listener.event(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device)); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 301 | assertEquals("incorrect remove count", 3, providerService.locationRemoveCount); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void removeHostByDevicePortDown() { |
| 306 | provider.modified(CTX_FOR_REMOVE); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 307 | testProcessor.process(new TestArpPacketContext(DEV1)); |
| 308 | testProcessor.process(new TestArpPacketContext(DEV4)); |
| 309 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 310 | Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH, |
| 311 | "m", "h", "s", "n", new ChassisId(0L)); |
| 312 | deviceService.listener.event(new DeviceEvent(PORT_UPDATED, device, |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 313 | new DefaultPort(device, portNumber(INPORT), false))); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 314 | assertEquals("incorrect remove count", 1, providerService.locationRemoveCount); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 315 | |
| 316 | device = new DefaultDevice(ProviderId.NONE, deviceId(DEV4), SWITCH, |
| 317 | "m", "h", "s", "n", new ChassisId(0L)); |
| 318 | deviceService.listener.event(new DeviceEvent(PORT_UPDATED, device, |
| 319 | new DefaultPort(device, portNumber(INPORT), false))); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 320 | assertEquals("incorrect remove count", 2, providerService.locationRemoveCount); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 323 | /** |
| 324 | * When receiving ARP, updates location and IP. |
| 325 | */ |
| 326 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 327 | public void receiveArp() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 328 | testProcessor.process(new TestArpPacketContext(DEV1)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 329 | assertThat("receiveArp. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 330 | providerService.descriptions.size(), is(1)); |
| 331 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 332 | assertThat(descr.location(), is(LOCATION)); |
| 333 | assertThat(descr.hwAddress(), is(MAC)); |
| 334 | assertThat(descr.ipAddress().toArray()[0], is(IP_ADDRESS)); |
| 335 | assertThat(descr.vlan(), is(VLAN)); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * When receiving IPv4, updates location only. |
| 340 | */ |
| 341 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 342 | public void receiveIpv4() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 343 | testProcessor.process(new TestIpv4PacketContext(DEV1)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 344 | assertThat("receiveIpv4. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 345 | providerService.descriptions.size(), is(1)); |
| 346 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 347 | assertThat(descr.location(), is(LOCATION)); |
| 348 | assertThat(descr.hwAddress(), is(MAC)); |
| 349 | assertThat(descr.ipAddress().size(), is(0)); |
| 350 | assertThat(descr.vlan(), is(VLAN)); |
| 351 | } |
| 352 | |
| 353 | /** |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 354 | * When receiving DHCP REQUEST, update MAC, location of client. |
| 355 | * When receiving DHCP ACK, update MAC, location of server and IP of client. |
| 356 | */ |
| 357 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 358 | public void receiveDhcp() { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 359 | TestUtils.setField(provider, "useDhcp", true); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 360 | // DHCP Request |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 361 | testProcessor.process(new TestDhcpRequestPacketContext(DEV1, VLAN)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 362 | assertThat("receiveDhcpRequest. One host description expected", |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 363 | providerService.descriptions.size(), is(1)); |
| 364 | // Should learn the MAC and location of DHCP client |
| 365 | HostDescription descr = providerService.descriptions.get(0); |
| 366 | assertThat(descr.location(), is(LOCATION)); |
| 367 | assertThat(descr.hwAddress(), is(MAC)); |
| 368 | assertThat(descr.ipAddress().size(), is(0)); |
| 369 | assertThat(descr.vlan(), is(VLAN)); |
| 370 | |
| 371 | // DHCP Ack |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 372 | testProcessor.process(new TestDhcpAckPacketContext(DEV1, false)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 373 | assertThat("receiveDhcpAck. Two additional host descriptions expected", |
Yi Tseng | cfa637a | 2017-07-26 17:24:10 -0700 | [diff] [blame^] | 374 | providerService.descriptions.size(), is(3)); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 375 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 376 | // Should also learn the MAC, location of DHCP server |
| 377 | HostDescription descr2 = providerService.descriptions.get(1); |
| 378 | assertThat(descr2.location(), is(LOCATION3)); |
| 379 | assertThat(descr2.hwAddress(), is(MAC3)); |
| 380 | assertThat(descr2.ipAddress().size(), is(0)); |
| 381 | assertThat(descr2.vlan(), is(VLAN)); |
| 382 | |
Yi Tseng | cfa637a | 2017-07-26 17:24:10 -0700 | [diff] [blame^] | 383 | // Should update the IP address of the client. |
| 384 | HostDescription descr3 = providerService.descriptions.get(2); |
| 385 | assertThat(descr3.location(), is(LOCATION)); |
| 386 | assertThat(descr3.hwAddress(), is(MAC)); |
| 387 | assertThat(descr3.ipAddress().size(), is(1)); |
| 388 | IpAddress ip = descr3.ipAddress().iterator().next(); |
| 389 | assertThat(ip, is(IP_ADDRESS.getIp4Address())); |
| 390 | assertThat(descr3.vlan(), is(VLAN)); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * The host store should not updated when we disabled "useDhcp". |
| 395 | */ |
| 396 | @Test |
| 397 | public void receiveDhcpButNotEnabled() { |
| 398 | TestUtils.setField(provider, "useDhcp", false); |
| 399 | // DHCP Request |
| 400 | testProcessor.process(new TestDhcpRequestPacketContext(DEV1, VLAN)); |
| 401 | assertThat("receiveDhcpButNotEnabled. No host description expected", |
| 402 | providerService.descriptions.size(), is(0)); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 406 | * When receiving NeighborAdvertisement, updates location and IP. |
| 407 | */ |
| 408 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 409 | public void receiveNa() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 410 | testProcessor.process(new TestNaPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 411 | assertThat("receiveNa. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 412 | providerService.descriptions.size(), is(1)); |
| 413 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 414 | assertThat(descr.location(), is(LOCATION2)); |
| 415 | assertThat(descr.hwAddress(), is(MAC2)); |
| 416 | assertThat(descr.ipAddress().toArray()[0], is(IP_ADDRESS2)); |
| 417 | assertThat(descr.vlan(), is(VLAN)); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * When receiving NeighborSolicitation, updates location and IP. |
| 422 | */ |
| 423 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 424 | public void receiveNs() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 425 | testProcessor.process(new TestNsPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 426 | assertThat("receiveNs. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 427 | providerService.descriptions.size(), is(1)); |
| 428 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 429 | assertThat(descr.location(), is(LOCATION2)); |
| 430 | assertThat(descr.hwAddress(), is(MAC2)); |
| 431 | assertThat(descr.ipAddress().toArray()[0], is(IP_ADDRESS2)); |
| 432 | assertThat(descr.vlan(), is(VLAN)); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * When receiving RouterAdvertisement, ignores it. |
| 437 | */ |
| 438 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 439 | public void receivesRa() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 440 | testProcessor.process(new TestRAPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 441 | assertThat("receivesRa. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 442 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
| 446 | * When receiving RouterSolicitation, ignores it. |
| 447 | */ |
| 448 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 449 | public void receiveRs() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 450 | testProcessor.process(new TestRSPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 451 | assertThat("receiveRs. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 452 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
| 456 | * When receiving Duplicate Address Detection (DAD), ignores it. |
| 457 | */ |
| 458 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 459 | public void receiveDad() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 460 | testProcessor.process(new TestDadPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 461 | assertThat("receiveDad. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 462 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
| 466 | * When receiving IPv6 multicast packet, ignores it. |
| 467 | */ |
| 468 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 469 | public void receiveIpv6Multicast() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 470 | testProcessor.process(new TestIpv6McastPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 471 | assertThat("receiveIpv6Multicast. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 472 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
| 476 | * When receiving IPv6 unicast packet, updates location only. |
| 477 | */ |
| 478 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 479 | public void receiveIpv6Unicast() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 480 | testProcessor.process(new TestIpv6PacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 481 | assertThat("receiveIpv6Unicast. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 482 | providerService.descriptions.size(), is(1)); |
| 483 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 484 | assertThat(descr.location(), is(LOCATION2)); |
| 485 | assertThat(descr.hwAddress(), is(MAC2)); |
| 486 | assertThat(descr.ipAddress().size(), is(0)); |
| 487 | assertThat(descr.vlan(), is(VLAN)); |
| 488 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 489 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 490 | @After |
| 491 | public void tearDown() { |
| 492 | provider.deactivate(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 493 | provider.coreService = null; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 494 | provider.providerRegistry = null; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | private class TestHostRegistry implements HostProviderRegistry { |
| 498 | |
| 499 | @Override |
| 500 | public HostProviderService register(HostProvider provider) { |
| 501 | providerService = new TestHostProviderService(provider); |
| 502 | return providerService; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void unregister(HostProvider provider) { |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public Set<ProviderId> getProviders() { |
| 511 | return null; |
| 512 | } |
| 513 | |
| 514 | } |
| 515 | |
| 516 | private class TestHostProviderService |
| 517 | extends AbstractProviderService<HostProvider> |
| 518 | implements HostProviderService { |
| 519 | |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 520 | List<HostDescription> descriptions = Lists.newArrayList(); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 521 | int hostRemoveCount; |
| 522 | int ipRemoveCount; |
| 523 | int locationRemoveCount; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 524 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 525 | public void clear() { |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 526 | descriptions.clear(); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 527 | hostRemoveCount = 0; |
| 528 | ipRemoveCount = 0; |
| 529 | locationRemoveCount = 0; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 530 | } |
| 531 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 532 | protected TestHostProviderService(HostProvider provider) { |
| 533 | super(provider); |
| 534 | } |
| 535 | |
| 536 | @Override |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 537 | public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) { |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 538 | descriptions.add(hostDescription); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public void hostVanished(HostId hostId) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 543 | hostRemoveCount++; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 544 | } |
| 545 | |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 546 | @Override |
| 547 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 548 | ipRemoveCount++; |
| 549 | } |
| 550 | |
| 551 | @Override |
| 552 | public void removeLocationFromHost(HostId hostId, HostLocation location) { |
| 553 | locationRemoveCount++; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 554 | } |
| 555 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 558 | private class TestPacketService extends PacketServiceAdapter { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 559 | @Override |
| 560 | public void addProcessor(PacketProcessor processor, int priority) { |
| 561 | testProcessor = processor; |
| 562 | } |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 563 | } |
| 564 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 565 | private class TestTopologyService extends TopologyServiceAdapter { |
| 566 | @Override |
| 567 | public boolean isInfrastructure(Topology topology, |
| 568 | ConnectPoint connectPoint) { |
| 569 | //simulate DPID3 as an infrastructure switch |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 570 | if ((connectPoint.deviceId()).equals(deviceId(DEV3)) || |
| 571 | connectPoint.deviceId().equals(deviceId(DEV6))) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 572 | return true; |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | } |
| 577 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 578 | /** |
| 579 | * Generates ARP packet. |
| 580 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 581 | private class TestArpPacketContext extends PacketContextAdapter { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 582 | private final String deviceId; |
| 583 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 584 | public TestArpPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 585 | super(0, null, null, false); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 586 | this.deviceId = deviceId; |
| 587 | } |
| 588 | |
| 589 | @Override |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 590 | public InboundPacket inPacket() { |
| 591 | ARP arp = new ARP(); |
| 592 | arp.setSenderProtocolAddress(IP) |
| 593 | .setSenderHardwareAddress(MAC.toBytes()) |
| 594 | .setTargetHardwareAddress(BCMAC.toBytes()) |
| 595 | .setTargetProtocolAddress(IP); |
| 596 | |
| 597 | Ethernet eth = new Ethernet(); |
| 598 | eth.setEtherType(Ethernet.TYPE_ARP) |
| 599 | .setVlanID(VLAN.toShort()) |
| 600 | .setSourceMACAddress(MAC.toBytes()) |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 601 | .setDestinationMACAddress(BCMAC) |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 602 | .setPayload(arp); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 603 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 604 | portNumber(INPORT)); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 605 | return new DefaultInboundPacket(receivedFrom, eth, |
| 606 | ByteBuffer.wrap(eth.serialize())); |
| 607 | } |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 608 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 609 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 610 | /** |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 611 | * Generates IPv4 Unicast packet. |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 612 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 613 | private class TestIpv4PacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 614 | private final String deviceId; |
| 615 | |
| 616 | public TestIpv4PacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 617 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 618 | this.deviceId = deviceId; |
| 619 | } |
| 620 | |
| 621 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 622 | public InboundPacket inPacket() { |
| 623 | IPv4 ipv4 = new IPv4(); |
| 624 | ipv4.setDestinationAddress("10.0.0.1"); |
| 625 | ipv4.setSourceAddress(IP_ADDRESS.toString()); |
| 626 | Ethernet eth = new Ethernet(); |
| 627 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 628 | .setVlanID(VLAN.toShort()) |
| 629 | .setSourceMACAddress(MAC) |
| 630 | .setDestinationMACAddress(MacAddress.valueOf("00:00:00:00:00:01")) |
| 631 | .setPayload(ipv4); |
| 632 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 633 | portNumber(INPORT)); |
| 634 | return new DefaultInboundPacket(receivedFrom, eth, |
| 635 | ByteBuffer.wrap(eth.serialize())); |
| 636 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 637 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 638 | /** |
| 639 | * Generates DHCP REQUEST packet. |
| 640 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 641 | private class TestDhcpRequestPacketContext extends PacketContextAdapter { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 642 | private final String deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 643 | private final VlanId vlanId; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 644 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 645 | public TestDhcpRequestPacketContext(String deviceId, VlanId vlanId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 646 | super(0, null, null, false); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 647 | this.deviceId = deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 648 | this.vlanId = vlanId; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | @Override |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 652 | public InboundPacket inPacket() { |
| 653 | byte[] dhcpMsgType = new byte[1]; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 654 | dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPREQUEST.getValue(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 655 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 656 | DhcpOption dhcpOption = new DhcpOption(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 657 | dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 658 | dhcpOption.setData(dhcpMsgType); |
| 659 | dhcpOption.setLength((byte) 1); |
| 660 | DHCP dhcp = new DHCP(); |
| 661 | dhcp.setOptions(Collections.singletonList(dhcpOption)); |
| 662 | dhcp.setClientHardwareAddress(MAC.toBytes()); |
| 663 | UDP udp = new UDP(); |
| 664 | udp.setPayload(dhcp); |
| 665 | udp.setSourcePort(UDP.DHCP_CLIENT_PORT); |
| 666 | udp.setDestinationPort(UDP.DHCP_SERVER_PORT); |
| 667 | IPv4 ipv4 = new IPv4(); |
| 668 | ipv4.setPayload(udp); |
| 669 | ipv4.setDestinationAddress(IP_ADDRESS3.toString()); |
| 670 | ipv4.setSourceAddress(IP_ADDRESS.toString()); |
| 671 | Ethernet eth = new Ethernet(); |
| 672 | eth.setEtherType(Ethernet.TYPE_IPV4) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 673 | .setVlanID(this.vlanId.toShort()) |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 674 | .setSourceMACAddress(MAC) |
| 675 | .setDestinationMACAddress(MAC3) |
| 676 | .setPayload(ipv4); |
| 677 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 678 | portNumber(INPORT)); |
| 679 | return new DefaultInboundPacket(receivedFrom, eth, |
| 680 | ByteBuffer.wrap(eth.serialize())); |
| 681 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Generates DHCP ACK packet. |
| 686 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 687 | private class TestDhcpAckPacketContext extends PacketContextAdapter { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 688 | private final String deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 689 | private final boolean withRelayinfo; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 690 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 691 | public TestDhcpAckPacketContext(String deviceId, boolean withRelayInfo) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 692 | super(0, null, null, false); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 693 | this.deviceId = deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 694 | this.withRelayinfo = withRelayInfo; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | @Override |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 698 | public InboundPacket inPacket() { |
| 699 | byte[] dhcpMsgType = new byte[1]; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 700 | dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPACK.getValue(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 701 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 702 | DhcpOption dhcpOption = new DhcpOption(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 703 | dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 704 | dhcpOption.setData(dhcpMsgType); |
| 705 | dhcpOption.setLength((byte) 1); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 706 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 707 | DHCP dhcp = new DHCP(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 708 | |
| 709 | if (withRelayinfo) { |
| 710 | CircuitId cid = new CircuitId(LOCATION.toString(), VLAN_100); |
| 711 | byte[] circuitId = cid.serialize(); |
| 712 | DhcpOption circuitIdSubOption = new DhcpOption(); |
| 713 | circuitIdSubOption.setCode(DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID.getValue()); |
| 714 | circuitIdSubOption.setData(circuitId); |
| 715 | circuitIdSubOption.setLength((byte) circuitId.length); |
| 716 | |
| 717 | DhcpRelayAgentOption relayInfoOption = new DhcpRelayAgentOption(); |
| 718 | relayInfoOption.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 719 | relayInfoOption.addSubOption(circuitIdSubOption); |
| 720 | dhcp.setOptions(ImmutableList.of(dhcpOption, relayInfoOption)); |
| 721 | dhcp.setGatewayIPAddress(GW_IFACE_ADDR.ipAddress().getIp4Address().toInt()); |
| 722 | } else { |
| 723 | dhcp.setOptions(ImmutableList.of(dhcpOption)); |
| 724 | } |
| 725 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 726 | dhcp.setClientHardwareAddress(MAC.toBytes()); |
| 727 | dhcp.setYourIPAddress(IP_ADDRESS.getIp4Address().toInt()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 728 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 729 | UDP udp = new UDP(); |
| 730 | udp.setPayload(dhcp); |
| 731 | udp.setSourcePort(UDP.DHCP_SERVER_PORT); |
| 732 | udp.setDestinationPort(UDP.DHCP_CLIENT_PORT); |
| 733 | IPv4 ipv4 = new IPv4(); |
| 734 | ipv4.setPayload(udp); |
| 735 | ipv4.setDestinationAddress(IP_ADDRESS.toString()); |
| 736 | ipv4.setSourceAddress(IP_ADDRESS3.toString()); |
| 737 | Ethernet eth = new Ethernet(); |
| 738 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 739 | .setVlanID(VLAN.toShort()) |
| 740 | .setSourceMACAddress(MAC3) |
| 741 | .setDestinationMACAddress(MAC) |
| 742 | .setPayload(ipv4); |
| 743 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 744 | portNumber(INPORT2)); |
| 745 | return new DefaultInboundPacket(receivedFrom, eth, |
| 746 | ByteBuffer.wrap(eth.serialize())); |
| 747 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 748 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 749 | |
| 750 | /** |
| 751 | * Generates NeighborAdvertisement packet. |
| 752 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 753 | private class TestNaPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 754 | private final String deviceId; |
| 755 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 756 | public TestNaPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 757 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 758 | this.deviceId = deviceId; |
| 759 | } |
| 760 | |
| 761 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 762 | public InboundPacket inPacket() { |
| 763 | NeighborAdvertisement na = new NeighborAdvertisement(); |
| 764 | ICMP6 icmp6 = new ICMP6(); |
| 765 | icmp6.setPayload(na); |
| 766 | IPv6 ipv6 = new IPv6(); |
| 767 | ipv6.setPayload(icmp6); |
| 768 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 769 | ipv6.setSourceAddress(IP2); |
| 770 | Ethernet eth = new Ethernet(); |
| 771 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 772 | .setVlanID(VLAN.toShort()) |
| 773 | .setSourceMACAddress(MAC2.toBytes()) |
| 774 | .setDestinationMACAddress(BCMAC2) |
| 775 | .setPayload(ipv6); |
| 776 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 777 | portNumber(INPORT)); |
| 778 | return new DefaultInboundPacket(receivedFrom, eth, |
| 779 | ByteBuffer.wrap(eth.serialize())); |
| 780 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Generates NeighborSolicitation packet. |
| 785 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 786 | private class TestNsPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 787 | private final String deviceId; |
| 788 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 789 | public TestNsPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 790 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 791 | this.deviceId = deviceId; |
| 792 | } |
| 793 | |
| 794 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 795 | public InboundPacket inPacket() { |
| 796 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 797 | ICMP6 icmp6 = new ICMP6(); |
| 798 | icmp6.setPayload(ns); |
| 799 | IPv6 ipv6 = new IPv6(); |
| 800 | ipv6.setPayload(icmp6); |
| 801 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1:ff00:0000").toOctets()); |
| 802 | ipv6.setSourceAddress(IP2); |
| 803 | Ethernet eth = new Ethernet(); |
| 804 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 805 | .setVlanID(VLAN.toShort()) |
| 806 | .setSourceMACAddress(MAC2.toBytes()) |
| 807 | .setDestinationMACAddress(BCMAC2) |
| 808 | .setPayload(ipv6); |
| 809 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 810 | portNumber(INPORT)); |
| 811 | return new DefaultInboundPacket(receivedFrom, eth, |
| 812 | ByteBuffer.wrap(eth.serialize())); |
| 813 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Generates Duplicate Address Detection packet. |
| 818 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 819 | private class TestDadPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 820 | private final String deviceId; |
| 821 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 822 | public TestDadPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 823 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 824 | this.deviceId = deviceId; |
| 825 | } |
| 826 | |
| 827 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 828 | public InboundPacket inPacket() { |
| 829 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 830 | ICMP6 icmp6 = new ICMP6(); |
| 831 | icmp6.setPayload(ns); |
| 832 | IPv6 ipv6 = new IPv6(); |
| 833 | ipv6.setPayload(icmp6); |
| 834 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 835 | ipv6.setSourceAddress(Ip6Address.valueOf("::").toOctets()); |
| 836 | Ethernet eth = new Ethernet(); |
| 837 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 838 | .setVlanID(VLAN.toShort()) |
| 839 | .setSourceMACAddress(MAC2.toBytes()) |
| 840 | .setDestinationMACAddress(BCMAC2) |
| 841 | .setPayload(ipv6); |
| 842 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 843 | portNumber(INPORT)); |
| 844 | return new DefaultInboundPacket(receivedFrom, eth, |
| 845 | ByteBuffer.wrap(eth.serialize())); |
| 846 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Generates Router Solicitation packet. |
| 851 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 852 | private class TestRSPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 853 | private final String deviceId; |
| 854 | |
| 855 | public TestRSPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 856 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 857 | this.deviceId = deviceId; |
| 858 | } |
| 859 | |
| 860 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 861 | public InboundPacket inPacket() { |
| 862 | RouterSolicitation ns = new RouterSolicitation(); |
| 863 | ICMP6 icmp6 = new ICMP6(); |
| 864 | icmp6.setPayload(ns); |
| 865 | IPv6 ipv6 = new IPv6(); |
| 866 | ipv6.setPayload(icmp6); |
| 867 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::2").toOctets()); |
| 868 | ipv6.setSourceAddress(Ip6Address.valueOf("::").toOctets()); |
| 869 | Ethernet eth = new Ethernet(); |
| 870 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 871 | .setVlanID(VLAN.toShort()) |
| 872 | .setSourceMACAddress(MAC2.toBytes()) |
| 873 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:02")) |
| 874 | .setPayload(ipv6); |
| 875 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 876 | portNumber(INPORT)); |
| 877 | return new DefaultInboundPacket(receivedFrom, eth, |
| 878 | ByteBuffer.wrap(eth.serialize())); |
| 879 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /** |
| 883 | * Generates Router Advertisement packet. |
| 884 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 885 | private class TestRAPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 886 | private final String deviceId; |
| 887 | |
| 888 | public TestRAPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 889 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 890 | this.deviceId = deviceId; |
| 891 | } |
| 892 | |
| 893 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 894 | public InboundPacket inPacket() { |
| 895 | RouterAdvertisement ns = new RouterAdvertisement(); |
| 896 | ICMP6 icmp6 = new ICMP6(); |
| 897 | icmp6.setPayload(ns); |
| 898 | IPv6 ipv6 = new IPv6(); |
| 899 | ipv6.setPayload(icmp6); |
| 900 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 901 | ipv6.setSourceAddress(IP2); |
| 902 | Ethernet eth = new Ethernet(); |
| 903 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 904 | .setVlanID(VLAN.toShort()) |
| 905 | .setSourceMACAddress(MAC2.toBytes()) |
| 906 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:01")) |
| 907 | .setPayload(ipv6); |
| 908 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 909 | portNumber(INPORT)); |
| 910 | return new DefaultInboundPacket(receivedFrom, eth, |
| 911 | ByteBuffer.wrap(eth.serialize())); |
| 912 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Generates IPv6 Multicast packet. |
| 917 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 918 | private class TestIpv6McastPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 919 | private final String deviceId; |
| 920 | |
| 921 | public TestIpv6McastPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 922 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 923 | this.deviceId = deviceId; |
| 924 | } |
| 925 | |
| 926 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 927 | public InboundPacket inPacket() { |
| 928 | IPv6 ipv6 = new IPv6(); |
| 929 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 930 | ipv6.setSourceAddress(IP2); |
| 931 | Ethernet eth = new Ethernet(); |
| 932 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 933 | .setVlanID(VLAN.toShort()) |
| 934 | .setSourceMACAddress(MAC2.toBytes()) |
| 935 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:01")) |
| 936 | .setPayload(ipv6); |
| 937 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 938 | portNumber(INPORT)); |
| 939 | return new DefaultInboundPacket(receivedFrom, eth, |
| 940 | ByteBuffer.wrap(eth.serialize())); |
| 941 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Generates IPv6 Unicast packet. |
| 946 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 947 | private class TestIpv6PacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 948 | private final String deviceId; |
| 949 | |
| 950 | public TestIpv6PacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 951 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 952 | this.deviceId = deviceId; |
| 953 | } |
| 954 | |
| 955 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 956 | public InboundPacket inPacket() { |
| 957 | IPv6 ipv6 = new IPv6(); |
| 958 | ipv6.setDestinationAddress(Ip6Address.valueOf("1000::1").toOctets()); |
| 959 | ipv6.setSourceAddress(IP2); |
| 960 | Ethernet eth = new Ethernet(); |
| 961 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 962 | .setVlanID(VLAN.toShort()) |
| 963 | .setSourceMACAddress(MAC2) |
| 964 | .setDestinationMACAddress(MacAddress.valueOf("00:00:00:00:00:01")) |
| 965 | .setPayload(ipv6); |
| 966 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 967 | portNumber(INPORT)); |
| 968 | return new DefaultInboundPacket(receivedFrom, eth, |
| 969 | ByteBuffer.wrap(eth.serialize())); |
| 970 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 973 | private class TestDeviceService extends DeviceServiceAdapter { |
| 974 | private DeviceListener listener; |
| 975 | |
| 976 | @Override |
| 977 | public void addListener(DeviceListener listener) { |
| 978 | this.listener = listener; |
| 979 | } |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 980 | |
| 981 | @Override |
| 982 | public Iterable<Device> getDevices() { |
| 983 | return Collections.emptyList(); |
| 984 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | private class TestHostService extends HostServiceAdapter { |
| 988 | @Override |
| 989 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 990 | ConnectPoint cp1 = new ConnectPoint(deviceId(DEV1), portNumber(INPORT)); |
| 991 | ConnectPoint cp2 = new ConnectPoint(deviceId(DEV4), portNumber(INPORT)); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 992 | ConnectPoint cp3 = new ConnectPoint(deviceId(DEV1), portNumber(INPORT2)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 993 | if (connectPoint.equals(cp1)) { |
| 994 | return ImmutableSet.of(HOST); |
| 995 | } else if (connectPoint.equals(cp2)) { |
| 996 | return ImmutableSet.of(HOST2); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 997 | } else if (connectPoint.equals(cp3)) { |
| 998 | return ImmutableSet.of(HOST3); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 999 | } else { |
| 1000 | return ImmutableSet.of(); |
| 1001 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | @Override |
| 1005 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 1006 | if (deviceId.equals(deviceId(DEV1))) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1007 | return ImmutableSet.of(HOST, HOST3); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 1008 | } else if (deviceId.equals(deviceId(DEV4))) { |
| 1009 | return ImmutableSet.of(HOST2); |
| 1010 | } else { |
| 1011 | return ImmutableSet.of(); |
| 1012 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1015 | @Override |
| 1016 | public Host getHost(HostId hostId) { |
| 1017 | if (hostId.equals(HostId.hostId(MAC, VLAN))) { |
| 1018 | return HOST; |
| 1019 | } else if (hostId.equals(HostId.hostId(MAC2, VLAN))) { |
| 1020 | return HOST2; |
| 1021 | } else if (hostId.equals(HostId.hostId(MAC3, VLAN))) { |
| 1022 | return HOST3; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1023 | } else if (hostId.equals(HostId.hostId(MAC, VLAN_100))) { |
| 1024 | return HOST_VLAN_100; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1025 | } |
| 1026 | return null; |
| 1027 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1028 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1029 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1030 | private class TestInterfaceService implements InterfaceService { |
| 1031 | @Override |
| 1032 | public Set<Interface> getInterfaces() { |
| 1033 | return null; |
| 1034 | } |
| 1035 | |
| 1036 | @Override |
| 1037 | public Interface getInterfaceByName(ConnectPoint connectPoint, String name) { |
| 1038 | return null; |
| 1039 | } |
| 1040 | |
| 1041 | @Override |
| 1042 | public Set<Interface> getInterfacesByPort(ConnectPoint port) { |
| 1043 | return null; |
| 1044 | } |
| 1045 | |
| 1046 | public Set<Interface> getInterfacesByIp(IpAddress ip) { |
| 1047 | if (ip.equals(GW_IFACE_ADDR.ipAddress())) { |
| 1048 | return ImmutableSet.of(GW_IFACE); |
| 1049 | } else { |
| 1050 | return ImmutableSet.of(); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | @Override |
| 1055 | public Set<Interface> getInterfacesByVlan(VlanId vlan) { |
| 1056 | return null; |
| 1057 | } |
| 1058 | |
| 1059 | @Override |
| 1060 | public Interface getMatchingInterface(IpAddress ip) { |
| 1061 | return null; |
| 1062 | } |
| 1063 | |
| 1064 | @Override |
| 1065 | public Set<Interface> getMatchingInterfaces(IpAddress ip) { |
| 1066 | return null; |
| 1067 | } |
| 1068 | |
| 1069 | @Override |
| 1070 | public void addListener(InterfaceListener listener) { |
| 1071 | |
| 1072 | } |
| 1073 | |
| 1074 | @Override |
| 1075 | public void removeListener(InterfaceListener listener) { |
| 1076 | |
| 1077 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 1078 | } |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 1079 | } |