Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | 53fcac2 | 2023-01-11 13:25:01 -0500 | [diff] [blame^] | 2 | * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [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.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; |
Harsh Awasthi | 6bd7360 | 2022-01-31 18:40:25 +0530 | [diff] [blame] | 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.flow.instructions.Instruction; |
| 31 | import org.onosproject.net.flow.instructions.Instructions; |
| 32 | import org.onosproject.net.packet.OutboundPacket; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 33 | |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 34 | import java.io.IOException; |
Harsh Awasthi | 6bd7360 | 2022-01-31 18:40:25 +0530 | [diff] [blame] | 35 | import java.nio.ByteBuffer; |
| 36 | import java.util.List; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 37 | import java.util.Optional; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 40 | import static org.junit.Assert.assertNotNull; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 41 | import static org.junit.Assert.assertTrue; |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame] | 42 | import static org.onlab.junit.TestTools.assertAfter; |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Set of tests of the Mac Learner ONOS application component. |
| 46 | */ |
| 47 | public class MacLearnerManagerTest extends TestBaseMacLearner { |
| 48 | |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame] | 49 | private static final int DELAY = 250; |
| 50 | private static final int PROCESSING_LENGTH = 500; |
| 51 | |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 52 | @Before |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 53 | public void setUp() throws IOException { |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 54 | setUpApp(); |
| 55 | } |
| 56 | |
| 57 | @After |
| 58 | public void tearDown() { |
| 59 | this.macLearnerManager.deactivate(); |
| 60 | } |
| 61 | |
| 62 | private static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
Harsh Awasthi | 6bd7360 | 2022-01-31 18:40:25 +0530 | [diff] [blame] | 63 | public static final VlanId CLIENT_VLAN = VlanId.vlanId("100"); |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 64 | private static final VlanId CLIENT_QINQ_VLAN = VlanId.vlanId("200"); |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 65 | public static final DeviceId OLT_DEVICE_ID = DeviceId.deviceId("of:0000b86a974385f7"); |
| 66 | public static final PortNumber UNI_PORT = PortNumber.portNumber(16); |
| 67 | public static final ConnectPoint CLIENT_CP = new ConnectPoint(OLT_DEVICE_ID, UNI_PORT); |
| 68 | public static final DeviceId AGG_DEVICE_ID = DeviceId.deviceId("of:0000000000000001"); |
| 69 | public static final PortNumber AGG_OLT_PORT = PortNumber.portNumber(10); |
| 70 | public static final PortNumber OLT_NNI_PORT = PortNumber.portNumber(1048576); |
Harsh Awasthi | 6bd7360 | 2022-01-31 18:40:25 +0530 | [diff] [blame] | 71 | public static final ConnectPoint NNI_CP = new ConnectPoint(OLT_DEVICE_ID, OLT_NNI_PORT); |
| 72 | public static final String OLT_SERIAL_NUMBER = "BBSIM_OLT_1"; |
| 73 | private static final MacAddress SERVER_MAC = MacAddress.valueOf("00:00:00:00:00:11"); |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 74 | |
| 75 | @Test |
| 76 | public void testSingleTagDhcpPacket() { |
| 77 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 78 | CLIENT_VLAN, |
| 79 | VlanId.NONE, |
| 80 | CLIENT_CP)); |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame] | 81 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 82 | Optional<MacAddress> macAddress = |
| 83 | macLearnerManager.getMacMapping(CLIENT_CP.deviceId(), |
| 84 | CLIENT_CP.port(), CLIENT_VLAN); |
| 85 | assertTrue(macAddress.isPresent()); |
| 86 | assertEquals(CLIENT_MAC, macAddress.get()); |
| 87 | }); |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testDoubleTagDhcpPacket() { |
| 92 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 93 | CLIENT_VLAN, |
| 94 | CLIENT_QINQ_VLAN, |
| 95 | CLIENT_CP)); |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame] | 96 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 97 | Optional<MacAddress> macAddress = macLearnerManager.getMacMapping(CLIENT_CP.deviceId(), |
| 98 | CLIENT_CP.port(), CLIENT_QINQ_VLAN); |
| 99 | assertTrue(macAddress.isPresent()); |
| 100 | assertEquals(CLIENT_MAC, macAddress.get()); |
| 101 | }); |
| 102 | |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 105 | @Test |
| 106 | public void testHostProviding() { |
| 107 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 108 | CLIENT_VLAN, |
| 109 | CLIENT_QINQ_VLAN, |
| 110 | CLIENT_CP)); |
Andrea Campanella | bccc74e | 2020-09-30 14:05:24 +0200 | [diff] [blame] | 111 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 112 | HostId hostId = HostId.hostId(CLIENT_MAC, CLIENT_QINQ_VLAN); |
| 113 | Host host = hostService.getHost(hostId); |
| 114 | assertNotNull(host); |
| 115 | assertEquals(OLT_DEVICE_ID, host.location().deviceId()); |
| 116 | assertEquals(UNI_PORT, host.location().port()); |
| 117 | Optional<HostLocation> optAuxLoc = host.auxLocations().stream().findFirst(); |
| 118 | assertTrue(optAuxLoc.isPresent()); |
| 119 | assertEquals(AGG_DEVICE_ID, optAuxLoc.get().deviceId()); |
| 120 | assertEquals(AGG_OLT_PORT, optAuxLoc.get().port()); |
| 121 | }); |
Tunahan Sezen | 1f65c90 | 2020-09-08 13:10:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Harsh Awasthi | 6bd7360 | 2022-01-31 18:40:25 +0530 | [diff] [blame] | 124 | @Test |
| 125 | public void testDhcpForwardClientRequest() { |
| 126 | this.macLearnerManager.enableDhcpForward = true; |
| 127 | TestDhcpRequestPacketContext dhcpRequest = new TestDhcpRequestPacketContext(CLIENT_MAC, CLIENT_VLAN, |
| 128 | VlanId.NONE, CLIENT_CP); |
| 129 | ByteBuffer inBuffer = dhcpRequest.inPacket().unparsed(); |
| 130 | |
| 131 | packetService.processPacket(dhcpRequest); |
| 132 | |
| 133 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 134 | OutboundPacket emittedPacket = packetService.emittedPacket; |
| 135 | ByteBuffer outBuffer = emittedPacket.data(); |
| 136 | DeviceId deviceId = emittedPacket.sendThrough(); |
| 137 | TrafficTreatment treatment = emittedPacket.treatment(); |
| 138 | List<Instruction> instructions = treatment.allInstructions(); |
| 139 | |
| 140 | assertEquals(deviceId, OLT_DEVICE_ID); |
| 141 | for (Instruction instruction : instructions) { |
| 142 | if (instruction instanceof Instructions.OutputInstruction) { |
| 143 | assertEquals(OLT_NNI_PORT, ((Instructions.OutputInstruction) instruction).port()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Test for packet not modified |
| 148 | assertEquals(0, inBuffer.compareTo(outBuffer)); |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void testDhcpForwardServerResponse() { |
| 154 | this.macLearnerManager.enableDhcpForward = true; |
| 155 | testDhcpForwardClientRequest(); |
| 156 | |
| 157 | TestDhcpResponsePacketContext dhcpResponse = new TestDhcpResponsePacketContext(CLIENT_MAC, SERVER_MAC, |
| 158 | CLIENT_VLAN, VlanId.NONE, NNI_CP); |
| 159 | ByteBuffer inBuffer = dhcpResponse.inPacket().unparsed(); |
| 160 | |
| 161 | packetService.processPacket(dhcpResponse); |
| 162 | |
| 163 | assertAfter(DELAY, PROCESSING_LENGTH, () -> { |
| 164 | OutboundPacket emittedPacket = packetService.emittedPacket; |
| 165 | ByteBuffer outBuffer = emittedPacket.data(); |
| 166 | |
| 167 | DeviceId deviceId = emittedPacket.sendThrough(); |
| 168 | TrafficTreatment treatment = emittedPacket.treatment(); |
| 169 | List<Instruction> instructions = treatment.allInstructions(); |
| 170 | |
| 171 | assertEquals(deviceId, OLT_DEVICE_ID); |
| 172 | for (Instruction instruction : instructions) { |
| 173 | if (instruction instanceof Instructions.OutputInstruction) { |
| 174 | assertEquals(UNI_PORT, ((Instructions.OutputInstruction) instruction).port()); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Test for packet not modified |
| 179 | assertEquals(0, inBuffer.compareTo(outBuffer)); |
| 180 | }); |
| 181 | } |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 182 | } |