Yi Tseng | e72fbb5 | 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.node.ArrayNode; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.Ip6Address; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | /** |
| 30 | * DHCP server configuration. |
| 31 | */ |
| 32 | public class DhcpServerConfig { |
| 33 | private static final String DHCP_CONNECT_POINT = "dhcpServerConnectPoint"; |
| 34 | private static final String DHCP_SERVER_IP = "serverIps"; |
| 35 | private static final String DHCP_GATEWAY_IP = "gatewayIps"; |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame^] | 36 | private static final String RELAY_AGENT_IP = "relayAgentIps"; |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 37 | |
| 38 | private ConnectPoint connectPoint; |
| 39 | private Ip4Address serverIp4Addr; |
| 40 | private Ip4Address gatewayIp4Addr; |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame^] | 41 | private Ip4Address relayAgentIp4Addr; |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 42 | private Ip6Address serverIp6Addr; |
| 43 | private Ip6Address gatewayIp6Addr; |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame^] | 44 | private Ip6Address relayAgentIp6Addr; |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 45 | |
| 46 | protected DhcpServerConfig() { |
| 47 | // empty config not allowed here |
| 48 | } |
| 49 | |
| 50 | public DhcpServerConfig(JsonNode config) { |
| 51 | if (!config.has(DHCP_CONNECT_POINT)) { |
| 52 | // connect point doesn't exist |
| 53 | throw new IllegalArgumentException("Missing " + DHCP_CONNECT_POINT); |
| 54 | } |
| 55 | connectPoint = ConnectPoint.deviceConnectPoint(config.path(DHCP_CONNECT_POINT).asText()); |
| 56 | |
| 57 | if (!config.has(DHCP_SERVER_IP)) { |
| 58 | // server ip doesn't exist |
| 59 | throw new IllegalArgumentException("Missing " + DHCP_SERVER_IP); |
| 60 | } |
| 61 | ArrayNode serverIps = (ArrayNode) config.path(DHCP_SERVER_IP); |
| 62 | serverIps.forEach(node -> { |
| 63 | if (node.isTextual()) { |
| 64 | IpAddress ip = IpAddress.valueOf(node.asText()); |
| 65 | if (ip.isIp4() && serverIp4Addr == null) { |
| 66 | serverIp4Addr = ip.getIp4Address(); |
| 67 | } |
| 68 | if (ip.isIp6() && serverIp6Addr == null) { |
| 69 | serverIp6Addr = ip.getIp6Address(); |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame^] | 74 | if (config.has(DHCP_GATEWAY_IP)) { |
| 75 | ArrayNode gatewayIps = (ArrayNode) config.path(DHCP_GATEWAY_IP); |
| 76 | gatewayIps.forEach(node -> { |
| 77 | if (node.isTextual()) { |
| 78 | IpAddress ip = IpAddress.valueOf(node.asText()); |
| 79 | if (ip.isIp4() && gatewayIp4Addr == null) { |
| 80 | gatewayIp4Addr = ip.getIp4Address(); |
| 81 | } |
| 82 | if (ip.isIp6() && gatewayIp6Addr == null) { |
| 83 | gatewayIp6Addr = ip.getIp6Address(); |
| 84 | } |
| 85 | } |
| 86 | }); |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 87 | } |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame^] | 88 | if (config.has(RELAY_AGENT_IP)) { |
| 89 | ArrayNode relayAgentIps = (ArrayNode) config.path(RELAY_AGENT_IP); |
| 90 | relayAgentIps.forEach(node -> { |
| 91 | if (node.isTextual()) { |
| 92 | IpAddress ip = IpAddress.valueOf(node.asText()); |
| 93 | if (ip.isIp4() && relayAgentIp4Addr == null) { |
| 94 | relayAgentIp4Addr = ip.getIp4Address(); |
| 95 | } |
| 96 | if (ip.isIp6() && relayAgentIp6Addr == null) { |
| 97 | relayAgentIp6Addr = ip.getIp6Address(); |
| 98 | } |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 99 | } |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame^] | 100 | }); |
| 101 | } |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Verify a json config is a valid DHCP server config. |
| 106 | * |
| 107 | * @param jsonConfig the json config |
| 108 | * @return true if valid; false otherwise |
| 109 | */ |
| 110 | public static boolean isValid(JsonNode jsonConfig) { |
| 111 | return jsonConfig.has(DHCP_CONNECT_POINT) && jsonConfig.has(DHCP_SERVER_IP); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the dhcp server connect point. |
| 116 | * |
| 117 | * @return dhcp server connect point |
| 118 | */ |
| 119 | public Optional<ConnectPoint> getDhcpServerConnectPoint() { |
| 120 | return Optional.ofNullable(connectPoint); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the IPv4 address of DHCP server. |
| 125 | * |
| 126 | * @return IPv4 address of server; empty value if not set |
| 127 | */ |
| 128 | public Optional<Ip4Address> getDhcpServerIp4() { |
| 129 | return Optional.ofNullable(serverIp4Addr); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the optional IPv4 address of dhcp gateway, if configured. |
| 134 | * This option is typically used if the dhcp server is not directly attached |
| 135 | * to a switch; For example, the dhcp server may be reached via an external |
| 136 | * gateway connected to the dhcpserverConnectPoint. |
| 137 | * |
| 138 | * @return IPv4 address of gateway; empty value if not set |
| 139 | */ |
| 140 | public Optional<Ip4Address> getDhcpGatewayIp4() { |
| 141 | return Optional.ofNullable(gatewayIp4Addr); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the IPv6 address of DHCP server. |
| 146 | * |
| 147 | * @return IPv6 address of server ; empty value if not set |
| 148 | */ |
| 149 | public Optional<Ip6Address> getDhcpServerIp6() { |
| 150 | return Optional.ofNullable(serverIp6Addr); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the optional IPv6 address of dhcp gateway, if configured. |
| 155 | * This option is typically used if the dhcp server is not directly attached |
| 156 | * to a switch; For example, the dhcp server may be reached via an external |
| 157 | * gateway connected to the dhcpserverConnectPoint. |
| 158 | * |
| 159 | * @return IPv6 address of gateway; empty value if not set |
| 160 | */ |
| 161 | public Optional<Ip6Address> getDhcpGatewayIp6() { |
| 162 | return Optional.ofNullable(gatewayIp6Addr); |
| 163 | } |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame^] | 164 | |
| 165 | /** |
| 166 | * Returns the optional IPv4 address for relay agent, if configured. |
| 167 | * This option is used if we want to replace the giaddr field in DHCPv4 |
| 168 | * payload. |
| 169 | * |
| 170 | * @return the giaddr; empty value if not set |
| 171 | */ |
| 172 | public Optional<Ip4Address> getRelayAgentIp4() { |
| 173 | return Optional.ofNullable(relayAgentIp4Addr); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns the optional IPv6 address for relay agent, if configured. |
| 178 | * This option is used if we want to replace the link-address field in DHCPv6 |
| 179 | * payload. |
| 180 | * |
| 181 | * @return the giaddr; empty value if not set |
| 182 | */ |
| 183 | public Optional<Ip6Address> getRelayAgentIp6() { |
| 184 | return Optional.ofNullable(relayAgentIp6Addr); |
| 185 | } |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 186 | } |