blob: bddfea3dd09870df31955bd8800dcc6fdf4ec543 [file] [log] [blame]
Tunahan Sezen03e55272020-04-18 09:18:53 +00001/*
Joey Armstrong53fcac22023-01-11 13:25:01 -05002 * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors
Tunahan Sezen03e55272020-04-18 09:18:53 +00003 *
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 */
16package org.opencord.maclearner.app.impl;
17
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.ConnectPoint;
Tunahan Sezen1f65c902020-09-08 13:10:16 +000024import org.onosproject.net.DeviceId;
25import org.onosproject.net.Host;
26import org.onosproject.net.HostId;
27import org.onosproject.net.HostLocation;
28import org.onosproject.net.PortNumber;
Harsh Awasthi6bd73602022-01-31 18:40:25 +053029import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.flow.instructions.Instruction;
31import org.onosproject.net.flow.instructions.Instructions;
32import org.onosproject.net.packet.OutboundPacket;
Tunahan Sezen03e55272020-04-18 09:18:53 +000033
Tunahan Sezen1f65c902020-09-08 13:10:16 +000034import java.io.IOException;
Harsh Awasthi6bd73602022-01-31 18:40:25 +053035import java.nio.ByteBuffer;
36import java.util.List;
Tunahan Sezen03e55272020-04-18 09:18:53 +000037import java.util.Optional;
38
39import static org.junit.Assert.assertEquals;
Tunahan Sezen1f65c902020-09-08 13:10:16 +000040import static org.junit.Assert.assertNotNull;
Tunahan Sezen03e55272020-04-18 09:18:53 +000041import static org.junit.Assert.assertTrue;
Andrea Campanellabccc74e2020-09-30 14:05:24 +020042import static org.onlab.junit.TestTools.assertAfter;
Tunahan Sezen03e55272020-04-18 09:18:53 +000043
44/**
45 * Set of tests of the Mac Learner ONOS application component.
46 */
47public class MacLearnerManagerTest extends TestBaseMacLearner {
48
Andrea Campanellabccc74e2020-09-30 14:05:24 +020049 private static final int DELAY = 250;
50 private static final int PROCESSING_LENGTH = 500;
51
Tunahan Sezen03e55272020-04-18 09:18:53 +000052 @Before
Tunahan Sezen1f65c902020-09-08 13:10:16 +000053 public void setUp() throws IOException {
Tunahan Sezen03e55272020-04-18 09:18:53 +000054 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 Awasthi6bd73602022-01-31 18:40:25 +053063 public static final VlanId CLIENT_VLAN = VlanId.vlanId("100");
Tunahan Sezen03e55272020-04-18 09:18:53 +000064 private static final VlanId CLIENT_QINQ_VLAN = VlanId.vlanId("200");
Tunahan Sezen1f65c902020-09-08 13:10:16 +000065 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 Awasthi6bd73602022-01-31 18:40:25 +053071 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 Sezen03e55272020-04-18 09:18:53 +000074
75 @Test
76 public void testSingleTagDhcpPacket() {
77 packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC,
78 CLIENT_VLAN,
79 VlanId.NONE,
80 CLIENT_CP));
Andrea Campanellabccc74e2020-09-30 14:05:24 +020081 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 Sezen03e55272020-04-18 09:18:53 +000088 }
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 Campanellabccc74e2020-09-30 14:05:24 +020096 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 Sezen03e55272020-04-18 09:18:53 +0000103 }
104
Tunahan Sezen1f65c902020-09-08 13:10:16 +0000105 @Test
106 public void testHostProviding() {
107 packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC,
108 CLIENT_VLAN,
109 CLIENT_QINQ_VLAN,
110 CLIENT_CP));
Andrea Campanellabccc74e2020-09-30 14:05:24 +0200111 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 Sezen1f65c902020-09-08 13:10:16 +0000122 }
123
Harsh Awasthi6bd73602022-01-31 18:40:25 +0530124 @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 Sezen03e55272020-04-18 09:18:53 +0000182}