blob: 908d47010300e6dd56fc12963838de45b3466dde [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
samanwita palf28207b2015-09-04 10:41:56 -07003 *
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.onosproject.dhcp.impl;
17
samanwita pal2a313402015-09-14 16:03:22 -070018import org.onlab.packet.Ip4Address;
19import org.onlab.packet.MacAddress;
samanwita palf28207b2015-09-04 10:41:56 -070020import org.onosproject.core.ApplicationId;
21import org.onosproject.net.config.Config;
22import org.onosproject.net.config.basics.BasicElementConfig;
23
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080024import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
25import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
26
samanwita palf28207b2015-09-04 10:41:56 -070027/**
28 * DHCP Config class.
29 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070030public class DhcpConfig extends Config<ApplicationId> {
samanwita palf28207b2015-09-04 10:41:56 -070031
32 public static final String MY_IP = "ip";
33 public static final String MY_MAC = "mac";
34 public static final String SUBNET_MASK = "subnet";
35 public static final String BROADCAST_ADDRESS = "broadcast";
36 public static final String ROUTER_ADDRESS = "router";
37 public static final String DOMAIN_SERVER = "domain";
38 public static final String TTL = "ttl";
39 public static final String LEASE_TIME = "lease";
40 public static final String RENEW_TIME = "renew";
41 public static final String REBIND_TIME = "rebind";
samanwita pal2a313402015-09-14 16:03:22 -070042 public static final String TIMER_DELAY = "delay";
43 public static final String DEFAULT_TIMEOUT = "timeout";
44 public static final String START_IP = "startip";
45 public static final String END_IP = "endip";
samanwita palf28207b2015-09-04 10:41:56 -070046
samanwita pal7ccc2bc2015-09-14 19:53:15 -070047 public static final int DEFAULT = -1;
48
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080049 @Override
50 public boolean isValid() {
51 // FIXME: Sweep through and revisit the validation assertions
52 // For now, this is just a demonstration of potential uses
53 return hasOnlyFields(MY_IP, MY_MAC, SUBNET_MASK, BROADCAST_ADDRESS,
54 ROUTER_ADDRESS, DOMAIN_SERVER, TTL, LEASE_TIME,
55 RENEW_TIME, REBIND_TIME, TIMER_DELAY, DEFAULT_TIMEOUT,
56 START_IP, END_IP) &&
57 isIpAddress(MY_IP, MANDATORY) && isMacAddress(MY_MAC, MANDATORY) &&
58 isIpAddress(START_IP, MANDATORY) && isIpAddress(END_IP, MANDATORY) &&
59 isNumber(LEASE_TIME, OPTIONAL, 1) && isNumber(REBIND_TIME, OPTIONAL, 1) &&
60 isNumber(DEFAULT_TIMEOUT, OPTIONAL, 1, 3600);
61 }
62
samanwita palf28207b2015-09-04 10:41:56 -070063 /**
64 * Returns the dhcp server ip.
65 *
66 * @return ip address or null if not set
67 */
samanwita pal2a313402015-09-14 16:03:22 -070068 public Ip4Address ip() {
69 String ip = get(MY_IP, null);
70 return ip != null ? Ip4Address.valueOf(ip) : null;
samanwita palf28207b2015-09-04 10:41:56 -070071 }
72
73 /**
74 * Sets the dhcp server ip.
75 *
76 * @param ip new ip address; null to clear
77 * @return self
78 */
79 public BasicElementConfig ip(String ip) {
80 return (BasicElementConfig) setOrClear(MY_IP, ip);
81 }
82
83 /**
84 * Returns the dhcp server mac.
85 *
86 * @return server mac or null if not set
87 */
samanwita pal2a313402015-09-14 16:03:22 -070088 public MacAddress mac() {
89 String mac = get(MY_MAC, null);
90 return mac != null ? MacAddress.valueOf(mac) : null;
samanwita palf28207b2015-09-04 10:41:56 -070091 }
92
93 /**
94 * Sets the dhcp server mac.
95 *
96 * @param mac new mac address; null to clear
97 * @return self
98 */
99 public BasicElementConfig mac(String mac) {
100 return (BasicElementConfig) setOrClear(MY_MAC, mac);
101 }
102
103 /**
104 * Returns the subnet mask.
105 *
106 * @return subnet mask or null if not set
107 */
samanwita pal2a313402015-09-14 16:03:22 -0700108 public Ip4Address subnetMask() {
109 String ip = get(SUBNET_MASK, null);
110 return ip != null ? Ip4Address.valueOf(ip) : null;
samanwita palf28207b2015-09-04 10:41:56 -0700111 }
112
113 /**
114 * Sets the subnet mask.
115 *
116 * @param subnet new subnet mask; null to clear
117 * @return self
118 */
119 public BasicElementConfig subnetMask(String subnet) {
120 return (BasicElementConfig) setOrClear(SUBNET_MASK, subnet);
121 }
122
123 /**
124 * Returns the broadcast address.
125 *
126 * @return broadcast address or null if not set
127 */
samanwita pal2a313402015-09-14 16:03:22 -0700128 public Ip4Address broadcastAddress() {
129 String ip = get(BROADCAST_ADDRESS, null);
130 return ip != null ? Ip4Address.valueOf(ip) : null;
samanwita palf28207b2015-09-04 10:41:56 -0700131 }
132
133 /**
134 * Sets the broadcast address.
135 *
136 * @param broadcast new broadcast address; null to clear
137 * @return self
138 */
139 public BasicElementConfig broadcastAddress(String broadcast) {
140 return (BasicElementConfig) setOrClear(BROADCAST_ADDRESS, broadcast);
141 }
142
143 /**
144 * Returns the Time To Live for the reply packets.
145 *
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700146 * @return ttl or -1 if not set
samanwita palf28207b2015-09-04 10:41:56 -0700147 */
samanwita pal2a313402015-09-14 16:03:22 -0700148 public int ttl() {
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700149 return get(TTL, DEFAULT);
samanwita palf28207b2015-09-04 10:41:56 -0700150 }
151
152 /**
153 * Sets the Time To Live for the reply packets.
154 *
155 * @param ttl new ttl; null to clear
156 * @return self
157 */
samanwita pal2a313402015-09-14 16:03:22 -0700158 public BasicElementConfig ttl(int ttl) {
samanwita palf28207b2015-09-04 10:41:56 -0700159 return (BasicElementConfig) setOrClear(TTL, ttl);
160 }
161
162 /**
163 * Returns the Lease Time offered by the DHCP Server.
164 *
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700165 * @return lease time or -1 if not set
samanwita palf28207b2015-09-04 10:41:56 -0700166 */
samanwita pal2a313402015-09-14 16:03:22 -0700167 public int leaseTime() {
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700168 return get(LEASE_TIME, DEFAULT);
samanwita palf28207b2015-09-04 10:41:56 -0700169 }
170
171 /**
172 * Sets the Lease Time offered by the DHCP Server.
173 *
174 * @param lease new lease time; null to clear
175 * @return self
176 */
samanwita pal2a313402015-09-14 16:03:22 -0700177 public BasicElementConfig leaseTime(int lease) {
samanwita palf28207b2015-09-04 10:41:56 -0700178 return (BasicElementConfig) setOrClear(LEASE_TIME, lease);
179 }
180
181 /**
182 * Returns the Renew Time offered by the DHCP Server.
183 *
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700184 * @return renew time or -1 if not set
samanwita palf28207b2015-09-04 10:41:56 -0700185 */
samanwita pal2a313402015-09-14 16:03:22 -0700186 public int renewTime() {
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700187 return get(RENEW_TIME, DEFAULT);
samanwita palf28207b2015-09-04 10:41:56 -0700188 }
189
190 /**
191 * Sets the Renew Time offered by the DHCP Server.
192 *
193 * @param renew new renew time; null to clear
194 * @return self
195 */
samanwita pal2a313402015-09-14 16:03:22 -0700196 public BasicElementConfig renewTime(int renew) {
samanwita palf28207b2015-09-04 10:41:56 -0700197 return (BasicElementConfig) setOrClear(RENEW_TIME, renew);
198 }
199
200 /**
201 * Returns the Rebind Time offered by the DHCP Server.
202 *
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700203 * @return rebind time or -1 if not set
samanwita palf28207b2015-09-04 10:41:56 -0700204 */
samanwita pal2a313402015-09-14 16:03:22 -0700205 public int rebindTime() {
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700206 return get(REBIND_TIME, DEFAULT);
samanwita palf28207b2015-09-04 10:41:56 -0700207 }
208
209 /**
210 * Sets the Rebind Time offered by the DHCP Server.
211 *
212 * @param rebind new rebind time; null to clear
213 * @return self
214 */
samanwita pal2a313402015-09-14 16:03:22 -0700215 public BasicElementConfig rebindTime(int rebind) {
samanwita palf28207b2015-09-04 10:41:56 -0700216 return (BasicElementConfig) setOrClear(REBIND_TIME, rebind);
217 }
218
219 /**
220 * Returns the Router Address.
221 *
222 * @return router address or null if not set
223 */
samanwita pal2a313402015-09-14 16:03:22 -0700224 public Ip4Address routerAddress() {
225 String ip = get(ROUTER_ADDRESS, null);
226 return ip != null ? Ip4Address.valueOf(ip) : null;
samanwita palf28207b2015-09-04 10:41:56 -0700227 }
228
229 /**
230 * Sets the Router Address.
231 *
232 * @param router new router address; null to clear
233 * @return self
234 */
235 public BasicElementConfig routerAddress(String router) {
236 return (BasicElementConfig) setOrClear(ROUTER_ADDRESS, router);
237 }
238
239 /**
240 * Returns the Domain Server Address.
241 *
242 * @return domain server address or null if not set
243 */
samanwita pal2a313402015-09-14 16:03:22 -0700244 public Ip4Address domainServer() {
245 String ip = get(DOMAIN_SERVER, null);
246 return ip != null ? Ip4Address.valueOf(ip) : null;
samanwita palf28207b2015-09-04 10:41:56 -0700247 }
248
249 /**
250 * Sets the Domain Server Address.
251 *
252 * @param domain new domain server address; null to clear
253 * @return self
254 */
255 public BasicElementConfig domainServer(String domain) {
256 return (BasicElementConfig) setOrClear(DOMAIN_SERVER, domain);
257 }
samanwita pal2a313402015-09-14 16:03:22 -0700258
259 /**
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700260 * Returns the delay in minutes after which the dhcp server will purge expired entries.
samanwita pal2a313402015-09-14 16:03:22 -0700261 *
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700262 * @return time delay or -1 if not set
samanwita pal2a313402015-09-14 16:03:22 -0700263 */
264 public int timerDelay() {
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700265 return get(TIMER_DELAY, DEFAULT);
samanwita pal2a313402015-09-14 16:03:22 -0700266 }
267
268 /**
269 * Sets the delay after which the dhcp server will purge expired entries.
270 *
271 * @param delay new time delay; null to clear
272 * @return self
273 */
274 public BasicElementConfig timerDelay(int delay) {
275 return (BasicElementConfig) setOrClear(TIMER_DELAY, delay);
276 }
277
278 /**
279 * Returns the default timeout for pending assignments.
280 *
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700281 * @return default timeout or -1 if not set
samanwita pal2a313402015-09-14 16:03:22 -0700282 */
283 public int defaultTimeout() {
samanwita pal7ccc2bc2015-09-14 19:53:15 -0700284 return get(DEFAULT_TIMEOUT, DEFAULT);
samanwita pal2a313402015-09-14 16:03:22 -0700285 }
286
287 /**
288 * Sets the default timeout for pending assignments.
289 *
290 * @param defaultTimeout new default timeout; null to clear
291 * @return self
292 */
293 public BasicElementConfig defaultTimeout(int defaultTimeout) {
294 return (BasicElementConfig) setOrClear(DEFAULT_TIMEOUT, defaultTimeout);
295 }
296
297 /**
298 * Returns the start IP for the available IP Range.
299 *
300 * @return start IP or null if not set
301 */
302 public Ip4Address startIp() {
303 String ip = get(START_IP, null);
304 return ip != null ? Ip4Address.valueOf(ip) : null;
305 }
306
307 /**
308 * Sets the start IP for the available IP Range.
309 *
310 * @param startIp new start IP; null to clear
311 * @return self
312 */
313 public BasicElementConfig startIp(String startIp) {
314 return (BasicElementConfig) setOrClear(START_IP, startIp);
315 }
316
317 /**
318 * Returns the end IP for the available IP Range.
319 *
320 * @return end IP or null if not set
321 */
322 public Ip4Address endIp() {
323 String ip = get(END_IP, null);
324 return ip != null ? Ip4Address.valueOf(ip) : null;
325 }
326
327 /**
328 * Sets the end IP for the available IP Range.
329 *
330 * @param endIp new end IP; null to clear
331 * @return self
332 */
333 public BasicElementConfig endIp(String endIp) {
334 return (BasicElementConfig) setOrClear(END_IP, endIp);
335 }
samanwita palf28207b2015-09-04 10:41:56 -0700336}