blob: ba4de8dbf9dd775e69631c27b3d22213cfb3f170 [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 Carrerodadb3572019-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 Carrerodadb3572019-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.impl.packet.DhcpOption82;
41
Saurav Dasb4e3e102018-10-02 15:31:17 -070042import com.google.common.collect.Lists;
Amit Ghosh8951f042017-08-10 13:48:10 +010043
44public class DhcpL2RelayTest extends DhcpL2RelayTestBase {
45
Amit Ghosh8951f042017-08-10 13:48:10 +010046 private DhcpL2Relay dhcpL2Relay;
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -030047 private SimpleDhcpL2RelayCountersStore store;
Amit Ghosh8951f042017-08-10 13:48:10 +010048
49 ComponentConfigService mockConfigService =
50 EasyMock.createMock(ComponentConfigService.class);
51
52 /**
53 * Sets up the services required by the dhcpl2relay app.
54 */
55 @Before
56 public void setUp() {
57 dhcpL2Relay = new DhcpL2Relay();
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -030058 dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry();
Saurav Dasb4e3e102018-10-02 15:31:17 -070059 dhcpL2Relay.coreService = new MockCoreServiceAdapter();
60 dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter();
Amit Ghosh8951f042017-08-10 13:48:10 +010061 dhcpL2Relay.packetService = new MockPacketService();
62 dhcpL2Relay.componentConfigService = mockConfigService;
63 dhcpL2Relay.deviceService = new MockDeviceService();
Gamze Abakac806c6c2018-12-03 12:49:46 +000064 dhcpL2Relay.sadisService = new MockSadisService();
Amit Ghosh8951f042017-08-10 13:48:10 +010065 dhcpL2Relay.hostService = new MockHostService();
66 dhcpL2Relay.mastershipService = new MockMastershipService();
Jonathan Hartc36c9552018-07-31 15:07:53 -040067 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -030068 dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService);
69 dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext());
70 store = new SimpleDhcpL2RelayCountersStore();
71 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
72 store.activate();
73 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010074 }
75
76 /**
77 * Tears down the dhcpL2Relay application.
78 */
79 @After
80 public void tearDown() {
81 dhcpL2Relay.deactivate();
82 }
83
84 /**
85 * Tests the DHCP relay app by sending DHCP discovery Packet.
86 *
87 * @throws Exception when an unhandled error occurs
88 */
89 @Test
90 public void testDhcpDiscover() throws Exception {
91 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053092 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +010093
94 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
95
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053096 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +010097 compareClientPackets(discoverPacket, discoverRelayed);
98 }
99
100 /**
101 * Tests the DHCP relay app by sending DHCP Request Packet.
102 *
103 * @throws Exception when an unhandled error occurs
104 */
105 @Test
106 public void testDhcpRequest() throws Exception {
107 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530108 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100109
110 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
111
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530112 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100113 compareClientPackets(requestPacket, requestRelayed);
114 }
115
116 /**
117 * Tests the DHCP relay app by sending DHCP Offer Packet.
118 *
119 * @throws Exception when an unhandled error occurs
120 */
121 @Test
122 public void testDhcpOffer() {
123 // (1) Sending DHCP discover packet
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300124 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
125 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100126
127 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
128
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530129 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100130 compareServerPackets(offerPacket, offerRelayed);
131 }
132
133 /**
134 * Tests the DHCP relay app by sending DHCP Ack Packet.
135 *
136 * @throws Exception when an unhandled error occurs
137 */
138 @Test
139 public void testDhcpAck() {
140
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300141 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
142 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100143
144 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
145
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530146 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100147 compareServerPackets(ackPacket, ackRelayed);
148 }
149
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300150 /**
151 * Tests the DHCP global counters.
152 */
153 @Test
154 public void testDhcpGlobalCounters() {
155 long discoveryValue = 0;
156 long offerValue = 0;
157 long requestValue = 0;
158 long ackValue = 0;
159
160 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
161 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
162 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
163 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
164 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
165 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
166
167 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
168 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
169 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
170 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
171
172 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
173 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
174 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
175 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
176 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
177 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
178 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
179 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER,
180 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
181
182 assertEquals((long) 1, discoveryValue);
183 assertEquals((long) 1, offerValue);
184 assertEquals((long) 1, requestValue);
185 assertEquals((long) 1, ackValue);
186 }
187
188 /**
189 * Tests the DHCP per subscriber counters.
190 *
191 */
192 @Test
193 public void testDhcpPerSubscriberCounters() {
194 long discoveryValue;
195 long offerValue;
196 long requestValue;
197 long ackValue;
198
199 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
200 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
201 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
202 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
203 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
204 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
205
206 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
207 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
208 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
209 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
210
211 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
212 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
213 DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
214 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
215 DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue();
216 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
217 DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue();
218 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
219 DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue();
220
221 assertEquals((long) 1, discoveryValue);
222 assertEquals((long) 1, offerValue);
223 assertEquals((long) 1, requestValue);
224 assertEquals((long) 1, ackValue);
225 }
226
227 /**
228 * Tests the schedule function to publish the counters to kafka.
229 *
230 */
231 @Test
232 public void testSchedulePublishCountersToKafka() {
233 MockExecutor executor = new MockExecutor(dhcpL2Relay.refreshService);
234 dhcpL2Relay.refreshTask = executor.scheduleWithFixedDelay(
235 dhcpL2Relay.publishCountersToKafka, 0, 10, TimeUnit.SECONDS);
236 executor.assertLastMethodCalled("scheduleWithFixedDelay", 0, 10, TimeUnit.SECONDS);
237 }
238
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530239 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
240 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
241 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100242 sent.setVlanID(CLIENT_C_TAG.toShort());
243
244 IPv4 ipv4Packet = (IPv4) sent.getPayload();
245 UDP udpPacket = (UDP) ipv4Packet.getPayload();
246 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
247
Jonathan Hartedbf6422018-05-02 17:30:05 -0700248 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100249 DhcpOption82 option82 = new DhcpOption82();
250 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
251
Jonathan Hartedbf6422018-05-02 17:30:05 -0700252 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100253 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
254 .setData(option82.toByteArray())
255 .setLength(option82.length());
256
257 options.add(options.size() - 1, option);
258 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530259 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100260
261 }
262
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530263 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
264 sent.setDestinationMACAddress(CLIENT_MAC);
265 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100266 sent.setVlanID(CLIENT_C_TAG.toShort());
267
268 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530269 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100270 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530271 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100272 0, byteBuffer.array().length);
273 } catch (Exception e) {
274 }
275 assertEquals(expectedPacket, relayed);
276
277 }
Marcos Aurelio Carrerodadb3572019-11-25 13:34:25 -0300278}