blob: 4106ef1b47130681c945b7a95c312021d6047830 [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;
Amit Ghosh8951f042017-08-10 13:48:10 +010019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Jonathan Hartc36c9552018-07-31 15:07:53 -040022import org.onlab.junit.TestUtils;
Amit Ghosh8951f042017-08-10 13:48:10 +010023import org.onlab.packet.DHCP;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053024import org.onlab.packet.Ethernet;
Amit Ghosh8951f042017-08-10 13:48:10 +010025import org.onlab.packet.IPv4;
Amit Ghosh8951f042017-08-10 13:48:10 +010026import org.onlab.packet.UDP;
Jonathan Hartedbf6422018-05-02 17:30:05 -070027import org.onlab.packet.dhcp.DhcpOption;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030028import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart77ca3152020-02-21 14:31:21 -080029import org.onosproject.cluster.ClusterServiceAdapter;
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 Hart77ca3152020-02-21 14:31:21 -080033import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080034import org.onosproject.store.service.TestStorageService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030035import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
Jonathan Hart77ca3152020-02-21 14:31:21 -080036import org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030037import org.opencord.dhcpl2relay.impl.packet.DhcpOption82;
38
Jonathan Hart617bc3e2020-02-14 10:42:23 -080039import java.nio.ByteBuffer;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080040import java.util.List;
41import java.util.Map;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080042
Jonathan Hart77ca3152020-02-21 14:31:21 -080043import static org.easymock.EasyMock.createMock;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080044import 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 =
Jonathan Hart77ca3152020-02-21 14:31:21 -080052 createMock(ComponentConfigService.class);
Amit Ghosh8951f042017-08-10 13:48:10 +010053
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();
Jonathan Hart77ca3152020-02-21 14:31:21 -080076 store.storageService = new TestStorageService();
77 store.leadershipService = new LeadershipServiceAdapter();
78 store.clusterService = new ClusterServiceAdapter();
79 store.clusterCommunicationService = new ClusterCommunicationServiceAdapter();
80 store.componentConfigService = mockConfigService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030081 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
Jonathan Hart77ca3152020-02-21 14:31:21 -080082 store.activate(new MockComponentContext());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030083 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010084 }
85
86 /**
87 * Tears down the dhcpL2Relay application.
88 */
89 @After
90 public void tearDown() {
91 dhcpL2Relay.deactivate();
92 }
93
94 /**
95 * Tests the DHCP relay app by sending DHCP discovery Packet.
96 *
97 * @throws Exception when an unhandled error occurs
98 */
99 @Test
100 public void testDhcpDiscover() throws Exception {
101 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530102 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100103
104 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
105
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530106 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100107 compareClientPackets(discoverPacket, discoverRelayed);
108 }
109
110 /**
111 * Tests the DHCP relay app by sending DHCP Request Packet.
112 *
113 * @throws Exception when an unhandled error occurs
114 */
115 @Test
116 public void testDhcpRequest() throws Exception {
117 // (1) Sending DHCP discover packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530118 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Amit Ghosh8951f042017-08-10 13:48:10 +0100119
120 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
121
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530122 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100123 compareClientPackets(requestPacket, requestRelayed);
124 }
125
126 /**
127 * Tests the DHCP relay app by sending DHCP Offer Packet.
128 *
129 * @throws Exception when an unhandled error occurs
130 */
131 @Test
132 public void testDhcpOffer() {
133 // (1) Sending DHCP discover packet
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300134 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
135 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100136
137 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
138
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530139 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100140 compareServerPackets(offerPacket, offerRelayed);
141 }
142
143 /**
144 * Tests the DHCP relay app by sending DHCP Ack Packet.
145 *
146 * @throws Exception when an unhandled error occurs
147 */
148 @Test
149 public void testDhcpAck() {
150
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300151 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
152 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100153
154 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
155
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530156 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100157 compareServerPackets(ackPacket, ackRelayed);
158 }
159
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300160 /**
Arjun E K05ad20b2020-03-13 13:25:17 +0000161 * Tests the DHCP relay app by sending DHCP Nak Packet.
162 *
163 * @throws Exception when an unhandled error occurs
164 */
165 @Test
166 public void testDhcpNak() {
167
168 Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC,
169 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
170
171 sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
172
173 Ethernet nakRelayed = (Ethernet) getPacket();
174 compareServerPackets(nakPacket, nakRelayed);
175 }
176
177 /**
178 * Tests the DHCP relay app by sending DHCP Decline Packet.
179 *
180 * @throws Exception when an unhandled error occurs
181 */
182 @Test
183 public void testDhcpDecline() {
184
185 Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC);
186
187 sendPacket(declinePacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
188
189 Ethernet declineRelayed = (Ethernet) getPacket();
190 compareClientPackets(declinePacket, declineRelayed);
191 }
192
193 /**
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300194 * Tests the DHCP global counters.
195 */
196 @Test
197 public void testDhcpGlobalCounters() {
198 long discoveryValue = 0;
199 long offerValue = 0;
200 long requestValue = 0;
201 long ackValue = 0;
202
203 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
204 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
205 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
206 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
207 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
208 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
209
210 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
211 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
212 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
213 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
214
Jonathan Hart77ca3152020-02-21 14:31:21 -0800215 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300216 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800217 DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300218 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800219 DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300220 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800221 DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300222 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800223 DhcpL2RelayCounterNames.valueOf("DHCPACK")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300224
Jonathan Hart77ca3152020-02-21 14:31:21 -0800225 assertEquals(1, discoveryValue);
226 assertEquals(1, offerValue);
227 assertEquals(1, requestValue);
228 assertEquals(1, ackValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300229 }
230
231 /**
232 * Tests the DHCP per subscriber counters.
233 *
234 */
235 @Test
236 public void testDhcpPerSubscriberCounters() {
237 long discoveryValue;
238 long offerValue;
239 long requestValue;
240 long ackValue;
241
242 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
243 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
244 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
245 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
246 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
247 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
248
249 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
250 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
251 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
252 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
253
Jonathan Hart77ca3152020-02-21 14:31:21 -0800254 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300255 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800256 DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300257 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800258 DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300259 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800260 DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300261 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800262 DhcpL2RelayCounterNames.valueOf("DHCPACK")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300263
Jonathan Hart77ca3152020-02-21 14:31:21 -0800264 assertEquals(1, discoveryValue);
265 assertEquals(1, offerValue);
266 assertEquals(1, requestValue);
267 assertEquals(1, ackValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300268 }
269
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530270 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
271 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
272 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100273 sent.setVlanID(CLIENT_C_TAG.toShort());
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000274 sent.setPriorityCode((byte) CLIENT_C_PBIT);
Amit Ghosh8951f042017-08-10 13:48:10 +0100275
276 IPv4 ipv4Packet = (IPv4) sent.getPayload();
277 UDP udpPacket = (UDP) ipv4Packet.getPayload();
278 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
279
Jonathan Hartedbf6422018-05-02 17:30:05 -0700280 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100281 DhcpOption82 option82 = new DhcpOption82();
282 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
283
Jonathan Hartedbf6422018-05-02 17:30:05 -0700284 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100285 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
286 .setData(option82.toByteArray())
287 .setLength(option82.length());
288
289 options.add(options.size() - 1, option);
290 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530291 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100292
293 }
294
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530295 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
296 sent.setDestinationMACAddress(CLIENT_MAC);
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000297 sent.setQinQVID(NOT_PROVIDED);
298 sent.setQinQPriorityCode((byte) NOT_PROVIDED);
Amit Ghosh8951f042017-08-10 13:48:10 +0100299 sent.setVlanID(CLIENT_C_TAG.toShort());
300
301 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530302 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100303 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530304 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100305 0, byteBuffer.array().length);
306 } catch (Exception e) {
307 }
308 assertEquals(expectedPacket, relayed);
309
310 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000311
312 private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800313
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000314 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800315 public void incrementCounter(String counterClass, DhcpL2RelayCounterNames counterType) {
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000316
317 }
318
319 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800320 public void setCounter(String counterClass, DhcpL2RelayCounterNames counterType, Long value) {
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000321
322 }
323
324 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800325 public DhcpL2RelayStatistics getCounters() {
326 return new DhcpL2RelayStatistics();
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000327 }
328
329 @Override
330 public void resetCounters(String counterClass) {
331
332 }
Jonathan Hart77ca3152020-02-21 14:31:21 -0800333
334 @Override
335 public void setDelegate(DhcpL2RelayStoreDelegate delegate) {
336 }
337
338 @Override
339 public void unsetDelegate(DhcpL2RelayStoreDelegate delegate) {
340 }
341
342 @Override
343 public boolean hasDelegate() {
344 return false;
345 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000346 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300347}