samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.dhcp.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.DHCP; |
| 23 | import org.onlab.packet.DHCPOption; |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 24 | import org.onlab.packet.DHCPPacketType; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.onlab.packet.IPv4; |
| 27 | import org.onlab.packet.Ip4Address; |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.UDP; |
| 31 | import org.onosproject.core.CoreServiceAdapter; |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 32 | import org.onosproject.dhcp.DhcpStore; |
| 33 | import org.onosproject.dhcp.IpAssignment; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.HostId; |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 36 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 37 | import org.onosproject.net.host.HostDescription; |
| 38 | import org.onosproject.net.host.HostProvider; |
| 39 | import org.onosproject.net.host.HostProviderRegistry; |
| 40 | import org.onosproject.net.host.HostProviderService; |
| 41 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 42 | import org.onosproject.net.packet.DefaultPacketContext; |
| 43 | import org.onosproject.net.packet.InboundPacket; |
| 44 | import org.onosproject.net.packet.OutboundPacket; |
| 45 | import org.onosproject.net.packet.PacketContext; |
| 46 | import org.onosproject.net.packet.PacketProcessor; |
| 47 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 48 | import org.onosproject.net.provider.AbstractProvider; |
| 49 | import org.onosproject.net.provider.AbstractProviderService; |
| 50 | import org.onosproject.net.provider.ProviderId; |
| 51 | |
| 52 | import java.nio.ByteBuffer; |
| 53 | import java.util.ArrayList; |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 54 | import java.util.Date; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 55 | import java.util.HashMap; |
| 56 | import java.util.List; |
| 57 | import java.util.Map; |
| 58 | import java.util.Set; |
| 59 | |
| 60 | import static org.junit.Assert.assertEquals; |
| 61 | import static org.junit.Assert.fail; |
| 62 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 63 | |
| 64 | /** |
| 65 | * Set of tests of the ONOS application component. |
| 66 | */ |
| 67 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 68 | public class DhcpManagerTest { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 69 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 70 | private DhcpManager dhcpXManager; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 71 | |
| 72 | protected PacketProcessor packetProcessor; |
| 73 | |
| 74 | protected HostProviderService hostProviderService; |
| 75 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 76 | private static final HostId CLIENT1_HOST = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1a")); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 77 | |
| 78 | private static final String EXPECTED_IP = "10.2.0.2"; |
| 79 | |
| 80 | private static final Ip4Address BROADCAST = Ip4Address.valueOf("255.255.255.255"); |
| 81 | |
| 82 | private static final int TRANSACTION_ID = 1000; |
| 83 | |
| 84 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 85 | |
| 86 | @Before |
| 87 | public void setUp() { |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 88 | dhcpXManager = new DhcpManager(); |
| 89 | dhcpXManager.cfgService = new TestNetworkConfigRegistry(); |
| 90 | dhcpXManager.packetService = new TestPacketService(); |
| 91 | dhcpXManager.coreService = new TestCoreService(); |
| 92 | dhcpXManager.dhcpStore = new TestDhcpStore(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 93 | hostProviderService = new TestHostProviderService(new TestHostProvider()); |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 94 | dhcpXManager.hostProviderService = hostProviderService; |
| 95 | dhcpXManager.hostProviderRegistry = new TestHostRegistry(); |
| 96 | dhcpXManager.activate(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @After |
| 100 | public void tearDown() { |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 101 | dhcpXManager.deactivate(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests the response to a DHCP Discover Packet. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testDiscover() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 109 | Ethernet reply = constructDhcpPacket(DHCPPacketType.DHCPDISCOVER); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 110 | sendPacket(reply); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests the response to a DHCP Request Packet. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testRequest() { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 118 | Ethernet reply = constructDhcpPacket(DHCPPacketType.DHCPREQUEST); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 119 | sendPacket(reply); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Sends an Ethernet packet to the process method of the Packet Processor. |
| 124 | * @param reply Ethernet packet |
| 125 | */ |
| 126 | private void sendPacket(Ethernet reply) { |
| 127 | final ByteBuffer byteBuffer = ByteBuffer.wrap(reply.serialize()); |
| 128 | InboundPacket inPacket = new DefaultInboundPacket(connectPoint("1", 1), |
| 129 | reply, |
| 130 | byteBuffer); |
| 131 | |
| 132 | PacketContext context = new TestPacketContext(127L, inPacket, null, false); |
| 133 | packetProcessor.process(context); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Constructs an Ethernet packet containing a DHCP Payload. |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 138 | * @param packetType DHCP Message Type |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 139 | * @return Ethernet packet |
| 140 | */ |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 141 | private Ethernet constructDhcpPacket(DHCPPacketType packetType) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 142 | |
| 143 | // Ethernet Frame. |
| 144 | Ethernet ethReply = new Ethernet(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 145 | ethReply.setSourceMACAddress(CLIENT1_HOST.mac()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 146 | ethReply.setDestinationMACAddress(MacAddress.BROADCAST); |
| 147 | ethReply.setEtherType(Ethernet.TYPE_IPV4); |
| 148 | ethReply.setVlanID((short) 2); |
| 149 | |
| 150 | // IP Packet |
| 151 | IPv4 ipv4Reply = new IPv4(); |
| 152 | ipv4Reply.setSourceAddress(0); |
| 153 | ipv4Reply.setDestinationAddress(BROADCAST.toInt()); |
| 154 | ipv4Reply.setTtl((byte) 127); |
| 155 | |
| 156 | // UDP Datagram. |
| 157 | UDP udpReply = new UDP(); |
| 158 | udpReply.setSourcePort((byte) UDP.DHCP_CLIENT_PORT); |
| 159 | udpReply.setDestinationPort((byte) UDP.DHCP_SERVER_PORT); |
| 160 | |
| 161 | // DHCP Payload. |
| 162 | DHCP dhcpReply = new DHCP(); |
| 163 | dhcpReply.setOpCode(DHCP.OPCODE_REQUEST); |
| 164 | |
| 165 | dhcpReply.setYourIPAddress(0); |
| 166 | dhcpReply.setServerIPAddress(0); |
| 167 | |
| 168 | dhcpReply.setTransactionId(TRANSACTION_ID); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 169 | dhcpReply.setClientHardwareAddress(CLIENT1_HOST.mac().toBytes()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 170 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 171 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 172 | |
| 173 | // DHCP Options. |
| 174 | DHCPOption option = new DHCPOption(); |
| 175 | List<DHCPOption> optionList = new ArrayList<>(); |
| 176 | |
| 177 | // DHCP Message Type. |
| 178 | option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 179 | option.setLength((byte) 1); |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 180 | byte[] optionData = {(byte) packetType.getValue()}; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 181 | option.setData(optionData); |
| 182 | optionList.add(option); |
| 183 | |
| 184 | // DHCP Requested IP. |
| 185 | option = new DHCPOption(); |
| 186 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()); |
| 187 | option.setLength((byte) 4); |
| 188 | optionData = Ip4Address.valueOf(EXPECTED_IP).toOctets(); |
| 189 | option.setData(optionData); |
| 190 | optionList.add(option); |
| 191 | |
| 192 | // End Option. |
| 193 | option = new DHCPOption(); |
| 194 | option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 195 | option.setLength((byte) 1); |
| 196 | optionList.add(option); |
| 197 | |
| 198 | dhcpReply.setOptions(optionList); |
| 199 | |
| 200 | udpReply.setPayload(dhcpReply); |
| 201 | ipv4Reply.setPayload(udpReply); |
| 202 | ethReply.setPayload(ipv4Reply); |
| 203 | |
| 204 | return ethReply; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Validates the contents of the packet sent by the DHCP Manager. |
| 209 | * @param packet Ethernet packet received |
| 210 | */ |
| 211 | private void validatePacket(Ethernet packet) { |
| 212 | DHCP dhcpPacket = (DHCP) packet.getPayload().getPayload().getPayload(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 213 | assertEquals(MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()), CLIENT1_HOST.mac()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 214 | assertEquals(Ip4Address.valueOf(dhcpPacket.getYourIPAddress()), Ip4Address.valueOf(EXPECTED_IP)); |
| 215 | assertEquals(dhcpPacket.getTransactionId(), TRANSACTION_ID); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Mocks the DHCPStore. |
| 220 | */ |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 221 | private final class TestDhcpStore implements DhcpStore { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 222 | |
| 223 | |
| 224 | public void populateIPPoolfromRange(Ip4Address startIP, Ip4Address endIP) { |
| 225 | } |
| 226 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 227 | public Ip4Address suggestIP(HostId hostId, Ip4Address requestedIP) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 228 | return Ip4Address.valueOf(EXPECTED_IP); |
| 229 | } |
| 230 | |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 231 | public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean fromOpenStack, |
| 232 | List<Ip4Address> addressList) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
| 236 | public void setDefaultTimeoutForPurge(int timeInSeconds) { |
| 237 | } |
| 238 | |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 239 | public Ip4Address releaseIP(HostId hostId) { |
| 240 | return null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 241 | } |
| 242 | |
samanwita pal | 0bff430 | 2015-09-15 13:37:00 -0700 | [diff] [blame] | 243 | public Map<HostId, IpAssignment> listAssignedMapping() { |
| 244 | return listAllMapping(); |
| 245 | } |
| 246 | |
| 247 | public Map<HostId, IpAssignment> listAllMapping() { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 248 | Map<HostId, IpAssignment> map = new HashMap<>(); |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 249 | IpAssignment assignment = IpAssignment.builder() |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 250 | .ipAddress(Ip4Address.valueOf(EXPECTED_IP)) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 251 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned) |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 252 | .leasePeriod(300) |
| 253 | .timestamp(new Date()) |
| 254 | .build(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 255 | map.put(CLIENT1_HOST, assignment); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 256 | return map; |
| 257 | } |
| 258 | |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 259 | public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean fromOpenStack, |
| 260 | List<Ip4Address> addressList) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | |
| 264 | public boolean removeStaticIP(MacAddress macID) { |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | public Iterable<Ip4Address> getAvailableIPs() { |
| 269 | List<Ip4Address> ipList = new ArrayList<>(); |
| 270 | ipList.add(Ip4Address.valueOf(EXPECTED_IP)); |
| 271 | return ImmutableSet.copyOf(ipList); |
| 272 | } |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 273 | public IpAssignment getIpAssignmentFromAllocationMap(HostId hostId) { |
| 274 | return null; |
| 275 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Mocks the DefaultPacket context. |
| 280 | */ |
| 281 | private final class TestPacketContext extends DefaultPacketContext { |
| 282 | private TestPacketContext(long time, InboundPacket inPkt, |
| 283 | OutboundPacket outPkt, boolean block) { |
| 284 | super(time, inPkt, outPkt, block); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void send() { |
| 289 | // We don't send anything out. |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Keeps a reference to the PacketProcessor and verifies the OutboundPackets. |
| 295 | */ |
| 296 | private class TestPacketService extends PacketServiceAdapter { |
| 297 | |
| 298 | @Override |
| 299 | public void addProcessor(PacketProcessor processor, int priority) { |
| 300 | packetProcessor = processor; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void emit(OutboundPacket packet) { |
| 305 | try { |
| 306 | Ethernet eth = Ethernet.deserializer().deserialize(packet.data().array(), |
| 307 | 0, packet.data().array().length); |
| 308 | validatePacket(eth); |
| 309 | } catch (Exception e) { |
| 310 | fail(e.getMessage()); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Mocks the CoreService. |
| 317 | */ |
| 318 | private class TestCoreService extends CoreServiceAdapter { |
| 319 | |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Mocks the NetworkConfigRegistry. |
| 324 | */ |
| 325 | private class TestNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
| 326 | |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Mocks the HostProviderService. |
| 331 | */ |
| 332 | private class TestHostProviderService extends AbstractProviderService<HostProvider> |
| 333 | implements HostProviderService { |
| 334 | |
| 335 | protected TestHostProviderService(HostProvider provider) { |
| 336 | super(provider); |
| 337 | } |
| 338 | |
| 339 | @Override |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 340 | public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 341 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void hostVanished(HostId hostId) { |
| 346 | } |
| 347 | |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 348 | @Override |
| 349 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
| 350 | |
| 351 | } |
| 352 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Mocks the HostProvider. |
| 357 | */ |
| 358 | private static class TestHostProvider extends AbstractProvider |
| 359 | implements HostProvider { |
| 360 | |
| 361 | protected TestHostProvider() { |
| 362 | super(PID); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public ProviderId id() { |
| 367 | return PID; |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public void triggerProbe(Host host) { |
| 372 | } |
| 373 | |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Mocks the HostProviderRegistry. |
| 378 | */ |
| 379 | private class TestHostRegistry implements HostProviderRegistry { |
| 380 | |
| 381 | @Override |
| 382 | public HostProviderService register(HostProvider provider) { |
| 383 | return hostProviderService; |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public void unregister(HostProvider provider) { |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public Set<ProviderId> getProviders() { |
| 392 | return null; |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | |
| 397 | } |