Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1 | /* |
| 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 Scandolo | 57af5d1 | 2019-04-29 17:11:41 -0700 | [diff] [blame] | 16 | package org.opencord.dhcpl2relay.impl; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 17 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 22 | import org.onlab.junit.TestUtils; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 23 | import org.onlab.packet.DHCP; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 25 | import org.onlab.packet.IPv4; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 26 | import org.onlab.packet.UDP; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 27 | import org.onlab.packet.dhcp.DhcpOption; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 28 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 29 | import org.onosproject.cluster.ClusterServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.LeadershipServiceAdapter; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 33 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 34 | import org.onosproject.store.service.TestStorageService; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 35 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 36 | import org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 37 | import org.opencord.dhcpl2relay.impl.packet.DhcpOption82; |
| 38 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 39 | import java.nio.ByteBuffer; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 40 | import java.util.List; |
| 41 | import java.util.Map; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 42 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 43 | import static org.easymock.EasyMock.createMock; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 44 | import static org.junit.Assert.assertEquals; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 45 | |
| 46 | public class DhcpL2RelayTest extends DhcpL2RelayTestBase { |
| 47 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 48 | private DhcpL2Relay dhcpL2Relay; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 49 | private SimpleDhcpL2RelayCountersStore store; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 50 | |
| 51 | ComponentConfigService mockConfigService = |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 52 | createMock(ComponentConfigService.class); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Sets up the services required by the dhcpl2relay app. |
| 56 | */ |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | dhcpL2Relay = new DhcpL2Relay(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 60 | dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 61 | dhcpL2Relay.coreService = new MockCoreServiceAdapter(); |
| 62 | dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 63 | dhcpL2Relay.packetService = new MockPacketService(); |
| 64 | dhcpL2Relay.componentConfigService = mockConfigService; |
| 65 | dhcpL2Relay.deviceService = new MockDeviceService(); |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 66 | dhcpL2Relay.sadisService = new MockSadisService(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 67 | dhcpL2Relay.hostService = new MockHostService(); |
| 68 | dhcpL2Relay.mastershipService = new MockMastershipService(); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 69 | dhcpL2Relay.dhcpL2RelayCounters = new MockDhcpL2RelayCountersStore(); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 70 | dhcpL2Relay.storageService = new TestStorageService(); |
| 71 | dhcpL2Relay.leadershipService = new LeadershipServiceAdapter(); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 72 | TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 73 | dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService); |
| 74 | dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext()); |
| 75 | store = new SimpleDhcpL2RelayCountersStore(); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 76 | 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 81 | TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher()); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 82 | store.activate(new MockComponentContext()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 83 | dhcpL2Relay.dhcpL2RelayCounters = this.store; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 84 | } |
| 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 Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 102 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 103 | |
| 104 | sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 105 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 106 | Ethernet discoverRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 107 | 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 Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 118 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 119 | |
| 120 | sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 121 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 122 | Ethernet requestRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 123 | 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 134 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, |
| 135 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 136 | |
| 137 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 138 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 139 | Ethernet offerRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 140 | 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 151 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, |
| 152 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 153 | |
| 154 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 155 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 156 | Ethernet ackRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 157 | compareServerPackets(ackPacket, ackRelayed); |
| 158 | } |
| 159 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 160 | /** |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 161 | * 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 194 | * 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 215 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 216 | discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 217 | DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 218 | offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 219 | DhcpL2RelayCounterNames.valueOf("DHCPOFFER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 220 | requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 221 | DhcpL2RelayCounterNames.valueOf("DHCPREQUEST"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 222 | ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 223 | DhcpL2RelayCounterNames.valueOf("DHCPACK"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 224 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 225 | assertEquals(1, discoveryValue); |
| 226 | assertEquals(1, offerValue); |
| 227 | assertEquals(1, requestValue); |
| 228 | assertEquals(1, ackValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 229 | } |
| 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 254 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 255 | discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 256 | DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 257 | offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 258 | DhcpL2RelayCounterNames.valueOf("DHCPOFFER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 259 | requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 260 | DhcpL2RelayCounterNames.valueOf("DHCPREQUEST"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 261 | ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 262 | DhcpL2RelayCounterNames.valueOf("DHCPACK"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 263 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 264 | assertEquals(1, discoveryValue); |
| 265 | assertEquals(1, offerValue); |
| 266 | assertEquals(1, requestValue); |
| 267 | assertEquals(1, ackValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 268 | } |
| 269 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 270 | public void compareClientPackets(Ethernet sent, Ethernet relayed) { |
| 271 | sent.setSourceMACAddress(OLT_MAC_ADDRESS); |
| 272 | sent.setQinQVID(CLIENT_S_TAG.toShort()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 273 | sent.setVlanID(CLIENT_C_TAG.toShort()); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 274 | sent.setPriorityCode((byte) CLIENT_C_PBIT); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 275 | |
| 276 | IPv4 ipv4Packet = (IPv4) sent.getPayload(); |
| 277 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 278 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 279 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 280 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 281 | DhcpOption82 option82 = new DhcpOption82(); |
| 282 | option82.setAgentCircuitId(CLIENT_CIRCUIT_ID); |
| 283 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 284 | DhcpOption option = new DhcpOption() |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 285 | .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 Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 291 | assertEquals(sent, relayed); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 292 | |
| 293 | } |
| 294 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 295 | public void compareServerPackets(Ethernet sent, Ethernet relayed) { |
| 296 | sent.setDestinationMACAddress(CLIENT_MAC); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 297 | sent.setQinQVID(NOT_PROVIDED); |
| 298 | sent.setQinQPriorityCode((byte) NOT_PROVIDED); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 299 | sent.setVlanID(CLIENT_C_TAG.toShort()); |
| 300 | |
| 301 | final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 302 | Ethernet expectedPacket = null; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 303 | try { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 304 | expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(), |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 305 | 0, byteBuffer.array().length); |
| 306 | } catch (Exception e) { |
| 307 | } |
| 308 | assertEquals(expectedPacket, relayed); |
| 309 | |
| 310 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 311 | |
| 312 | private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore { |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 313 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 314 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 315 | public void incrementCounter(String counterClass, DhcpL2RelayCounterNames counterType) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 316 | |
| 317 | } |
| 318 | |
| 319 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 320 | public void setCounter(String counterClass, DhcpL2RelayCounterNames counterType, Long value) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 321 | |
| 322 | } |
| 323 | |
| 324 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 325 | public DhcpL2RelayStatistics getCounters() { |
| 326 | return new DhcpL2RelayStatistics(); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void resetCounters(String counterClass) { |
| 331 | |
| 332 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 333 | |
| 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 Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 346 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 347 | } |