blob: 09d70c031f511578f1fb635e6e9ceceb5097bc8f [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 Abaka2dcd1f92020-01-31 06:51:43 +000021import java.util.HashMap;
Saurav Dasb4e3e102018-10-02 15:31:17 -070022import java.util.List;
Marcos Aurelio Carrerodadb3572019-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 Carrerodadb3572019-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.impl.packet.DhcpOption82;
42
Saurav Dasb4e3e102018-10-02 15:31:17 -070043import com.google.common.collect.Lists;
Amit Ghosh8951f042017-08-10 13:48:10 +010044
45public class DhcpL2RelayTest extends DhcpL2RelayTestBase {
46
Amit Ghosh8951f042017-08-10 13:48:10 +010047 private DhcpL2Relay dhcpL2Relay;
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -030048 private SimpleDhcpL2RelayCountersStore store;
Amit Ghosh8951f042017-08-10 13:48:10 +010049
50 ComponentConfigService mockConfigService =
51 EasyMock.createMock(ComponentConfigService.class);
52
53 /**
54 * Sets up the services required by the dhcpl2relay app.
55 */
56 @Before
57 public void setUp() {
58 dhcpL2Relay = new DhcpL2Relay();
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -030059 dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry();
Saurav Dasb4e3e102018-10-02 15:31:17 -070060 dhcpL2Relay.coreService = new MockCoreServiceAdapter();
61 dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter();
Amit Ghosh8951f042017-08-10 13:48:10 +010062 dhcpL2Relay.packetService = new MockPacketService();
63 dhcpL2Relay.componentConfigService = mockConfigService;
64 dhcpL2Relay.deviceService = new MockDeviceService();
Gamze Abakac806c6c2018-12-03 12:49:46 +000065 dhcpL2Relay.sadisService = new MockSadisService();
Amit Ghosh8951f042017-08-10 13:48:10 +010066 dhcpL2Relay.hostService = new MockHostService();
67 dhcpL2Relay.mastershipService = new MockMastershipService();
Gamze Abaka2dcd1f92020-01-31 06:51:43 +000068 dhcpL2Relay.dhcpL2RelayCounters = new MockDhcpL2RelayCountersStore();
Jonathan Hartc36c9552018-07-31 15:07:53 -040069 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -030070 dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService);
71 dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext());
72 store = new SimpleDhcpL2RelayCountersStore();
73 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
74 store.activate();
75 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010076 }
77
78 /**
79 * Tears down the dhcpL2Relay application.
80 */
81 @After
82 public void tearDown() {
83 dhcpL2Relay.deactivate();
84 }
85
86 /**
87 * Tests the DHCP relay app by sending DHCP discovery Packet.
88 *
89 * @throws Exception when an unhandled error occurs
90 */
91 @Test
92 public void testDhcpDiscover() throws Exception {
93 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053094 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +010095
96 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
97
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053098 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +010099 compareClientPackets(discoverPacket, discoverRelayed);
100 }
101
102 /**
103 * Tests the DHCP relay app by sending DHCP Request Packet.
104 *
105 * @throws Exception when an unhandled error occurs
106 */
107 @Test
108 public void testDhcpRequest() throws Exception {
109 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530110 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100111
112 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
113
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530114 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100115 compareClientPackets(requestPacket, requestRelayed);
116 }
117
118 /**
119 * Tests the DHCP relay app by sending DHCP Offer Packet.
120 *
121 * @throws Exception when an unhandled error occurs
122 */
123 @Test
124 public void testDhcpOffer() {
125 // (1) Sending DHCP discover packet
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300126 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
127 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100128
129 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
130
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530131 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100132 compareServerPackets(offerPacket, offerRelayed);
133 }
134
135 /**
136 * Tests the DHCP relay app by sending DHCP Ack Packet.
137 *
138 * @throws Exception when an unhandled error occurs
139 */
140 @Test
141 public void testDhcpAck() {
142
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300143 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
144 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100145
146 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
147
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530148 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100149 compareServerPackets(ackPacket, ackRelayed);
150 }
151
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300152 /**
153 * Tests the DHCP global counters.
154 */
155 @Test
156 public void testDhcpGlobalCounters() {
157 long discoveryValue = 0;
158 long offerValue = 0;
159 long requestValue = 0;
160 long ackValue = 0;
161
162 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
163 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
164 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
165 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
166 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
167 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
168
169 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
170 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
171 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
172 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
173
174 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
175 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
176 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
177 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
178 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
179 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
180 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
181 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
182 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
183
184 assertEquals((long) 1, discoveryValue);
185 assertEquals((long) 1, offerValue);
186 assertEquals((long) 1, requestValue);
187 assertEquals((long) 1, ackValue);
188 }
189
190 /**
191 * Tests the DHCP per subscriber counters.
192 *
193 */
194 @Test
195 public void testDhcpPerSubscriberCounters() {
196 long discoveryValue;
197 long offerValue;
198 long requestValue;
199 long ackValue;
200
201 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
202 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
203 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
204 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
205 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
206 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
207
208 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
209 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
210 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
211 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
212
213 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
214 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
215 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
216 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
217 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
218 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
219 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
220 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
221 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
222
223 assertEquals((long) 1, discoveryValue);
224 assertEquals((long) 1, offerValue);
225 assertEquals((long) 1, requestValue);
226 assertEquals((long) 1, ackValue);
227 }
228
229 /**
230 * Tests the schedule function to publish the counters to kafka.
231 *
232 */
233 @Test
234 public void testSchedulePublishCountersToKafka() {
235 MockExecutor executor = new MockExecutor(dhcpL2Relay.refreshService);
236 dhcpL2Relay.refreshTask = executor.scheduleWithFixedDelay(
237 dhcpL2Relay.publishCountersToKafka, 0, 10, TimeUnit.SECONDS);
238 executor.assertLastMethodCalled("scheduleWithFixedDelay", 0, 10, TimeUnit.SECONDS);
239 }
240
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530241 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
242 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
243 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100244 sent.setVlanID(CLIENT_C_TAG.toShort());
Gamze Abaka2dcd1f92020-01-31 06:51:43 +0000245 sent.setPriorityCode((byte) CLIENT_C_PBIT);
Amit Ghosh8951f042017-08-10 13:48:10 +0100246
247 IPv4 ipv4Packet = (IPv4) sent.getPayload();
248 UDP udpPacket = (UDP) ipv4Packet.getPayload();
249 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
250
Jonathan Hartedbf6422018-05-02 17:30:05 -0700251 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100252 DhcpOption82 option82 = new DhcpOption82();
253 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
254
Jonathan Hartedbf6422018-05-02 17:30:05 -0700255 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100256 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
257 .setData(option82.toByteArray())
258 .setLength(option82.length());
259
260 options.add(options.size() - 1, option);
261 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530262 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100263
264 }
265
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530266 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
267 sent.setDestinationMACAddress(CLIENT_MAC);
Gamze Abaka2dcd1f92020-01-31 06:51:43 +0000268 sent.setQinQVID(NOT_PROVIDED);
269 sent.setQinQPriorityCode((byte) NOT_PROVIDED);
Amit Ghosh8951f042017-08-10 13:48:10 +0100270 sent.setVlanID(CLIENT_C_TAG.toShort());
271
272 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530273 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100274 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530275 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100276 0, byteBuffer.array().length);
277 } catch (Exception e) {
278 }
279 assertEquals(expectedPacket, relayed);
280
281 }
Gamze Abaka2dcd1f92020-01-31 06:51:43 +0000282
283 private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore {
284 @Override
285 public void initCounters(String counterClass) {
286
287 }
288
289 @Override
290 public void incrementCounter(String counterClass, DhcpL2RelayCounters counterType) {
291
292 }
293
294 @Override
295 public void setCounter(String counterClass, DhcpL2RelayCounters counterType, Long value) {
296
297 }
298
299 @Override
300 public Map<DhcpL2RelayCountersIdentifier, AtomicLong> getCountersMap() {
301 return new HashMap<>();
302 }
303
304 @Override
305 public void resetCounters(String counterClass) {
306
307 }
308 }
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300309}