Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [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 | */ |
| 17 | |
| 18 | package org.onosproject.dhcprelay.config; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.google.common.io.Resources; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.Ip4Address; |
| 25 | import org.onlab.packet.Ip6Address; |
| 26 | import org.onosproject.TestApplicationId; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | |
| 32 | import static org.junit.Assert.*; |
| 33 | import static org.onosproject.dhcprelay.DhcpRelayManager.DHCP_RELAY_APP; |
| 34 | |
| 35 | /** |
| 36 | * Tests for DHCP relay app configuration. |
| 37 | */ |
| 38 | public class DhcpRelayConfigTest { |
| 39 | private static final String CONFIG_FILE_PATH = "dhcp-relay.json"; |
| 40 | private static final String INVALID_CONFIG_FILE_PATH = "invalid-dhcp-relay.json"; |
| 41 | private static final ApplicationId APP_ID = new TestApplicationId("DhcpRelayTest"); |
| 42 | private static final ConnectPoint DEFAULT_CONNECT_POINT = ConnectPoint.deviceConnectPoint("of:0000000000000002/2"); |
| 43 | private static final Ip4Address DEFAULT_SERVER_IP = Ip4Address.valueOf("172.168.10.2"); |
| 44 | private static final Ip4Address DEFAULT_GATEWAY_IP = Ip4Address.valueOf("192.168.10.254"); |
| 45 | private static final Ip6Address DEFAULT_SERVER_IP_V6 = Ip6Address.valueOf("2000::200:1"); |
| 46 | private static final Ip6Address DEFAULT_GATEWAY_IP_V6 = Ip6Address.valueOf("1000::100:1"); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 47 | private static final ConnectPoint INDIRECT_CONNECT_POINT = ConnectPoint.deviceConnectPoint("of:0000000000000002/3"); |
| 48 | private static final Ip4Address INDIRECT_SERVER_IP = Ip4Address.valueOf("172.168.10.3"); |
| 49 | |
| 50 | @Test |
| 51 | public void testDefaultConfig() throws IOException { |
| 52 | ObjectMapper om = new ObjectMapper(); |
| 53 | JsonNode json = om.readTree(Resources.getResource(CONFIG_FILE_PATH)); |
| 54 | DefaultDhcpRelayConfig config = new DefaultDhcpRelayConfig(); |
| 55 | json = json.path("apps").path(DHCP_RELAY_APP).path(DefaultDhcpRelayConfig.KEY); |
| 56 | config.init(APP_ID, DefaultDhcpRelayConfig.KEY, json, om, null); |
| 57 | |
| 58 | assertEquals(1, config.dhcpServerConfigs().size()); |
| 59 | DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0); |
| 60 | assertEquals(DEFAULT_CONNECT_POINT, serverConfig.getDhcpServerConnectPoint().orElse(null)); |
| 61 | assertEquals(DEFAULT_SERVER_IP, serverConfig.getDhcpServerIp4().orElse(null)); |
| 62 | assertEquals(DEFAULT_GATEWAY_IP, serverConfig.getDhcpGatewayIp4().orElse(null)); |
| 63 | assertEquals(DEFAULT_SERVER_IP_V6, serverConfig.getDhcpServerIp6().orElse(null)); |
| 64 | assertEquals(DEFAULT_GATEWAY_IP_V6, serverConfig.getDhcpGatewayIp6().orElse(null)); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testIndirectConfig() throws IOException { |
| 69 | ObjectMapper om = new ObjectMapper(); |
| 70 | JsonNode json = om.readTree(Resources.getResource(CONFIG_FILE_PATH)); |
| 71 | IndirectDhcpRelayConfig config = new IndirectDhcpRelayConfig(); |
| 72 | json = json.path("apps").path(DHCP_RELAY_APP).path(IndirectDhcpRelayConfig.KEY); |
| 73 | config.init(APP_ID, IndirectDhcpRelayConfig.KEY, json, om, null); |
| 74 | |
| 75 | assertEquals(1, config.dhcpServerConfigs().size()); |
| 76 | DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0); |
| 77 | assertEquals(INDIRECT_CONNECT_POINT, serverConfig.getDhcpServerConnectPoint().orElse(null)); |
| 78 | assertEquals(INDIRECT_SERVER_IP, serverConfig.getDhcpServerIp4().orElse(null)); |
| 79 | assertNull(serverConfig.getDhcpGatewayIp4().orElse(null)); |
| 80 | assertNull(serverConfig.getDhcpServerIp6().orElse(null)); |
| 81 | assertNull(serverConfig.getDhcpGatewayIp6().orElse(null)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testInvalidConfig() throws IOException { |
| 86 | ObjectMapper om = new ObjectMapper(); |
| 87 | JsonNode json = om.readTree(Resources.getResource(INVALID_CONFIG_FILE_PATH)); |
| 88 | DefaultDhcpRelayConfig config = new DefaultDhcpRelayConfig(); |
| 89 | json = json.path("apps").path(DHCP_RELAY_APP).path(DefaultDhcpRelayConfig.KEY); |
| 90 | config.init(APP_ID, DefaultDhcpRelayConfig.KEY, json, om, null); |
| 91 | assertFalse(config.isValid()); |
| 92 | } |
| 93 | } |