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