blob: 66c9b4ed9218e67a704e16cec2caaa09f59967b8 [file] [log] [blame]
Amit Ghosh8951f042017-08-10 13:48:10 +01001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
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
7 *
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.
15 */
16package org.opencord.dhcpl2relay;
17
Amit Ghosh8951f042017-08-10 13:48:10 +010018import com.google.common.collect.ImmutableSet;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053019import com.google.common.collect.Lists;
20import org.easymock.EasyMock;
Amit Ghosh8951f042017-08-10 13:48:10 +010021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
Amit Ghosh8951f042017-08-10 13:48:10 +010024import org.onlab.osgi.ComponentContextAdapter;
25import org.onlab.packet.ChassisId;
26import org.onlab.packet.DHCP;
27import org.onlab.packet.DHCPOption;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053028import org.onlab.packet.Ethernet;
Amit Ghosh8951f042017-08-10 13:48:10 +010029import org.onlab.packet.IPv4;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053030import org.onlab.packet.Ip4Address;
31import org.onlab.packet.IpAddress;
Amit Ghosh8951f042017-08-10 13:48:10 +010032import org.onlab.packet.MacAddress;
33import org.onlab.packet.UDP;
34import org.onlab.packet.VlanId;
Amit Ghosh8951f042017-08-10 13:48:10 +010035import org.onosproject.cfg.ComponentConfigService;
36import org.onosproject.core.CoreServiceAdapter;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053037import org.onosproject.mastership.MastershipServiceAdapter;
38import org.onosproject.net.AnnotationKeys;
39import org.onosproject.net.Annotations;
Amit Ghosh8951f042017-08-10 13:48:10 +010040import org.onosproject.net.ConnectPoint;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053041import org.onosproject.net.DefaultAnnotations;
Amit Ghosh8951f042017-08-10 13:48:10 +010042import org.onosproject.net.DefaultDevice;
43import org.onosproject.net.DefaultHost;
44import org.onosproject.net.Device;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053045import org.onosproject.net.DeviceId;
Amit Ghosh8951f042017-08-10 13:48:10 +010046import org.onosproject.net.Element;
Amit Ghosh8951f042017-08-10 13:48:10 +010047import org.onosproject.net.Host;
48import org.onosproject.net.HostId;
49import org.onosproject.net.HostLocation;
Amit Ghosh8951f042017-08-10 13:48:10 +010050import org.onosproject.net.Port;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053051import org.onosproject.net.PortNumber;
52import org.onosproject.net.config.Config;
53import org.onosproject.net.config.NetworkConfigRegistryAdapter;
54import org.onosproject.net.device.DeviceServiceAdapter;
55import org.onosproject.net.host.HostServiceAdapter;
56import org.onosproject.net.provider.ProviderId;
Amit Ghosh8951f042017-08-10 13:48:10 +010057import org.opencord.dhcpl2relay.packet.DhcpOption82;
58import org.opencord.sadis.SubscriberAndDeviceInformation;
59import org.opencord.sadis.SubscriberAndDeviceInformationService;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053060import org.slf4j.Logger;
61import org.slf4j.LoggerFactory;
Amit Ghosh8951f042017-08-10 13:48:10 +010062
63import java.nio.ByteBuffer;
64import java.util.List;
65import java.util.Set;
66
Amit Ghosh8951f042017-08-10 13:48:10 +010067import static org.junit.Assert.assertEquals;
68
69public class DhcpL2RelayTest extends DhcpL2RelayTestBase {
70
71 private final Logger log = LoggerFactory.getLogger(getClass());
72
73 private DhcpL2Relay dhcpL2Relay;
74
75 private static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:00:00:00:00:01");
76 private static final VlanId CLIENT_C_TAG = VlanId.vlanId((short) 999);
77 private static final VlanId CLIENT_S_TAG = VlanId.vlanId((short) 111);
78 private static final String CLIENT_NAS_PORT_ID = "PON 1/1";
79 private static final String CLIENT_CIRCUIT_ID = "CIR-PON 1/1";
80
81 private static final String OLT_DEV_ID = "of:00000000000000aa";
82 private static final MacAddress OLT_MAC_ADDRESS = MacAddress.valueOf("01:02:03:04:05:06");
83 private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId(OLT_DEV_ID);
84
85 private static final ConnectPoint SERVER_CONNECT_POINT =
86 ConnectPoint.deviceConnectPoint("of:0000000000000001/5");
87
88 private static final String SCHEME_NAME = "dhcpl2relay";
89 private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder()
90 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build();
91
92 ComponentConfigService mockConfigService =
93 EasyMock.createMock(ComponentConfigService.class);
94
95 /**
96 * Sets up the services required by the dhcpl2relay app.
97 */
98 @Before
99 public void setUp() {
100 dhcpL2Relay = new DhcpL2Relay();
101 dhcpL2Relay.cfgService = new TestNetworkConfigRegistry();
102 dhcpL2Relay.coreService = new CoreServiceAdapter();
103 dhcpL2Relay.packetService = new MockPacketService();
104 dhcpL2Relay.componentConfigService = mockConfigService;
105 dhcpL2Relay.deviceService = new MockDeviceService();
106 dhcpL2Relay.subsService = new MockSubService();
107 dhcpL2Relay.hostService = new MockHostService();
108 dhcpL2Relay.mastershipService = new MockMastershipService();
109 dhcpL2Relay.activate(new ComponentContextAdapter());
110 }
111
112 /**
113 * Tears down the dhcpL2Relay application.
114 */
115 @After
116 public void tearDown() {
117 dhcpL2Relay.deactivate();
118 }
119
120 /**
121 * Tests the DHCP relay app by sending DHCP discovery Packet.
122 *
123 * @throws Exception when an unhandled error occurs
124 */
125 @Test
126 public void testDhcpDiscover() throws Exception {
127 // (1) Sending DHCP discover packet
128 System.out.println("Sending pakcet");
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530129 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100130
131 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
132
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530133 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100134 compareClientPackets(discoverPacket, discoverRelayed);
135 }
136
137 /**
138 * Tests the DHCP relay app by sending DHCP Request Packet.
139 *
140 * @throws Exception when an unhandled error occurs
141 */
142 @Test
143 public void testDhcpRequest() throws Exception {
144 // (1) Sending DHCP discover packet
145 System.out.println("Sending pakcet");
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530146 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100147
148 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
149
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530150 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100151 compareClientPackets(requestPacket, requestRelayed);
152 }
153
154 /**
155 * Tests the DHCP relay app by sending DHCP Offer Packet.
156 *
157 * @throws Exception when an unhandled error occurs
158 */
159 @Test
160 public void testDhcpOffer() {
161 // (1) Sending DHCP discover packet
162 System.out.println("Sending pakcet");
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530163 Ethernet offerPacket = constructDhcpOfferPacket(MacAddress.valueOf("bb:bb:bb:bb:bb:bb"),
Amit Ghosh8951f042017-08-10 13:48:10 +0100164 CLIENT_MAC, "1.1.1.1", "2.2.2.2");
165
166 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
167
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530168 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100169 compareServerPackets(offerPacket, offerRelayed);
170 }
171
172 /**
173 * Tests the DHCP relay app by sending DHCP Ack Packet.
174 *
175 * @throws Exception when an unhandled error occurs
176 */
177 @Test
178 public void testDhcpAck() {
179
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530180 Ethernet ackPacket = constructDhcpAckPacket(MacAddress.valueOf("bb:bb:bb:bb:bb:bb"),
Amit Ghosh8951f042017-08-10 13:48:10 +0100181 CLIENT_MAC, "1.1.1.1", "2.2.2.2");
182
183 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
184
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530185 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100186 compareServerPackets(ackPacket, ackRelayed);
187 }
188
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530189 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
190 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
191 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100192 sent.setVlanID(CLIENT_C_TAG.toShort());
193
194 IPv4 ipv4Packet = (IPv4) sent.getPayload();
195 UDP udpPacket = (UDP) ipv4Packet.getPayload();
196 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
197
198 List<DHCPOption> options = Lists.newArrayList(dhcpPacket.getOptions());
199 DhcpOption82 option82 = new DhcpOption82();
200 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
201
202 DHCPOption option = new DHCPOption()
203 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
204 .setData(option82.toByteArray())
205 .setLength(option82.length());
206
207 options.add(options.size() - 1, option);
208 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530209 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100210
211 }
212
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530213 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
214 sent.setDestinationMACAddress(CLIENT_MAC);
215 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100216 sent.setVlanID(CLIENT_C_TAG.toShort());
217
218 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530219 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100220 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530221 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100222 0, byteBuffer.array().length);
223 } catch (Exception e) {
224 }
225 assertEquals(expectedPacket, relayed);
226
227 }
228
229 private class MockDevice extends DefaultDevice {
230
231 public MockDevice(ProviderId providerId, DeviceId id, Type type,
232 String manufacturer, String hwVersion, String swVersion,
233 String serialNumber, ChassisId chassisId, Annotations... annotations) {
234 super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber,
235 chassisId, annotations);
236 }
237 }
238
239 private class MockDeviceService extends DeviceServiceAdapter {
240
241 private ProviderId providerId = new ProviderId("of", "foo");
242 private final Device device1 = new MockDevice(providerId, DEVICE_ID_1, Device.Type.SWITCH,
243 "foo.inc", "0", "0", OLT_DEV_ID, new ChassisId(),
244 DEVICE_ANNOTATIONS);
245
246 @Override
247 public Device getDevice(DeviceId devId) {
248 return device1;
249
250 }
251
252 @Override
253 public Port getPort(ConnectPoint cp) {
254 return new MockPort();
255 }
256
257 @Override
258 public boolean isAvailable(DeviceId d) {
259 return true;
260 }
261 }
262
263 private class MockPort implements Port {
264
265 @Override
266 public boolean isEnabled() {
267 return true;
268 }
269 public long portSpeed() {
270 return 1000;
271 }
272 public Element element() {
273 return null;
274 }
275 public PortNumber number() {
276 return null;
277 }
278 public Annotations annotations() {
279 return new MockAnnotations();
280 }
281 public Type type() {
282 return Port.Type.FIBER;
283 }
284
285 private class MockAnnotations implements Annotations {
286
287 @Override
288 public String value(String val) {
289 return "PON 1/1";
290 }
291 public Set<String> keys() {
292 return null;
293 }
294 }
295 }
296
297 private class MockSubService implements SubscriberAndDeviceInformationService {
298 MockSubscriberAndDeviceInformation device =
299 new MockSubscriberAndDeviceInformation(OLT_DEV_ID, VlanId.NONE, VlanId.NONE, null, null,
300 OLT_MAC_ADDRESS, Ip4Address.valueOf("10.10.10.10"));
301 MockSubscriberAndDeviceInformation sub =
302 new MockSubscriberAndDeviceInformation(CLIENT_NAS_PORT_ID, CLIENT_C_TAG,
303 CLIENT_S_TAG, CLIENT_NAS_PORT_ID, CLIENT_CIRCUIT_ID, null, null);
304 @Override
305 public SubscriberAndDeviceInformation get(String id) {
306 if (id.equals(OLT_DEV_ID)) {
307 return device;
308 } else {
309 return sub;
310 }
311 }
312
313 @Override
314 public void invalidateAll() {}
315 public void invalidateId(String id) {}
316 public SubscriberAndDeviceInformation getfromCache(String id) {
317 return null;
318 }
319 }
320
321 private class MockMastershipService extends MastershipServiceAdapter {
322 @Override
323 public boolean isLocalMaster(DeviceId d) {
324 return true;
325 }
326 }
327
328 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
329
330 MockSubscriberAndDeviceInformation(String id, VlanId ctag,
331 VlanId stag, String nasPortId,
332 String circuitId, MacAddress hardId,
333 Ip4Address ipAddress) {
334 this.setCTag(ctag);
335 this.setHardwareIdentifier(hardId);
336 this.setId(id);
337 this.setIPAddress(ipAddress);
338 this.setSTag(stag);
339 this.setNasPortId(nasPortId);
340 this.setCircuitId(circuitId);
341 }
342 }
343
344 private class MockHostService extends HostServiceAdapter {
345
346 @Override
347 public Set<Host> getHostsByMac(MacAddress mac) {
348
349 HostLocation loc = new HostLocation(DEVICE_ID_1, PortNumber.portNumber(22), 0);
350
351 IpAddress ip = IpAddress.valueOf("10.100.200.10");
352
353 Host h = new DefaultHost(ProviderId.NONE, HostId.hostId(mac, VlanId.NONE),
354 mac, VlanId.NONE, loc, ImmutableSet.of(ip));
355
356 return ImmutableSet.of(h);
357 }
358 }
359
360
361 /**
362 * Mocks the AAAConfig class to force usage of an unroutable address for the
363 * RADIUS server.
364 */
365 static class MockDhcpL2RealyConfig extends DhcpL2RelayConfig {
366 @Override
367 public Set<ConnectPoint> getDhcpServerConnectPoint() {
368 return ImmutableSet.of(SERVER_CONNECT_POINT);
369 }
370 }
371
372 /**
373 * Mocks the network config registry.
374 */
375 @SuppressWarnings("unchecked")
376 private static final class TestNetworkConfigRegistry
377 extends NetworkConfigRegistryAdapter {
378 @Override
379 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
380 DhcpL2RelayConfig dhcpConfig = new MockDhcpL2RealyConfig();
381 return (C) dhcpConfig;
382 }
383 }
384}
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401