blob: adb59dbc5d622b8541a50d8f472156ea33560419 [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;
36
37/**
38 * Set of tests of the Mac Learner ONOS application component.
39 */
40public class MacLearnerManagerTest extends TestBaseMacLearner {
41
42 @Before
Tunahan Sezen1f65c902020-09-08 13:10:16 +000043 public void setUp() throws IOException {
Tunahan Sezen03e55272020-04-18 09:18:53 +000044 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 Sezen1f65c902020-09-08 13:10:16 +000055 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 Sezen03e55272020-04-18 09:18:53 +000061
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 Sezen1f65c902020-09-08 13:10:16 +000086 @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 Sezen03e55272020-04-18 09:18:53 +0000103}