blob: 6d0524e389a9ad6ddbc8ad548f68c6c088206422 [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 */
Matteo Scandolo57af5d12019-04-29 17:11:41 -070016package org.opencord.dhcpl2relay.impl;
Amit Ghosh8951f042017-08-10 13:48:10 +010017
Saurav Dasb4e3e102018-10-02 15:31:17 -070018import static org.junit.Assert.assertEquals;
19
20import java.nio.ByteBuffer;
Gamze Abakaa64b3bc2020-01-31 06:51:43 +000021import java.util.HashMap;
Saurav Dasb4e3e102018-10-02 15:31:17 -070022import java.util.List;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030023import java.util.Map;
24import java.util.concurrent.atomic.AtomicLong;
25import java.util.concurrent.TimeUnit;
Saurav Dasb4e3e102018-10-02 15:31:17 -070026
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053027import org.easymock.EasyMock;
Amit Ghosh8951f042017-08-10 13:48:10 +010028import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
Jonathan Hartc36c9552018-07-31 15:07:53 -040031import org.onlab.junit.TestUtils;
Amit Ghosh8951f042017-08-10 13:48:10 +010032import org.onlab.packet.DHCP;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053033import org.onlab.packet.Ethernet;
Amit Ghosh8951f042017-08-10 13:48:10 +010034import org.onlab.packet.IPv4;
Amit Ghosh8951f042017-08-10 13:48:10 +010035import org.onlab.packet.UDP;
Jonathan Hartedbf6422018-05-02 17:30:05 -070036import org.onlab.packet.dhcp.DhcpOption;
Amit Ghosh8951f042017-08-10 13:48:10 +010037
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030038import org.onosproject.cfg.ComponentConfigService;
39import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
41import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
42import org.opencord.dhcpl2relay.impl.packet.DhcpOption82;
43
Saurav Dasb4e3e102018-10-02 15:31:17 -070044import com.google.common.collect.Lists;
Amit Ghosh8951f042017-08-10 13:48:10 +010045
46public class DhcpL2RelayTest extends DhcpL2RelayTestBase {
47
Amit Ghosh8951f042017-08-10 13:48:10 +010048 private DhcpL2Relay dhcpL2Relay;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030049 private SimpleDhcpL2RelayCountersStore store;
Amit Ghosh8951f042017-08-10 13:48:10 +010050
51 ComponentConfigService mockConfigService =
52 EasyMock.createMock(ComponentConfigService.class);
53
54 /**
55 * Sets up the services required by the dhcpl2relay app.
56 */
57 @Before
58 public void setUp() {
59 dhcpL2Relay = new DhcpL2Relay();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030060 dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry();
Saurav Dasb4e3e102018-10-02 15:31:17 -070061 dhcpL2Relay.coreService = new MockCoreServiceAdapter();
62 dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter();
Amit Ghosh8951f042017-08-10 13:48:10 +010063 dhcpL2Relay.packetService = new MockPacketService();
64 dhcpL2Relay.componentConfigService = mockConfigService;
65 dhcpL2Relay.deviceService = new MockDeviceService();
Gamze Abakac806c6c2018-12-03 12:49:46 +000066 dhcpL2Relay.sadisService = new MockSadisService();
Amit Ghosh8951f042017-08-10 13:48:10 +010067 dhcpL2Relay.hostService = new MockHostService();
68 dhcpL2Relay.mastershipService = new MockMastershipService();
Gamze Abakaa64b3bc2020-01-31 06:51:43 +000069 dhcpL2Relay.dhcpL2RelayCounters = new MockDhcpL2RelayCountersStore();
Jonathan Hartc36c9552018-07-31 15:07:53 -040070 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030071 dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService);
72 dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext());
73 store = new SimpleDhcpL2RelayCountersStore();
74 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
75 store.activate();
76 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010077 }
78
79 /**
80 * Tears down the dhcpL2Relay application.
81 */
82 @After
83 public void tearDown() {
84 dhcpL2Relay.deactivate();
85 }
86
87 /**
88 * Tests the DHCP relay app by sending DHCP discovery Packet.
89 *
90 * @throws Exception when an unhandled error occurs
91 */
92 @Test
93 public void testDhcpDiscover() throws Exception {
94 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053095 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +010096
97 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
98
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053099 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100100 compareClientPackets(discoverPacket, discoverRelayed);
101 }
102
103 /**
104 * Tests the DHCP relay app by sending DHCP Request Packet.
105 *
106 * @throws Exception when an unhandled error occurs
107 */
108 @Test
109 public void testDhcpRequest() throws Exception {
110 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530111 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100112
113 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
114
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530115 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100116 compareClientPackets(requestPacket, requestRelayed);
117 }
118
119 /**
120 * Tests the DHCP relay app by sending DHCP Offer Packet.
121 *
122 * @throws Exception when an unhandled error occurs
123 */
124 @Test
125 public void testDhcpOffer() {
126 // (1) Sending DHCP discover packet
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300127 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
128 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100129
130 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
131
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530132 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100133 compareServerPackets(offerPacket, offerRelayed);
134 }
135
136 /**
137 * Tests the DHCP relay app by sending DHCP Ack Packet.
138 *
139 * @throws Exception when an unhandled error occurs
140 */
141 @Test
142 public void testDhcpAck() {
143
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300144 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
145 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100146
147 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
148
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530149 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100150 compareServerPackets(ackPacket, ackRelayed);
151 }
152
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300153 /**
Arjun E K05ad20b2020-03-13 13:25:17 +0000154 * Tests the DHCP relay app by sending DHCP Nak Packet.
155 *
156 * @throws Exception when an unhandled error occurs
157 */
158 @Test
159 public void testDhcpNak() {
160
161 Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC,
162 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
163
164 sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
165
166 Ethernet nakRelayed = (Ethernet) getPacket();
167 compareServerPackets(nakPacket, nakRelayed);
168 }
169
170 /**
171 * Tests the DHCP relay app by sending DHCP Decline Packet.
172 *
173 * @throws Exception when an unhandled error occurs
174 */
175 @Test
176 public void testDhcpDecline() {
177
178 Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC);
179
180 sendPacket(declinePacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
181
182 Ethernet declineRelayed = (Ethernet) getPacket();
183 compareClientPackets(declinePacket, declineRelayed);
184 }
185
186 /**
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300187 * Tests the DHCP global counters.
188 */
189 @Test
190 public void testDhcpGlobalCounters() {
191 long discoveryValue = 0;
192 long offerValue = 0;
193 long requestValue = 0;
194 long ackValue = 0;
195
196 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
197 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
198 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
199 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
200 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
201 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
202
203 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
204 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
205 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
206 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
207
208 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
209 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
210 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
211 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
212 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
213 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
214 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
215 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
216 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
217
218 assertEquals((long) 1, discoveryValue);
219 assertEquals((long) 1, offerValue);
220 assertEquals((long) 1, requestValue);
221 assertEquals((long) 1, ackValue);
222 }
223
224 /**
225 * Tests the DHCP per subscriber counters.
226 *
227 */
228 @Test
229 public void testDhcpPerSubscriberCounters() {
230 long discoveryValue;
231 long offerValue;
232 long requestValue;
233 long ackValue;
234
235 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
236 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
237 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
238 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
239 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
240 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
241
242 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
243 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
244 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
245 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
246
247 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
248 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
249 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
250 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
251 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
252 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
253 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
254 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
255 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
256
257 assertEquals((long) 1, discoveryValue);
258 assertEquals((long) 1, offerValue);
259 assertEquals((long) 1, requestValue);
260 assertEquals((long) 1, ackValue);
261 }
262
263 /**
264 * Tests the schedule function to publish the counters to kafka.
265 *
266 */
267 @Test
268 public void testSchedulePublishCountersToKafka() {
269 MockExecutor executor = new MockExecutor(dhcpL2Relay.refreshService);
270 dhcpL2Relay.refreshTask = executor.scheduleWithFixedDelay(
271 dhcpL2Relay.publishCountersToKafka, 0, 10, TimeUnit.SECONDS);
272 executor.assertLastMethodCalled("scheduleWithFixedDelay", 0, 10, TimeUnit.SECONDS);
273 }
274
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530275 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
276 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
277 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100278 sent.setVlanID(CLIENT_C_TAG.toShort());
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000279 sent.setPriorityCode((byte) CLIENT_C_PBIT);
Amit Ghosh8951f042017-08-10 13:48:10 +0100280
281 IPv4 ipv4Packet = (IPv4) sent.getPayload();
282 UDP udpPacket = (UDP) ipv4Packet.getPayload();
283 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
284
Jonathan Hartedbf6422018-05-02 17:30:05 -0700285 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100286 DhcpOption82 option82 = new DhcpOption82();
287 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
288
Jonathan Hartedbf6422018-05-02 17:30:05 -0700289 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100290 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
291 .setData(option82.toByteArray())
292 .setLength(option82.length());
293
294 options.add(options.size() - 1, option);
295 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530296 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100297
298 }
299
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530300 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
301 sent.setDestinationMACAddress(CLIENT_MAC);
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000302 sent.setQinQVID(NOT_PROVIDED);
303 sent.setQinQPriorityCode((byte) NOT_PROVIDED);
Amit Ghosh8951f042017-08-10 13:48:10 +0100304 sent.setVlanID(CLIENT_C_TAG.toShort());
305
306 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530307 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100308 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530309 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100310 0, byteBuffer.array().length);
311 } catch (Exception e) {
312 }
313 assertEquals(expectedPacket, relayed);
314
315 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000316
317 private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore {
318 @Override
319 public void initCounters(String counterClass) {
320
321 }
322
323 @Override
324 public void incrementCounter(String counterClass, DhcpL2RelayCounters counterType) {
325
326 }
327
328 @Override
329 public void setCounter(String counterClass, DhcpL2RelayCounters counterType, Long value) {
330
331 }
332
333 @Override
334 public Map<DhcpL2RelayCountersIdentifier, AtomicLong> getCountersMap() {
335 return new HashMap<>();
336 }
337
338 @Override
339 public void resetCounters(String counterClass) {
340
341 }
342 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300343}