gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.dhcprelay; |
| 17 | |
| 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.net.ConnectPoint; |
| 20 | import org.onosproject.net.config.Config; |
| 21 | |
| 22 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 23 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 24 | import org.onlab.packet.Ip4Address; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 25 | /** |
| 26 | * DHCP Relay Config class. |
| 27 | */ |
| 28 | public class DhcpRelayConfig extends Config<ApplicationId> { |
| 29 | |
| 30 | private static final String DHCP_CONNECT_POINT = "dhcpserverConnectPoint"; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 31 | private static final String DHCP_SERVER_IP = "serverip"; |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 32 | private static final String DHCP_GATEWAY_IP = "gatewayip"; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 33 | |
| 34 | @Override |
| 35 | public boolean isValid() { |
| 36 | |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 37 | return hasOnlyFields(DHCP_CONNECT_POINT, DHCP_SERVER_IP, DHCP_GATEWAY_IP) && |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 38 | isConnectPoint(DHCP_CONNECT_POINT, MANDATORY) && |
| 39 | isIpAddress(DHCP_SERVER_IP, MANDATORY) && |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 40 | isIpAddress(DHCP_GATEWAY_IP, OPTIONAL); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the dhcp server connect point. |
| 45 | * |
| 46 | * @return dhcp server connect point |
| 47 | */ |
| 48 | public ConnectPoint getDhcpServerConnectPoint() { |
| 49 | return ConnectPoint.deviceConnectPoint(object.path(DHCP_CONNECT_POINT).asText()); |
| 50 | } |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Returns the dhcp server ip. |
| 54 | * |
| 55 | * @return ip address or null if not set |
| 56 | */ |
| 57 | public Ip4Address getDhcpServerIp() { |
| 58 | String ip = get(DHCP_SERVER_IP, null); |
| 59 | return ip != null ? Ip4Address.valueOf(ip) : null; |
| 60 | } |
| 61 | |
| 62 | /** |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 63 | * Returns the optional dhcp gateway ip, if configured. This option is |
| 64 | * typically used if the dhcp server is not directly attached to a switch; |
| 65 | * For example, the dhcp server may be reached via an external gateway connected |
| 66 | * to the dhcpserverConnectPoint. |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 67 | * |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 68 | * @return gateway ip or null if not set |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 69 | */ |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 70 | public Ip4Address getDhcpGatewayIp() { |
| 71 | String gip = get(DHCP_GATEWAY_IP, null); |
| 72 | return gip != null ? Ip4Address.valueOf(gip) : null; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 73 | } |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 74 | } |