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 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 18 | import static org.easymock.EasyMock.createMock; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.fail; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 26 | import java.util.stream.Collectors; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 27 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 31 | import org.onlab.junit.TestUtils; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 32 | import org.onlab.packet.DHCP; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 33 | import org.onlab.packet.DeserializationException; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 34 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 35 | import org.onlab.packet.IPv4; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 36 | import org.onlab.packet.MacAddress; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 37 | import org.onlab.packet.UDP; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 38 | import org.onlab.packet.VlanId; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 39 | import org.onlab.packet.dhcp.DhcpOption; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 40 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 41 | import org.onosproject.cluster.ClusterServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 42 | import org.onosproject.cluster.LeadershipServiceAdapter; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 43 | import org.onosproject.net.ConnectPoint; |
| 44 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 45 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 46 | import org.onosproject.store.service.TestStorageService; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 47 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 48 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 49 | import org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 50 | import org.opencord.dhcpl2relay.impl.packet.DhcpOption82Data; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 51 | import org.slf4j.Logger; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 52 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 53 | import com.google.common.collect.Lists; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 54 | import com.google.common.util.concurrent.MoreExecutors; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 55 | |
| 56 | public class DhcpL2RelayTest extends DhcpL2RelayTestBase { |
| 57 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 58 | private DhcpL2Relay dhcpL2Relay; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 59 | private SimpleDhcpL2RelayCountersStore store; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 60 | private final Logger log = getLogger(getClass()); |
| 61 | Map<String, DhcpAllocationInfo> allocs; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 62 | |
| 63 | ComponentConfigService mockConfigService = |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 64 | createMock(ComponentConfigService.class); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Sets up the services required by the dhcpl2relay app. |
| 68 | */ |
| 69 | @Before |
| 70 | public void setUp() { |
| 71 | dhcpL2Relay = new DhcpL2Relay(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 72 | dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 73 | dhcpL2Relay.coreService = new MockCoreServiceAdapter(); |
| 74 | dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 75 | dhcpL2Relay.packetService = new MockPacketService(); |
| 76 | dhcpL2Relay.componentConfigService = mockConfigService; |
| 77 | dhcpL2Relay.deviceService = new MockDeviceService(); |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 78 | dhcpL2Relay.sadisService = new MockSadisService(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 79 | dhcpL2Relay.hostService = new MockHostService(); |
| 80 | dhcpL2Relay.mastershipService = new MockMastershipService(); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 81 | dhcpL2Relay.dhcpL2RelayCounters = new MockDhcpL2RelayCountersStore(); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 82 | dhcpL2Relay.storageService = new TestStorageService(); |
| 83 | dhcpL2Relay.leadershipService = new LeadershipServiceAdapter(); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 84 | TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 85 | dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService); |
| 86 | dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext()); |
| 87 | store = new SimpleDhcpL2RelayCountersStore(); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 88 | store.storageService = new TestStorageService(); |
| 89 | store.leadershipService = new LeadershipServiceAdapter(); |
| 90 | store.clusterService = new ClusterServiceAdapter(); |
| 91 | store.clusterCommunicationService = new ClusterCommunicationServiceAdapter(); |
| 92 | store.componentConfigService = mockConfigService; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 93 | TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher()); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 94 | TestUtils.setField(dhcpL2Relay, "packetProcessorExecutor", MoreExecutors.newDirectExecutorService()); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 95 | store.activate(new MockComponentContext()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 96 | dhcpL2Relay.dhcpL2RelayCounters = this.store; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tears down the dhcpL2Relay application. |
| 101 | */ |
| 102 | @After |
| 103 | public void tearDown() { |
| 104 | dhcpL2Relay.deactivate(); |
| 105 | } |
| 106 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 107 | private void checkAllocation(DHCP.MsgType messageType, String circuitId) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 108 | ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 109 | + String.valueOf(CLIENT_PORT)); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 110 | allocs = dhcpL2Relay.getAllocationInfo(); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 111 | assertEquals(1, allocs.size()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 112 | allocs.forEach((k, v) -> { |
| 113 | log.info("Allocation {} : {}", k, v); |
| 114 | assertEquals(v.type(), messageType); |
| 115 | assertEquals(v.macAddress(), CLIENT_MAC); |
| 116 | assertEquals(v.location(), clientCp); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 117 | assertEquals(v.circuitId(), circuitId); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 118 | }); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void testMultipleAllocations() throws Exception { |
| 123 | dhcpL2Relay.clearAllocations(); |
| 124 | // Trigger a discover from one RG on port 32 |
| 125 | MacAddress mac32 = MacAddress.valueOf("b4:96:91:0c:4f:e4"); |
| 126 | VlanId vlan32a = VlanId.vlanId((short) 801); |
| 127 | Ethernet discover32a = constructDhcpDiscoverPacket( |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 128 | mac32, vlan32a, (short) 0); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 129 | ConnectPoint client32 = ConnectPoint.deviceConnectPoint( |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 130 | "of:0000b86a974385f7/32"); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 131 | sendPacket(discover32a, client32); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 132 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 133 | allocs = dhcpL2Relay.getAllocationInfo(); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 134 | assertEquals(1, allocs.size()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 135 | |
| 136 | //Trigger a discover from another RG on port 4112 |
| 137 | MacAddress mac4112 = MacAddress.valueOf("b4:96:91:0c:4f:c9"); |
| 138 | VlanId vlan4112 = VlanId.vlanId((short) 101); |
| 139 | Ethernet discover4112 = constructDhcpDiscoverPacket( |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 140 | mac4112, vlan4112, |
| 141 | (short) 0); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 142 | ConnectPoint client4112 = ConnectPoint.deviceConnectPoint( |
| 143 | "of:0000b86a974385f7/4112"); |
| 144 | sendPacket(discover4112, client4112); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 145 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 146 | allocs = dhcpL2Relay.getAllocationInfo(); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 147 | assertEquals(2, allocs.size()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 148 | |
| 149 | // Trigger a discover for another service with a different vlan |
| 150 | // from the same UNI port 32 |
| 151 | VlanId vlan32b = VlanId.vlanId((short) 802); |
| 152 | Ethernet discover32b = constructDhcpDiscoverPacket( |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 153 | mac32, vlan32b, (short) 0); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 154 | sendPacket(discover32b, client32); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 155 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 156 | allocs = dhcpL2Relay.getAllocationInfo(); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 157 | assertEquals(3, allocs.size()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 158 | |
| 159 | allocs.forEach((k, v) -> { |
| 160 | log.info("Allocation {} : {}", k, v); |
| 161 | assertEquals(v.type(), DHCP.MsgType.DHCPDISCOVER); |
| 162 | if (v.subscriberId().equals("ALPHe3d1cea3-1")) { |
| 163 | assertEquals(v.macAddress(), mac32); |
| 164 | assertEquals(v.location(), client32); |
| 165 | if (!(v.vlanId().equals(vlan32a) || v.vlanId().equals(vlan32b))) { |
| 166 | assert false; |
| 167 | } |
| 168 | } else if (v.subscriberId().equals("ALPHe3d1ceb7-1")) { |
| 169 | assertEquals(v.macAddress(), mac4112); |
| 170 | assertEquals(v.location(), client4112); |
| 171 | assertEquals(v.vlanId(), vlan4112); |
| 172 | } else { |
| 173 | assert false; |
| 174 | } |
| 175 | }); |
| 176 | |
| 177 | dhcpL2Relay.clearAllocations(); |
| 178 | assert dhcpL2Relay.getAllocationInfo().size() == 0; |
| 179 | } |
| 180 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 181 | /** |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 182 | * Tests the DHCP relay app by sending DHCP discovery Packet. The circuitId |
| 183 | * and remote-Id for this client is operator defined in MockSadis. |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 184 | * |
| 185 | * @throws Exception when an unhandled error occurs |
| 186 | */ |
| 187 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 188 | public void testDhcpDiscover() throws Exception { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 189 | // Sending DHCP Discover packet |
| 190 | dhcpL2Relay.clearAllocations(); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 191 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 192 | ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 193 | + String.valueOf(CLIENT_PORT)); |
| 194 | // send a copy of the packet as the app code modifies the sent packet |
| 195 | sendPacket(discoverPacket.duplicate(), clientCp); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 196 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 197 | Ethernet discoverRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 198 | compareClientPackets(discoverPacket, discoverRelayed); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 199 | checkAllocation(DHCP.MsgType.DHCPDISCOVER, CLIENT_CIRCUIT_ID); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Tests the addition of app-defined circuit id, when this client's |
| 204 | * MockSadis config for circiutId is empty. The remoteId is configured. |
| 205 | * |
| 206 | * @throws Exception when an unhandled error occurs |
| 207 | */ |
| 208 | @Test |
| 209 | public void testDhcpDiscoverEmptyCircuitId() throws Exception { |
| 210 | dhcpL2Relay.clearAllocations(); |
| 211 | MacAddress mac32 = MacAddress.valueOf("b4:96:91:0c:4f:e4"); |
| 212 | VlanId vlan32a = VlanId.vlanId((short) 801); // defined in mockSadis |
| 213 | VlanId qinq32a = VlanId.vlanId((short) 111); |
| 214 | Ethernet discover32a = constructDhcpDiscoverPacket(mac32, vlan32a, |
| 215 | (short) 0); |
| 216 | ConnectPoint client32 = ConnectPoint |
| 217 | .deviceConnectPoint("of:0000b86a974385f7/32"); |
| 218 | sendPacket(discover32a.duplicate(), client32); |
| 219 | Ethernet discoverRelayed = (Ethernet) getPacket(); |
| 220 | // empty circuitId in sadis for client32 should result in app defined |
| 221 | // circuitId |
| 222 | String expectedCircuitId = client32 + ":vlan" + vlan32a + ":pcp-1"; |
| 223 | compareClientPackets(discover32a, discoverRelayed, |
| 224 | qinq32a, vlan32a, CLIENT_C_PBIT, |
| 225 | expectedCircuitId, |
| 226 | CLIENT_REMOTE_ID); |
| 227 | allocs = dhcpL2Relay.getAllocationInfo(); |
| 228 | allocs.forEach((k, v) -> { |
| 229 | log.info("Allocation {} : {}", k, v); |
| 230 | assertEquals(v.circuitId(), expectedCircuitId); |
| 231 | }); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Tests the addition of app-defined circuit id, when this client's |
| 236 | * MockSadis config for circuitId and remoteId are null. In addition, it |
| 237 | * tests that the configured downstream-pcp is included in the circuitId. |
| 238 | * |
| 239 | * @throws Exception when an unhandled error occurs |
| 240 | */ |
| 241 | @Test |
| 242 | public void testDhcpDiscoverNullIds() throws Exception { |
| 243 | dhcpL2Relay.clearAllocations(); |
| 244 | MacAddress mac4112 = MacAddress.valueOf("b4:96:91:0c:4f:c9"); |
| 245 | VlanId vlan4112 = VlanId.vlanId((short) 101); |
| 246 | VlanId qinq4112 = VlanId.vlanId((short) 222); |
| 247 | Ethernet discover4112 = constructDhcpDiscoverPacket(mac4112, vlan4112, |
| 248 | (short) 0); |
| 249 | ConnectPoint client4112 = ConnectPoint |
| 250 | .deviceConnectPoint("of:0000b86a974385f7/4112"); |
| 251 | sendPacket(discover4112.duplicate(), client4112); |
| 252 | Ethernet discoverRelayed = (Ethernet) getPacket(); |
| 253 | // null circuitId in sadis for client32 should result in app defined |
| 254 | // circuitId. remoteId should not be there. Correct downstream pbit |
| 255 | // should be used |
| 256 | String expectedCircuitId = client4112 + ":vlan" + vlan4112 + ":pcp5"; |
| 257 | compareClientPackets(discover4112, discoverRelayed, |
| 258 | qinq4112, vlan4112, CLIENT_C_PBIT, |
| 259 | expectedCircuitId, |
| 260 | null); |
| 261 | allocs = dhcpL2Relay.getAllocationInfo(); |
| 262 | allocs.forEach((k, v) -> { |
| 263 | log.info("Allocation {} : {}", k, v); |
| 264 | assertEquals(v.circuitId(), expectedCircuitId); |
| 265 | }); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Tests the DHCP relay app by sending DHCP Request Packet. |
| 270 | * |
| 271 | * @throws Exception when an unhandled error occurs |
| 272 | */ |
| 273 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 274 | public void testDhcpRequest() throws Exception { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 275 | // Sending DHCP Request packet |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 276 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 277 | ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 278 | + String.valueOf(CLIENT_PORT)); |
| 279 | sendPacket(requestPacket.duplicate(), clientCp); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 280 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 281 | Ethernet requestRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 282 | compareClientPackets(requestPacket, requestRelayed); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 283 | checkAllocation(DHCP.MsgType.DHCPREQUEST, CLIENT_CIRCUIT_ID); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 287 | * Tests the DHCP relay app by sending DHCP Offer Packet with app-defined |
| 288 | * circuit id. App should use the circuit id for forwarding. |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 289 | * |
| 290 | * @throws Exception when an unhandled error occurs |
| 291 | */ |
| 292 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 293 | public void testDhcpOffer() throws InterruptedException { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 294 | // Sending DHCP Offer packet |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 295 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, CLIENT_MAC, |
| 296 | DESTINATION_ADDRESS_IP, |
| 297 | DHCP_CLIENT_IP_ADDRESS); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 298 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 299 | + String.valueOf(UPLINK_PORT))); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 300 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 301 | Ethernet offerRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 302 | compareServerPackets(offerPacket, offerRelayed); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 303 | String expectedCircuitId = OLT_DEV_ID + "/" + CLIENT_PORT + ":vlan" |
| 304 | + CLIENT_C_TAG + ":pcp" + CLIENT_C_PBIT; |
| 305 | checkAllocation(DHCP.MsgType.DHCPOFFER, expectedCircuitId); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 309 | * Tests the DHCP relay app by sending DHCP Ack Packet with operator defined |
| 310 | * circuit id. App should ignore circuit Id and do a host lookup. |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 311 | * |
| 312 | * @throws Exception when an unhandled error occurs |
| 313 | */ |
| 314 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 315 | public void testDhcpAck() throws InterruptedException { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 316 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 317 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, CLIENT_MAC, |
| 318 | DESTINATION_ADDRESS_IP, |
| 319 | DHCP_CLIENT_IP_ADDRESS); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 320 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 321 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 322 | + String.valueOf(UPLINK_PORT))); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 323 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 324 | Ethernet ackRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 325 | compareServerPackets(ackPacket, ackRelayed); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 326 | checkAllocation(DHCP.MsgType.DHCPACK, CLIENT_CIRCUIT_ID); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 329 | /** |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 330 | * Tests the DHCP relay app by sending DHCP Nak Packet. |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 331 | * Tests app-defined option82, but uses incorrect connectPoint - packet |
| 332 | * should still be forwarded to this connectPoint (ie without host lookup). |
| 333 | * Also pbit in circuitId is -1, which means original pbit should be retained |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 334 | * |
| 335 | * @throws Exception when an unhandled error occurs |
| 336 | */ |
| 337 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 338 | public void testDhcpNak() throws InterruptedException { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 339 | VlanId fakeVlan = VlanId.vlanId((short) 50); |
| 340 | short fakePcp = (short) 4; // should be retained |
| 341 | VlanId expectedVlan = VlanId.vlanId((short) 111); |
| 342 | // relayed packet should have vlan 111 and retain pcp4 and be sent out |
| 343 | // of port32 |
| 344 | ConnectPoint fakeCp = ConnectPoint.fromString("of:0000b86a974385f7/32"); |
| 345 | String fakeCircuitId = fakeCp + ":vlan" |
| 346 | + expectedVlan + ":pcp-1"; |
| 347 | Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC, CLIENT_MAC, |
| 348 | DESTINATION_ADDRESS_IP, |
| 349 | DHCP_CLIENT_IP_ADDRESS, |
| 350 | fakeVlan, |
| 351 | fakePcp); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 352 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 353 | sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
| 354 | + String.valueOf(UPLINK_PORT))); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 355 | |
| 356 | Ethernet nakRelayed = (Ethernet) getPacket(); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 357 | compareServerPackets(nakPacket, nakRelayed, expectedVlan, fakePcp); |
| 358 | |
| 359 | allocs = dhcpL2Relay.getAllocationInfo(); |
| 360 | assertEquals(1, allocs.size()); |
| 361 | allocs.forEach((k, v) -> { |
| 362 | log.info("Allocation {} : {}", k, v); |
| 363 | assertEquals(v.type(), DHCP.MsgType.DHCPNAK); |
| 364 | assertEquals(v.macAddress(), CLIENT_MAC); |
| 365 | assertEquals(v.location(), fakeCp); |
| 366 | assertEquals(v.circuitId(), fakeCircuitId); |
| 367 | }); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Tests the DHCP relay app by sending DHCP Decline Packet. |
| 372 | * |
| 373 | * @throws Exception when an unhandled error occurs |
| 374 | */ |
| 375 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 376 | public void testDhcpDecline() throws InterruptedException { |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 377 | |
| 378 | Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC); |
| 379 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 380 | sendPacket(declinePacket.duplicate(), |
| 381 | ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 382 | |
| 383 | Ethernet declineRelayed = (Ethernet) getPacket(); |
| 384 | compareClientPackets(declinePacket, declineRelayed); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 385 | checkAllocation(DHCP.MsgType.DHCPDECLINE, CLIENT_CIRCUIT_ID); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /** |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 389 | * Tests the DHCP global counters. |
| 390 | */ |
| 391 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 392 | public void testDhcpGlobalCounters() throws InterruptedException { |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 393 | |
| 394 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
| 395 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 396 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 397 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
| 398 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 399 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 400 | |
| 401 | sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 402 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 403 | sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 404 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 405 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 406 | |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 407 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
| 408 | long discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
| 409 | DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"))); |
| 410 | long offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
| 411 | DhcpL2RelayCounterNames.valueOf("DHCPOFFER"))); |
| 412 | long requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
| 413 | DhcpL2RelayCounterNames.valueOf("DHCPREQUEST"))); |
| 414 | long ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
| 415 | DhcpL2RelayCounterNames.valueOf("DHCPACK"))); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 416 | assertEquals(1, discoveryValue); |
| 417 | assertEquals(1, offerValue); |
| 418 | assertEquals(1, requestValue); |
| 419 | assertEquals(1, ackValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Tests the DHCP per subscriber counters. |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 424 | */ |
| 425 | @Test |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 426 | public void testDhcpPerSubscriberCounters() throws Exception { |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 427 | |
| 428 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
| 429 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, |
| 430 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
| 431 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
| 432 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, |
| 433 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
| 434 | |
| 435 | sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 436 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 437 | sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 438 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 439 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 440 | |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 441 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
| 442 | long discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 443 | DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"))); |
| 444 | long offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 445 | DhcpL2RelayCounterNames.valueOf("DHCPOFFER"))); |
| 446 | long requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 447 | DhcpL2RelayCounterNames.valueOf("DHCPREQUEST"))); |
| 448 | long ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 449 | DhcpL2RelayCounterNames.valueOf("DHCPACK"))); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 450 | assertEquals(1, discoveryValue); |
| 451 | assertEquals(1, offerValue); |
| 452 | assertEquals(1, requestValue); |
| 453 | assertEquals(1, ackValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 454 | } |
| 455 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 456 | public void compareClientPackets(Ethernet sent, Ethernet relayed) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 457 | compareClientPackets(sent, relayed, CLIENT_S_TAG, CLIENT_C_TAG, |
| 458 | CLIENT_C_PBIT, CLIENT_CIRCUIT_ID, |
| 459 | CLIENT_REMOTE_ID); |
| 460 | } |
| 461 | |
| 462 | public void compareClientPackets(Ethernet sent, Ethernet relayed, |
| 463 | VlanId expectedQinQ, |
| 464 | VlanId expectedVlan, short expectedPcp, |
| 465 | String expectedCircuitId, |
| 466 | String expectedRemoteId) { |
| 467 | // convert the sent packet to the expected relayed packet |
| 468 | sent.setSourceMACAddress(OLT_MAC_ADDRESS); // due to netconfig test in setup |
| 469 | sent.setQinQVID(expectedQinQ.toShort()); |
| 470 | sent.setQinQTPID((short) 0x8100); |
| 471 | sent.setVlanID(expectedVlan.toShort()); |
| 472 | sent.setPriorityCode((byte) expectedPcp); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 473 | |
| 474 | IPv4 ipv4Packet = (IPv4) sent.getPayload(); |
| 475 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 476 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 477 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 478 | |
| 479 | DhcpOption82Data option82 = new DhcpOption82Data(); |
| 480 | option82.setAgentCircuitId(expectedCircuitId); |
| 481 | option82.setAgentRemoteId(expectedRemoteId); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 482 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 483 | DhcpOption option = new DhcpOption() |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 484 | .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 485 | .setData(option82.toByteArray()) |
| 486 | .setLength(option82.length()); |
| 487 | |
| 488 | options.add(options.size() - 1, option); |
| 489 | dhcpPacket.setOptions(options); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 490 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 491 | byte[] sb = sent.serialize(); |
| 492 | Ethernet expectedPacket = null; |
| 493 | try { |
| 494 | expectedPacket = Ethernet.deserializer() |
| 495 | .deserialize(sb, 0, sb.length); |
| 496 | } catch (DeserializationException e) { |
| 497 | log.error("exeption: {}", e.getMessage()); |
| 498 | fail(); |
| 499 | } |
| 500 | verifyDhcpOptions(expectedPacket, relayed); |
| 501 | assertEquals(expectedPacket, relayed); |
| 502 | } |
| 503 | |
| 504 | public void verifyDhcpOptions(Ethernet expected, Ethernet relayed) { |
| 505 | DHCP de = ((DHCP) ((UDP) ((IPv4) expected.getPayload()).getPayload()) |
| 506 | .getPayload()); |
| 507 | DHCP dr = ((DHCP) ((UDP) ((IPv4) relayed.getPayload()).getPayload()) |
| 508 | .getPayload()); |
| 509 | List<DhcpOption> del = de.getOptions(); |
| 510 | List<DhcpOption> der = dr.getOptions(); |
| 511 | assertEquals(del.size(), der.size()); |
| 512 | for (int i = 0; i < del.size(); i++) { |
| 513 | assertEquals(del.get(i), der.get(i)); |
| 514 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 517 | public void compareServerPackets(Ethernet sent, Ethernet relayed) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 518 | compareServerPackets(sent, relayed, CLIENT_C_TAG, CLIENT_C_PBIT); |
| 519 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 520 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 521 | public void compareServerPackets(Ethernet sent, Ethernet relayed, |
| 522 | VlanId expectedVlan, short expectedPcp) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 523 | try { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 524 | // modify sent packet to create expected packet |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 525 | sent.setDestinationMACAddress(CLIENT_MAC); |
| 526 | sent.setQinQVID(NOT_PROVIDED); |
| 527 | sent.setQinQPriorityCode((byte) NOT_PROVIDED); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 528 | sent.setVlanID(expectedVlan.toShort()); |
| 529 | sent.setPriorityCode((byte) expectedPcp); |
| 530 | DHCP d = ((DHCP) ((UDP) ((IPv4) sent.getPayload()).getPayload()) |
| 531 | .getPayload()); |
| 532 | List<DhcpOption> newOptions = d.getOptions().stream() |
| 533 | .filter(option -> option |
| 534 | .getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID |
| 535 | .getValue()) |
| 536 | .collect(Collectors.toList()); |
| 537 | d.setOptions(newOptions); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 538 | |
| 539 | final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize()); |
| 540 | Ethernet expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(), |
| 541 | 0, byteBuffer.array().length); |
| 542 | assertEquals(expectedPacket, relayed); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 543 | } catch (Exception e) { |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 544 | log.error(e.getMessage()); |
| 545 | fail(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 546 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 547 | |
| 548 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 549 | |
| 550 | private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore { |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 551 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 552 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 553 | public void incrementCounter(String counterClass, DhcpL2RelayCounterNames counterType) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 554 | |
| 555 | } |
| 556 | |
| 557 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 558 | public void setCounter(String counterClass, DhcpL2RelayCounterNames counterType, Long value) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 559 | |
| 560 | } |
| 561 | |
| 562 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 563 | public DhcpL2RelayStatistics getCounters() { |
| 564 | return new DhcpL2RelayStatistics(); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | @Override |
| 568 | public void resetCounters(String counterClass) { |
| 569 | |
| 570 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 571 | |
| 572 | @Override |
| 573 | public void setDelegate(DhcpL2RelayStoreDelegate delegate) { |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public void unsetDelegate(DhcpL2RelayStoreDelegate delegate) { |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public boolean hasDelegate() { |
| 582 | return false; |
| 583 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 584 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 585 | } |