blob: 81a2411e1e8dc469ad7ac1c558e23203d80c8aff [file] [log] [blame]
Yi Tseng2fe8f3f2017-09-07 16:22:51 -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
17package org.onosproject.dhcprelay.api;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.base.Objects;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.dhcprelay.config.DhcpServerConfig;
24
25import java.util.Optional;
26
27/**
28 * class contains DHCP server information.
29 */
30public class DhcpServerInfo extends DhcpServerConfig {
31 public enum Version {
32 DHCP_V4,
33 DHCP_V6
34 }
35 private MacAddress dhcpConnectMac;
36 private VlanId dhcpConnectVlan;
37 private Version version;
38
39 /**
40 * Creates DHCP server information from config.
41 *
42 * @param config DHCP server config
43 * @param version DHCP version for the server
44 */
45 public DhcpServerInfo(DhcpServerConfig config, Version version) {
46 this.connectPoint = config.getDhcpServerConnectPoint().orElse(null);
47 this.version = version;
48
49 switch (version) {
50 case DHCP_V4:
51 this.serverIp4Addr = config.getDhcpServerIp4().orElse(null);
52 this.gatewayIp4Addr = config.getDhcpGatewayIp4().orElse(null);
53 this.relayAgentIp4Addr = config.getRelayAgentIp4().orElse(null);
54 break;
55 case DHCP_V6:
56 this.serverIp6Addr = config.getDhcpServerIp6().orElse(null);
57 this.gatewayIp6Addr = config.getDhcpGatewayIp6().orElse(null);
58 this.relayAgentIp6Addr = config.getRelayAgentIp6().orElse(null);
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)
122 .add("gatewayIp4Addr", gatewayIp4Addr)
123 .add("relayAgentIp4Addr", relayAgentIp4Addr);
124 break;
125 case DHCP_V6:
126 toStringHelper
127 .add("serverIp6Addr", serverIp6Addr)
128 .add("gatewayIp6Addr", gatewayIp6Addr)
129 .add("relayAgentIp6Addr", relayAgentIp6Addr);
130 break;
131 default:
132 break;
133 }
134 return toStringHelper.toString();
135 }
136
137 @Override
138 public boolean equals(Object o) {
139 if (this == o) {
140 return true;
141 }
142 if (!(o instanceof DhcpServerInfo)) {
143 return false;
144 }
145 DhcpServerInfo that = (DhcpServerInfo) o;
146 return super.equals(o) &&
147 Objects.equal(dhcpConnectMac, that.dhcpConnectMac) &&
148 Objects.equal(dhcpConnectVlan, that.dhcpConnectVlan) &&
149 version == that.version;
150 }
151
152 @Override
153 public int hashCode() {
154 return Objects.hashCode(super.hashCode(), dhcpConnectMac, dhcpConnectVlan, version);
155 }
156}