Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [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.dhcpl2relay.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.easymock.EasyMock; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 22 | import org.onlab.junit.TestUtils; |
| 23 | import org.onlab.osgi.ComponentContextAdapter; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.LeadershipServiceAdapter; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.config.Config; |
| 28 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 29 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 30 | import org.onosproject.store.service.TestStorageService; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 31 | |
| 32 | import java.util.Set; |
| 33 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 34 | import static org.hamcrest.Matchers.is; |
| 35 | import static org.junit.Assert.assertNull; |
| 36 | import static org.junit.Assert.assertThat; |
| 37 | import static org.junit.Assert.assertTrue; |
| 38 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 39 | /** |
| 40 | * Tests for DHCP relay app configuration. |
| 41 | */ |
| 42 | public class DhcpL2RelayConfigTest extends DhcpL2RelayTestBase { |
| 43 | |
| 44 | static final boolean USE_OLT_ULPORT_FOR_PKT_INOUT = true; |
| 45 | static final boolean MODIFY_SRC_DST_MAC = true; |
| 46 | |
| 47 | private DhcpL2Relay dhcpL2Relay; |
| 48 | |
| 49 | ComponentConfigService mockConfigService = |
| 50 | EasyMock.createMock(ComponentConfigService.class); |
| 51 | |
| 52 | /** |
| 53 | * Sets up the services required by the dhcpl2relay app. |
| 54 | */ |
| 55 | @Before |
| 56 | public void setUp() { |
| 57 | dhcpL2Relay = new DhcpL2Relay(); |
| 58 | dhcpL2Relay.cfgService = new TestNetworkConfigRegistry(); |
| 59 | dhcpL2Relay.coreService = new MockCoreServiceAdapter(); |
| 60 | dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter(); |
| 61 | dhcpL2Relay.packetService = new MockPacketService(); |
| 62 | dhcpL2Relay.componentConfigService = mockConfigService; |
| 63 | dhcpL2Relay.deviceService = new MockDeviceService(); |
| 64 | dhcpL2Relay.sadisService = new MockSadisService(); |
| 65 | dhcpL2Relay.hostService = new MockHostService(); |
| 66 | dhcpL2Relay.mastershipService = new MockMastershipService(); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 67 | dhcpL2Relay.storageService = new TestStorageService(); |
| 68 | dhcpL2Relay.leadershipService = new LeadershipServiceAdapter(); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 69 | SimpleDhcpL2RelayCountersStore store = new SimpleDhcpL2RelayCountersStore(); |
| 70 | store.componentConfigService = mockConfigService; |
| 71 | dhcpL2Relay.dhcpL2RelayCounters = store; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 72 | TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher()); |
| 73 | dhcpL2Relay.activate(new ComponentContextAdapter()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Mocks the network config registry. |
| 78 | */ |
| 79 | static class MockDhcpL2RelayConfig extends DhcpL2RelayConfig { |
| 80 | @Override |
| 81 | public Set<ConnectPoint> getDhcpServerConnectPoint() { |
| 82 | return ImmutableSet.of(SERVER_CONNECT_POINT); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean getModifySrcDstMacAddresses() { |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean getUseOltUplinkForServerPktInOut() { |
| 92 | return true; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests the default configuration. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testConfig() { |
| 101 | assertThat(dhcpL2Relay.useOltUplink, is(USE_OLT_ULPORT_FOR_PKT_INOUT)); |
| 102 | assertThat(dhcpL2Relay.modifyClientPktsSrcDstMac, is(MODIFY_SRC_DST_MAC)); |
| 103 | assertNull(dhcpL2Relay.dhcpServerConnectPoint.get()); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests if dhcpl2relay app has been configured. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testDhcpL2RelayConfigured() { |
| 111 | assertTrue(dhcpL2Relay.configured()); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Mocks the network config registry. |
| 116 | */ |
| 117 | @SuppressWarnings("unchecked") |
| 118 | static final class TestNetworkConfigRegistry |
| 119 | extends NetworkConfigRegistryAdapter { |
| 120 | @Override |
| 121 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 122 | DhcpL2RelayConfig dhcpConfig = new MockDhcpL2RelayConfig(); |
| 123 | return (C) dhcpConfig; |
| 124 | } |
| 125 | } |
| 126 | } |