blob: 89a6ef820c35b33f257f5ae7e558dcedef588687 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.host.impl;
alshabib4a179dc2014-10-17 17:17:01 -070017
Yi Tseng7a38f9a2017-06-09 14:36:40 -070018import com.google.common.collect.ImmutableList;
Thomas Vachuska27bee092015-06-23 19:03:10 -070019import com.google.common.collect.ImmutableSet;
Charles Chan420e7372016-11-17 10:58:17 -080020import com.google.common.collect.Lists;
Madan Jampania3770c32015-12-11 12:07:41 -080021import com.google.common.util.concurrent.MoreExecutors;
alshabib4a179dc2014-10-17 17:17:01 -070022import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
Charles Chan07f15f22018-05-08 21:35:50 -070025import org.onlab.junit.TestTools;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070026import org.onlab.junit.TestUtils;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080027import org.onlab.osgi.ComponentContextAdapter;
28import org.onlab.packet.ARP;
29import org.onlab.packet.ChassisId;
Charles Chane6067892016-11-17 10:23:46 -080030import org.onlab.packet.DHCP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070031import org.onlab.packet.DHCP6;
32import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
33import org.onlab.packet.dhcp.Dhcp6Duid;
34import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
35import org.onlab.packet.dhcp.Dhcp6IaNaOption;
36import org.onlab.packet.dhcp.Dhcp6Option;
Yi Tsengc7403c22017-06-19 16:23:22 -070037import org.onlab.packet.dhcp.DhcpOption;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080038import org.onlab.packet.Ethernet;
Charles Chan5d5e0622015-09-25 13:00:06 -070039import org.onlab.packet.ICMP6;
40import org.onlab.packet.IPv4;
41import org.onlab.packet.IPv6;
42import org.onlab.packet.Ip6Address;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080043import org.onlab.packet.IpAddress;
44import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080045import org.onlab.packet.UDP;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080046import org.onlab.packet.VlanId;
Charles Chan5d5e0622015-09-25 13:00:06 -070047import org.onlab.packet.ndp.NeighborAdvertisement;
48import org.onlab.packet.ndp.NeighborSolicitation;
49import org.onlab.packet.ndp.RouterAdvertisement;
50import org.onlab.packet.ndp.RouterSolicitation;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070051import org.onosproject.cfg.ComponentConfigAdapter;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080052import org.onosproject.core.ApplicationId;
53import org.onosproject.core.CoreService;
54import org.onosproject.core.DefaultApplicationId;
Ray Milkeyfacf2862017-08-03 11:58:29 -070055import org.onosproject.net.intf.Interface;
56import org.onosproject.net.intf.InterfaceListener;
57import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080058import org.onosproject.net.ConnectPoint;
59import org.onosproject.net.DefaultDevice;
60import org.onosproject.net.DefaultHost;
61import org.onosproject.net.DefaultPort;
62import org.onosproject.net.Device;
63import org.onosproject.net.DeviceId;
64import org.onosproject.net.Host;
65import org.onosproject.net.HostId;
66import org.onosproject.net.HostLocation;
67import org.onosproject.net.device.DeviceEvent;
68import org.onosproject.net.device.DeviceListener;
69import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.host.HostDescription;
71import org.onosproject.net.host.HostProvider;
72import org.onosproject.net.host.HostProviderRegistry;
73import org.onosproject.net.host.HostProviderService;
74import org.onosproject.net.host.HostServiceAdapter;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070075import org.onosproject.net.host.InterfaceIpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080076import org.onosproject.net.packet.DefaultInboundPacket;
77import org.onosproject.net.packet.InboundPacket;
Andrey Komarov2398d962016-09-26 15:11:23 +030078import org.onosproject.net.packet.PacketContextAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080079import org.onosproject.net.packet.PacketProcessor;
Thomas Vachuska27bee092015-06-23 19:03:10 -070080import org.onosproject.net.packet.PacketServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080081import org.onosproject.net.provider.AbstractProviderService;
82import org.onosproject.net.provider.ProviderId;
83import org.onosproject.net.topology.Topology;
84import org.onosproject.net.topology.TopologyServiceAdapter;
alshabib4a179dc2014-10-17 17:17:01 -070085
Thomas Vachuska27bee092015-06-23 19:03:10 -070086import java.nio.ByteBuffer;
87import java.util.Collections;
88import java.util.Dictionary;
89import java.util.Hashtable;
Charles Chan420e7372016-11-17 10:58:17 -080090import java.util.List;
Thomas Vachuska27bee092015-06-23 19:03:10 -070091import java.util.Set;
92
Michele Santuari72f6bee2016-11-18 12:56:30 +010093import static org.easymock.EasyMock.createMock;
94import static org.easymock.EasyMock.expect;
95import static org.easymock.EasyMock.replay;
Charles Chan5d5e0622015-09-25 13:00:06 -070096import static org.hamcrest.Matchers.is;
Michele Santuari72f6bee2016-11-18 12:56:30 +010097import static org.junit.Assert.assertEquals;
98import static org.junit.Assert.assertNotEquals;
99import static org.junit.Assert.assertNotNull;
100import static org.junit.Assert.assertThat;
Charles Chan178b0f52017-10-08 23:51:22 -0400101import static org.junit.Assert.assertTrue;
Thomas Vachuska27bee092015-06-23 19:03:10 -0700102import static org.onlab.packet.VlanId.vlanId;
103import static org.onosproject.net.Device.Type.SWITCH;
104import static org.onosproject.net.DeviceId.deviceId;
105import static org.onosproject.net.HostId.hostId;
106import static org.onosproject.net.PortNumber.portNumber;
Michele Santuari72f6bee2016-11-18 12:56:30 +0100107import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
108import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
109import static org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED;
Thomas Vachuska33601602014-11-19 03:32:15 -0800110
alshabib4a179dc2014-10-17 17:17:01 -0700111public class HostLocationProviderTest {
Charles Chan8b0fdaa2016-02-23 18:35:27 -0800112 private static final ProviderId PROVIDER_ID =
113 new ProviderId("of", "org.onosproject.provider.host");
114
Charles Chan07f15f22018-05-08 21:35:50 -0700115 private static final int ASSERTION_DELAY = 100; // millis
116
alshabib4a179dc2014-10-17 17:17:01 -0700117 private static final Integer INPORT = 10;
Charles Chane6067892016-11-17 10:23:46 -0800118 private static final Integer INPORT2 = 11;
alshabib4a179dc2014-10-17 17:17:01 -0700119 private static final String DEV1 = "of:1";
120 private static final String DEV2 = "of:2";
121 private static final String DEV3 = "of:3";
Charles Chan5d5e0622015-09-25 13:00:06 -0700122 private static final String DEV4 = "of:4";
123 private static final String DEV5 = "of:5";
124 private static final String DEV6 = "of:6";
alshabib4a179dc2014-10-17 17:17:01 -0700125
Thomas Vachuska33601602014-11-19 03:32:15 -0800126 private static final VlanId VLAN = vlanId();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700127 private static final VlanId VLAN_100 = VlanId.vlanId("100");
Charles Chan5d5e0622015-09-25 13:00:06 -0700128
129 // IPv4 Host
alshabib4a179dc2014-10-17 17:17:01 -0700130 private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
131 private static final MacAddress BCMAC = MacAddress.valueOf("ff:ff:ff:ff:ff:ff");
132 private static final byte[] IP = new byte[]{10, 0, 0, 1};
Thomas Vachuska33601602014-11-19 03:32:15 -0800133 private static final IpAddress IP_ADDRESS =
134 IpAddress.valueOf(IpAddress.Version.INET, IP);
135 private static final HostLocation LOCATION =
136 new HostLocation(deviceId(DEV1), portNumber(INPORT), 0L);
Thomas Vachuska33601602014-11-19 03:32:15 -0800137 private static final DefaultHost HOST =
Charles Chan8b0fdaa2016-02-23 18:35:27 -0800138 new DefaultHost(PROVIDER_ID, hostId(MAC), MAC,
Charles Chane6067892016-11-17 10:23:46 -0800139 VLAN, LOCATION,
140 ImmutableSet.of(IP_ADDRESS));
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700141 private static final DefaultHost HOST_VLAN_100 =
142 new DefaultHost(PROVIDER_ID, hostId(MAC, VLAN_100), MAC,
143 VLAN_100, LOCATION,
144 ImmutableSet.of(IP_ADDRESS));
Thomas Vachuska33601602014-11-19 03:32:15 -0800145
Charles Chan5d5e0622015-09-25 13:00:06 -0700146 // IPv6 Host
147 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:22:00:00:02");
148 private static final MacAddress BCMAC2 = MacAddress.valueOf("33:33:00:00:00:01");
149 private static final byte[] IP2 = Ip6Address.valueOf("1000::1").toOctets();
Charles Chan178b0f52017-10-08 23:51:22 -0400150 private static final byte[] LLIP2 = IPv6.getLinkLocalAddress(MAC2.toBytes());
Charles Chan5d5e0622015-09-25 13:00:06 -0700151 private static final IpAddress IP_ADDRESS2 =
152 IpAddress.valueOf(IpAddress.Version.INET6, IP2);
Charles Chan178b0f52017-10-08 23:51:22 -0400153 private static final IpAddress LLIP_ADDRESS2 =
154 IpAddress.valueOf(IpAddress.Version.INET6, LLIP2);
Charles Chan5d5e0622015-09-25 13:00:06 -0700155 private static final HostLocation LOCATION2 =
156 new HostLocation(deviceId(DEV4), portNumber(INPORT), 0L);
157 private static final DefaultHost HOST2 =
Charles Chan8b0fdaa2016-02-23 18:35:27 -0800158 new DefaultHost(PROVIDER_ID, hostId(MAC2), MAC2,
Charles Chane6067892016-11-17 10:23:46 -0800159 VLAN, LOCATION2,
160 ImmutableSet.of(IP_ADDRESS2));
161
162 // DHCP Server
163 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:33:00:00:03");
164 private static final byte[] IP3 = new byte[]{10, 0, 0, 2};
165 private static final IpAddress IP_ADDRESS3 =
166 IpAddress.valueOf(IpAddress.Version.INET, IP3);
Charles Chane6067892016-11-17 10:23:46 -0800167 private static final HostLocation LOCATION3 =
168 new HostLocation(deviceId(DEV1), portNumber(INPORT2), 0L);
169 private static final DefaultHost HOST3 =
170 new DefaultHost(PROVIDER_ID, hostId(MAC3), MAC3,
171 VLAN, LOCATION3,
172 ImmutableSet.of(IP_ADDRESS3));
Charles Chan5d5e0622015-09-25 13:00:06 -0700173
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700174 // DHCP6 Server
175 private static final MacAddress DHCP6_SERVER_MAC = MacAddress.valueOf("00:00:44:00:00:04");
176 private static final IpAddress DHCP6_SERVER_IP =
177 IpAddress.valueOf("2000::1:1000");
178
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700179 // Gateway information for relay agent
180 private static final InterfaceIpAddress GW_IFACE_ADDR = InterfaceIpAddress.valueOf("10.0.1.1/32");
181 private static final Interface GW_IFACE = new Interface("gateway",
182 LOCATION,
183 ImmutableList.of(GW_IFACE_ADDR),
184 null,
185 VLAN_100);
186
Thomas Vachuska33601602014-11-19 03:32:15 -0800187 private static final ComponentContextAdapter CTX_FOR_REMOVE =
188 new ComponentContextAdapter() {
189 @Override
190 public Dictionary getProperties() {
191 Hashtable<String, String> props = new Hashtable<>();
192 props.put("hostRemovalEnabled", "true");
193 return props;
194 }
195 };
196
197 public static final ComponentContextAdapter CTX_FOR_NO_REMOVE =
198 new ComponentContextAdapter() {
199 @Override
200 public Dictionary getProperties() {
201 return new Hashtable();
202 }
203 };
204
alshabib4a179dc2014-10-17 17:17:01 -0700205 private final HostLocationProvider provider = new HostLocationProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800206 private final TestHostRegistry hostRegistry = new TestHostRegistry();
alshabib4a179dc2014-10-17 17:17:01 -0700207 private final TestTopologyService topoService = new TestTopologyService();
Thomas Vachuska33601602014-11-19 03:32:15 -0800208 private final TestDeviceService deviceService = new TestDeviceService();
209 private final TestHostService hostService = new TestHostService();
alshabib4a179dc2014-10-17 17:17:01 -0700210 private final TestPacketService packetService = new TestPacketService();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700211 private final TestInterfaceService interfaceService = new TestInterfaceService();
alshabib4a179dc2014-10-17 17:17:01 -0700212
213 private PacketProcessor testProcessor;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800214 private CoreService coreService;
alshabib4a179dc2014-10-17 17:17:01 -0700215 private TestHostProviderService providerService;
216
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800217 private ApplicationId appId =
218 new DefaultApplicationId(100, "org.onosproject.provider.host");
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800219
alshabib4a179dc2014-10-17 17:17:01 -0700220 @Before
221 public void setUp() {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800222 coreService = createMock(CoreService.class);
223 expect(coreService.registerApplication(appId.name()))
Thomas Vachuska27bee092015-06-23 19:03:10 -0700224 .andReturn(appId).anyTimes();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800225 replay(coreService);
226
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700227 provider.cfgService = new ComponentConfigAdapter();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800228 provider.coreService = coreService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800229
Thomas Vachuska33601602014-11-19 03:32:15 -0800230 provider.providerRegistry = hostRegistry;
alshabib4a179dc2014-10-17 17:17:01 -0700231 provider.topologyService = topoService;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800232 provider.packetService = packetService;
Thomas Vachuska33601602014-11-19 03:32:15 -0800233 provider.deviceService = deviceService;
234 provider.hostService = hostService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700235 provider.interfaceService = interfaceService;
alshabib4a179dc2014-10-17 17:17:01 -0700236
Thomas Vachuska33601602014-11-19 03:32:15 -0800237 provider.activate(CTX_FOR_NO_REMOVE);
Madan Jampania3770c32015-12-11 12:07:41 -0800238
Charles Chanff79dd92018-06-01 16:33:48 -0700239 provider.deviceEventHandler = MoreExecutors.newDirectExecutorService();
alshabib4a179dc2014-10-17 17:17:01 -0700240 }
241
242 @Test
243 public void basics() {
244 assertNotNull("registration expected", providerService);
245 assertEquals("incorrect provider", provider, providerService.provider());
246 }
247
248 @Test
249 public void events() {
Charles Chan420e7372016-11-17 10:58:17 -0800250 // New host. Expect one additional host description.
Charles Chan5d5e0622015-09-25 13:00:06 -0700251 testProcessor.process(new TestArpPacketContext(DEV1));
Charles Chan07f15f22018-05-08 21:35:50 -0700252 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("New host expected",
253 providerService.descriptions.size(), is(1)));
alshabib4a179dc2014-10-17 17:17:01 -0700254
Charles Chan420e7372016-11-17 10:58:17 -0800255 // The host moved to new switch. Expect one additional host description.
256 // The second host description should have a different location.
Charles Chan5d5e0622015-09-25 13:00:06 -0700257 testProcessor.process(new TestArpPacketContext(DEV2));
Charles Chan07f15f22018-05-08 21:35:50 -0700258 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("Host motion expected",
259 providerService.descriptions.size(), is(2)));
260 final HostLocation loc11 = providerService.descriptions.get(0).location();
261 final HostLocation loc12 = providerService.descriptions.get(1).location();
262 TestTools.assertAfter(ASSERTION_DELAY, () -> assertNotEquals("Host location should be different",
263 loc11, loc12));
alshabib4a179dc2014-10-17 17:17:01 -0700264
Charles Chan420e7372016-11-17 10:58:17 -0800265 // The host was misheard on a spine. Expect no additional host description.
Charles Chan5d5e0622015-09-25 13:00:06 -0700266 testProcessor.process(new TestArpPacketContext(DEV3));
Charles Chan07f15f22018-05-08 21:35:50 -0700267 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("Host misheard on spine switch",
268 providerService.descriptions.size(), is(2)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700269
270 providerService.clear();
271
Charles Chan178b0f52017-10-08 23:51:22 -0400272 // New host. Expect two additional host descriptions. One for target IP. One for dest IP.
Jonathan Hartb35540a2015-11-17 09:30:56 -0800273 testProcessor.process(new TestNaPacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700274 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("New host expected",
275 providerService.descriptions.size(), is(2)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700276
Charles Chan178b0f52017-10-08 23:51:22 -0400277 // The host moved to new switch. Expect two additional host descriptions.
278 // The 3rd and 4th host description should have a different location.
Jonathan Hartb35540a2015-11-17 09:30:56 -0800279 testProcessor.process(new TestNaPacketContext(DEV5));
Charles Chan07f15f22018-05-08 21:35:50 -0700280 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("Host motion expected",
281 providerService.descriptions.size(), is(4)));
282 final HostLocation loc21 = providerService.descriptions.get(0).location();
283 final HostLocation loc22 = providerService.descriptions.get(1).location();
284 final HostLocation loc23 = providerService.descriptions.get(2).location();
285 final HostLocation loc24 = providerService.descriptions.get(3).location();
286 TestTools.assertAfter(ASSERTION_DELAY, () -> assertEquals("1st and 2nd location should be equal",
287 loc21, loc22));
288 TestTools.assertAfter(ASSERTION_DELAY, () -> assertEquals("3rd and 4th location should be equal",
289 loc23, loc24));
290 TestTools.assertAfter(ASSERTION_DELAY, () -> assertNotEquals("1st and 3rd location should be different",
291 loc21, loc23));
Charles Chan5d5e0622015-09-25 13:00:06 -0700292
Charles Chan420e7372016-11-17 10:58:17 -0800293 // The host was misheard on a spine. Expect no additional host description.
Jonathan Hartb35540a2015-11-17 09:30:56 -0800294 testProcessor.process(new TestNaPacketContext(DEV6));
Charles Chan07f15f22018-05-08 21:35:50 -0700295 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("Host misheard on spine switch",
296 providerService.descriptions.size(), is(4)));
alshabib4a179dc2014-10-17 17:17:01 -0700297 }
298
Thomas Vachuska33601602014-11-19 03:32:15 -0800299 @Test
300 public void removeHostByDeviceRemove() {
301 provider.modified(CTX_FOR_REMOVE);
Charles Chan5d5e0622015-09-25 13:00:06 -0700302 testProcessor.process(new TestArpPacketContext(DEV1));
Jonathan Hartb35540a2015-11-17 09:30:56 -0800303 testProcessor.process(new TestNaPacketContext(DEV4));
Charles Chan5d5e0622015-09-25 13:00:06 -0700304
Thomas Vachuska33601602014-11-19 03:32:15 -0800305 Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH,
306 "m", "h", "s", "n", new ChassisId(0L));
307 deviceService.listener.event(new DeviceEvent(DEVICE_REMOVED, device));
Charles Chan888e20a2017-05-01 15:44:23 -0700308 assertEquals("incorrect remove count", 2, providerService.locationRemoveCount);
Charles Chan5d5e0622015-09-25 13:00:06 -0700309
310 device = new DefaultDevice(ProviderId.NONE, deviceId(DEV4), SWITCH,
311 "m", "h", "s", "n", new ChassisId(0L));
312 deviceService.listener.event(new DeviceEvent(DEVICE_REMOVED, device));
Charles Chan888e20a2017-05-01 15:44:23 -0700313 assertEquals("incorrect remove count", 3, providerService.locationRemoveCount);
Thomas Vachuska33601602014-11-19 03:32:15 -0800314 }
315
316 @Test
317 public void removeHostByDeviceOffline() {
318 provider.modified(CTX_FOR_REMOVE);
Charles Chan5d5e0622015-09-25 13:00:06 -0700319 testProcessor.process(new TestArpPacketContext(DEV1));
320 testProcessor.process(new TestArpPacketContext(DEV4));
321
Thomas Vachuska33601602014-11-19 03:32:15 -0800322 Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH,
323 "m", "h", "s", "n", new ChassisId(0L));
324 deviceService.listener.event(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device));
Charles Chan888e20a2017-05-01 15:44:23 -0700325 assertEquals("incorrect remove count", 2, providerService.locationRemoveCount);
Charles Chan5d5e0622015-09-25 13:00:06 -0700326
327 device = new DefaultDevice(ProviderId.NONE, deviceId(DEV4), SWITCH,
328 "m", "h", "s", "n", new ChassisId(0L));
329 deviceService.listener.event(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device));
Charles Chan888e20a2017-05-01 15:44:23 -0700330 assertEquals("incorrect remove count", 3, providerService.locationRemoveCount);
Thomas Vachuska33601602014-11-19 03:32:15 -0800331 }
332
333 @Test
334 public void removeHostByDevicePortDown() {
335 provider.modified(CTX_FOR_REMOVE);
Charles Chan5d5e0622015-09-25 13:00:06 -0700336 testProcessor.process(new TestArpPacketContext(DEV1));
337 testProcessor.process(new TestArpPacketContext(DEV4));
338
Thomas Vachuska33601602014-11-19 03:32:15 -0800339 Device device = new DefaultDevice(ProviderId.NONE, deviceId(DEV1), SWITCH,
340 "m", "h", "s", "n", new ChassisId(0L));
341 deviceService.listener.event(new DeviceEvent(PORT_UPDATED, device,
Charles Chan5d5e0622015-09-25 13:00:06 -0700342 new DefaultPort(device, portNumber(INPORT), false)));
Charles Chan888e20a2017-05-01 15:44:23 -0700343 assertEquals("incorrect remove count", 1, providerService.locationRemoveCount);
Charles Chan5d5e0622015-09-25 13:00:06 -0700344
345 device = new DefaultDevice(ProviderId.NONE, deviceId(DEV4), SWITCH,
346 "m", "h", "s", "n", new ChassisId(0L));
347 deviceService.listener.event(new DeviceEvent(PORT_UPDATED, device,
348 new DefaultPort(device, portNumber(INPORT), false)));
Charles Chan888e20a2017-05-01 15:44:23 -0700349 assertEquals("incorrect remove count", 2, providerService.locationRemoveCount);
Thomas Vachuska33601602014-11-19 03:32:15 -0800350 }
351
Charles Chan5d5e0622015-09-25 13:00:06 -0700352 /**
353 * When receiving ARP, updates location and IP.
354 */
355 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300356 public void receiveArp() {
Charles Chan5d5e0622015-09-25 13:00:06 -0700357 testProcessor.process(new TestArpPacketContext(DEV1));
Charles Chan07f15f22018-05-08 21:35:50 -0700358 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveArp. One host description expected",
359 providerService.descriptions.size(), is(1)));
Charles Chan420e7372016-11-17 10:58:17 -0800360 HostDescription descr = providerService.descriptions.get(0);
Charles Chan07f15f22018-05-08 21:35:50 -0700361 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.location(), is(LOCATION)));
362 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.hwAddress(), is(MAC)));
363 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.ipAddress().toArray()[0], is(IP_ADDRESS)));
364 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.vlan(), is(VLAN)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700365 }
366
367 /**
368 * When receiving IPv4, updates location only.
369 */
370 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300371 public void receiveIpv4() {
Charles Chan5d5e0622015-09-25 13:00:06 -0700372 testProcessor.process(new TestIpv4PacketContext(DEV1));
Charles Chan07f15f22018-05-08 21:35:50 -0700373 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveIpv4. One host description expected",
374 providerService.descriptions.size(), is(1)));
Charles Chan420e7372016-11-17 10:58:17 -0800375 HostDescription descr = providerService.descriptions.get(0);
Charles Chan07f15f22018-05-08 21:35:50 -0700376 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.location(), is(LOCATION)));
377 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.hwAddress(), is(MAC)));
378 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.ipAddress().size(), is(0)));
379 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.vlan(), is(VLAN)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700380 }
381
382 /**
Charles Chane6067892016-11-17 10:23:46 -0800383 * When receiving DHCP REQUEST, update MAC, location of client.
384 * When receiving DHCP ACK, update MAC, location of server and IP of client.
385 */
386 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300387 public void receiveDhcp() {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700388 TestUtils.setField(provider, "useDhcp", true);
Charles Chane6067892016-11-17 10:23:46 -0800389 // DHCP Request
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700390 testProcessor.process(new TestDhcpRequestPacketContext(DEV1, VLAN));
Charles Chan07f15f22018-05-08 21:35:50 -0700391 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveDhcpRequest. One host description expected",
392 providerService.descriptions.size(), is(1)));
Charles Chane6067892016-11-17 10:23:46 -0800393 // Should learn the MAC and location of DHCP client
394 HostDescription descr = providerService.descriptions.get(0);
Charles Chan07f15f22018-05-08 21:35:50 -0700395 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.location(), is(LOCATION)));
396 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.hwAddress(), is(MAC)));
397 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.ipAddress().size(), is(0)));
398 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.vlan(), is(VLAN)));
Charles Chane6067892016-11-17 10:23:46 -0800399
400 // DHCP Ack
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700401 testProcessor.process(new TestDhcpAckPacketContext(DEV1));
Charles Chan07f15f22018-05-08 21:35:50 -0700402 TestTools.assertAfter(ASSERTION_DELAY, () ->
403 assertThat("receiveDhcpAck. Two additional host descriptions expected",
404 providerService.descriptions.size(), is(3)));
Charles Chane6067892016-11-17 10:23:46 -0800405
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700406 // Should also learn the MAC, location of DHCP server
407 HostDescription descr2 = providerService.descriptions.get(1);
Charles Chan07f15f22018-05-08 21:35:50 -0700408 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.location(), is(LOCATION3)));
409 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.hwAddress(), is(MAC3)));
410 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.ipAddress().size(), is(0)));
411 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.vlan(), is(VLAN)));
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700412
Yi Tsengcfa637a2017-07-26 17:24:10 -0700413 // Should update the IP address of the client.
414 HostDescription descr3 = providerService.descriptions.get(2);
Charles Chan07f15f22018-05-08 21:35:50 -0700415 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.location(), is(LOCATION)));
416 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.hwAddress(), is(MAC)));
417 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.ipAddress().size(), is(1)));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700418 IpAddress ip = descr3.ipAddress().iterator().next();
Charles Chan07f15f22018-05-08 21:35:50 -0700419 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(ip, is(IP_ADDRESS.getIp4Address())));
420 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.vlan(), is(VLAN)));
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700421 }
422
423 /**
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700424 * When receiving DHCPv6 REQUEST, update MAC, location of client.
425 * When receiving DHCPv6 ACK, update MAC, location of server and IP of client.
426 */
427 @Test
428 public void receiveDhcp6() {
429 TestUtils.setField(provider, "useDhcp6", true);
430 // DHCP Request
431 testProcessor.process(new TestDhcp6RequestPacketContext(DEV4, VLAN));
Charles Chan07f15f22018-05-08 21:35:50 -0700432 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveDhcpRequest. One host description expected",
433 providerService.descriptions.size(), is(1)));
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700434 // Should learn the MAC and location of DHCP client
435 HostDescription descr = providerService.descriptions.get(0);
Charles Chan07f15f22018-05-08 21:35:50 -0700436 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.location(), is(LOCATION2)));
437 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.hwAddress(), is(MAC2)));
438 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.ipAddress().size(), is(0)));
439 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.vlan(), is(VLAN)));
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700440
441 // DHCP Ack
442 testProcessor.process(new TestDhcp6AckPacketContext(DEV1));
Charles Chan07f15f22018-05-08 21:35:50 -0700443 TestTools.assertAfter(ASSERTION_DELAY, () ->
444 assertThat("receiveDhcpAck. Two additional host descriptions expected",
445 providerService.descriptions.size(), is(3)));
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700446
447 // Should also learn the MAC, location of DHCP server
448 HostDescription descr2 = providerService.descriptions.get(1);
Charles Chan07f15f22018-05-08 21:35:50 -0700449 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.location(), is(LOCATION3)));
450 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.hwAddress(), is(DHCP6_SERVER_MAC)));
451 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.ipAddress().size(), is(0)));
452 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr2.vlan(), is(VLAN)));
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700453
454 // Should update the IP address of the DHCP client.
455 HostDescription descr3 = providerService.descriptions.get(2);
Charles Chan07f15f22018-05-08 21:35:50 -0700456 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.location(), is(LOCATION2)));
457 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.hwAddress(), is(MAC2)));
458 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.ipAddress().size(), is(1)));
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700459 IpAddress ip = descr3.ipAddress().iterator().next();
Charles Chan07f15f22018-05-08 21:35:50 -0700460 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(ip, is(IP_ADDRESS2.getIp6Address())));
461 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr3.vlan(), is(VLAN)));
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700462 }
463
464 /**
Charles Chan5d5e0622015-09-25 13:00:06 -0700465 * When receiving NeighborAdvertisement, updates location and IP.
Charles Chan178b0f52017-10-08 23:51:22 -0400466 * We should also expect that target IP is learnt.
Charles Chan5d5e0622015-09-25 13:00:06 -0700467 */
468 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300469 public void receiveNa() {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800470 testProcessor.process(new TestNaPacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700471 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveNa. One host description expected",
472 providerService.descriptions.size(), is(2)));
473 final HostDescription descr0 = providerService.descriptions.get(0);
474 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr0.location(), is(LOCATION2)));
475 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr0.hwAddress(), is(MAC2)));
476 TestTools.assertAfter(ASSERTION_DELAY, () -> assertTrue(descr0.ipAddress().contains(LLIP_ADDRESS2)));
477 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr0.vlan(), is(VLAN)));
Charles Chan178b0f52017-10-08 23:51:22 -0400478
Charles Chan07f15f22018-05-08 21:35:50 -0700479 final HostDescription descr1 = providerService.descriptions.get(1);
480 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr1.location(), is(LOCATION2)));
481 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr1.hwAddress(), is(MAC2)));
482 TestTools.assertAfter(ASSERTION_DELAY, () -> assertTrue(descr1.ipAddress().contains(IP_ADDRESS2)));
483 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr1.vlan(), is(VLAN)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700484 }
485
486 /**
487 * When receiving NeighborSolicitation, updates location and IP.
488 */
489 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300490 public void receiveNs() {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800491 testProcessor.process(new TestNsPacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700492 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveNs. One host description expected",
493 providerService.descriptions.size(), is(1)));
Charles Chan420e7372016-11-17 10:58:17 -0800494 HostDescription descr = providerService.descriptions.get(0);
Charles Chan07f15f22018-05-08 21:35:50 -0700495 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.location(), is(LOCATION2)));
496 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.hwAddress(), is(MAC2)));
497 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.ipAddress().toArray()[0], is(IP_ADDRESS2)));
498 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.vlan(), is(VLAN)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700499 }
500
501 /**
502 * When receiving RouterAdvertisement, ignores it.
503 */
504 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300505 public void receivesRa() {
Charles Chan5d5e0622015-09-25 13:00:06 -0700506 testProcessor.process(new TestRAPacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700507 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receivesRa. No host description expected",
508 providerService.descriptions.size(), is(0)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700509 }
510
511 /**
512 * When receiving RouterSolicitation, ignores it.
513 */
514 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300515 public void receiveRs() {
Charles Chan5d5e0622015-09-25 13:00:06 -0700516 testProcessor.process(new TestRSPacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700517 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveRs. No host description expected",
518 providerService.descriptions.size(), is(0)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700519 }
520
521 /**
522 * When receiving Duplicate Address Detection (DAD), ignores it.
523 */
524 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300525 public void receiveDad() {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800526 testProcessor.process(new TestDadPacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700527 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveDad. No host description expected",
528 providerService.descriptions.size(), is(0)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700529 }
530
531 /**
532 * When receiving IPv6 multicast packet, ignores it.
533 */
534 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300535 public void receiveIpv6Multicast() {
Charles Chan5d5e0622015-09-25 13:00:06 -0700536 testProcessor.process(new TestIpv6McastPacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700537 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveIpv6Multicast. No host description expected",
538 providerService.descriptions.size(), is(0)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700539 }
540
541 /**
542 * When receiving IPv6 unicast packet, updates location only.
543 */
544 @Test
Andrey Komarov2398d962016-09-26 15:11:23 +0300545 public void receiveIpv6Unicast() {
Charles Chan5d5e0622015-09-25 13:00:06 -0700546 testProcessor.process(new TestIpv6PacketContext(DEV4));
Charles Chan07f15f22018-05-08 21:35:50 -0700547 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat("receiveIpv6Unicast. One host description expected",
548 providerService.descriptions.size(), is(1)));
Charles Chan420e7372016-11-17 10:58:17 -0800549 HostDescription descr = providerService.descriptions.get(0);
Charles Chan07f15f22018-05-08 21:35:50 -0700550 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.location(), is(LOCATION2)));
551 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.hwAddress(), is(MAC2)));
552 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.ipAddress().size(), is(0)));
553 TestTools.assertAfter(ASSERTION_DELAY, () -> assertThat(descr.vlan(), is(VLAN)));
Charles Chan5d5e0622015-09-25 13:00:06 -0700554 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800555
alshabib4a179dc2014-10-17 17:17:01 -0700556 @After
557 public void tearDown() {
558 provider.deactivate();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800559 provider.coreService = null;
alshabib4a179dc2014-10-17 17:17:01 -0700560 provider.providerRegistry = null;
alshabib4a179dc2014-10-17 17:17:01 -0700561 }
562
563 private class TestHostRegistry implements HostProviderRegistry {
564
565 @Override
566 public HostProviderService register(HostProvider provider) {
567 providerService = new TestHostProviderService(provider);
568 return providerService;
569 }
570
571 @Override
572 public void unregister(HostProvider provider) {
573 }
574
575 @Override
576 public Set<ProviderId> getProviders() {
577 return null;
578 }
579
580 }
581
582 private class TestHostProviderService
583 extends AbstractProviderService<HostProvider>
584 implements HostProviderService {
585
Charles Chan420e7372016-11-17 10:58:17 -0800586 List<HostDescription> descriptions = Lists.newArrayList();
Charles Chan888e20a2017-05-01 15:44:23 -0700587 int hostRemoveCount;
588 int ipRemoveCount;
589 int locationRemoveCount;
alshabib4a179dc2014-10-17 17:17:01 -0700590
Charles Chan5d5e0622015-09-25 13:00:06 -0700591 public void clear() {
Charles Chan420e7372016-11-17 10:58:17 -0800592 descriptions.clear();
Charles Chan888e20a2017-05-01 15:44:23 -0700593 hostRemoveCount = 0;
594 ipRemoveCount = 0;
595 locationRemoveCount = 0;
Charles Chan5d5e0622015-09-25 13:00:06 -0700596 }
597
alshabib4a179dc2014-10-17 17:17:01 -0700598 protected TestHostProviderService(HostProvider provider) {
599 super(provider);
600 }
601
602 @Override
Brian O'Connorf107bd72015-09-21 15:31:03 -0700603 public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
Charles Chan420e7372016-11-17 10:58:17 -0800604 descriptions.add(hostDescription);
alshabib4a179dc2014-10-17 17:17:01 -0700605 }
606
607 @Override
608 public void hostVanished(HostId hostId) {
Charles Chan888e20a2017-05-01 15:44:23 -0700609 hostRemoveCount++;
alshabib4a179dc2014-10-17 17:17:01 -0700610 }
611
samanwita palc40e5ed2015-09-24 11:01:51 -0700612 @Override
613 public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
Charles Chan888e20a2017-05-01 15:44:23 -0700614 ipRemoveCount++;
615 }
616
617 @Override
618 public void removeLocationFromHost(HostId hostId, HostLocation location) {
619 locationRemoveCount++;
samanwita palc40e5ed2015-09-24 11:01:51 -0700620 }
621
alshabib4a179dc2014-10-17 17:17:01 -0700622 }
623
Thomas Vachuska27bee092015-06-23 19:03:10 -0700624 private class TestPacketService extends PacketServiceAdapter {
alshabib4a179dc2014-10-17 17:17:01 -0700625 @Override
626 public void addProcessor(PacketProcessor processor, int priority) {
627 testProcessor = processor;
628 }
alshabib4a179dc2014-10-17 17:17:01 -0700629 }
630
alshabib4a179dc2014-10-17 17:17:01 -0700631 private class TestTopologyService extends TopologyServiceAdapter {
632 @Override
633 public boolean isInfrastructure(Topology topology,
634 ConnectPoint connectPoint) {
635 //simulate DPID3 as an infrastructure switch
Charles Chan5d5e0622015-09-25 13:00:06 -0700636 if ((connectPoint.deviceId()).equals(deviceId(DEV3)) ||
637 connectPoint.deviceId().equals(deviceId(DEV6))) {
alshabib4a179dc2014-10-17 17:17:01 -0700638 return true;
639 }
640 return false;
641 }
642 }
643
Charles Chan5d5e0622015-09-25 13:00:06 -0700644 /**
645 * Generates ARP packet.
646 */
Andrey Komarov2398d962016-09-26 15:11:23 +0300647 private class TestArpPacketContext extends PacketContextAdapter {
alshabib4a179dc2014-10-17 17:17:01 -0700648 private final String deviceId;
649
Charles Chan5d5e0622015-09-25 13:00:06 -0700650 public TestArpPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300651 super(0, null, null, false);
alshabib4a179dc2014-10-17 17:17:01 -0700652 this.deviceId = deviceId;
653 }
654
655 @Override
alshabib4a179dc2014-10-17 17:17:01 -0700656 public InboundPacket inPacket() {
657 ARP arp = new ARP();
658 arp.setSenderProtocolAddress(IP)
659 .setSenderHardwareAddress(MAC.toBytes())
660 .setTargetHardwareAddress(BCMAC.toBytes())
661 .setTargetProtocolAddress(IP);
662
663 Ethernet eth = new Ethernet();
664 eth.setEtherType(Ethernet.TYPE_ARP)
665 .setVlanID(VLAN.toShort())
666 .setSourceMACAddress(MAC.toBytes())
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800667 .setDestinationMACAddress(BCMAC)
alshabib4a179dc2014-10-17 17:17:01 -0700668 .setPayload(arp);
Thomas Vachuska33601602014-11-19 03:32:15 -0800669 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
670 portNumber(INPORT));
alshabib4a179dc2014-10-17 17:17:01 -0700671 return new DefaultInboundPacket(receivedFrom, eth,
672 ByteBuffer.wrap(eth.serialize()));
673 }
alshabib4a179dc2014-10-17 17:17:01 -0700674 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800675
Charles Chan5d5e0622015-09-25 13:00:06 -0700676 /**
Charles Chane6067892016-11-17 10:23:46 -0800677 * Generates IPv4 Unicast packet.
Charles Chan5d5e0622015-09-25 13:00:06 -0700678 */
Andrey Komarov2398d962016-09-26 15:11:23 +0300679 private class TestIpv4PacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -0700680 private final String deviceId;
681
682 public TestIpv4PacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300683 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -0700684 this.deviceId = deviceId;
685 }
686
687 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -0700688 public InboundPacket inPacket() {
689 IPv4 ipv4 = new IPv4();
690 ipv4.setDestinationAddress("10.0.0.1");
691 ipv4.setSourceAddress(IP_ADDRESS.toString());
692 Ethernet eth = new Ethernet();
693 eth.setEtherType(Ethernet.TYPE_IPV4)
694 .setVlanID(VLAN.toShort())
695 .setSourceMACAddress(MAC)
696 .setDestinationMACAddress(MacAddress.valueOf("00:00:00:00:00:01"))
697 .setPayload(ipv4);
698 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
699 portNumber(INPORT));
700 return new DefaultInboundPacket(receivedFrom, eth,
701 ByteBuffer.wrap(eth.serialize()));
702 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700703 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700704
Charles Chane6067892016-11-17 10:23:46 -0800705 /**
706 * Generates DHCP REQUEST packet.
707 */
Andrey Komarov2398d962016-09-26 15:11:23 +0300708 private class TestDhcpRequestPacketContext extends PacketContextAdapter {
Charles Chane6067892016-11-17 10:23:46 -0800709 private final String deviceId;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700710 private final VlanId vlanId;
Charles Chane6067892016-11-17 10:23:46 -0800711
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700712 public TestDhcpRequestPacketContext(String deviceId, VlanId vlanId) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300713 super(0, null, null, false);
Charles Chane6067892016-11-17 10:23:46 -0800714 this.deviceId = deviceId;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700715 this.vlanId = vlanId;
Charles Chane6067892016-11-17 10:23:46 -0800716 }
717
718 @Override
Charles Chane6067892016-11-17 10:23:46 -0800719 public InboundPacket inPacket() {
720 byte[] dhcpMsgType = new byte[1];
Yi Tsengc7403c22017-06-19 16:23:22 -0700721 dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPREQUEST.getValue();
Charles Chane6067892016-11-17 10:23:46 -0800722
Yi Tsengc7403c22017-06-19 16:23:22 -0700723 DhcpOption dhcpOption = new DhcpOption();
Charles Chane6067892016-11-17 10:23:46 -0800724 dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue());
725 dhcpOption.setData(dhcpMsgType);
726 dhcpOption.setLength((byte) 1);
727 DHCP dhcp = new DHCP();
728 dhcp.setOptions(Collections.singletonList(dhcpOption));
729 dhcp.setClientHardwareAddress(MAC.toBytes());
730 UDP udp = new UDP();
731 udp.setPayload(dhcp);
732 udp.setSourcePort(UDP.DHCP_CLIENT_PORT);
733 udp.setDestinationPort(UDP.DHCP_SERVER_PORT);
734 IPv4 ipv4 = new IPv4();
735 ipv4.setPayload(udp);
736 ipv4.setDestinationAddress(IP_ADDRESS3.toString());
737 ipv4.setSourceAddress(IP_ADDRESS.toString());
738 Ethernet eth = new Ethernet();
739 eth.setEtherType(Ethernet.TYPE_IPV4)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700740 .setVlanID(this.vlanId.toShort())
Charles Chane6067892016-11-17 10:23:46 -0800741 .setSourceMACAddress(MAC)
742 .setDestinationMACAddress(MAC3)
743 .setPayload(ipv4);
744 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
745 portNumber(INPORT));
746 return new DefaultInboundPacket(receivedFrom, eth,
747 ByteBuffer.wrap(eth.serialize()));
748 }
Charles Chane6067892016-11-17 10:23:46 -0800749 }
750
751 /**
752 * Generates DHCP ACK packet.
753 */
Andrey Komarov2398d962016-09-26 15:11:23 +0300754 private class TestDhcpAckPacketContext extends PacketContextAdapter {
Charles Chane6067892016-11-17 10:23:46 -0800755 private final String deviceId;
756
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700757 public TestDhcpAckPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300758 super(0, null, null, false);
Charles Chane6067892016-11-17 10:23:46 -0800759 this.deviceId = deviceId;
760 }
761
762 @Override
Charles Chane6067892016-11-17 10:23:46 -0800763 public InboundPacket inPacket() {
764 byte[] dhcpMsgType = new byte[1];
Yi Tsengc7403c22017-06-19 16:23:22 -0700765 dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPACK.getValue();
Charles Chane6067892016-11-17 10:23:46 -0800766
Yi Tsengc7403c22017-06-19 16:23:22 -0700767 DhcpOption dhcpOption = new DhcpOption();
Charles Chane6067892016-11-17 10:23:46 -0800768 dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue());
769 dhcpOption.setData(dhcpMsgType);
770 dhcpOption.setLength((byte) 1);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700771
Charles Chane6067892016-11-17 10:23:46 -0800772 DHCP dhcp = new DHCP();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700773 dhcp.setOptions(ImmutableList.of(dhcpOption));
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700774
Charles Chane6067892016-11-17 10:23:46 -0800775 dhcp.setClientHardwareAddress(MAC.toBytes());
776 dhcp.setYourIPAddress(IP_ADDRESS.getIp4Address().toInt());
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700777
Charles Chane6067892016-11-17 10:23:46 -0800778 UDP udp = new UDP();
779 udp.setPayload(dhcp);
780 udp.setSourcePort(UDP.DHCP_SERVER_PORT);
781 udp.setDestinationPort(UDP.DHCP_CLIENT_PORT);
782 IPv4 ipv4 = new IPv4();
783 ipv4.setPayload(udp);
784 ipv4.setDestinationAddress(IP_ADDRESS.toString());
785 ipv4.setSourceAddress(IP_ADDRESS3.toString());
786 Ethernet eth = new Ethernet();
787 eth.setEtherType(Ethernet.TYPE_IPV4)
788 .setVlanID(VLAN.toShort())
789 .setSourceMACAddress(MAC3)
790 .setDestinationMACAddress(MAC)
791 .setPayload(ipv4);
792 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
793 portNumber(INPORT2));
794 return new DefaultInboundPacket(receivedFrom, eth,
795 ByteBuffer.wrap(eth.serialize()));
796 }
Charles Chane6067892016-11-17 10:23:46 -0800797 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700798
799 /**
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700800 * Generates DHCPv6 REQUEST packet.
801 */
802 private class TestDhcp6RequestPacketContext extends PacketContextAdapter {
803 private final String deviceId;
804 private final VlanId vlanId;
805
806 public TestDhcp6RequestPacketContext(String deviceId, VlanId vlanId) {
807 super(0, null, null, false);
808 this.deviceId = deviceId;
809 this.vlanId = vlanId;
810 }
811
812 @Override
813 public InboundPacket inPacket() {
814
815 DHCP6 dhcp6 = new DHCP6();
816 dhcp6.setMsgType(DHCP6.MsgType.REQUEST.value());
817 List<Dhcp6Option> options = Lists.newArrayList();
818
819 // IA address
820 Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
821 iaAddressOption.setIp6Address(IP_ADDRESS2.getIp6Address());
822
823 // IA NA
824 Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
825 iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
826 options.add(iaNaOption);
827
828 dhcp6.setOptions(options);
829
830 UDP udp = new UDP();
831 udp.setPayload(dhcp6);
832 udp.setSourcePort(UDP.DHCP_V6_CLIENT_PORT);
833 udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
834 IPv6 ipv6 = new IPv6();
835 ipv6.setPayload(udp);
836 ipv6.setDestinationAddress(Ip6Address.ZERO.toOctets());
837 ipv6.setSourceAddress(Ip6Address.ZERO.toOctets());
838 ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
839 Ethernet eth = new Ethernet();
840 eth.setEtherType(Ethernet.TYPE_IPV6)
841 .setVlanID(this.vlanId.toShort())
842 .setSourceMACAddress(MAC2)
843 .setDestinationMACAddress(DHCP6_SERVER_MAC)
844 .setPayload(ipv6);
845 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
846 portNumber(INPORT));
847 return new DefaultInboundPacket(receivedFrom, eth,
848 ByteBuffer.wrap(eth.serialize()));
849 }
850 }
851
852 /**
853 * Generates DHCPv6 ACK packet.
854 */
855 private class TestDhcp6AckPacketContext extends PacketContextAdapter {
856 private final String deviceId;
857
858 public TestDhcp6AckPacketContext(String deviceId) {
859 super(0, null, null, false);
860 this.deviceId = deviceId;
861 }
862
863 @Override
864 public InboundPacket inPacket() {
865 DHCP6 dhcp6 = new DHCP6();
866 dhcp6.setMsgType(DHCP6.MsgType.REPLY.value());
867 List<Dhcp6Option> options = Lists.newArrayList();
868
869 // IA address
870 Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
871 iaAddressOption.setIp6Address(IP_ADDRESS2.getIp6Address());
872
873 // IA NA
874 Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
875 iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
876 options.add(iaNaOption);
877
878 // Client ID
879 Dhcp6Duid duid = new Dhcp6Duid();
880 duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
881 duid.setHardwareType((short) 1);
882 duid.setDuidTime(0);
883 duid.setLinkLayerAddress(MAC2.toBytes());
884 Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
885 clientIdOption.setDuid(duid);
886 options.add(clientIdOption);
887 dhcp6.setOptions(options);
888
889 UDP udp = new UDP();
890 udp.setPayload(dhcp6);
891 udp.setSourcePort(UDP.DHCP_V6_CLIENT_PORT);
892 udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
893 IPv6 ipv6 = new IPv6();
894 ipv6.setPayload(udp);
895 ipv6.setDestinationAddress(Ip6Address.ZERO.toOctets());
896 ipv6.setSourceAddress(Ip6Address.ZERO.toOctets());
897 ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
898 Ethernet eth = new Ethernet();
899 eth.setEtherType(Ethernet.TYPE_IPV6)
900 .setVlanID(VLAN.toShort())
901 .setSourceMACAddress(DHCP6_SERVER_MAC)
902 .setDestinationMACAddress(MAC2)
903 .setPayload(ipv6);
904 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
905 portNumber(INPORT2));
906 return new DefaultInboundPacket(receivedFrom, eth,
907 ByteBuffer.wrap(eth.serialize()));
908 }
909 }
910
911 /**
Charles Chan5d5e0622015-09-25 13:00:06 -0700912 * Generates NeighborAdvertisement packet.
913 */
Andrey Komarov2398d962016-09-26 15:11:23 +0300914 private class TestNaPacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -0700915 private final String deviceId;
916
Jonathan Hartb35540a2015-11-17 09:30:56 -0800917 public TestNaPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300918 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -0700919 this.deviceId = deviceId;
920 }
921
922 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -0700923 public InboundPacket inPacket() {
924 NeighborAdvertisement na = new NeighborAdvertisement();
Charles Chan178b0f52017-10-08 23:51:22 -0400925 na.setTargetAddress(IP2);
Charles Chan5d5e0622015-09-25 13:00:06 -0700926 ICMP6 icmp6 = new ICMP6();
927 icmp6.setPayload(na);
928 IPv6 ipv6 = new IPv6();
929 ipv6.setPayload(icmp6);
930 ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets());
Charles Chan178b0f52017-10-08 23:51:22 -0400931 ipv6.setSourceAddress(LLIP2);
Charles Chan5d5e0622015-09-25 13:00:06 -0700932 Ethernet eth = new Ethernet();
933 eth.setEtherType(Ethernet.TYPE_IPV6)
934 .setVlanID(VLAN.toShort())
935 .setSourceMACAddress(MAC2.toBytes())
936 .setDestinationMACAddress(BCMAC2)
937 .setPayload(ipv6);
938 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
939 portNumber(INPORT));
940 return new DefaultInboundPacket(receivedFrom, eth,
941 ByteBuffer.wrap(eth.serialize()));
942 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700943 }
944
945 /**
946 * Generates NeighborSolicitation packet.
947 */
Andrey Komarov2398d962016-09-26 15:11:23 +0300948 private class TestNsPacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -0700949 private final String deviceId;
950
Jonathan Hartb35540a2015-11-17 09:30:56 -0800951 public TestNsPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300952 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -0700953 this.deviceId = deviceId;
954 }
955
956 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -0700957 public InboundPacket inPacket() {
958 NeighborSolicitation ns = new NeighborSolicitation();
959 ICMP6 icmp6 = new ICMP6();
960 icmp6.setPayload(ns);
961 IPv6 ipv6 = new IPv6();
962 ipv6.setPayload(icmp6);
963 ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1:ff00:0000").toOctets());
964 ipv6.setSourceAddress(IP2);
965 Ethernet eth = new Ethernet();
966 eth.setEtherType(Ethernet.TYPE_IPV6)
967 .setVlanID(VLAN.toShort())
968 .setSourceMACAddress(MAC2.toBytes())
969 .setDestinationMACAddress(BCMAC2)
970 .setPayload(ipv6);
971 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
972 portNumber(INPORT));
973 return new DefaultInboundPacket(receivedFrom, eth,
974 ByteBuffer.wrap(eth.serialize()));
975 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700976 }
977
978 /**
979 * Generates Duplicate Address Detection packet.
980 */
Andrey Komarov2398d962016-09-26 15:11:23 +0300981 private class TestDadPacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -0700982 private final String deviceId;
983
Jonathan Hartb35540a2015-11-17 09:30:56 -0800984 public TestDadPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300985 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -0700986 this.deviceId = deviceId;
987 }
988
989 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -0700990 public InboundPacket inPacket() {
991 NeighborSolicitation ns = new NeighborSolicitation();
992 ICMP6 icmp6 = new ICMP6();
993 icmp6.setPayload(ns);
994 IPv6 ipv6 = new IPv6();
995 ipv6.setPayload(icmp6);
996 ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets());
997 ipv6.setSourceAddress(Ip6Address.valueOf("::").toOctets());
998 Ethernet eth = new Ethernet();
999 eth.setEtherType(Ethernet.TYPE_IPV6)
1000 .setVlanID(VLAN.toShort())
1001 .setSourceMACAddress(MAC2.toBytes())
1002 .setDestinationMACAddress(BCMAC2)
1003 .setPayload(ipv6);
1004 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
1005 portNumber(INPORT));
1006 return new DefaultInboundPacket(receivedFrom, eth,
1007 ByteBuffer.wrap(eth.serialize()));
1008 }
Charles Chan5d5e0622015-09-25 13:00:06 -07001009 }
1010
1011 /**
1012 * Generates Router Solicitation packet.
1013 */
Andrey Komarov2398d962016-09-26 15:11:23 +03001014 private class TestRSPacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -07001015 private final String deviceId;
1016
1017 public TestRSPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +03001018 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -07001019 this.deviceId = deviceId;
1020 }
1021
1022 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -07001023 public InboundPacket inPacket() {
1024 RouterSolicitation ns = new RouterSolicitation();
1025 ICMP6 icmp6 = new ICMP6();
1026 icmp6.setPayload(ns);
1027 IPv6 ipv6 = new IPv6();
1028 ipv6.setPayload(icmp6);
1029 ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::2").toOctets());
1030 ipv6.setSourceAddress(Ip6Address.valueOf("::").toOctets());
1031 Ethernet eth = new Ethernet();
1032 eth.setEtherType(Ethernet.TYPE_IPV6)
1033 .setVlanID(VLAN.toShort())
1034 .setSourceMACAddress(MAC2.toBytes())
1035 .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:02"))
1036 .setPayload(ipv6);
1037 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
1038 portNumber(INPORT));
1039 return new DefaultInboundPacket(receivedFrom, eth,
1040 ByteBuffer.wrap(eth.serialize()));
1041 }
Charles Chan5d5e0622015-09-25 13:00:06 -07001042 }
1043
1044 /**
1045 * Generates Router Advertisement packet.
1046 */
Andrey Komarov2398d962016-09-26 15:11:23 +03001047 private class TestRAPacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -07001048 private final String deviceId;
1049
1050 public TestRAPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +03001051 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -07001052 this.deviceId = deviceId;
1053 }
1054
1055 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -07001056 public InboundPacket inPacket() {
1057 RouterAdvertisement ns = new RouterAdvertisement();
1058 ICMP6 icmp6 = new ICMP6();
1059 icmp6.setPayload(ns);
1060 IPv6 ipv6 = new IPv6();
1061 ipv6.setPayload(icmp6);
1062 ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets());
1063 ipv6.setSourceAddress(IP2);
1064 Ethernet eth = new Ethernet();
1065 eth.setEtherType(Ethernet.TYPE_IPV6)
1066 .setVlanID(VLAN.toShort())
1067 .setSourceMACAddress(MAC2.toBytes())
1068 .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:01"))
1069 .setPayload(ipv6);
1070 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
1071 portNumber(INPORT));
1072 return new DefaultInboundPacket(receivedFrom, eth,
1073 ByteBuffer.wrap(eth.serialize()));
1074 }
Charles Chan5d5e0622015-09-25 13:00:06 -07001075 }
1076
1077 /**
1078 * Generates IPv6 Multicast packet.
1079 */
Andrey Komarov2398d962016-09-26 15:11:23 +03001080 private class TestIpv6McastPacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -07001081 private final String deviceId;
1082
1083 public TestIpv6McastPacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +03001084 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -07001085 this.deviceId = deviceId;
1086 }
1087
1088 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -07001089 public InboundPacket inPacket() {
1090 IPv6 ipv6 = new IPv6();
1091 ipv6.setDestinationAddress(Ip6Address.valueOf("ff02::1").toOctets());
1092 ipv6.setSourceAddress(IP2);
1093 Ethernet eth = new Ethernet();
1094 eth.setEtherType(Ethernet.TYPE_IPV6)
1095 .setVlanID(VLAN.toShort())
1096 .setSourceMACAddress(MAC2.toBytes())
1097 .setDestinationMACAddress(MacAddress.valueOf("33:33:00:00:00:01"))
1098 .setPayload(ipv6);
1099 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
1100 portNumber(INPORT));
1101 return new DefaultInboundPacket(receivedFrom, eth,
1102 ByteBuffer.wrap(eth.serialize()));
1103 }
Charles Chan5d5e0622015-09-25 13:00:06 -07001104 }
1105
1106 /**
1107 * Generates IPv6 Unicast packet.
1108 */
Andrey Komarov2398d962016-09-26 15:11:23 +03001109 private class TestIpv6PacketContext extends PacketContextAdapter {
Charles Chan5d5e0622015-09-25 13:00:06 -07001110 private final String deviceId;
1111
1112 public TestIpv6PacketContext(String deviceId) {
Andrey Komarov2398d962016-09-26 15:11:23 +03001113 super(0, null, null, false);
Charles Chan5d5e0622015-09-25 13:00:06 -07001114 this.deviceId = deviceId;
1115 }
1116
1117 @Override
Charles Chan5d5e0622015-09-25 13:00:06 -07001118 public InboundPacket inPacket() {
1119 IPv6 ipv6 = new IPv6();
1120 ipv6.setDestinationAddress(Ip6Address.valueOf("1000::1").toOctets());
1121 ipv6.setSourceAddress(IP2);
1122 Ethernet eth = new Ethernet();
1123 eth.setEtherType(Ethernet.TYPE_IPV6)
1124 .setVlanID(VLAN.toShort())
1125 .setSourceMACAddress(MAC2)
1126 .setDestinationMACAddress(MacAddress.valueOf("00:00:00:00:00:01"))
1127 .setPayload(ipv6);
1128 ConnectPoint receivedFrom = new ConnectPoint(deviceId(deviceId),
1129 portNumber(INPORT));
1130 return new DefaultInboundPacket(receivedFrom, eth,
1131 ByteBuffer.wrap(eth.serialize()));
1132 }
Charles Chan5d5e0622015-09-25 13:00:06 -07001133 }
1134
Thomas Vachuska33601602014-11-19 03:32:15 -08001135 private class TestDeviceService extends DeviceServiceAdapter {
1136 private DeviceListener listener;
1137
1138 @Override
1139 public void addListener(DeviceListener listener) {
1140 this.listener = listener;
1141 }
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -08001142
1143 @Override
1144 public Iterable<Device> getDevices() {
1145 return Collections.emptyList();
1146 }
Thomas Vachuska33601602014-11-19 03:32:15 -08001147 }
1148
1149 private class TestHostService extends HostServiceAdapter {
1150 @Override
1151 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Charles Chan5d5e0622015-09-25 13:00:06 -07001152 ConnectPoint cp1 = new ConnectPoint(deviceId(DEV1), portNumber(INPORT));
1153 ConnectPoint cp2 = new ConnectPoint(deviceId(DEV4), portNumber(INPORT));
Charles Chane6067892016-11-17 10:23:46 -08001154 ConnectPoint cp3 = new ConnectPoint(deviceId(DEV1), portNumber(INPORT2));
Charles Chan5d5e0622015-09-25 13:00:06 -07001155 if (connectPoint.equals(cp1)) {
1156 return ImmutableSet.of(HOST);
1157 } else if (connectPoint.equals(cp2)) {
1158 return ImmutableSet.of(HOST2);
Charles Chane6067892016-11-17 10:23:46 -08001159 } else if (connectPoint.equals(cp3)) {
1160 return ImmutableSet.of(HOST3);
Charles Chan5d5e0622015-09-25 13:00:06 -07001161 } else {
1162 return ImmutableSet.of();
1163 }
Thomas Vachuska33601602014-11-19 03:32:15 -08001164 }
1165
1166 @Override
1167 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Charles Chan5d5e0622015-09-25 13:00:06 -07001168 if (deviceId.equals(deviceId(DEV1))) {
Charles Chane6067892016-11-17 10:23:46 -08001169 return ImmutableSet.of(HOST, HOST3);
Charles Chan5d5e0622015-09-25 13:00:06 -07001170 } else if (deviceId.equals(deviceId(DEV4))) {
1171 return ImmutableSet.of(HOST2);
1172 } else {
1173 return ImmutableSet.of();
1174 }
Thomas Vachuska33601602014-11-19 03:32:15 -08001175 }
1176
Charles Chane6067892016-11-17 10:23:46 -08001177 @Override
1178 public Host getHost(HostId hostId) {
1179 if (hostId.equals(HostId.hostId(MAC, VLAN))) {
1180 return HOST;
1181 } else if (hostId.equals(HostId.hostId(MAC2, VLAN))) {
1182 return HOST2;
1183 } else if (hostId.equals(HostId.hostId(MAC3, VLAN))) {
1184 return HOST3;
Yi Tseng7a38f9a2017-06-09 14:36:40 -07001185 } else if (hostId.equals(HostId.hostId(MAC, VLAN_100))) {
1186 return HOST_VLAN_100;
Charles Chane6067892016-11-17 10:23:46 -08001187 }
1188 return null;
1189 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -07001190 }
Charles Chane6067892016-11-17 10:23:46 -08001191
Yi Tseng7a38f9a2017-06-09 14:36:40 -07001192 private class TestInterfaceService implements InterfaceService {
1193 @Override
1194 public Set<Interface> getInterfaces() {
1195 return null;
1196 }
1197
1198 @Override
1199 public Interface getInterfaceByName(ConnectPoint connectPoint, String name) {
1200 return null;
1201 }
1202
1203 @Override
1204 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
1205 return null;
1206 }
1207
Saurav Das9a554292018-04-27 18:42:30 -07001208 @Override
Yi Tseng7a38f9a2017-06-09 14:36:40 -07001209 public Set<Interface> getInterfacesByIp(IpAddress ip) {
1210 if (ip.equals(GW_IFACE_ADDR.ipAddress())) {
1211 return ImmutableSet.of(GW_IFACE);
1212 } else {
1213 return ImmutableSet.of();
1214 }
1215 }
1216
1217 @Override
1218 public Set<Interface> getInterfacesByVlan(VlanId vlan) {
1219 return null;
1220 }
1221
1222 @Override
1223 public Interface getMatchingInterface(IpAddress ip) {
1224 return null;
1225 }
1226
1227 @Override
1228 public Set<Interface> getMatchingInterfaces(IpAddress ip) {
1229 return null;
1230 }
1231
1232 @Override
1233 public void addListener(InterfaceListener listener) {
1234
1235 }
1236
1237 @Override
1238 public void removeListener(InterfaceListener listener) {
1239
1240 }
Saurav Das9a554292018-04-27 18:42:30 -07001241
1242 @Override
1243 public boolean isConfigured(ConnectPoint connectPoint) {
1244 return false;
1245 }
Thomas Vachuska33601602014-11-19 03:32:15 -08001246 }
alshabib4a179dc2014-10-17 17:17:01 -07001247}