blob: e37470b5b80b913dde6c7fa105dfcc6ce28d1d6f [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;
19import static org.junit.Assert.assertEquals;
20import static org.slf4j.LoggerFactory.getLogger;
21
22import java.nio.ByteBuffer;
23import java.util.List;
24import java.util.Map;
25
Amit Ghosh8951f042017-08-10 13:48:10 +010026import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
Jonathan Hartc36c9552018-07-31 15:07:53 -040029import org.onlab.junit.TestUtils;
Amit Ghosh8951f042017-08-10 13:48:10 +010030import org.onlab.packet.DHCP;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053031import org.onlab.packet.Ethernet;
Amit Ghosh8951f042017-08-10 13:48:10 +010032import org.onlab.packet.IPv4;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070033import org.onlab.packet.MacAddress;
Amit Ghosh8951f042017-08-10 13:48:10 +010034import org.onlab.packet.UDP;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070035import org.onlab.packet.VlanId;
Jonathan Hartedbf6422018-05-02 17:30:05 -070036import org.onlab.packet.dhcp.DhcpOption;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030037import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart77ca3152020-02-21 14:31:21 -080038import org.onosproject.cluster.ClusterServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080039import org.onosproject.cluster.LeadershipServiceAdapter;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030040import org.onosproject.net.ConnectPoint;
41import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
Jonathan Hart77ca3152020-02-21 14:31:21 -080042import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080043import org.onosproject.store.service.TestStorageService;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070044import org.opencord.dhcpl2relay.DhcpAllocationInfo;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030045import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
Jonathan Hart77ca3152020-02-21 14:31:21 -080046import org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030047import org.opencord.dhcpl2relay.impl.packet.DhcpOption82;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070048import org.slf4j.Logger;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030049
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070050import com.google.common.collect.Lists;
Amit Ghosh8951f042017-08-10 13:48:10 +010051
52public class DhcpL2RelayTest extends DhcpL2RelayTestBase {
53
Amit Ghosh8951f042017-08-10 13:48:10 +010054 private DhcpL2Relay dhcpL2Relay;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030055 private SimpleDhcpL2RelayCountersStore store;
Saurav Dasbd5ce9c2020-09-04 18:46:45 -070056 private final Logger log = getLogger(getClass());
57 Map<String, DhcpAllocationInfo> allocs;
Amit Ghosh8951f042017-08-10 13:48:10 +010058
59 ComponentConfigService mockConfigService =
Jonathan Hart77ca3152020-02-21 14:31:21 -080060 createMock(ComponentConfigService.class);
Amit Ghosh8951f042017-08-10 13:48:10 +010061
62 /**
63 * Sets up the services required by the dhcpl2relay app.
64 */
65 @Before
66 public void setUp() {
67 dhcpL2Relay = new DhcpL2Relay();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030068 dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry();
Saurav Dasb4e3e102018-10-02 15:31:17 -070069 dhcpL2Relay.coreService = new MockCoreServiceAdapter();
70 dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter();
Amit Ghosh8951f042017-08-10 13:48:10 +010071 dhcpL2Relay.packetService = new MockPacketService();
72 dhcpL2Relay.componentConfigService = mockConfigService;
73 dhcpL2Relay.deviceService = new MockDeviceService();
Gamze Abakac806c6c2018-12-03 12:49:46 +000074 dhcpL2Relay.sadisService = new MockSadisService();
Amit Ghosh8951f042017-08-10 13:48:10 +010075 dhcpL2Relay.hostService = new MockHostService();
76 dhcpL2Relay.mastershipService = new MockMastershipService();
Gamze Abakaa64b3bc2020-01-31 06:51:43 +000077 dhcpL2Relay.dhcpL2RelayCounters = new MockDhcpL2RelayCountersStore();
Jonathan Hart617bc3e2020-02-14 10:42:23 -080078 dhcpL2Relay.storageService = new TestStorageService();
79 dhcpL2Relay.leadershipService = new LeadershipServiceAdapter();
Jonathan Hartc36c9552018-07-31 15:07:53 -040080 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030081 dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService);
82 dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext());
83 store = new SimpleDhcpL2RelayCountersStore();
Jonathan Hart77ca3152020-02-21 14:31:21 -080084 store.storageService = new TestStorageService();
85 store.leadershipService = new LeadershipServiceAdapter();
86 store.clusterService = new ClusterServiceAdapter();
87 store.clusterCommunicationService = new ClusterCommunicationServiceAdapter();
88 store.componentConfigService = mockConfigService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030089 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
Jonathan Hart77ca3152020-02-21 14:31:21 -080090 store.activate(new MockComponentContext());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030091 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Amit Ghosh8951f042017-08-10 13:48:10 +010092 }
93
94 /**
95 * Tears down the dhcpL2Relay application.
96 */
97 @After
98 public void tearDown() {
99 dhcpL2Relay.deactivate();
100 }
101
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700102 private void checkAllocation(DHCP.MsgType messageType) {
103 ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
104 + String.valueOf(CLIENT_PORT));
105 allocs = dhcpL2Relay.getAllocationInfo();
106 assert allocs.size() == 1;
107 allocs.forEach((k, v) -> {
108 log.info("Allocation {} : {}", k, v);
109 assertEquals(v.type(), messageType);
110 assertEquals(v.macAddress(), CLIENT_MAC);
111 assertEquals(v.location(), clientCp);
112 });
113 }
114
115 @Test
116 public void testMultipleAllocations() throws Exception {
117 dhcpL2Relay.clearAllocations();
118 // Trigger a discover from one RG on port 32
119 MacAddress mac32 = MacAddress.valueOf("b4:96:91:0c:4f:e4");
120 VlanId vlan32a = VlanId.vlanId((short) 801);
121 Ethernet discover32a = constructDhcpDiscoverPacket(
122 mac32, vlan32a, (short) 0);
123 ConnectPoint client32 = ConnectPoint.deviceConnectPoint(
124 "of:0000b86a974385f7/32");
125 sendPacket(discover32a, client32);
126 allocs = dhcpL2Relay.getAllocationInfo();
127 assert allocs.size() == 1;
128
129 //Trigger a discover from another RG on port 4112
130 MacAddress mac4112 = MacAddress.valueOf("b4:96:91:0c:4f:c9");
131 VlanId vlan4112 = VlanId.vlanId((short) 101);
132 Ethernet discover4112 = constructDhcpDiscoverPacket(
133 mac4112, vlan4112,
134 (short) 0);
135 ConnectPoint client4112 = ConnectPoint.deviceConnectPoint(
136 "of:0000b86a974385f7/4112");
137 sendPacket(discover4112, client4112);
138 allocs = dhcpL2Relay.getAllocationInfo();
139 assert allocs.size() == 2;
140
141 // Trigger a discover for another service with a different vlan
142 // from the same UNI port 32
143 VlanId vlan32b = VlanId.vlanId((short) 802);
144 Ethernet discover32b = constructDhcpDiscoverPacket(
145 mac32, vlan32b, (short) 0);
146 sendPacket(discover32b, client32);
147 allocs = dhcpL2Relay.getAllocationInfo();
148 assert allocs.size() == 3;
149
150 allocs.forEach((k, v) -> {
151 log.info("Allocation {} : {}", k, v);
152 assertEquals(v.type(), DHCP.MsgType.DHCPDISCOVER);
153 if (v.subscriberId().equals("ALPHe3d1cea3-1")) {
154 assertEquals(v.macAddress(), mac32);
155 assertEquals(v.location(), client32);
156 if (!(v.vlanId().equals(vlan32a) || v.vlanId().equals(vlan32b))) {
157 assert false;
158 }
159 } else if (v.subscriberId().equals("ALPHe3d1ceb7-1")) {
160 assertEquals(v.macAddress(), mac4112);
161 assertEquals(v.location(), client4112);
162 assertEquals(v.vlanId(), vlan4112);
163 } else {
164 assert false;
165 }
166 });
167
168 dhcpL2Relay.clearAllocations();
169 assert dhcpL2Relay.getAllocationInfo().size() == 0;
170 }
171
Amit Ghosh8951f042017-08-10 13:48:10 +0100172 /**
173 * Tests the DHCP relay app by sending DHCP discovery Packet.
174 *
175 * @throws Exception when an unhandled error occurs
176 */
177 @Test
178 public void testDhcpDiscover() throws Exception {
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700179 // Sending DHCP Discover packet
180 dhcpL2Relay.clearAllocations();
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530181 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700182 ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
183 + String.valueOf(CLIENT_PORT));
184 sendPacket(discoverPacket, clientCp);
Amit Ghosh8951f042017-08-10 13:48:10 +0100185
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530186 Ethernet discoverRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100187 compareClientPackets(discoverPacket, discoverRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700188 checkAllocation(DHCP.MsgType.DHCPDISCOVER);
Amit Ghosh8951f042017-08-10 13:48:10 +0100189 }
190
191 /**
192 * Tests the DHCP relay app by sending DHCP Request Packet.
193 *
194 * @throws Exception when an unhandled error occurs
195 */
196 @Test
197 public void testDhcpRequest() throws Exception {
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700198 // Sending DHCP Request packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530199 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700200 ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
201 + String.valueOf(CLIENT_PORT));
202 sendPacket(requestPacket, clientCp);
Amit Ghosh8951f042017-08-10 13:48:10 +0100203
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530204 Ethernet requestRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100205 compareClientPackets(requestPacket, requestRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700206 checkAllocation(DHCP.MsgType.DHCPREQUEST);
Amit Ghosh8951f042017-08-10 13:48:10 +0100207 }
208
209 /**
210 * Tests the DHCP relay app by sending DHCP Offer Packet.
211 *
212 * @throws Exception when an unhandled error occurs
213 */
214 @Test
215 public void testDhcpOffer() {
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700216 // Sending DHCP Offer packet
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300217 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
218 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700219 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
220 + String.valueOf(UPLINK_PORT)));
Amit Ghosh8951f042017-08-10 13:48:10 +0100221
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530222 Ethernet offerRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100223 compareServerPackets(offerPacket, offerRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700224 checkAllocation(DHCP.MsgType.DHCPOFFER);
Amit Ghosh8951f042017-08-10 13:48:10 +0100225 }
226
227 /**
228 * Tests the DHCP relay app by sending DHCP Ack Packet.
229 *
230 * @throws Exception when an unhandled error occurs
231 */
232 @Test
233 public void testDhcpAck() {
234
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300235 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
236 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
Amit Ghosh8951f042017-08-10 13:48:10 +0100237
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700238 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
239 + String.valueOf(UPLINK_PORT)));
Amit Ghosh8951f042017-08-10 13:48:10 +0100240
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530241 Ethernet ackRelayed = (Ethernet) getPacket();
Amit Ghosh8951f042017-08-10 13:48:10 +0100242 compareServerPackets(ackPacket, ackRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700243 checkAllocation(DHCP.MsgType.DHCPACK);
Amit Ghosh8951f042017-08-10 13:48:10 +0100244 }
245
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300246 /**
Arjun E K05ad20b2020-03-13 13:25:17 +0000247 * Tests the DHCP relay app by sending DHCP Nak Packet.
248 *
249 * @throws Exception when an unhandled error occurs
250 */
251 @Test
252 public void testDhcpNak() {
253
254 Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC,
255 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
256
257 sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
258
259 Ethernet nakRelayed = (Ethernet) getPacket();
260 compareServerPackets(nakPacket, nakRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700261 checkAllocation(DHCP.MsgType.DHCPNAK);
Arjun E K05ad20b2020-03-13 13:25:17 +0000262 }
263
264 /**
265 * Tests the DHCP relay app by sending DHCP Decline Packet.
266 *
267 * @throws Exception when an unhandled error occurs
268 */
269 @Test
270 public void testDhcpDecline() {
271
272 Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC);
273
274 sendPacket(declinePacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
275
276 Ethernet declineRelayed = (Ethernet) getPacket();
277 compareClientPackets(declinePacket, declineRelayed);
Saurav Dasbd5ce9c2020-09-04 18:46:45 -0700278 checkAllocation(DHCP.MsgType.DHCPDECLINE);
Arjun E K05ad20b2020-03-13 13:25:17 +0000279 }
280
281 /**
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300282 * Tests the DHCP global counters.
283 */
284 @Test
285 public void testDhcpGlobalCounters() {
286 long discoveryValue = 0;
287 long offerValue = 0;
288 long requestValue = 0;
289 long ackValue = 0;
290
291 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
292 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
293 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
294 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
295 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
296 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
297
298 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
299 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
300 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
301 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
302
Jonathan Hart77ca3152020-02-21 14:31:21 -0800303 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300304 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800305 DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300306 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800307 DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300308 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800309 DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300310 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800311 DhcpL2RelayCounterNames.valueOf("DHCPACK")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300312
Jonathan Hart77ca3152020-02-21 14:31:21 -0800313 assertEquals(1, discoveryValue);
314 assertEquals(1, offerValue);
315 assertEquals(1, requestValue);
316 assertEquals(1, ackValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300317 }
318
319 /**
320 * Tests the DHCP per subscriber counters.
321 *
322 */
323 @Test
324 public void testDhcpPerSubscriberCounters() {
325 long discoveryValue;
326 long offerValue;
327 long requestValue;
328 long ackValue;
329
330 Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
331 Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
332 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
333 Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
334 Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
335 CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
336
337 sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
338 sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
339 sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
340 sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
341
Jonathan Hart77ca3152020-02-21 14:31:21 -0800342 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300343 discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800344 DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300345 offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800346 DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300347 requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800348 DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300349 ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800350 DhcpL2RelayCounterNames.valueOf("DHCPACK")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300351
Jonathan Hart77ca3152020-02-21 14:31:21 -0800352 assertEquals(1, discoveryValue);
353 assertEquals(1, offerValue);
354 assertEquals(1, requestValue);
355 assertEquals(1, ackValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300356 }
357
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530358 public void compareClientPackets(Ethernet sent, Ethernet relayed) {
359 sent.setSourceMACAddress(OLT_MAC_ADDRESS);
360 sent.setQinQVID(CLIENT_S_TAG.toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100361 sent.setVlanID(CLIENT_C_TAG.toShort());
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000362 sent.setPriorityCode((byte) CLIENT_C_PBIT);
Amit Ghosh8951f042017-08-10 13:48:10 +0100363
364 IPv4 ipv4Packet = (IPv4) sent.getPayload();
365 UDP udpPacket = (UDP) ipv4Packet.getPayload();
366 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
367
Jonathan Hartedbf6422018-05-02 17:30:05 -0700368 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Amit Ghosh8951f042017-08-10 13:48:10 +0100369 DhcpOption82 option82 = new DhcpOption82();
370 option82.setAgentCircuitId(CLIENT_CIRCUIT_ID);
371
Jonathan Hartedbf6422018-05-02 17:30:05 -0700372 DhcpOption option = new DhcpOption()
Amit Ghosh8951f042017-08-10 13:48:10 +0100373 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
374 .setData(option82.toByteArray())
375 .setLength(option82.length());
376
377 options.add(options.size() - 1, option);
378 dhcpPacket.setOptions(options);
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530379 assertEquals(sent, relayed);
Amit Ghosh8951f042017-08-10 13:48:10 +0100380
381 }
382
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530383 public void compareServerPackets(Ethernet sent, Ethernet relayed) {
384 sent.setDestinationMACAddress(CLIENT_MAC);
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000385 sent.setQinQVID(NOT_PROVIDED);
386 sent.setQinQPriorityCode((byte) NOT_PROVIDED);
Amit Ghosh8951f042017-08-10 13:48:10 +0100387 sent.setVlanID(CLIENT_C_TAG.toShort());
388
389 final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530390 Ethernet expectedPacket = null;
Amit Ghosh8951f042017-08-10 13:48:10 +0100391 try {
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530392 expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
Amit Ghosh8951f042017-08-10 13:48:10 +0100393 0, byteBuffer.array().length);
394 } catch (Exception e) {
395 }
396 assertEquals(expectedPacket, relayed);
397
398 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000399
400 private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800401
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000402 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800403 public void incrementCounter(String counterClass, DhcpL2RelayCounterNames counterType) {
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000404
405 }
406
407 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800408 public void setCounter(String counterClass, DhcpL2RelayCounterNames counterType, Long value) {
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000409
410 }
411
412 @Override
Jonathan Hart77ca3152020-02-21 14:31:21 -0800413 public DhcpL2RelayStatistics getCounters() {
414 return new DhcpL2RelayStatistics();
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000415 }
416
417 @Override
418 public void resetCounters(String counterClass) {
419
420 }
Jonathan Hart77ca3152020-02-21 14:31:21 -0800421
422 @Override
423 public void setDelegate(DhcpL2RelayStoreDelegate delegate) {
424 }
425
426 @Override
427 public void unsetDelegate(DhcpL2RelayStoreDelegate delegate) {
428 }
429
430 @Override
431 public boolean hasDelegate() {
432 return false;
433 }
Gamze Abakaa64b3bc2020-01-31 06:51:43 +0000434 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300435}