blob: 984195388e72440ea395696df899d013f3b4995c [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;
21import java.util.List;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030022import java.util.Map;
23import java.util.concurrent.atomic.AtomicLong;
24import java.util.concurrent.TimeUnit;
Saurav Dasb4e3e102018-10-02 15:31:17 -070025
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053026import org.easymock.EasyMock;
Amit Ghosh8951f042017-08-10 13:48:10 +010027import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
Jonathan Hartc36c9552018-07-31 15:07:53 -040030import org.onlab.junit.TestUtils;
Amit Ghosh8951f042017-08-10 13:48:10 +010031import org.onlab.packet.DHCP;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053032import org.onlab.packet.Ethernet;
Amit Ghosh8951f042017-08-10 13:48:10 +010033import org.onlab.packet.IPv4;
Amit Ghosh8951f042017-08-10 13:48:10 +010034import org.onlab.packet.UDP;
Jonathan Hartedbf6422018-05-02 17:30:05 -070035import org.onlab.packet.dhcp.DhcpOption;
Amit Ghosh8951f042017-08-10 13:48:10 +010036
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030037import org.onosproject.cfg.ComponentConfigService;
38import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
40import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
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 Carreroeaf02b82019-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 Carreroeaf02b82019-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();
Jonathan Hartc36c9552018-07-31 15:07:53 -040068 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030069 dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService);
70 dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext());
71 store = new SimpleDhcpL2RelayCountersStore();
72 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
73 store.activate();
74 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010075 }
76
77 /**
78 * Tears down the dhcpL2Relay application.
79 */
80 @After
81 public void tearDown() {
82 dhcpL2Relay.deactivate();
83 }
84
85 /**
86 * Tests the DHCP relay app by sending DHCP discovery Packet.
87 *
88 * @throws Exception when an unhandled error occurs
89 */
90 @Test
91 public void testDhcpDiscover() throws Exception {
92 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053093 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +010094
95 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
96
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053097 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +010098 compareClientPackets(discoverPacket, discoverRelayed);
99 }
100
101 /**
102 * Tests the DHCP relay app by sending DHCP Request Packet.
103 *
104 * @throws Exception when an unhandled error occurs
105 */
106 @Test
107 public void testDhcpRequest() throws Exception {
108 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530109 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100110
111 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
112
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530113 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100114 compareClientPackets(requestPacket, requestRelayed);
115 }
116
117 /**
118 * Tests the DHCP relay app by sending DHCP Offer Packet.
119 *
120 * @throws Exception when an unhandled error occurs
121 */
122 @Test
123 public void testDhcpOffer() {
124 // (1) Sending DHCP discover packet
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300125 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
126 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100127
128 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
129
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530130 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100131 compareServerPackets(offerPacket, offerRelayed);
132 }
133
134 /**
135 * Tests the DHCP relay app by sending DHCP Ack Packet.
136 *
137 * @throws Exception when an unhandled error occurs
138 */
139 @Test
140 public void testDhcpAck() {
141
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300142 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
143 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100144
145 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
146
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530147 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100148 compareServerPackets(ackPacket, ackRelayed);
149 }
150
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300151 /**
152 * Tests the DHCP global counters.
153 */
154 @Test
155 public void testDhcpGlobalCounters() {
156 long discoveryValue = 0;
157 long offerValue = 0;
158 long requestValue = 0;
159 long ackValue = 0;
160
161 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
162 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
163 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
164 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
165 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
166 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
167
168 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
169 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
170 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
171 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
172
173 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
174 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
175 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
176 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
177 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
178 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
179 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
180 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
181 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
182
183 assertEquals((long) 1, discoveryValue);
184 assertEquals((long) 1, offerValue);
185 assertEquals((long) 1, requestValue);
186 assertEquals((long) 1, ackValue);
187 }
188
189 /**
190 * Tests the DHCP per subscriber counters.
191 *
192 */
193 @Test
194 public void testDhcpPerSubscriberCounters() {
195 long discoveryValue;
196 long offerValue;
197 long requestValue;
198 long ackValue;
199
200 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
201 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
202 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
203 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
204 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
205 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
206
207 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
208 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
209 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
210 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
211
212 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
213 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
214 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
215 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
216 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
217 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
218 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
219 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
220 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
221
222 assertEquals((long) 1, discoveryValue);
223 assertEquals((long) 1, offerValue);
224 assertEquals((long) 1, requestValue);
225 assertEquals((long) 1, ackValue);
226 }
227
228 /**
229 * Tests the schedule function to publish the counters to kafka.
230 *
231 */
232 @Test
233 public void testSchedulePublishCountersToKafka() {
234 MockExecutor executor = new MockExecutor(dhcpL2Relay.refreshService);
235 dhcpL2Relay.refreshTask = executor.scheduleWithFixedDelay(
236 dhcpL2Relay.publishCountersToKafka, 0, 10, TimeUnit.SECONDS);
237 executor.assertLastMethodCalled("scheduleWithFixedDelay", 0, 10, TimeUnit.SECONDS);
238 }
239
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530240 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
241 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
242 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100243 sent.setVlanID(CLIENT_C_TAG.toShort());
244
245 IPv4 ipv4Packet = (IPv4) sent.getPayload();
246 UDP udpPacket = (UDP) ipv4Packet.getPayload();
247 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
248
Jonathan Hartedbf6422018-05-02 17:30:05 -0700249 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100250 DhcpOption82 option82 = new DhcpOption82();
251 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
252
Jonathan Hartedbf6422018-05-02 17:30:05 -0700253 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100254 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
255 .setData(option82.toByteArray())
256 .setLength(option82.length());
257
258 options.add(options.size() - 1, option);
259 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530260 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100261
262 }
263
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530264 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
265 sent.setDestinationMACAddress(CLIENT_MAC);
266 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100267 sent.setVlanID(CLIENT_C_TAG.toShort());
268
269 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530270 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100271 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530272 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100273 0, byteBuffer.array().length);
274 } catch (Exception e) {
275 }
276 assertEquals(expectedPacket, relayed);
277
278 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300279}