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