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; |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 29 | import org.onlab.junit.TestUtils; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 30 | import org.onlab.packet.DHCP; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 31 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 32 | import org.onlab.packet.IPv4; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 33 | import org.onlab.packet.MacAddress; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 34 | import org.onlab.packet.UDP; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 35 | import org.onlab.packet.VlanId; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 36 | import org.onlab.packet.dhcp.DhcpOption; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 37 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 38 | import org.onosproject.cluster.ClusterServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 39 | import org.onosproject.cluster.LeadershipServiceAdapter; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 40 | import org.onosproject.net.ConnectPoint; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 42 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 43 | import org.onosproject.store.service.TestStorageService; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 44 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 45 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 46 | import org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 47 | import org.opencord.dhcpl2relay.impl.packet.DhcpOption82; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 49 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 50 | import com.google.common.collect.Lists; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 51 | |
| 52 | public class DhcpL2RelayTest extends DhcpL2RelayTestBase { |
| 53 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 54 | private DhcpL2Relay dhcpL2Relay; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 55 | private SimpleDhcpL2RelayCountersStore store; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
| 57 | Map<String, DhcpAllocationInfo> allocs; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 58 | |
| 59 | ComponentConfigService mockConfigService = |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 60 | createMock(ComponentConfigService.class); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Sets up the services required by the dhcpl2relay app. |
| 64 | */ |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | dhcpL2Relay = new DhcpL2Relay(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 68 | dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 69 | dhcpL2Relay.coreService = new MockCoreServiceAdapter(); |
| 70 | dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 71 | dhcpL2Relay.packetService = new MockPacketService(); |
| 72 | dhcpL2Relay.componentConfigService = mockConfigService; |
| 73 | dhcpL2Relay.deviceService = new MockDeviceService(); |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 74 | dhcpL2Relay.sadisService = new MockSadisService(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 75 | dhcpL2Relay.hostService = new MockHostService(); |
| 76 | dhcpL2Relay.mastershipService = new MockMastershipService(); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 77 | dhcpL2Relay.dhcpL2RelayCounters = new MockDhcpL2RelayCountersStore(); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 78 | dhcpL2Relay.storageService = new TestStorageService(); |
| 79 | dhcpL2Relay.leadershipService = new LeadershipServiceAdapter(); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 80 | TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 81 | dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService); |
| 82 | dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext()); |
| 83 | store = new SimpleDhcpL2RelayCountersStore(); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 84 | 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 89 | TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher()); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 90 | store.activate(new MockComponentContext()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 91 | dhcpL2Relay.dhcpL2RelayCounters = this.store; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tears down the dhcpL2Relay application. |
| 96 | */ |
| 97 | @After |
| 98 | public void tearDown() { |
| 99 | dhcpL2Relay.deactivate(); |
| 100 | } |
| 101 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 102 | 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 Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 172 | /** |
| 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 Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 179 | // Sending DHCP Discover packet |
| 180 | dhcpL2Relay.clearAllocations(); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 181 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 182 | ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
| 183 | + String.valueOf(CLIENT_PORT)); |
| 184 | sendPacket(discoverPacket, clientCp); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 185 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 186 | Ethernet discoverRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 187 | compareClientPackets(discoverPacket, discoverRelayed); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 188 | checkAllocation(DHCP.MsgType.DHCPDISCOVER); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 189 | } |
| 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 Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 198 | // Sending DHCP Request packet |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 199 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 200 | ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
| 201 | + String.valueOf(CLIENT_PORT)); |
| 202 | sendPacket(requestPacket, clientCp); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 203 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 204 | Ethernet requestRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 205 | compareClientPackets(requestPacket, requestRelayed); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 206 | checkAllocation(DHCP.MsgType.DHCPREQUEST); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 207 | } |
| 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 Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 216 | // Sending DHCP Offer packet |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 217 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, |
| 218 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 219 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
| 220 | + String.valueOf(UPLINK_PORT))); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 221 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 222 | Ethernet offerRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 223 | compareServerPackets(offerPacket, offerRelayed); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 224 | checkAllocation(DHCP.MsgType.DHCPOFFER); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 225 | } |
| 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 235 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, |
| 236 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 237 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 238 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" |
| 239 | + String.valueOf(UPLINK_PORT))); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 240 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 241 | Ethernet ackRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 242 | compareServerPackets(ackPacket, ackRelayed); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 243 | checkAllocation(DHCP.MsgType.DHCPACK); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 246 | /** |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 247 | * 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 Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 261 | checkAllocation(DHCP.MsgType.DHCPNAK); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 262 | } |
| 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 Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 278 | checkAllocation(DHCP.MsgType.DHCPDECLINE); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 282 | * 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 303 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 304 | discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 305 | DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 306 | offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 307 | DhcpL2RelayCounterNames.valueOf("DHCPOFFER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 308 | requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 309 | DhcpL2RelayCounterNames.valueOf("DHCPREQUEST"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 310 | ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 311 | DhcpL2RelayCounterNames.valueOf("DHCPACK"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 312 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 313 | assertEquals(1, discoveryValue); |
| 314 | assertEquals(1, offerValue); |
| 315 | assertEquals(1, requestValue); |
| 316 | assertEquals(1, ackValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 317 | } |
| 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 342 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 343 | discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 344 | DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 345 | offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 346 | DhcpL2RelayCounterNames.valueOf("DHCPOFFER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 347 | requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 348 | DhcpL2RelayCounterNames.valueOf("DHCPREQUEST"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 349 | ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 350 | DhcpL2RelayCounterNames.valueOf("DHCPACK"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 351 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 352 | assertEquals(1, discoveryValue); |
| 353 | assertEquals(1, offerValue); |
| 354 | assertEquals(1, requestValue); |
| 355 | assertEquals(1, ackValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 356 | } |
| 357 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 358 | public void compareClientPackets(Ethernet sent, Ethernet relayed) { |
| 359 | sent.setSourceMACAddress(OLT_MAC_ADDRESS); |
| 360 | sent.setQinQVID(CLIENT_S_TAG.toShort()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 361 | sent.setVlanID(CLIENT_C_TAG.toShort()); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 362 | sent.setPriorityCode((byte) CLIENT_C_PBIT); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 363 | |
| 364 | IPv4 ipv4Packet = (IPv4) sent.getPayload(); |
| 365 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 366 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 367 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 368 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 369 | DhcpOption82 option82 = new DhcpOption82(); |
| 370 | option82.setAgentCircuitId(CLIENT_CIRCUIT_ID); |
| 371 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 372 | DhcpOption option = new DhcpOption() |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 373 | .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 Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 379 | assertEquals(sent, relayed); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 380 | |
| 381 | } |
| 382 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 383 | public void compareServerPackets(Ethernet sent, Ethernet relayed) { |
| 384 | sent.setDestinationMACAddress(CLIENT_MAC); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 385 | sent.setQinQVID(NOT_PROVIDED); |
| 386 | sent.setQinQPriorityCode((byte) NOT_PROVIDED); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 387 | sent.setVlanID(CLIENT_C_TAG.toShort()); |
| 388 | |
| 389 | final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 390 | Ethernet expectedPacket = null; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 391 | try { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 392 | expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(), |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 393 | 0, byteBuffer.array().length); |
| 394 | } catch (Exception e) { |
| 395 | } |
| 396 | assertEquals(expectedPacket, relayed); |
| 397 | |
| 398 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 399 | |
| 400 | private class MockDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore { |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 401 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 402 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 403 | public void incrementCounter(String counterClass, DhcpL2RelayCounterNames counterType) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 404 | |
| 405 | } |
| 406 | |
| 407 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 408 | public void setCounter(String counterClass, DhcpL2RelayCounterNames counterType, Long value) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 409 | |
| 410 | } |
| 411 | |
| 412 | @Override |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 413 | public DhcpL2RelayStatistics getCounters() { |
| 414 | return new DhcpL2RelayStatistics(); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void resetCounters(String counterClass) { |
| 419 | |
| 420 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 421 | |
| 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 Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 434 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 435 | } |