Yi Tseng | 919b2df | 2017-09-07 16:22:51 -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 | package org.onosproject.dhcprelay.api; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Objects; |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
| 25 | |
| 26 | import java.util.Optional; |
| 27 | |
| 28 | /** |
| 29 | * class contains DHCP server information. |
| 30 | */ |
| 31 | public class DhcpServerInfo extends DhcpServerConfig { |
| 32 | public enum Version { |
| 33 | DHCP_V4, |
| 34 | DHCP_V6 |
| 35 | } |
| 36 | private MacAddress dhcpConnectMac; |
| 37 | private VlanId dhcpConnectVlan; |
| 38 | private Version version; |
| 39 | |
| 40 | /** |
| 41 | * Creates DHCP server information from config. |
| 42 | * |
| 43 | * @param config DHCP server config |
| 44 | * @param version DHCP version for the server |
| 45 | */ |
| 46 | public DhcpServerInfo(DhcpServerConfig config, Version version) { |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 47 | this.relayAgentIps = Maps.newHashMap(config.getRelayAgentIps()); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 48 | this.connectPoint = config.getDhcpServerConnectPoint().orElse(null); |
| 49 | this.version = version; |
| 50 | |
| 51 | switch (version) { |
| 52 | case DHCP_V4: |
| 53 | this.serverIp4Addr = config.getDhcpServerIp4().orElse(null); |
| 54 | this.gatewayIp4Addr = config.getDhcpGatewayIp4().orElse(null); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 55 | break; |
| 56 | case DHCP_V6: |
| 57 | this.serverIp6Addr = config.getDhcpServerIp6().orElse(null); |
| 58 | this.gatewayIp6Addr = config.getDhcpGatewayIp6().orElse(null); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 59 | break; |
| 60 | default: |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets DHCP server or gateway mac address. |
| 67 | * |
| 68 | * @param dhcpConnectMac the mac address |
| 69 | */ |
| 70 | public void setDhcpConnectMac(MacAddress dhcpConnectMac) { |
| 71 | this.dhcpConnectMac = dhcpConnectMac; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets DHCP server or gateway vlan id. |
| 76 | * |
| 77 | * @param dhcpConnectVlan the vlan id |
| 78 | */ |
| 79 | public void setDhcpConnectVlan(VlanId dhcpConnectVlan) { |
| 80 | this.dhcpConnectVlan = dhcpConnectVlan; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets DHCP server or gateway mac address. |
| 85 | * |
| 86 | * @return the mac address |
| 87 | */ |
| 88 | public Optional<MacAddress> getDhcpConnectMac() { |
| 89 | return Optional.ofNullable(dhcpConnectMac); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets DHCP server or gateway vlan id. |
| 94 | * |
| 95 | * @return the vlan id. |
| 96 | */ |
| 97 | public Optional<VlanId> getDhcpConnectVlan() { |
| 98 | return Optional.ofNullable(dhcpConnectVlan); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get DHCP version of the DHCP server. |
| 103 | * |
| 104 | * @return the version; can be DHCP_V4 or DHCP_V6 |
| 105 | */ |
| 106 | public Version getVersion() { |
| 107 | return version; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | MoreObjects.ToStringHelper toStringHelper = MoreObjects.toStringHelper(this); |
| 113 | toStringHelper |
| 114 | .add("dhcpConnectMac", dhcpConnectMac) |
| 115 | .add("dhcpConnectVlan", dhcpConnectVlan) |
| 116 | .add("connectPoint", connectPoint) |
| 117 | .add("version", version); |
| 118 | switch (version) { |
| 119 | case DHCP_V4: |
| 120 | toStringHelper |
| 121 | .add("serverIp4Addr", serverIp4Addr) |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 122 | .add("gatewayIp4Addr", gatewayIp4Addr); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 123 | break; |
| 124 | case DHCP_V6: |
| 125 | toStringHelper |
| 126 | .add("serverIp6Addr", serverIp6Addr) |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 127 | .add("gatewayIp6Addr", gatewayIp6Addr); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 128 | break; |
| 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | return toStringHelper.toString(); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public boolean equals(Object o) { |
| 137 | if (this == o) { |
| 138 | return true; |
| 139 | } |
| 140 | if (!(o instanceof DhcpServerInfo)) { |
| 141 | return false; |
| 142 | } |
| 143 | DhcpServerInfo that = (DhcpServerInfo) o; |
| 144 | return super.equals(o) && |
| 145 | Objects.equal(dhcpConnectMac, that.dhcpConnectMac) && |
| 146 | Objects.equal(dhcpConnectVlan, that.dhcpConnectVlan) && |
| 147 | version == that.version; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int hashCode() { |
| 152 | return Objects.hashCode(super.hashCode(), dhcpConnectMac, dhcpConnectVlan, version); |
| 153 | } |
| 154 | } |