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 | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.List; |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 22 | import java.util.Map; |
| 23 | import java.util.concurrent.atomic.AtomicLong; |
| 24 | import java.util.concurrent.TimeUnit; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 25 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 26 | import org.easymock.EasyMock; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 30 | import org.onlab.junit.TestUtils; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 31 | import org.onlab.packet.DHCP; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 32 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 33 | import org.onlab.packet.IPv4; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 34 | import org.onlab.packet.UDP; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 35 | import org.onlab.packet.dhcp.DhcpOption; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 36 | |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 37 | import org.onosproject.cfg.ComponentConfigService; |
| 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 40 | import org.opencord.dhcpl2relay.impl.packet.DhcpOption82; |
| 41 | |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 42 | import com.google.common.collect.Lists; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 43 | |
| 44 | public class DhcpL2RelayTest extends DhcpL2RelayTestBase { |
| 45 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 46 | private DhcpL2Relay dhcpL2Relay; |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 47 | private SimpleDhcpL2RelayCountersStore store; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 48 | |
| 49 | ComponentConfigService mockConfigService = |
| 50 | EasyMock.createMock(ComponentConfigService.class); |
| 51 | |
| 52 | /** |
| 53 | * Sets up the services required by the dhcpl2relay app. |
| 54 | */ |
| 55 | @Before |
| 56 | public void setUp() { |
| 57 | dhcpL2Relay = new DhcpL2Relay(); |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 58 | dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 59 | dhcpL2Relay.coreService = new MockCoreServiceAdapter(); |
| 60 | dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 61 | dhcpL2Relay.packetService = new MockPacketService(); |
| 62 | dhcpL2Relay.componentConfigService = mockConfigService; |
| 63 | dhcpL2Relay.deviceService = new MockDeviceService(); |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 64 | dhcpL2Relay.sadisService = new MockSadisService(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 65 | dhcpL2Relay.hostService = new MockHostService(); |
| 66 | dhcpL2Relay.mastershipService = new MockMastershipService(); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 67 | TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher()); |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 68 | dhcpL2Relay.refreshService = new MockExecutor(dhcpL2Relay.refreshService); |
| 69 | dhcpL2Relay.activate(new DhcpL2RelayTestBase.MockComponentContext()); |
| 70 | store = new SimpleDhcpL2RelayCountersStore(); |
| 71 | TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher()); |
| 72 | store.activate(); |
| 73 | dhcpL2Relay.dhcpL2RelayCounters = this.store; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Tears down the dhcpL2Relay application. |
| 78 | */ |
| 79 | @After |
| 80 | public void tearDown() { |
| 81 | dhcpL2Relay.deactivate(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests the DHCP relay app by sending DHCP discovery Packet. |
| 86 | * |
| 87 | * @throws Exception when an unhandled error occurs |
| 88 | */ |
| 89 | @Test |
| 90 | public void testDhcpDiscover() throws Exception { |
| 91 | // (1) Sending DHCP discover packet |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 92 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 93 | |
| 94 | sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 95 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 96 | Ethernet discoverRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 97 | compareClientPackets(discoverPacket, discoverRelayed); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests the DHCP relay app by sending DHCP Request Packet. |
| 102 | * |
| 103 | * @throws Exception when an unhandled error occurs |
| 104 | */ |
| 105 | @Test |
| 106 | public void testDhcpRequest() throws Exception { |
| 107 | // (1) Sending DHCP discover packet |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 108 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 109 | |
| 110 | sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 111 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 112 | Ethernet requestRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 113 | compareClientPackets(requestPacket, requestRelayed); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests the DHCP relay app by sending DHCP Offer Packet. |
| 118 | * |
| 119 | * @throws Exception when an unhandled error occurs |
| 120 | */ |
| 121 | @Test |
| 122 | public void testDhcpOffer() { |
| 123 | // (1) Sending DHCP discover packet |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 124 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, |
| 125 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 126 | |
| 127 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 128 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 129 | Ethernet offerRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 130 | compareServerPackets(offerPacket, offerRelayed); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Tests the DHCP relay app by sending DHCP Ack Packet. |
| 135 | * |
| 136 | * @throws Exception when an unhandled error occurs |
| 137 | */ |
| 138 | @Test |
| 139 | public void testDhcpAck() { |
| 140 | |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 141 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, |
| 142 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 143 | |
| 144 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 145 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 146 | Ethernet ackRelayed = (Ethernet) getPacket(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 147 | compareServerPackets(ackPacket, ackRelayed); |
| 148 | } |
| 149 | |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 150 | /** |
| 151 | * Tests the DHCP global counters. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testDhcpGlobalCounters() { |
| 155 | long discoveryValue = 0; |
| 156 | long offerValue = 0; |
| 157 | long requestValue = 0; |
| 158 | long ackValue = 0; |
| 159 | |
| 160 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
| 161 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, |
| 162 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
| 163 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
| 164 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, |
| 165 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
| 166 | |
| 167 | sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 168 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 169 | sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 170 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 171 | |
| 172 | Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap(); |
| 173 | discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER, |
| 174 | DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue(); |
| 175 | offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER, |
| 176 | DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue(); |
| 177 | requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER, |
| 178 | DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue(); |
| 179 | ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayCountersIdentifier.GLOBAL_COUNTER, |
| 180 | DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue(); |
| 181 | |
| 182 | assertEquals((long) 1, discoveryValue); |
| 183 | assertEquals((long) 1, offerValue); |
| 184 | assertEquals((long) 1, requestValue); |
| 185 | assertEquals((long) 1, ackValue); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Tests the DHCP per subscriber counters. |
| 190 | * |
| 191 | */ |
| 192 | @Test |
| 193 | public void testDhcpPerSubscriberCounters() { |
| 194 | long discoveryValue; |
| 195 | long offerValue; |
| 196 | long requestValue; |
| 197 | long ackValue; |
| 198 | |
| 199 | Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC); |
| 200 | Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC, |
| 201 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
| 202 | Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC); |
| 203 | Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC, |
| 204 | CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS); |
| 205 | |
| 206 | sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 207 | sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 208 | sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 209 | sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1)); |
| 210 | |
| 211 | Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap(); |
| 212 | discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 213 | DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue(); |
| 214 | offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 215 | DhcpL2RelayCounters.valueOf("DHCPOFFER"))).longValue(); |
| 216 | requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 217 | DhcpL2RelayCounters.valueOf("DHCPREQUEST"))).longValue(); |
| 218 | ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
| 219 | DhcpL2RelayCounters.valueOf("DHCPACK"))).longValue(); |
| 220 | |
| 221 | assertEquals((long) 1, discoveryValue); |
| 222 | assertEquals((long) 1, offerValue); |
| 223 | assertEquals((long) 1, requestValue); |
| 224 | assertEquals((long) 1, ackValue); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Tests the schedule function to publish the counters to kafka. |
| 229 | * |
| 230 | */ |
| 231 | @Test |
| 232 | public void testSchedulePublishCountersToKafka() { |
| 233 | MockExecutor executor = new MockExecutor(dhcpL2Relay.refreshService); |
| 234 | dhcpL2Relay.refreshTask = executor.scheduleWithFixedDelay( |
| 235 | dhcpL2Relay.publishCountersToKafka, 0, 10, TimeUnit.SECONDS); |
| 236 | executor.assertLastMethodCalled("scheduleWithFixedDelay", 0, 10, TimeUnit.SECONDS); |
| 237 | } |
| 238 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 239 | public void compareClientPackets(Ethernet sent, Ethernet relayed) { |
| 240 | sent.setSourceMACAddress(OLT_MAC_ADDRESS); |
| 241 | sent.setQinQVID(CLIENT_S_TAG.toShort()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 242 | sent.setVlanID(CLIENT_C_TAG.toShort()); |
| 243 | |
| 244 | IPv4 ipv4Packet = (IPv4) sent.getPayload(); |
| 245 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 246 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 247 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 248 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 249 | DhcpOption82 option82 = new DhcpOption82(); |
| 250 | option82.setAgentCircuitId(CLIENT_CIRCUIT_ID); |
| 251 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 252 | DhcpOption option = new DhcpOption() |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 253 | .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 254 | .setData(option82.toByteArray()) |
| 255 | .setLength(option82.length()); |
| 256 | |
| 257 | options.add(options.size() - 1, option); |
| 258 | dhcpPacket.setOptions(options); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 259 | assertEquals(sent, relayed); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 260 | |
| 261 | } |
| 262 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 263 | public void compareServerPackets(Ethernet sent, Ethernet relayed) { |
| 264 | sent.setDestinationMACAddress(CLIENT_MAC); |
| 265 | sent.setQinQVID(CLIENT_S_TAG.toShort()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 266 | sent.setVlanID(CLIENT_C_TAG.toShort()); |
| 267 | |
| 268 | final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 269 | Ethernet expectedPacket = null; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 270 | try { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 271 | expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(), |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 272 | 0, byteBuffer.array().length); |
| 273 | } catch (Exception e) { |
| 274 | } |
| 275 | assertEquals(expectedPacket, relayed); |
| 276 | |
| 277 | } |
Marcos Aurelio Carrero | dadb357 | 2019-11-25 13:34:25 -0300 | [diff] [blame^] | 278 | } |