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