blob: 42664ccffa3fac6abb48490b0a2ef2e28d2f2193 [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
Jonathan Hart617bc3e2020-02-14 10:42:23 -080018import com.google.common.collect.Lists;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053019import org.easymock.EasyMock;
Amit Ghosh8951f042017-08-10 13:48:10 +010020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Jonathan Hartc36c9552018-07-31 15:07:53 -040023import org.onlab.junit.TestUtils;
Amit Ghosh8951f042017-08-10 13:48:10 +010024import org.onlab.packet.DHCP;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053025import org.onlab.packet.Ethernet;
Amit Ghosh8951f042017-08-10 13:48:10 +010026import org.onlab.packet.IPv4;
Amit Ghosh8951f042017-08-10 13:48:10 +010027import org.onlab.packet.UDP;
Jonathan Hartedbf6422018-05-02 17:30:05 -070028import org.onlab.packet.dhcp.DhcpOption;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030029import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080030import org.onosproject.cluster.LeadershipServiceAdapter;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030031import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080033import org.onosproject.store.service.TestStorageService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030034import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
35import org.opencord.dhcpl2relay.impl.packet.DhcpOption82;
36
Jonathan Hart617bc3e2020-02-14 10:42:23 -080037import java.nio.ByteBuffer;
38import java.util.HashMap;
39import java.util.List;
40import java.util.Map;
41import java.util.concurrent.TimeUnit;
42import java.util.concurrent.atomic.AtomicLong;
43
44import static org.junit.Assert.assertEquals;
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 Hart617bc3e2020-02-14 10:42:23 -080070 dhcpL2Relay.storageService = new TestStorageService();
71 dhcpL2Relay.leadershipService = new LeadershipServiceAdapter();
Jonathan Hartc36c9552018-07-31 15:07:53 -040072 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030073 dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService);
74 dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext());
75 store = new SimpleDhcpL2RelayCountersStore();
76 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
77 store.activate();
78 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010079 }
80
81 /**
82 * Tears down the dhcpL2Relay application.
83 */
84 @After
85 public void tearDown() {
86 dhcpL2Relay.deactivate();
87 }
88
89 /**
90 * Tests the DHCP relay app by sending DHCP discovery Packet.
91 *
92 * @throws Exception when an unhandled error occurs
93 */
94 @Test
95 public void testDhcpDiscover() throws Exception {
96 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053097 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +010098
99 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
100
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530101 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100102 compareClientPackets(discoverPacket, discoverRelayed);
103 }
104
105 /**
106 * Tests the DHCP relay app by sending DHCP Request Packet.
107 *
108 * @throws Exception when an unhandled error occurs
109 */
110 @Test
111 public void testDhcpRequest() throws Exception {
112 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530113 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100114
115 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
116
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530117 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100118 compareClientPackets(requestPacket, requestRelayed);
119 }
120
121 /**
122 * Tests the DHCP relay app by sending DHCP Offer Packet.
123 *
124 * @throws Exception when an unhandled error occurs
125 */
126 @Test
127 public void testDhcpOffer() {
128 // (1) Sending DHCP discover packet
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300129 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
130 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100131
132 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
133
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530134 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100135 compareServerPackets(offerPacket, offerRelayed);
136 }
137
138 /**
139 * Tests the DHCP relay app by sending DHCP Ack Packet.
140 *
141 * @throws Exception when an unhandled error occurs
142 */
143 @Test
144 public void testDhcpAck() {
145
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300146 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
147 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100148
149 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
150
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530151 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100152 compareServerPackets(ackPacket, ackRelayed);
153 }
154
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300155 /**
Arjun E K05ad20b2020-03-13 13:25:17 +0000156 * Tests the DHCP relay app by sending DHCP Nak Packet.
157 *
158 * @throws Exception when an unhandled error occurs
159 */
160 @Test
161 public void testDhcpNak() {
162
163 Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC,
164 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
165
166 sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
167
168 Ethernet nakRelayed = (Ethernet) getPacket();
169 compareServerPackets(nakPacket, nakRelayed);
170 }
171
172 /**
173 * Tests the DHCP relay app by sending DHCP Decline Packet.
174 *
175 * @throws Exception when an unhandled error occurs
176 */
177 @Test
178 public void testDhcpDecline() {
179
180 Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC);
181
182 sendPacket(declinePacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
183
184 Ethernet declineRelayed = (Ethernet) getPacket();
185 compareClientPackets(declinePacket, declineRelayed);
186 }
187
188 /**
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300189 * Tests the DHCP global counters.
190 */
191 @Test
192 public void testDhcpGlobalCounters() {
193 long discoveryValue = 0;
194 long offerValue = 0;
195 long requestValue = 0;
196 long ackValue = 0;
197
198 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
199 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
200 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
201 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
202 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
203 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
204
205 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
206 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
207 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
208 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
209
210 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
211 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
212 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
213 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
214 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
215 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
216 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
217 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
218 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
219
220 assertEquals((long) 1, discoveryValue);
221 assertEquals((long) 1, offerValue);
222 assertEquals((long) 1, requestValue);
223 assertEquals((long) 1, ackValue);
224 }
225
226 /**
227 * Tests the DHCP per subscriber counters.
228 *
229 */
230 @Test
231 public void testDhcpPerSubscriberCounters() {
232 long discoveryValue;
233 long offerValue;
234 long requestValue;
235 long ackValue;
236
237 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
238 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
239 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
240 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
241 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
242 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
243
244 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
245 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
246 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
247 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
248
249 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
250 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
251 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
252 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
253 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
254 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
255 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
256 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
257 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
258
259 assertEquals((long) 1, discoveryValue);
260 assertEquals((long) 1, offerValue);
261 assertEquals((long) 1, requestValue);
262 assertEquals((long) 1, ackValue);
263 }
264
265 /**
266 * Tests the schedule function to publish the counters to kafka.
267 *
268 */
269 @Test
270 public void testSchedulePublishCountersToKafka() {
271 MockExecutor executor = new MockExecutor(dhcpL2Relay.refreshService);
272 dhcpL2Relay.refreshTask = executor.scheduleWithFixedDelay(
273 dhcpL2Relay.publishCountersToKafka, 0, 10, TimeUnit.SECONDS);
274 executor.assertLastMethodCalled("scheduleWithFixedDelay", 0, 10, TimeUnit.SECONDS);
275 }
276
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530277 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
278 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
279 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100280 sent.setVlanID(CLIENT_C_TAG.toShort());
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000281 sent.setPriorityCode((byte) CLIENT_C_PBIT);
Amit Ghosh8951f042017-08-10 13:48:10 +0100282
283 IPv4 ipv4Packet = (IPv4) sent.getPayload();
284 UDP udpPacket = (UDP) ipv4Packet.getPayload();
285 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
286
Jonathan Hartedbf6422018-05-02 17:30:05 -0700287 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100288 DhcpOption82 option82 = new DhcpOption82();
289 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
290
Jonathan Hartedbf6422018-05-02 17:30:05 -0700291 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100292 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
293 .setData(option82.toByteArray())
294 .setLength(option82.length());
295
296 options.add(options.size() - 1, option);
297 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530298 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100299
300 }
301
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530302 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
303 sent.setDestinationMACAddress(CLIENT_MAC);
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000304 sent.setQinQVID(NOT_PROVIDED);
305 sent.setQinQPriorityCode((byte) NOT_PROVIDED);
Amit Ghosh8951f042017-08-10 13:48:10 +0100306 sent.setVlanID(CLIENT_C_TAG.toShort());
307
308 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530309 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100310 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530311 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100312 0, byteBuffer.array().length);
313 } catch (Exception e) {
314 }
315 assertEquals(expectedPacket, relayed);
316
317 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000318
319 private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore {
320 @Override
321 public void initCounters(String counterClass) {
322
323 }
324
325 @Override
326 public void incrementCounter(String counterClass, DhcpL2RelayCounters counterType) {
327
328 }
329
330 @Override
331 public void setCounter(String counterClass, DhcpL2RelayCounters counterType, Long value) {
332
333 }
334
335 @Override
336 public Map<DhcpL2RelayCountersIdentifier, AtomicLong> getCountersMap() {
337 return new HashMap<>();
338 }
339
340 @Override
341 public void resetCounters(String counterClass) {
342
343 }
344 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300345}