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 | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 374 | providerService.descriptions.size(), is(2)); |
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 | |
| 383 | // Should not update the IP address of the host. |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * The host store should not updated when we disabled "useDhcp". |
| 388 | */ |
| 389 | @Test |
| 390 | public void receiveDhcpButNotEnabled() { |
| 391 | TestUtils.setField(provider, "useDhcp", false); |
| 392 | // DHCP Request |
| 393 | testProcessor.process(new TestDhcpRequestPacketContext(DEV1, VLAN)); |
| 394 | assertThat("receiveDhcpButNotEnabled. No host description expected", |
| 395 | providerService.descriptions.size(), is(0)); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 399 | * When receiving NeighborAdvertisement, updates location and IP. |
| 400 | */ |
| 401 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 402 | public void receiveNa() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 403 | testProcessor.process(new TestNaPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 404 | assertThat("receiveNa. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 405 | providerService.descriptions.size(), is(1)); |
| 406 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 407 | assertThat(descr.location(), is(LOCATION2)); |
| 408 | assertThat(descr.hwAddress(), is(MAC2)); |
| 409 | assertThat(descr.ipAddress().toArray()[0], is(IP_ADDRESS2)); |
| 410 | assertThat(descr.vlan(), is(VLAN)); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * When receiving NeighborSolicitation, updates location and IP. |
| 415 | */ |
| 416 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 417 | public void receiveNs() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 418 | testProcessor.process(new TestNsPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 419 | assertThat("receiveNs. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 420 | providerService.descriptions.size(), is(1)); |
| 421 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 422 | assertThat(descr.location(), is(LOCATION2)); |
| 423 | assertThat(descr.hwAddress(), is(MAC2)); |
| 424 | assertThat(descr.ipAddress().toArray()[0], is(IP_ADDRESS2)); |
| 425 | assertThat(descr.vlan(), is(VLAN)); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * When receiving RouterAdvertisement, ignores it. |
| 430 | */ |
| 431 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 432 | public void receivesRa() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 433 | testProcessor.process(new TestRAPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 434 | assertThat("receivesRa. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 435 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * When receiving RouterSolicitation, ignores it. |
| 440 | */ |
| 441 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 442 | public void receiveRs() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 443 | testProcessor.process(new TestRSPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 444 | assertThat("receiveRs. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 445 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
| 449 | * When receiving Duplicate Address Detection (DAD), ignores it. |
| 450 | */ |
| 451 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 452 | public void receiveDad() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 453 | testProcessor.process(new TestDadPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 454 | assertThat("receiveDad. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 455 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
| 459 | * When receiving IPv6 multicast packet, ignores it. |
| 460 | */ |
| 461 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 462 | public void receiveIpv6Multicast() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 463 | testProcessor.process(new TestIpv6McastPacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 464 | assertThat("receiveIpv6Multicast. No host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 465 | providerService.descriptions.size(), is(0)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
| 469 | * When receiving IPv6 unicast packet, updates location only. |
| 470 | */ |
| 471 | @Test |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 472 | public void receiveIpv6Unicast() { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 473 | testProcessor.process(new TestIpv6PacketContext(DEV4)); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 474 | assertThat("receiveIpv6Unicast. One host description expected", |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 475 | providerService.descriptions.size(), is(1)); |
| 476 | HostDescription descr = providerService.descriptions.get(0); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 477 | assertThat(descr.location(), is(LOCATION2)); |
| 478 | assertThat(descr.hwAddress(), is(MAC2)); |
| 479 | assertThat(descr.ipAddress().size(), is(0)); |
| 480 | assertThat(descr.vlan(), is(VLAN)); |
| 481 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 482 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 483 | @After |
| 484 | public void tearDown() { |
| 485 | provider.deactivate(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 486 | provider.coreService = null; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 487 | provider.providerRegistry = null; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | private class TestHostRegistry implements HostProviderRegistry { |
| 491 | |
| 492 | @Override |
| 493 | public HostProviderService register(HostProvider provider) { |
| 494 | providerService = new TestHostProviderService(provider); |
| 495 | return providerService; |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void unregister(HostProvider provider) { |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public Set<ProviderId> getProviders() { |
| 504 | return null; |
| 505 | } |
| 506 | |
| 507 | } |
| 508 | |
| 509 | private class TestHostProviderService |
| 510 | extends AbstractProviderService<HostProvider> |
| 511 | implements HostProviderService { |
| 512 | |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 513 | List<HostDescription> descriptions = Lists.newArrayList(); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 514 | int hostRemoveCount; |
| 515 | int ipRemoveCount; |
| 516 | int locationRemoveCount; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 517 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 518 | public void clear() { |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 519 | descriptions.clear(); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 520 | hostRemoveCount = 0; |
| 521 | ipRemoveCount = 0; |
| 522 | locationRemoveCount = 0; |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 523 | } |
| 524 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 525 | protected TestHostProviderService(HostProvider provider) { |
| 526 | super(provider); |
| 527 | } |
| 528 | |
| 529 | @Override |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 530 | public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) { |
Charles Chan | 420e737 | 2016-11-17 10:58:17 -0800 | [diff] [blame] | 531 | descriptions.add(hostDescription); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void hostVanished(HostId hostId) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 536 | hostRemoveCount++; |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 537 | } |
| 538 | |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 539 | @Override |
| 540 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 541 | ipRemoveCount++; |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void removeLocationFromHost(HostId hostId, HostLocation location) { |
| 546 | locationRemoveCount++; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 547 | } |
| 548 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 551 | private class TestPacketService extends PacketServiceAdapter { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 552 | @Override |
| 553 | public void addProcessor(PacketProcessor processor, int priority) { |
| 554 | testProcessor = processor; |
| 555 | } |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 556 | } |
| 557 | |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 558 | private class TestTopologyService extends TopologyServiceAdapter { |
| 559 | @Override |
| 560 | public boolean isInfrastructure(Topology topology, |
| 561 | ConnectPoint connectPoint) { |
| 562 | //simulate DPID3 as an infrastructure switch |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 563 | if ((connectPoint.deviceId()).equals(deviceId(DEV3)) || |
| 564 | connectPoint.deviceId().equals(deviceId(DEV6))) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 571 | /** |
| 572 | * Generates ARP packet. |
| 573 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 574 | private class TestArpPacketContext extends PacketContextAdapter { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 575 | private final String deviceId; |
| 576 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 577 | public TestArpPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 578 | super(0, null, null, false); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 579 | this.deviceId = deviceId; |
| 580 | } |
| 581 | |
| 582 | @Override |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 583 | public InboundPacket inPacket() { |
| 584 | ARP arp = new ARP(); |
| 585 | arp.setSenderProtocolAddress(IP) |
| 586 | .setSenderHardwareAddress(MAC.toBytes()) |
| 587 | .setTargetHardwareAddress(BCMAC.toBytes()) |
| 588 | .setTargetProtocolAddress(IP); |
| 589 | |
| 590 | Ethernet eth = new Ethernet(); |
| 591 | eth.setEtherType(Ethernet.TYPE_ARP) |
| 592 | .setVlanID(VLAN.toShort()) |
| 593 | .setSourceMACAddress(MAC.toBytes()) |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 594 | .setDestinationMACAddress(BCMAC) |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 595 | .setPayload(arp); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 596 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 597 | portNumber(INPORT)); |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 598 | return new DefaultInboundPacket(receivedFrom, eth, |
| 599 | ByteBuffer.wrap(eth.serialize())); |
| 600 | } |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 601 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 602 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 603 | /** |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 604 | * Generates IPv4 Unicast packet. |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 605 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 606 | private class TestIpv4PacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 607 | private final String deviceId; |
| 608 | |
| 609 | public TestIpv4PacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 610 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 611 | this.deviceId = deviceId; |
| 612 | } |
| 613 | |
| 614 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 615 | public InboundPacket inPacket() { |
| 616 | IPv4 ipv4 = new IPv4(); |
| 617 | ipv4.setDestinationAddress("10.0.0.1"); |
| 618 | ipv4.setSourceAddress(IP_ADDRESS.toString()); |
| 619 | Ethernet eth = new Ethernet(); |
| 620 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 621 | .setVlanID(VLAN.toShort()) |
| 622 | .setSourceMACAddress(MAC) |
| 623 | .setDestinationMACAddress(MacAddress.valueOf("00:00:00:00:00:01")) |
| 624 | .setPayload(ipv4); |
| 625 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 626 | portNumber(INPORT)); |
| 627 | return new DefaultInboundPacket(receivedFrom, eth, |
| 628 | ByteBuffer.wrap(eth.serialize())); |
| 629 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 630 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 631 | /** |
| 632 | * Generates DHCP REQUEST packet. |
| 633 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 634 | private class TestDhcpRequestPacketContext extends PacketContextAdapter { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 635 | private final String deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 636 | private final VlanId vlanId; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 637 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 638 | public TestDhcpRequestPacketContext(String deviceId, VlanId vlanId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 639 | super(0, null, null, false); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 640 | this.deviceId = deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 641 | this.vlanId = vlanId; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | @Override |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 645 | public InboundPacket inPacket() { |
| 646 | byte[] dhcpMsgType = new byte[1]; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 647 | dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPREQUEST.getValue(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 648 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 649 | DhcpOption dhcpOption = new DhcpOption(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 650 | dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 651 | dhcpOption.setData(dhcpMsgType); |
| 652 | dhcpOption.setLength((byte) 1); |
| 653 | DHCP dhcp = new DHCP(); |
| 654 | dhcp.setOptions(Collections.singletonList(dhcpOption)); |
| 655 | dhcp.setClientHardwareAddress(MAC.toBytes()); |
| 656 | UDP udp = new UDP(); |
| 657 | udp.setPayload(dhcp); |
| 658 | udp.setSourcePort(UDP.DHCP_CLIENT_PORT); |
| 659 | udp.setDestinationPort(UDP.DHCP_SERVER_PORT); |
| 660 | IPv4 ipv4 = new IPv4(); |
| 661 | ipv4.setPayload(udp); |
| 662 | ipv4.setDestinationAddress(IP_ADDRESS3.toString()); |
| 663 | ipv4.setSourceAddress(IP_ADDRESS.toString()); |
| 664 | Ethernet eth = new Ethernet(); |
| 665 | eth.setEtherType(Ethernet.TYPE_IPV4) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 666 | .setVlanID(this.vlanId.toShort()) |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 667 | .setSourceMACAddress(MAC) |
| 668 | .setDestinationMACAddress(MAC3) |
| 669 | .setPayload(ipv4); |
| 670 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 671 | portNumber(INPORT)); |
| 672 | return new DefaultInboundPacket(receivedFrom, eth, |
| 673 | ByteBuffer.wrap(eth.serialize())); |
| 674 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Generates DHCP ACK packet. |
| 679 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 680 | private class TestDhcpAckPacketContext extends PacketContextAdapter { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 681 | private final String deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 682 | private final boolean withRelayinfo; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 683 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 684 | public TestDhcpAckPacketContext(String deviceId, boolean withRelayInfo) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 685 | super(0, null, null, false); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 686 | this.deviceId = deviceId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 687 | this.withRelayinfo = withRelayInfo; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | @Override |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 691 | public InboundPacket inPacket() { |
| 692 | byte[] dhcpMsgType = new byte[1]; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 693 | dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPACK.getValue(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 694 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 695 | DhcpOption dhcpOption = new DhcpOption(); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 696 | dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 697 | dhcpOption.setData(dhcpMsgType); |
| 698 | dhcpOption.setLength((byte) 1); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 699 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 700 | DHCP dhcp = new DHCP(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 701 | |
| 702 | if (withRelayinfo) { |
| 703 | CircuitId cid = new CircuitId(LOCATION.toString(), VLAN_100); |
| 704 | byte[] circuitId = cid.serialize(); |
| 705 | DhcpOption circuitIdSubOption = new DhcpOption(); |
| 706 | circuitIdSubOption.setCode(DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID.getValue()); |
| 707 | circuitIdSubOption.setData(circuitId); |
| 708 | circuitIdSubOption.setLength((byte) circuitId.length); |
| 709 | |
| 710 | DhcpRelayAgentOption relayInfoOption = new DhcpRelayAgentOption(); |
| 711 | relayInfoOption.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 712 | relayInfoOption.addSubOption(circuitIdSubOption); |
| 713 | dhcp.setOptions(ImmutableList.of(dhcpOption, relayInfoOption)); |
| 714 | dhcp.setGatewayIPAddress(GW_IFACE_ADDR.ipAddress().getIp4Address().toInt()); |
| 715 | } else { |
| 716 | dhcp.setOptions(ImmutableList.of(dhcpOption)); |
| 717 | } |
| 718 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 719 | dhcp.setClientHardwareAddress(MAC.toBytes()); |
| 720 | dhcp.setYourIPAddress(IP_ADDRESS.getIp4Address().toInt()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 721 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 722 | UDP udp = new UDP(); |
| 723 | udp.setPayload(dhcp); |
| 724 | udp.setSourcePort(UDP.DHCP_SERVER_PORT); |
| 725 | udp.setDestinationPort(UDP.DHCP_CLIENT_PORT); |
| 726 | IPv4 ipv4 = new IPv4(); |
| 727 | ipv4.setPayload(udp); |
| 728 | ipv4.setDestinationAddress(IP_ADDRESS.toString()); |
| 729 | ipv4.setSourceAddress(IP_ADDRESS3.toString()); |
| 730 | Ethernet eth = new Ethernet(); |
| 731 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 732 | .setVlanID(VLAN.toShort()) |
| 733 | .setSourceMACAddress(MAC3) |
| 734 | .setDestinationMACAddress(MAC) |
| 735 | .setPayload(ipv4); |
| 736 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 737 | portNumber(INPORT2)); |
| 738 | return new DefaultInboundPacket(receivedFrom, eth, |
| 739 | ByteBuffer.wrap(eth.serialize())); |
| 740 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 741 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 742 | |
| 743 | /** |
| 744 | * Generates NeighborAdvertisement packet. |
| 745 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 746 | private class TestNaPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 747 | private final String deviceId; |
| 748 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 749 | public TestNaPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 750 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 751 | this.deviceId = deviceId; |
| 752 | } |
| 753 | |
| 754 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 755 | public InboundPacket inPacket() { |
| 756 | NeighborAdvertisement na = new NeighborAdvertisement(); |
| 757 | ICMP6 icmp6 = new ICMP6(); |
| 758 | icmp6.setPayload(na); |
| 759 | IPv6 ipv6 = new IPv6(); |
| 760 | ipv6.setPayload(icmp6); |
| 761 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 762 | ipv6.setSourceAddress(IP2); |
| 763 | Ethernet eth = new Ethernet(); |
| 764 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 765 | .setVlanID(VLAN.toShort()) |
| 766 | .setSourceMACAddress(MAC2.toBytes()) |
| 767 | .setDestinationMACAddress(BCMAC2) |
| 768 | .setPayload(ipv6); |
| 769 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 770 | portNumber(INPORT)); |
| 771 | return new DefaultInboundPacket(receivedFrom, eth, |
| 772 | ByteBuffer.wrap(eth.serialize())); |
| 773 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Generates NeighborSolicitation packet. |
| 778 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 779 | private class TestNsPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 780 | private final String deviceId; |
| 781 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 782 | public TestNsPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 783 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 784 | this.deviceId = deviceId; |
| 785 | } |
| 786 | |
| 787 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 788 | public InboundPacket inPacket() { |
| 789 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 790 | ICMP6 icmp6 = new ICMP6(); |
| 791 | icmp6.setPayload(ns); |
| 792 | IPv6 ipv6 = new IPv6(); |
| 793 | ipv6.setPayload(icmp6); |
| 794 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1:ff00:0000").toOctets()); |
| 795 | ipv6.setSourceAddress(IP2); |
| 796 | Ethernet eth = new Ethernet(); |
| 797 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 798 | .setVlanID(VLAN.toShort()) |
| 799 | .setSourceMACAddress(MAC2.toBytes()) |
| 800 | .setDestinationMACAddress(BCMAC2) |
| 801 | .setPayload(ipv6); |
| 802 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 803 | portNumber(INPORT)); |
| 804 | return new DefaultInboundPacket(receivedFrom, eth, |
| 805 | ByteBuffer.wrap(eth.serialize())); |
| 806 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Generates Duplicate Address Detection packet. |
| 811 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 812 | private class TestDadPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 813 | private final String deviceId; |
| 814 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 815 | public TestDadPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 816 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 817 | this.deviceId = deviceId; |
| 818 | } |
| 819 | |
| 820 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 821 | public InboundPacket inPacket() { |
| 822 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 823 | ICMP6 icmp6 = new ICMP6(); |
| 824 | icmp6.setPayload(ns); |
| 825 | IPv6 ipv6 = new IPv6(); |
| 826 | ipv6.setPayload(icmp6); |
| 827 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 828 | ipv6.setSourceAddress(Ip6Address.valueOf("::").toOctets()); |
| 829 | Ethernet eth = new Ethernet(); |
| 830 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 831 | .setVlanID(VLAN.toShort()) |
| 832 | .setSourceMACAddress(MAC2.toBytes()) |
| 833 | .setDestinationMACAddress(BCMAC2) |
| 834 | .setPayload(ipv6); |
| 835 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 836 | portNumber(INPORT)); |
| 837 | return new DefaultInboundPacket(receivedFrom, eth, |
| 838 | ByteBuffer.wrap(eth.serialize())); |
| 839 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Generates Router Solicitation packet. |
| 844 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 845 | private class TestRSPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 846 | private final String deviceId; |
| 847 | |
| 848 | public TestRSPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 849 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 850 | this.deviceId = deviceId; |
| 851 | } |
| 852 | |
| 853 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 854 | public InboundPacket inPacket() { |
| 855 | RouterSolicitation ns = new RouterSolicitation(); |
| 856 | ICMP6 icmp6 = new ICMP6(); |
| 857 | icmp6.setPayload(ns); |
| 858 | IPv6 ipv6 = new IPv6(); |
| 859 | ipv6.setPayload(icmp6); |
| 860 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::2").toOctets()); |
| 861 | ipv6.setSourceAddress(Ip6Address.valueOf("::").toOctets()); |
| 862 | Ethernet eth = new Ethernet(); |
| 863 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 864 | .setVlanID(VLAN.toShort()) |
| 865 | .setSourceMACAddress(MAC2.toBytes()) |
| 866 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:02")) |
| 867 | .setPayload(ipv6); |
| 868 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 869 | portNumber(INPORT)); |
| 870 | return new DefaultInboundPacket(receivedFrom, eth, |
| 871 | ByteBuffer.wrap(eth.serialize())); |
| 872 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Generates Router Advertisement packet. |
| 877 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 878 | private class TestRAPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 879 | private final String deviceId; |
| 880 | |
| 881 | public TestRAPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 882 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 883 | this.deviceId = deviceId; |
| 884 | } |
| 885 | |
| 886 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 887 | public InboundPacket inPacket() { |
| 888 | RouterAdvertisement ns = new RouterAdvertisement(); |
| 889 | ICMP6 icmp6 = new ICMP6(); |
| 890 | icmp6.setPayload(ns); |
| 891 | IPv6 ipv6 = new IPv6(); |
| 892 | ipv6.setPayload(icmp6); |
| 893 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 894 | ipv6.setSourceAddress(IP2); |
| 895 | Ethernet eth = new Ethernet(); |
| 896 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 897 | .setVlanID(VLAN.toShort()) |
| 898 | .setSourceMACAddress(MAC2.toBytes()) |
| 899 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:01")) |
| 900 | .setPayload(ipv6); |
| 901 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 902 | portNumber(INPORT)); |
| 903 | return new DefaultInboundPacket(receivedFrom, eth, |
| 904 | ByteBuffer.wrap(eth.serialize())); |
| 905 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Generates IPv6 Multicast packet. |
| 910 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 911 | private class TestIpv6McastPacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 912 | private final String deviceId; |
| 913 | |
| 914 | public TestIpv6McastPacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 915 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 916 | this.deviceId = deviceId; |
| 917 | } |
| 918 | |
| 919 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 920 | public InboundPacket inPacket() { |
| 921 | IPv6 ipv6 = new IPv6(); |
| 922 | ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets()); |
| 923 | ipv6.setSourceAddress(IP2); |
| 924 | Ethernet eth = new Ethernet(); |
| 925 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 926 | .setVlanID(VLAN.toShort()) |
| 927 | .setSourceMACAddress(MAC2.toBytes()) |
| 928 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:01")) |
| 929 | .setPayload(ipv6); |
| 930 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 931 | portNumber(INPORT)); |
| 932 | return new DefaultInboundPacket(receivedFrom, eth, |
| 933 | ByteBuffer.wrap(eth.serialize())); |
| 934 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Generates IPv6 Unicast packet. |
| 939 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 940 | private class TestIpv6PacketContext extends PacketContextAdapter { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 941 | private final String deviceId; |
| 942 | |
| 943 | public TestIpv6PacketContext(String deviceId) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 944 | super(0, null, null, false); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 945 | this.deviceId = deviceId; |
| 946 | } |
| 947 | |
| 948 | @Override |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 949 | public InboundPacket inPacket() { |
| 950 | IPv6 ipv6 = new IPv6(); |
| 951 | ipv6.setDestinationAddress(Ip6Address.valueOf("1000::1").toOctets()); |
| 952 | ipv6.setSourceAddress(IP2); |
| 953 | Ethernet eth = new Ethernet(); |
| 954 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 955 | .setVlanID(VLAN.toShort()) |
| 956 | .setSourceMACAddress(MAC2) |
| 957 | .setDestinationMACAddress(MacAddress.valueOf("00:00:00:00:00:01")) |
| 958 | .setPayload(ipv6); |
| 959 | ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId), |
| 960 | portNumber(INPORT)); |
| 961 | return new DefaultInboundPacket(receivedFrom, eth, |
| 962 | ByteBuffer.wrap(eth.serialize())); |
| 963 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 966 | private class TestDeviceService extends DeviceServiceAdapter { |
| 967 | private DeviceListener listener; |
| 968 | |
| 969 | @Override |
| 970 | public void addListener(DeviceListener listener) { |
| 971 | this.listener = listener; |
| 972 | } |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 973 | |
| 974 | @Override |
| 975 | public Iterable<Device> getDevices() { |
| 976 | return Collections.emptyList(); |
| 977 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | private class TestHostService extends HostServiceAdapter { |
| 981 | @Override |
| 982 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 983 | ConnectPoint cp1 = new ConnectPoint(deviceId(DEV1), portNumber(INPORT)); |
| 984 | ConnectPoint cp2 = new ConnectPoint(deviceId(DEV4), portNumber(INPORT)); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 985 | ConnectPoint cp3 = new ConnectPoint(deviceId(DEV1), portNumber(INPORT2)); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 986 | if (connectPoint.equals(cp1)) { |
| 987 | return ImmutableSet.of(HOST); |
| 988 | } else if (connectPoint.equals(cp2)) { |
| 989 | return ImmutableSet.of(HOST2); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 990 | } else if (connectPoint.equals(cp3)) { |
| 991 | return ImmutableSet.of(HOST3); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 992 | } else { |
| 993 | return ImmutableSet.of(); |
| 994 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | @Override |
| 998 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 999 | if (deviceId.equals(deviceId(DEV1))) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1000 | return ImmutableSet.of(HOST, HOST3); |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 1001 | } else if (deviceId.equals(deviceId(DEV4))) { |
| 1002 | return ImmutableSet.of(HOST2); |
| 1003 | } else { |
| 1004 | return ImmutableSet.of(); |
| 1005 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1008 | @Override |
| 1009 | public Host getHost(HostId hostId) { |
| 1010 | if (hostId.equals(HostId.hostId(MAC, VLAN))) { |
| 1011 | return HOST; |
| 1012 | } else if (hostId.equals(HostId.hostId(MAC2, VLAN))) { |
| 1013 | return HOST2; |
| 1014 | } else if (hostId.equals(HostId.hostId(MAC3, VLAN))) { |
| 1015 | return HOST3; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 1016 | } else if (hostId.equals(HostId.hostId(MAC, VLAN_100))) { |
| 1017 | return HOST_VLAN_100; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1018 | } |
| 1019 | return null; |
| 1020 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 1021 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 1022 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame^] | 1023 | private class TestInterfaceService implements InterfaceService { |
| 1024 | @Override |
| 1025 | public Set<Interface> getInterfaces() { |
| 1026 | return null; |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
| 1030 | public Interface getInterfaceByName(ConnectPoint connectPoint, String name) { |
| 1031 | return null; |
| 1032 | } |
| 1033 | |
| 1034 | @Override |
| 1035 | public Set<Interface> getInterfacesByPort(ConnectPoint port) { |
| 1036 | return null; |
| 1037 | } |
| 1038 | |
| 1039 | public Set<Interface> getInterfacesByIp(IpAddress ip) { |
| 1040 | if (ip.equals(GW_IFACE_ADDR.ipAddress())) { |
| 1041 | return ImmutableSet.of(GW_IFACE); |
| 1042 | } else { |
| 1043 | return ImmutableSet.of(); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | @Override |
| 1048 | public Set<Interface> getInterfacesByVlan(VlanId vlan) { |
| 1049 | return null; |
| 1050 | } |
| 1051 | |
| 1052 | @Override |
| 1053 | public Interface getMatchingInterface(IpAddress ip) { |
| 1054 | return null; |
| 1055 | } |
| 1056 | |
| 1057 | @Override |
| 1058 | public Set<Interface> getMatchingInterfaces(IpAddress ip) { |
| 1059 | return null; |
| 1060 | } |
| 1061 | |
| 1062 | @Override |
| 1063 | public void addListener(InterfaceListener listener) { |
| 1064 | |
| 1065 | } |
| 1066 | |
| 1067 | @Override |
| 1068 | public void removeListener(InterfaceListener listener) { |
| 1069 | |
| 1070 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 1071 | } |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 1072 | } |