Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [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 | */ |
| 16 | package org.opencord.maclearner.app.impl; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.net.ConnectPoint; |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame^] | 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.HostId; |
| 27 | import org.onosproject.net.HostLocation; |
| 28 | import org.onosproject.net.PortNumber; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 29 | |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame^] | 30 | import java.io.IOException; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 31 | import java.util.Optional; |
| 32 | |
| 33 | import static org.junit.Assert.assertEquals; |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame^] | 34 | import static org.junit.Assert.assertNotNull; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 35 | import static org.junit.Assert.assertTrue; |
| 36 | |
| 37 | /** |
| 38 | * Set of tests of the Mac Learner ONOS application component. |
| 39 | */ |
| 40 | public class MacLearnerManagerTest extends TestBaseMacLearner { |
| 41 | |
| 42 | @Before |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame^] | 43 | public void setUp() throws IOException { |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 44 | setUpApp(); |
| 45 | } |
| 46 | |
| 47 | @After |
| 48 | public void tearDown() { |
| 49 | this.macLearnerManager.deactivate(); |
| 50 | } |
| 51 | |
| 52 | private static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 53 | private static final VlanId CLIENT_VLAN = VlanId.vlanId("100"); |
| 54 | private static final VlanId CLIENT_QINQ_VLAN = VlanId.vlanId("200"); |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame^] | 55 | public static final DeviceId OLT_DEVICE_ID = DeviceId.deviceId("of:0000b86a974385f7"); |
| 56 | public static final PortNumber UNI_PORT = PortNumber.portNumber(16); |
| 57 | public static final ConnectPoint CLIENT_CP = new ConnectPoint(OLT_DEVICE_ID, UNI_PORT); |
| 58 | public static final DeviceId AGG_DEVICE_ID = DeviceId.deviceId("of:0000000000000001"); |
| 59 | public static final PortNumber AGG_OLT_PORT = PortNumber.portNumber(10); |
| 60 | public static final PortNumber OLT_NNI_PORT = PortNumber.portNumber(1048576); |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 61 | |
| 62 | @Test |
| 63 | public void testSingleTagDhcpPacket() { |
| 64 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 65 | CLIENT_VLAN, |
| 66 | VlanId.NONE, |
| 67 | CLIENT_CP)); |
| 68 | Optional<MacAddress> macAddress = macLearnerManager.getMacMapping(CLIENT_CP.deviceId(), |
| 69 | CLIENT_CP.port(), CLIENT_VLAN); |
| 70 | assertTrue(macAddress.isPresent()); |
| 71 | assertEquals(CLIENT_MAC, macAddress.get()); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testDoubleTagDhcpPacket() { |
| 76 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 77 | CLIENT_VLAN, |
| 78 | CLIENT_QINQ_VLAN, |
| 79 | CLIENT_CP)); |
| 80 | Optional<MacAddress> macAddress = macLearnerManager.getMacMapping(CLIENT_CP.deviceId(), |
| 81 | CLIENT_CP.port(), CLIENT_QINQ_VLAN); |
| 82 | assertTrue(macAddress.isPresent()); |
| 83 | assertEquals(CLIENT_MAC, macAddress.get()); |
| 84 | } |
| 85 | |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame^] | 86 | @Test |
| 87 | public void testHostProviding() { |
| 88 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 89 | CLIENT_VLAN, |
| 90 | CLIENT_QINQ_VLAN, |
| 91 | CLIENT_CP)); |
| 92 | HostId hostId = HostId.hostId(CLIENT_MAC, CLIENT_QINQ_VLAN); |
| 93 | Host host = hostService.getHost(hostId); |
| 94 | assertNotNull(host); |
| 95 | assertEquals(OLT_DEVICE_ID, host.location().deviceId()); |
| 96 | assertEquals(UNI_PORT, host.location().port()); |
| 97 | Optional<HostLocation> optAuxLoc = host.auxLocations().stream().findFirst(); |
| 98 | assertTrue(optAuxLoc.isPresent()); |
| 99 | assertEquals(AGG_DEVICE_ID, optAuxLoc.get().deviceId()); |
| 100 | assertEquals(AGG_OLT_PORT, optAuxLoc.get().port()); |
| 101 | } |
| 102 | |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 103 | } |