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