blob: dc221ab0e29d9790728a02bad75d7385b728f95e [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
22import java.util.Objects;
23import java.util.Set;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.FLAT;
27
28/**
29 * Default implementation class of kubevirt network.
30 */
31public final class DefaultKubevirtNetwork implements KubevirtNetwork {
32
33 private static final String NOT_NULL_MSG = "Network % cannot be null";
34
35 private final String networkId;
36 private final Type type;
37 private final String name;
38 private final Integer mtu;
39 private final String segmentId;
40 private final IpAddress gatewayIp;
41 private final String cidr;
42 private final Set<KubevirtHostRoute> hostRouts;
43 private final KubevirtIpPool ipPool;
44
45 /**
46 * Default constructor.
47 *
48 * @param networkId network identifier
49 * @param type type of network
50 * @param name network name
51 * @param mtu network MTU
52 * @param segmentId segment identifier
53 * @param gatewayIp gateway IP address
54 * @param cidr CIDR of network
55 * @param hostRouts a set of host routes
56 * @param ipPool IP pool
57 */
58 public DefaultKubevirtNetwork(String networkId, Type type, String name,
59 Integer mtu, String segmentId, IpAddress gatewayIp,
60 String cidr, Set<KubevirtHostRoute> hostRouts,
61 KubevirtIpPool ipPool) {
62 this.networkId = networkId;
63 this.type = type;
64 this.name = name;
65 this.mtu = mtu;
66 this.segmentId = segmentId;
67 this.gatewayIp = gatewayIp;
68 this.cidr = cidr;
69 this.hostRouts = hostRouts;
70 this.ipPool = ipPool;
71 }
72
73 @Override
74 public String networkId() {
75 return networkId;
76 }
77
78 @Override
79 public Type type() {
80 return type;
81 }
82
83 @Override
84 public String name() {
85 return name;
86 }
87
88 @Override
89 public Integer mtu() {
90 return mtu;
91 }
92
93 @Override
94 public String segmentId() {
95 return segmentId;
96 }
97
98 @Override
99 public IpAddress gatewayIp() {
100 return gatewayIp;
101 }
102
103 @Override
104 public String cidr() {
105 return cidr;
106 }
107
108 @Override
109 public Set<KubevirtHostRoute> hostRouts() {
110 return ImmutableSet.copyOf(hostRouts);
111 }
112
113 @Override
114 public KubevirtIpPool ipPool() {
115 return ipPool;
116 }
117
118 @Override
119 public boolean equals(Object o) {
120 if (this == o) {
121 return true;
122 }
123 if (o == null || getClass() != o.getClass()) {
124 return false;
125 }
126 DefaultKubevirtNetwork that = (DefaultKubevirtNetwork) o;
127 return networkId.equals(that.networkId) && type == that.type &&
128 name.equals(that.name) && mtu.equals(that.mtu) &&
129 gatewayIp.equals(that.gatewayIp) &&
130 cidr.equals(that.cidr) && hostRouts.equals(that.hostRouts) &&
131 ipPool.equals(that.ipPool);
132 }
133
134 @Override
135 public int hashCode() {
136 return Objects.hash(networkId, type, name, mtu, segmentId, gatewayIp,
137 cidr, hostRouts, ipPool);
138 }
139
140 @Override
141 public String toString() {
142 return MoreObjects.toStringHelper(this)
143 .add("networkId", networkId)
144 .add("type", type)
145 .add("name", name)
146 .add("mtu", mtu)
147 .add("segmentId", segmentId)
148 .add("gatewayIp", gatewayIp)
149 .add("cidr", cidr)
150 .add("hostRouts", hostRouts)
151 .add("ipPool", ipPool)
152 .toString();
153 }
154
155 /**
156 * Returns new builder instance.
157 *
158 * @return kubevirt port builder
159 */
160 public static Builder builder() {
161 return new Builder();
162 }
163
164 public static final class Builder implements KubevirtNetwork.Builder {
165
166 private String networkId;
167 private Type type;
168 private String name;
169 private Integer mtu;
170 private String segmentId;
171 private IpAddress gatewayIp;
172 private String cidr;
173 private Set<KubevirtHostRoute> hostRouts;
174 private KubevirtIpPool ipPool;
175
176 @Override
177 public KubevirtNetwork build() {
178 checkArgument(networkId != null, NOT_NULL_MSG, "networkId");
179 checkArgument(type != null, NOT_NULL_MSG, "type");
180 checkArgument(name != null, NOT_NULL_MSG, "name");
181 checkArgument(mtu != null, NOT_NULL_MSG, "mtu");
182 checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp");
183 checkArgument(cidr != null, NOT_NULL_MSG, "cidr");
184 checkArgument(ipPool != null, NOT_NULL_MSG, "ipPool");
185
186 if (type != FLAT) {
187 checkArgument(segmentId != null, NOT_NULL_MSG, "segmentId");
188 }
189
190 return new DefaultKubevirtNetwork(networkId, type, name, mtu, segmentId,
191 gatewayIp, cidr, hostRouts, ipPool);
192 }
193
194 @Override
195 public Builder networkId(String networkId) {
196 this.networkId = networkId;
197 return this;
198 }
199
200 @Override
201 public Builder name(String name) {
202 this.name = name;
203 return this;
204 }
205
206 @Override
207 public Builder type(Type type) {
208 this.type = type;
209 return this;
210 }
211
212 @Override
213 public Builder mtu(Integer mtu) {
214 this.mtu = mtu;
215 return this;
216 }
217
218 @Override
219 public Builder segmentId(String segmentId) {
220 this.segmentId = segmentId;
221 return this;
222 }
223
224 @Override
225 public Builder gatewayIp(IpAddress ipAddress) {
226 this.gatewayIp = ipAddress;
227 return this;
228 }
229
230 @Override
231 public Builder cidr(String cidr) {
232 this.cidr = cidr;
233 return this;
234 }
235
236 @Override
237 public Builder ipPool(KubevirtIpPool ipPool) {
238 this.ipPool = ipPool;
239 return this;
240 }
241
242 @Override
243 public Builder hostRoutes(Set<KubevirtHostRoute> hostRoutes) {
244 this.hostRouts = hostRoutes;
245 return this;
246 }
247 }
248}