samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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.onosproject.dhcp.impl; |
| 17 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onlab.packet.MacAddress; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.net.config.Config; |
| 22 | import org.onosproject.net.config.basics.BasicElementConfig; |
| 23 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 24 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 25 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 26 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 27 | /** |
| 28 | * DHCP Config class. |
| 29 | */ |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 30 | public class DhcpConfig extends Config<ApplicationId> { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 31 | |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 42 | 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 pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 46 | |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 47 | public static final int DEFAULT = -1; |
| 48 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 49 | @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 pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Returns the dhcp server ip. |
| 65 | * |
| 66 | * @return ip address or null if not set |
| 67 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 68 | public Ip4Address ip() { |
| 69 | String ip = get(MY_IP, null); |
| 70 | return ip != null ? Ip4Address.valueOf(ip) : null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 71 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 88 | public MacAddress mac() { |
| 89 | String mac = get(MY_MAC, null); |
| 90 | return mac != null ? MacAddress.valueOf(mac) : null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 91 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 108 | public Ip4Address subnetMask() { |
| 109 | String ip = get(SUBNET_MASK, null); |
| 110 | return ip != null ? Ip4Address.valueOf(ip) : null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 111 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 128 | public Ip4Address broadcastAddress() { |
| 129 | String ip = get(BROADCAST_ADDRESS, null); |
| 130 | return ip != null ? Ip4Address.valueOf(ip) : null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 131 | } |
| 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 pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 146 | * @return ttl or -1 if not set |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 147 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 148 | public int ttl() { |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 149 | return get(TTL, DEFAULT); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 150 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 158 | public BasicElementConfig ttl(int ttl) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 159 | return (BasicElementConfig) setOrClear(TTL, ttl); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns the Lease Time offered by the DHCP Server. |
| 164 | * |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 165 | * @return lease time or -1 if not set |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 166 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 167 | public int leaseTime() { |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 168 | return get(LEASE_TIME, DEFAULT); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 169 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 177 | public BasicElementConfig leaseTime(int lease) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 178 | return (BasicElementConfig) setOrClear(LEASE_TIME, lease); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns the Renew Time offered by the DHCP Server. |
| 183 | * |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 184 | * @return renew time or -1 if not set |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 185 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 186 | public int renewTime() { |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 187 | return get(RENEW_TIME, DEFAULT); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 188 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 196 | public BasicElementConfig renewTime(int renew) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 197 | return (BasicElementConfig) setOrClear(RENEW_TIME, renew); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns the Rebind Time offered by the DHCP Server. |
| 202 | * |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 203 | * @return rebind time or -1 if not set |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 204 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 205 | public int rebindTime() { |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 206 | return get(REBIND_TIME, DEFAULT); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 207 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 215 | public BasicElementConfig rebindTime(int rebind) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 216 | 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 224 | public Ip4Address routerAddress() { |
| 225 | String ip = get(ROUTER_ADDRESS, null); |
| 226 | return ip != null ? Ip4Address.valueOf(ip) : null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 227 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 244 | public Ip4Address domainServer() { |
| 245 | String ip = get(DOMAIN_SERVER, null); |
| 246 | return ip != null ? Ip4Address.valueOf(ip) : null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 247 | } |
| 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 pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 260 | * Returns the delay in minutes after which the dhcp server will purge expired entries. |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 261 | * |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 262 | * @return time delay or -1 if not set |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 263 | */ |
| 264 | public int timerDelay() { |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 265 | return get(TIMER_DELAY, DEFAULT); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 266 | } |
| 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 pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 281 | * @return default timeout or -1 if not set |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 282 | */ |
| 283 | public int defaultTimeout() { |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 284 | return get(DEFAULT_TIMEOUT, DEFAULT); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 285 | } |
| 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 pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 336 | } |