blob: adfbc5a3f3e1ef1987683e743543059e8eb131a8 [file] [log] [blame]
Tunahan Sezen03e55272020-04-18 09:18:53 +00001/*
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 */
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;
Tunahan Sezen03e55272020-04-18 09:18:53 +000029
Tunahan Sezen1f65c902020-09-08 13:10:16 +000030import java.io.IOException;
Tunahan Sezen03e55272020-04-18 09:18:53 +000031import java.util.Optional;
32
33import static org.junit.Assert.assertEquals;
Tunahan Sezen1f65c902020-09-08 13:10:16 +000034import static org.junit.Assert.assertNotNull;
Tunahan Sezen03e55272020-04-18 09:18:53 +000035import static org.junit.Assert.assertTrue;
Andrea Campanellabccc74e2020-09-30 14:05:24 +020036import static org.onlab.junit.TestTools.assertAfter;
Tunahan Sezen03e55272020-04-18 09:18:53 +000037
38/**
39 * Set of tests of the Mac Learner ONOS application component.
40 */
41public class MacLearnerManagerTest extends TestBaseMacLearner {
42
Andrea Campanellabccc74e2020-09-30 14:05:24 +020043 private static final int DELAY = 250;
44 private static final int PROCESSING_LENGTH = 500;
45
Tunahan Sezen03e55272020-04-18 09:18:53 +000046 @Before
Tunahan Sezen1f65c902020-09-08 13:10:16 +000047 public void setUp() throws IOException {
Tunahan Sezen03e55272020-04-18 09:18:53 +000048 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 Sezen1f65c902020-09-08 13:10:16 +000059 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 Sezen03e55272020-04-18 09:18:53 +000065
66 @Test
67 public void testSingleTagDhcpPacket() {
68 packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC,
69 CLIENT_VLAN,
70 VlanId.NONE,
71 CLIENT_CP));
Andrea Campanellabccc74e2020-09-30 14:05:24 +020072 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 Sezen03e55272020-04-18 09:18:53 +000079 }
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 Campanellabccc74e2020-09-30 14:05:24 +020087 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 Sezen03e55272020-04-18 09:18:53 +000094 }
95
Tunahan Sezen1f65c902020-09-08 13:10:16 +000096 @Test
97 public void testHostProviding() {
98 packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC,
99 CLIENT_VLAN,
100 CLIENT_QINQ_VLAN,
101 CLIENT_CP));
Andrea Campanellabccc74e2020-09-30 14:05:24 +0200102 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 Sezen1f65c902020-09-08 13:10:16 +0000113 }
114
Tunahan Sezen03e55272020-04-18 09:18:53 +0000115}