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; |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame^] | 36 | import static org.onlab.junit.TestTools.assertAfter; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Set of tests of the Mac Learner ONOS application component. |
| 40 | */ |
| 41 | public class MacLearnerManagerTest extends TestBaseMacLearner { |
| 42 | |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame^] | 43 | private static final int DELAY = 250; |
| 44 | private static final int PROCESSING_LENGTH = 500; |
| 45 | |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 46 | @Before |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 47 | public void setUp() throws IOException { |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 48 | setUpApp(); |
| 49 | } |
| 50 | |
| 51 | @After |
| 52 | public void tearDown() { |
| 53 | this.macLearnerManager.deactivate(); |
| 54 | } |
| 55 | |
| 56 | private static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 57 | private static final VlanId CLIENT_VLAN = VlanId.vlanId("100"); |
| 58 | private static final VlanId CLIENT_QINQ_VLAN = VlanId.vlanId("200"); |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 59 | public static final DeviceId OLT_DEVICE_ID = DeviceId.deviceId("of:0000b86a974385f7"); |
| 60 | public static final PortNumber UNI_PORT = PortNumber.portNumber(16); |
| 61 | public static final ConnectPoint CLIENT_CP = new ConnectPoint(OLT_DEVICE_ID, UNI_PORT); |
| 62 | public static final DeviceId AGG_DEVICE_ID = DeviceId.deviceId("of:0000000000000001"); |
| 63 | public static final PortNumber AGG_OLT_PORT = PortNumber.portNumber(10); |
| 64 | public static final PortNumber OLT_NNI_PORT = PortNumber.portNumber(1048576); |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 65 | |
| 66 | @Test |
| 67 | public void testSingleTagDhcpPacket() { |
| 68 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 69 | CLIENT_VLAN, |
| 70 | VlanId.NONE, |
| 71 | CLIENT_CP)); |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame^] | 72 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 73 | Optional<MacAddress> macAddress = |
| 74 | macLearnerManager.getMacMapping(CLIENT_CP.deviceId(), |
| 75 | CLIENT_CP.port(), CLIENT_VLAN); |
| 76 | assertTrue(macAddress.isPresent()); |
| 77 | assertEquals(CLIENT_MAC, macAddress.get()); |
| 78 | }); |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testDoubleTagDhcpPacket() { |
| 83 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 84 | CLIENT_VLAN, |
| 85 | CLIENT_QINQ_VLAN, |
| 86 | CLIENT_CP)); |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame^] | 87 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 88 | Optional<MacAddress> macAddress = macLearnerManager.getMacMapping(CLIENT_CP.deviceId(), |
| 89 | CLIENT_CP.port(), CLIENT_QINQ_VLAN); |
| 90 | assertTrue(macAddress.isPresent()); |
| 91 | assertEquals(CLIENT_MAC, macAddress.get()); |
| 92 | }); |
| 93 | |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 96 | @Test |
| 97 | public void testHostProviding() { |
| 98 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 99 | CLIENT_VLAN, |
| 100 | CLIENT_QINQ_VLAN, |
| 101 | CLIENT_CP)); |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame^] | 102 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 103 | HostId hostId = HostId.hostId(CLIENT_MAC, CLIENT_QINQ_VLAN); |
| 104 | Host host = hostService.getHost(hostId); |
| 105 | assertNotNull(host); |
| 106 | assertEquals(OLT_DEVICE_ID, host.location().deviceId()); |
| 107 | assertEquals(UNI_PORT, host.location().port()); |
| 108 | Optional<HostLocation> optAuxLoc = host.auxLocations().stream().findFirst(); |
| 109 | assertTrue(optAuxLoc.isPresent()); |
| 110 | assertEquals(AGG_DEVICE_ID, optAuxLoc.get().deviceId()); |
| 111 | assertEquals(AGG_OLT_PORT, optAuxLoc.get().port()); |
| 112 | }); |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 115 | } |