blob: 2451a7a2e86fafff37e82beb6c5bb51b656afcb4 [file] [log] [blame]
Yi Tsenge72fbb52017-08-02 15:03:31 -07001/*
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
18package org.onosproject.dhcprelay.config;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import org.onlab.packet.Ip4Address;
23import org.onlab.packet.Ip6Address;
24import org.onlab.packet.IpAddress;
25import org.onosproject.net.ConnectPoint;
26
27import java.util.Optional;
28
29/**
30 * DHCP server configuration.
31 */
32public 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";
36
37 private ConnectPoint connectPoint;
38 private Ip4Address serverIp4Addr;
39 private Ip4Address gatewayIp4Addr;
40 private Ip6Address serverIp6Addr;
41 private Ip6Address gatewayIp6Addr;
42
43 protected DhcpServerConfig() {
44 // empty config not allowed here
45 }
46
47 public DhcpServerConfig(JsonNode config) {
48 if (!config.has(DHCP_CONNECT_POINT)) {
49 // connect point doesn't exist
50 throw new IllegalArgumentException("Missing " + DHCP_CONNECT_POINT);
51 }
52 connectPoint = ConnectPoint.deviceConnectPoint(config.path(DHCP_CONNECT_POINT).asText());
53
54 if (!config.has(DHCP_SERVER_IP)) {
55 // server ip doesn't exist
56 throw new IllegalArgumentException("Missing " + DHCP_SERVER_IP);
57 }
58 ArrayNode serverIps = (ArrayNode) config.path(DHCP_SERVER_IP);
59 serverIps.forEach(node -> {
60 if (node.isTextual()) {
61 IpAddress ip = IpAddress.valueOf(node.asText());
62 if (ip.isIp4() && serverIp4Addr == null) {
63 serverIp4Addr = ip.getIp4Address();
64 }
65 if (ip.isIp6() && serverIp6Addr == null) {
66 serverIp6Addr = ip.getIp6Address();
67 }
68 }
69 });
70
71 if (!config.has(DHCP_GATEWAY_IP)) {
72 // gateway ip doesn't exist, ignore the gateway
73 return;
74 }
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 });
87 }
88
89 /**
90 * Verify a json config is a valid DHCP server config.
91 *
92 * @param jsonConfig the json config
93 * @return true if valid; false otherwise
94 */
95 public static boolean isValid(JsonNode jsonConfig) {
96 return jsonConfig.has(DHCP_CONNECT_POINT) && jsonConfig.has(DHCP_SERVER_IP);
97 }
98
99 /**
100 * Returns the dhcp server connect point.
101 *
102 * @return dhcp server connect point
103 */
104 public Optional<ConnectPoint> getDhcpServerConnectPoint() {
105 return Optional.ofNullable(connectPoint);
106 }
107
108 /**
109 * Returns the IPv4 address of DHCP server.
110 *
111 * @return IPv4 address of server; empty value if not set
112 */
113 public Optional<Ip4Address> getDhcpServerIp4() {
114 return Optional.ofNullable(serverIp4Addr);
115 }
116
117 /**
118 * Returns the optional IPv4 address of dhcp gateway, if configured.
119 * This option is typically used if the dhcp server is not directly attached
120 * to a switch; For example, the dhcp server may be reached via an external
121 * gateway connected to the dhcpserverConnectPoint.
122 *
123 * @return IPv4 address of gateway; empty value if not set
124 */
125 public Optional<Ip4Address> getDhcpGatewayIp4() {
126 return Optional.ofNullable(gatewayIp4Addr);
127 }
128
129 /**
130 * Returns the IPv6 address of DHCP server.
131 *
132 * @return IPv6 address of server ; empty value if not set
133 */
134 public Optional<Ip6Address> getDhcpServerIp6() {
135 return Optional.ofNullable(serverIp6Addr);
136 }
137
138 /**
139 * Returns the optional IPv6 address of dhcp gateway, if configured.
140 * This option is typically used if the dhcp server is not directly attached
141 * to a switch; For example, the dhcp server may be reached via an external
142 * gateway connected to the dhcpserverConnectPoint.
143 *
144 * @return IPv6 address of gateway; empty value if not set
145 */
146 public Optional<Ip6Address> getDhcpGatewayIp6() {
147 return Optional.ofNullable(gatewayIp6Addr);
148 }
149}