blob: 79c535d408ce68f0b4b7fe9d0a0c36a6d77470a5 [file] [log] [blame]
Jian Li3e81b182021-01-11 02:35:06 +09001/*
2 * Copyright 2021-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 */
16package org.onosproject.kubevirtnetworking.api;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.IpAddress;
21
Jian Li97e6fc32021-02-01 20:36:45 +090022import java.util.HashSet;
Jian Li3e81b182021-01-11 02:35:06 +090023import java.util.Objects;
24import java.util.Set;
25
26import static com.google.common.base.Preconditions.checkArgument;
27import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.FLAT;
28
29/**
30 * Default implementation class of kubevirt network.
31 */
32public final class DefaultKubevirtNetwork implements KubevirtNetwork {
33
34 private static final String NOT_NULL_MSG = "Network % cannot be null";
35
36 private final String networkId;
37 private final Type type;
38 private final String name;
39 private final Integer mtu;
40 private final String segmentId;
41 private final IpAddress gatewayIp;
42 private final String cidr;
Jian Lifeb84802021-01-12 16:34:49 +090043 private final Set<KubevirtHostRoute> hostRoutes;
Jian Li3e81b182021-01-11 02:35:06 +090044 private final KubevirtIpPool ipPool;
Jian Li97e6fc32021-02-01 20:36:45 +090045 private final Set<IpAddress> dnses;
Jian Li3e81b182021-01-11 02:35:06 +090046
47 /**
48 * Default constructor.
49 *
50 * @param networkId network identifier
51 * @param type type of network
52 * @param name network name
53 * @param mtu network MTU
54 * @param segmentId segment identifier
55 * @param gatewayIp gateway IP address
56 * @param cidr CIDR of network
Jian Lifeb84802021-01-12 16:34:49 +090057 * @param hostRoutes a set of host routes
Jian Li3e81b182021-01-11 02:35:06 +090058 * @param ipPool IP pool
Jian Li97e6fc32021-02-01 20:36:45 +090059 * @param dnses a set of DNSes
Jian Li3e81b182021-01-11 02:35:06 +090060 */
61 public DefaultKubevirtNetwork(String networkId, Type type, String name,
62 Integer mtu, String segmentId, IpAddress gatewayIp,
Jian Lifeb84802021-01-12 16:34:49 +090063 String cidr, Set<KubevirtHostRoute> hostRoutes,
Jian Li97e6fc32021-02-01 20:36:45 +090064 KubevirtIpPool ipPool, Set<IpAddress> dnses) {
Jian Li3e81b182021-01-11 02:35:06 +090065 this.networkId = networkId;
66 this.type = type;
67 this.name = name;
68 this.mtu = mtu;
69 this.segmentId = segmentId;
70 this.gatewayIp = gatewayIp;
71 this.cidr = cidr;
Jian Lifeb84802021-01-12 16:34:49 +090072 this.hostRoutes = hostRoutes;
Jian Li3e81b182021-01-11 02:35:06 +090073 this.ipPool = ipPool;
Jian Li97e6fc32021-02-01 20:36:45 +090074 this.dnses = dnses;
Jian Li3e81b182021-01-11 02:35:06 +090075 }
76
77 @Override
78 public String networkId() {
79 return networkId;
80 }
81
82 @Override
83 public Type type() {
84 return type;
85 }
86
87 @Override
88 public String name() {
89 return name;
90 }
91
92 @Override
93 public Integer mtu() {
94 return mtu;
95 }
96
97 @Override
98 public String segmentId() {
99 return segmentId;
100 }
101
102 @Override
103 public IpAddress gatewayIp() {
104 return gatewayIp;
105 }
106
107 @Override
108 public String cidr() {
109 return cidr;
110 }
111
112 @Override
Jian Lifeb84802021-01-12 16:34:49 +0900113 public Set<KubevirtHostRoute> hostRoutes() {
Jian Li16eed162021-01-15 16:58:48 +0900114 if (hostRoutes == null || hostRoutes.size() == 0) {
115 return ImmutableSet.of();
116 } else {
117 return ImmutableSet.copyOf(hostRoutes);
118 }
Jian Li3e81b182021-01-11 02:35:06 +0900119 }
120
121 @Override
122 public KubevirtIpPool ipPool() {
123 return ipPool;
124 }
125
126 @Override
Jian Li97e6fc32021-02-01 20:36:45 +0900127 public Set<IpAddress> dnses() {
128 if (dnses == null || dnses.size() == 0) {
129 return ImmutableSet.of();
130 } else {
131 return ImmutableSet.copyOf(dnses);
132 }
133 }
134
135 @Override
Jian Li3e81b182021-01-11 02:35:06 +0900136 public boolean equals(Object o) {
137 if (this == o) {
138 return true;
139 }
140 if (o == null || getClass() != o.getClass()) {
141 return false;
142 }
143 DefaultKubevirtNetwork that = (DefaultKubevirtNetwork) o;
144 return networkId.equals(that.networkId) && type == that.type &&
145 name.equals(that.name) && mtu.equals(that.mtu) &&
146 gatewayIp.equals(that.gatewayIp) &&
Jian Lifeb84802021-01-12 16:34:49 +0900147 cidr.equals(that.cidr) && hostRoutes.equals(that.hostRoutes) &&
Jian Li97e6fc32021-02-01 20:36:45 +0900148 ipPool.equals(that.ipPool) &&
149 dnses.equals(that.dnses);
Jian Li3e81b182021-01-11 02:35:06 +0900150 }
151
152 @Override
153 public int hashCode() {
154 return Objects.hash(networkId, type, name, mtu, segmentId, gatewayIp,
Jian Li97e6fc32021-02-01 20:36:45 +0900155 cidr, hostRoutes, ipPool, dnses);
Jian Li3e81b182021-01-11 02:35:06 +0900156 }
157
158 @Override
159 public String toString() {
160 return MoreObjects.toStringHelper(this)
161 .add("networkId", networkId)
162 .add("type", type)
163 .add("name", name)
164 .add("mtu", mtu)
165 .add("segmentId", segmentId)
166 .add("gatewayIp", gatewayIp)
167 .add("cidr", cidr)
Jian Lifeb84802021-01-12 16:34:49 +0900168 .add("hostRouts", hostRoutes)
Jian Li3e81b182021-01-11 02:35:06 +0900169 .add("ipPool", ipPool)
Jian Li97e6fc32021-02-01 20:36:45 +0900170 .add("dnses", dnses)
Jian Li3e81b182021-01-11 02:35:06 +0900171 .toString();
172 }
173
174 /**
175 * Returns new builder instance.
176 *
177 * @return kubevirt port builder
178 */
179 public static Builder builder() {
180 return new Builder();
181 }
182
183 public static final class Builder implements KubevirtNetwork.Builder {
184
185 private String networkId;
186 private Type type;
187 private String name;
188 private Integer mtu;
189 private String segmentId;
190 private IpAddress gatewayIp;
191 private String cidr;
192 private Set<KubevirtHostRoute> hostRouts;
193 private KubevirtIpPool ipPool;
Jian Li97e6fc32021-02-01 20:36:45 +0900194 private Set<IpAddress> dnses;
Jian Li3e81b182021-01-11 02:35:06 +0900195
196 @Override
197 public KubevirtNetwork build() {
198 checkArgument(networkId != null, NOT_NULL_MSG, "networkId");
199 checkArgument(type != null, NOT_NULL_MSG, "type");
200 checkArgument(name != null, NOT_NULL_MSG, "name");
201 checkArgument(mtu != null, NOT_NULL_MSG, "mtu");
202 checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp");
203 checkArgument(cidr != null, NOT_NULL_MSG, "cidr");
204 checkArgument(ipPool != null, NOT_NULL_MSG, "ipPool");
205
206 if (type != FLAT) {
207 checkArgument(segmentId != null, NOT_NULL_MSG, "segmentId");
208 }
209
Jian Li97e6fc32021-02-01 20:36:45 +0900210 if (dnses == null) {
211 dnses = new HashSet<>();
212 }
213
Jian Li3e81b182021-01-11 02:35:06 +0900214 return new DefaultKubevirtNetwork(networkId, type, name, mtu, segmentId,
Jian Li97e6fc32021-02-01 20:36:45 +0900215 gatewayIp, cidr, hostRouts, ipPool, dnses);
Jian Li3e81b182021-01-11 02:35:06 +0900216 }
217
218 @Override
219 public Builder networkId(String networkId) {
220 this.networkId = networkId;
221 return this;
222 }
223
224 @Override
225 public Builder name(String name) {
226 this.name = name;
227 return this;
228 }
229
230 @Override
231 public Builder type(Type type) {
232 this.type = type;
233 return this;
234 }
235
236 @Override
237 public Builder mtu(Integer mtu) {
238 this.mtu = mtu;
239 return this;
240 }
241
242 @Override
243 public Builder segmentId(String segmentId) {
244 this.segmentId = segmentId;
245 return this;
246 }
247
248 @Override
249 public Builder gatewayIp(IpAddress ipAddress) {
250 this.gatewayIp = ipAddress;
251 return this;
252 }
253
254 @Override
255 public Builder cidr(String cidr) {
256 this.cidr = cidr;
257 return this;
258 }
259
260 @Override
261 public Builder ipPool(KubevirtIpPool ipPool) {
262 this.ipPool = ipPool;
263 return this;
264 }
265
266 @Override
267 public Builder hostRoutes(Set<KubevirtHostRoute> hostRoutes) {
268 this.hostRouts = hostRoutes;
269 return this;
270 }
Jian Li97e6fc32021-02-01 20:36:45 +0900271
272 @Override
273 public KubevirtNetwork.Builder dnses(Set<IpAddress> dnses) {
274 this.dnses = dnses;
275 return this;
276 }
Jian Li3e81b182021-01-11 02:35:06 +0900277 }
278}