blob: c19b2ebb7a87fe0ba6934f8fa310b830a6ff941d [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 Dasbd5ce9c2020-09-04 18:46:45 -070018import static org.easymock.EasyMock.createMock;
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -080019import static org.junit.Assert.*;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070020import static org.slf4j.LoggerFactory.getLogger;
21
22import java.nio.ByteBuffer;
23import java.util.List;
24import java.util.Map;
25
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -080026import com.google.common.util.concurrent.MoreExecutors;
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;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070034import org.onlab.packet.MacAddress;
Amit Ghosh8951f042017-08-10 13:48:10 +010035import org.onlab.packet.UDP;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070036import org.onlab.packet.VlanId;
Jonathan Hartedbf6422018-05-02 17:30:05 -070037import org.onlab.packet.dhcp.DhcpOption;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030038import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart77ca3152020-02-21 14:31:21 -080039import org.onosproject.cluster.ClusterServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080040import org.onosproject.cluster.LeadershipServiceAdapter;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030041import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
Jonathan Hart77ca3152020-02-21 14:31:21 -080043import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080044import org.onosproject.store.service.TestStorageService;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070045import org.opencord.dhcpl2relay.DhcpAllocationInfo;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030046import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
Jonathan Hart77ca3152020-02-21 14:31:21 -080047import org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030048import org.opencord.dhcpl2relay.impl.packet.DhcpOption82;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070049import org.slf4j.Logger;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030050
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070051import com.google.common.collect.Lists;
Amit Ghosh8951f042017-08-10 13:48:10 +010052
53public class DhcpL2RelayTest extends DhcpL2RelayTestBase {
54
Amit Ghosh8951f042017-08-10 13:48:10 +010055 private DhcpL2Relay dhcpL2Relay;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030056 private SimpleDhcpL2RelayCountersStore store;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070057 private final Logger log = getLogger(getClass());
58 Map<String, DhcpAllocationInfo> allocs;
Amit Ghosh8951f042017-08-10 13:48:10 +010059
60 ComponentConfigService mockConfigService =
Jonathan Hart77ca3152020-02-21 14:31:21 -080061 createMock(ComponentConfigService.class);
Amit Ghosh8951f042017-08-10 13:48:10 +010062
63 /**
64 * Sets up the services required by the dhcpl2relay app.
65 */
66 @Before
67 public void setUp() {
68 dhcpL2Relay = new DhcpL2Relay();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030069 dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry();
Saurav Dasb4e3e102018-10-02 15:31:17 -070070 dhcpL2Relay.coreService = new MockCoreServiceAdapter();
71 dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter();
Amit Ghosh8951f042017-08-10 13:48:10 +010072 dhcpL2Relay.packetService = new MockPacketService();
73 dhcpL2Relay.componentConfigService = mockConfigService;
74 dhcpL2Relay.deviceService = new MockDeviceService();
Gamze Abakac806c6c2018-12-03 12:49:46 +000075 dhcpL2Relay.sadisService = new MockSadisService();
Amit Ghosh8951f042017-08-10 13:48:10 +010076 dhcpL2Relay.hostService = new MockHostService();
77 dhcpL2Relay.mastershipService = new MockMastershipService();
Gamze Abakaa64b3bc2020-01-31 06:51:43 +000078 dhcpL2Relay.dhcpL2RelayCounters = new MockDhcpL2RelayCountersStore();
Jonathan Hart617bc3e2020-02-14 10:42:23 -080079 dhcpL2Relay.storageService = new TestStorageService();
80 dhcpL2Relay.leadershipService = new LeadershipServiceAdapter();
Jonathan Hartc36c9552018-07-31 15:07:53 -040081 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030082 dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService);
83 dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext());
84 store = new SimpleDhcpL2RelayCountersStore();
Jonathan Hart77ca3152020-02-21 14:31:21 -080085 store.storageService = new TestStorageService();
86 store.leadershipService = new LeadershipServiceAdapter();
87 store.clusterService = new ClusterServiceAdapter();
88 store.clusterCommunicationService = new ClusterCommunicationServiceAdapter();
89 store.componentConfigService = mockConfigService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030090 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -080091 TestUtils.setField(dhcpL2Relay, "packetProcessorExecutor", MoreExecutors.newDirectExecutorService());
Jonathan Hart77ca3152020-02-21 14:31:21 -080092 store.activate(new MockComponentContext());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030093 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010094 }
95
96 /**
97 * Tears down the dhcpL2Relay application.
98 */
99 @After
100 public void tearDown() {
101 dhcpL2Relay.deactivate();
102 }
103
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700104 private void checkAllocation(DHCP.MsgType messageType) {
105 ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800106 + String.valueOf(CLIENT_PORT));
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700107 allocs = dhcpL2Relay.getAllocationInfo();
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800108 assertEquals(1, allocs.size());
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700109 allocs.forEach((k, v) -> {
110 log.info("Allocation {} : {}", k, v);
111 assertEquals(v.type(), messageType);
112 assertEquals(v.macAddress(), CLIENT_MAC);
113 assertEquals(v.location(), clientCp);
114 });
115 }
116
117 @Test
118 public void testMultipleAllocations() throws Exception {
119 dhcpL2Relay.clearAllocations();
120 // Trigger a discover from one RG on port 32
121 MacAddress mac32 = MacAddress.valueOf("b4:96:91:0c:4f:e4");
122 VlanId vlan32a = VlanId.vlanId((short) 801);
123 Ethernet discover32a = constructDhcpDiscoverPacket(
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800124 mac32, vlan32a, (short) 0);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700125 ConnectPoint client32 = ConnectPoint.deviceConnectPoint(
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800126 "of:0000b86a974385f7/32");
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700127 sendPacket(discover32a, client32);
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800128
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700129 allocs = dhcpL2Relay.getAllocationInfo();
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800130 assertEquals(1, allocs.size());
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700131
132 //Trigger a discover from another RG on port 4112
133 MacAddress mac4112 = MacAddress.valueOf("b4:96:91:0c:4f:c9");
134 VlanId vlan4112 = VlanId.vlanId((short) 101);
135 Ethernet discover4112 = constructDhcpDiscoverPacket(
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800136 mac4112, vlan4112,
137 (short) 0);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700138 ConnectPoint client4112 = ConnectPoint.deviceConnectPoint(
139 "of:0000b86a974385f7/4112");
140 sendPacket(discover4112, client4112);
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800141
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700142 allocs = dhcpL2Relay.getAllocationInfo();
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800143 assertEquals(2, allocs.size());
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700144
145 // Trigger a discover for another service with a different vlan
146 // from the same UNI port 32
147 VlanId vlan32b = VlanId.vlanId((short) 802);
148 Ethernet discover32b = constructDhcpDiscoverPacket(
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800149 mac32, vlan32b, (short) 0);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700150 sendPacket(discover32b, client32);
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800151
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700152 allocs = dhcpL2Relay.getAllocationInfo();
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800153 assertEquals(3, allocs.size());
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700154
155 allocs.forEach((k, v) -> {
156 log.info("Allocation {} : {}", k, v);
157 assertEquals(v.type(), DHCP.MsgType.DHCPDISCOVER);
158 if (v.subscriberId().equals("ALPHe3d1cea3-1")) {
159 assertEquals(v.macAddress(), mac32);
160 assertEquals(v.location(), client32);
161 if (!(v.vlanId().equals(vlan32a) || v.vlanId().equals(vlan32b))) {
162 assert false;
163 }
164 } else if (v.subscriberId().equals("ALPHe3d1ceb7-1")) {
165 assertEquals(v.macAddress(), mac4112);
166 assertEquals(v.location(), client4112);
167 assertEquals(v.vlanId(), vlan4112);
168 } else {
169 assert false;
170 }
171 });
172
173 dhcpL2Relay.clearAllocations();
174 assert dhcpL2Relay.getAllocationInfo().size() == 0;
175 }
176
Amit Ghosh8951f042017-08-10 13:48:10 +0100177 /**
178 * Tests the DHCP relay app by sending DHCP discovery Packet.
179 *
180 * @throws Exception when an unhandled error occurs
181 */
182 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800183 public void testDhcpDiscover() throws Exception {
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700184 // Sending DHCP Discover packet
185 dhcpL2Relay.clearAllocations();
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530186 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700187 ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800188 + String.valueOf(CLIENT_PORT));
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700189 sendPacket(discoverPacket, clientCp);
Amit Ghosh8951f042017-08-10 13:48:10 +0100190
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530191 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100192 compareClientPackets(discoverPacket, discoverRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700193 checkAllocation(DHCP.MsgType.DHCPDISCOVER);
Amit Ghosh8951f042017-08-10 13:48:10 +0100194 }
195
196 /**
197 * Tests the DHCP relay app by sending DHCP Request Packet.
198 *
199 * @throws Exception when an unhandled error occurs
200 */
201 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800202 public void testDhcpRequest() throws Exception {
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700203 // Sending DHCP Request packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530204 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700205 ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800206 + String.valueOf(CLIENT_PORT));
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700207 sendPacket(requestPacket, clientCp);
Amit Ghosh8951f042017-08-10 13:48:10 +0100208
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530209 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100210 compareClientPackets(requestPacket, requestRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700211 checkAllocation(DHCP.MsgType.DHCPREQUEST);
Amit Ghosh8951f042017-08-10 13:48:10 +0100212 }
213
214 /**
215 * Tests the DHCP relay app by sending DHCP Offer Packet.
216 *
217 * @throws Exception when an unhandled error occurs
218 */
219 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800220 public void testDhcpOffer() throws InterruptedException {
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700221 // Sending DHCP Offer packet
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300222 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800223 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700224 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800225 + String.valueOf(UPLINK_PORT)));
Amit Ghosh8951f042017-08-10 13:48:10 +0100226
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530227 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100228 compareServerPackets(offerPacket, offerRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700229 checkAllocation(DHCP.MsgType.DHCPOFFER);
Amit Ghosh8951f042017-08-10 13:48:10 +0100230 }
231
232 /**
233 * Tests the DHCP relay app by sending DHCP Ack Packet.
234 *
235 * @throws Exception when an unhandled error occurs
236 */
237 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800238 public void testDhcpAck() throws InterruptedException {
Amit Ghosh8951f042017-08-10 13:48:10 +0100239
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300240 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800241 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100242
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700243 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800244 + String.valueOf(UPLINK_PORT)));
Amit Ghosh8951f042017-08-10 13:48:10 +0100245
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530246 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100247 compareServerPackets(ackPacket, ackRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700248 checkAllocation(DHCP.MsgType.DHCPACK);
Amit Ghosh8951f042017-08-10 13:48:10 +0100249 }
250
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300251 /**
Arjun E K05ad20b2020-03-13 13:25:17 +0000252 * Tests the DHCP relay app by sending DHCP Nak Packet.
253 *
254 * @throws Exception when an unhandled error occurs
255 */
256 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800257 public void testDhcpNak() throws InterruptedException {
Arjun E K05ad20b2020-03-13 13:25:17 +0000258
259 Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC,
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800260 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Arjun E K05ad20b2020-03-13 13:25:17 +0000261
262 sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
263
264 Ethernet nakRelayed = (Ethernet) getPacket();
265 compareServerPackets(nakPacket, nakRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700266 checkAllocation(DHCP.MsgType.DHCPNAK);
Arjun E K05ad20b2020-03-13 13:25:17 +0000267 }
268
269 /**
270 * Tests the DHCP relay app by sending DHCP Decline Packet.
271 *
272 * @throws Exception when an unhandled error occurs
273 */
274 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800275 public void testDhcpDecline() throws InterruptedException {
Arjun E K05ad20b2020-03-13 13:25:17 +0000276
277 Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC);
278
279 sendPacket(declinePacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
280
281 Ethernet declineRelayed = (Ethernet) getPacket();
282 compareClientPackets(declinePacket, declineRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700283 checkAllocation(DHCP.MsgType.DHCPDECLINE);
Arjun E K05ad20b2020-03-13 13:25:17 +0000284 }
285
286 /**
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300287 * Tests the DHCP global counters.
288 */
289 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800290 public void testDhcpGlobalCounters() throws InterruptedException {
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300291
292 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
293 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800294 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300295 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
296 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800297 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300298
299 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
300 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
301 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
302 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
303
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300304
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800305 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
306 long discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
307 DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
308 long offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
309 DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
310 long requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
311 DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
312 long ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
313 DhcpL2RelayCounterNames.valueOf("DHCPACK")));
Jonathan Hart77ca3152020-02-21 14:31:21 -0800314 assertEquals(1, discoveryValue);
315 assertEquals(1, offerValue);
316 assertEquals(1, requestValue);
317 assertEquals(1, ackValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300318 }
319
320 /**
321 * Tests the DHCP per subscriber counters.
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300322 */
323 @Test
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800324 public void testDhcpPerSubscriberCounters() throws Exception {
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300325
326 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
327 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
328 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
329 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
330 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
331 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
332
333 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
334 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
335 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
336 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
337
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300338
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800339 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
340 long discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
341 DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
342 long offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
343 DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
344 long requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
345 DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
346 long ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
347 DhcpL2RelayCounterNames.valueOf("DHCPACK")));
Jonathan Hart77ca3152020-02-21 14:31:21 -0800348 assertEquals(1, discoveryValue);
349 assertEquals(1, offerValue);
350 assertEquals(1, requestValue);
351 assertEquals(1, ackValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300352 }
353
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530354 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
355 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
356 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100357 sent.setVlanID(CLIENT_C_TAG.toShort());
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000358 sent.setPriorityCode((byte) CLIENT_C_PBIT);
Amit Ghosh8951f042017-08-10 13:48:10 +0100359
360 IPv4 ipv4Packet = (IPv4) sent.getPayload();
361 UDP udpPacket = (UDP) ipv4Packet.getPayload();
362 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
363
Jonathan Hartedbf6422018-05-02 17:30:05 -0700364 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100365 DhcpOption82 option82 = new DhcpOption82();
366 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
367
Jonathan Hartedbf6422018-05-02 17:30:05 -0700368 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100369 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
370 .setData(option82.toByteArray())
371 .setLength(option82.length());
372
373 options.add(options.size() - 1, option);
374 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530375 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100376
377 }
378
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530379 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
Amit Ghosh8951f042017-08-10 13:48:10 +0100380
Amit Ghosh8951f042017-08-10 13:48:10 +0100381 try {
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800382 sent.setDestinationMACAddress(CLIENT_MAC);
383 sent.setQinQVID(NOT_PROVIDED);
384 sent.setQinQPriorityCode((byte) NOT_PROVIDED);
385 sent.setVlanID(CLIENT_C_TAG.toShort());
386
387 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
388 Ethernet expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
389 0, byteBuffer.array().length);
390 assertEquals(expectedPacket, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100391 } catch (Exception e) {
Matteo Scandoloa8b6eea2018-11-27 13:47:58 -0800392 log.error(e.getMessage());
393 fail();
Amit Ghosh8951f042017-08-10 13:48:10 +0100394 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100395
396 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000397
398 private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800399
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000400 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800401 public void incrementCounter(String counterClass, DhcpL2RelayCounterNames counterType) {
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000402
403 }
404
405 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800406 public void setCounter(String counterClass, DhcpL2RelayCounterNames counterType, Long value) {
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000407
408 }
409
410 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800411 public DhcpL2RelayStatistics getCounters() {
412 return new DhcpL2RelayStatistics();
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000413 }
414
415 @Override
416 public void resetCounters(String counterClass) {
417
418 }
Jonathan Hart77ca3152020-02-21 14:31:21 -0800419
420 @Override
421 public void setDelegate(DhcpL2RelayStoreDelegate delegate) {
422 }
423
424 @Override
425 public void unsetDelegate(DhcpL2RelayStoreDelegate delegate) {
426 }
427
428 @Override
429 public boolean hasDelegate() {
430 return false;
431 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000432 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300433}