blob: a0cab4ac76d5f49a5aba876591af89b0727f28a2 [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 /**
154 * Tests the DHCP global counters.
155 */
156 @Test
157 public void testDhcpGlobalCounters() {
158 long discoveryValue = 0;
159 long offerValue = 0;
160 long requestValue = 0;
161 long ackValue = 0;
162
163 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
164 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
165 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
166 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
167 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
168 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
169
170 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
171 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
172 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
173 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
174
175 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
176 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
177 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
178 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
179 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
180 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
181 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
182 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
183 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
184
185 assertEquals((long) 1, discoveryValue);
186 assertEquals((long) 1, offerValue);
187 assertEquals((long) 1, requestValue);
188 assertEquals((long) 1, ackValue);
189 }
190
191 /**
192 * Tests the DHCP per subscriber counters.
193 *
194 */
195 @Test
196 public void testDhcpPerSubscriberCounters() {
197 long discoveryValue;
198 long offerValue;
199 long requestValue;
200 long ackValue;
201
202 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
203 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
204 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
205 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
206 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
207 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
208
209 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
210 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
211 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
212 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
213
214 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
215 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
216 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
217 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
218 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
219 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
220 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
221 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
222 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
223
224 assertEquals((long) 1, discoveryValue);
225 assertEquals((long) 1, offerValue);
226 assertEquals((long) 1, requestValue);
227 assertEquals((long) 1, ackValue);
228 }
229
230 /**
231 * Tests the schedule function to publish the counters to kafka.
232 *
233 */
234 @Test
235 public void testSchedulePublishCountersToKafka() {
236 MockExecutor executor = new MockExecutor(dhcpL2Relay.refreshService);
237 dhcpL2Relay.refreshTask = executor.scheduleWithFixedDelay(
238 dhcpL2Relay.publishCountersToKafka, 0, 10, TimeUnit.SECONDS);
239 executor.assertLastMethodCalled("scheduleWithFixedDelay", 0, 10, TimeUnit.SECONDS);
240 }
241
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530242 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
243 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
244 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100245 sent.setVlanID(CLIENT_C_TAG.toShort());
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000246 sent.setPriorityCode((byte) CLIENT_C_PBIT);
Amit Ghosh8951f042017-08-10 13:48:10 +0100247
248 IPv4 ipv4Packet = (IPv4) sent.getPayload();
249 UDP udpPacket = (UDP) ipv4Packet.getPayload();
250 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
251
Jonathan Hartedbf6422018-05-02 17:30:05 -0700252 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100253 DhcpOption82 option82 = new DhcpOption82();
254 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
255
Jonathan Hartedbf6422018-05-02 17:30:05 -0700256 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100257 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
258 .setData(option82.toByteArray())
259 .setLength(option82.length());
260
261 options.add(options.size() - 1, option);
262 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530263 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100264
265 }
266
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530267 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
268 sent.setDestinationMACAddress(CLIENT_MAC);
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000269 sent.setQinQVID(NOT_PROVIDED);
270 sent.setQinQPriorityCode((byte) NOT_PROVIDED);
Amit Ghosh8951f042017-08-10 13:48:10 +0100271 sent.setVlanID(CLIENT_C_TAG.toShort());
272
273 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530274 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100275 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530276 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100277 0, byteBuffer.array().length);
278 } catch (Exception e) {
279 }
280 assertEquals(expectedPacket, relayed);
281
282 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000283
284 private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore {
285 @Override
286 public void initCounters(String counterClass) {
287
288 }
289
290 @Override
291 public void incrementCounter(String counterClass, DhcpL2RelayCounters counterType) {
292
293 }
294
295 @Override
296 public void setCounter(String counterClass, DhcpL2RelayCounters counterType, Long value) {
297
298 }
299
300 @Override
301 public Map<DhcpL2RelayCountersIdentifier, AtomicLong> getCountersMap() {
302 return new HashMap<>();
303 }
304
305 @Override
306 public void resetCounters(String counterClass) {
307
308 }
309 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300310}