blob: 246d6c4716cb3c5f01f7810bc84678e79cad24d5 [file] [log] [blame]
Jian Li4eb0cf42020-12-19 04:01:49 +09001/*
2 * Copyright 2020-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.kubevirtnode.api;
17
18import com.google.common.base.MoreObjects;
19import org.apache.commons.lang.StringUtils;
20import org.onlab.packet.IpAddress;
21import org.onosproject.net.DeviceId;
22
23import java.util.ArrayList;
24import java.util.Collection;
25import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkArgument;
28import static org.onosproject.kubevirtnode.api.Constants.DEFAULT_CLUSTER_NAME;
29
30/**
31 * Representation of a KubeVirt node.
32 */
33public class DefaultKubevirtNode implements KubevirtNode {
34
35 private static final String NOT_NULL_MSG = "Node % cannot be null";
36 private static final String OVSDB = "ovsdb:";
37
38 private final String clusterName;
39 private final String hostname;
40 private final Type type;
41 private final DeviceId intgBridge;
Jian Li4fe40e52021-01-06 03:29:58 +090042 private final DeviceId tunBridge;
Jian Li4eb0cf42020-12-19 04:01:49 +090043 private final IpAddress managementIp;
44 private final IpAddress dataIp;
45 private final KubevirtNodeState state;
46 private final Collection<KubevirtPhyInterface> phyIntfs;
47
48 /**
49 * A default constructor of kubevirt node.
50 *
51 * @param clusterName clusterName
52 * @param hostname hostname
53 * @param type node type
54 * @param intgBridge integration bridge
Jian Li4fe40e52021-01-06 03:29:58 +090055 * @param tunBridge tunnel bridge
Jian Li4eb0cf42020-12-19 04:01:49 +090056 * @param managementIp management IP address
57 * @param dataIp data IP address
58 * @param state node state
59 * @param phyIntfs physical interfaces
60 */
61 protected DefaultKubevirtNode(String clusterName, String hostname, Type type,
Jian Li4fe40e52021-01-06 03:29:58 +090062 DeviceId intgBridge, DeviceId tunBridge,
63 IpAddress managementIp, IpAddress dataIp,
64 KubevirtNodeState state,
Jian Li4eb0cf42020-12-19 04:01:49 +090065 Collection<KubevirtPhyInterface> phyIntfs) {
66 this.clusterName = clusterName;
67 this.hostname = hostname;
68 this.type = type;
69 this.intgBridge = intgBridge;
Jian Li4fe40e52021-01-06 03:29:58 +090070 this.tunBridge = tunBridge;
Jian Li4eb0cf42020-12-19 04:01:49 +090071 this.managementIp = managementIp;
72 this.dataIp = dataIp;
73 this.state = state;
74 this.phyIntfs = phyIntfs;
75 }
76
77 @Override
78 public String clusterName() {
79 return clusterName;
80 }
81
82 @Override
83 public String hostname() {
84 return hostname;
85 }
86
87 @Override
88 public Type type() {
89 return type;
90 }
91
92 @Override
93 public DeviceId ovsdb() {
94 return DeviceId.deviceId(OVSDB + managementIp().toString());
95 }
96
97 @Override
98 public DeviceId intgBridge() {
99 return intgBridge;
100 }
101
102 @Override
Jian Li4fe40e52021-01-06 03:29:58 +0900103 public DeviceId tunBridge() {
104 return tunBridge;
105 }
106
107 @Override
Jian Li4eb0cf42020-12-19 04:01:49 +0900108 public IpAddress managementIp() {
109 return managementIp;
110 }
111
112 @Override
113 public IpAddress dataIp() {
114 return dataIp;
115 }
116
117 @Override
118 public KubevirtNodeState state() {
119 return state;
120 }
121
122 @Override
123 public KubevirtNode updateState(KubevirtNodeState newState) {
124 return new Builder()
125 .hostname(hostname)
126 .clusterName(clusterName)
127 .type(type)
128 .intgBridge(intgBridge)
Jian Li4fe40e52021-01-06 03:29:58 +0900129 .tunBridge(tunBridge)
Jian Li4eb0cf42020-12-19 04:01:49 +0900130 .managementIp(managementIp)
131 .dataIp(dataIp)
132 .state(newState)
133 .phyIntfs(phyIntfs)
134 .build();
135 }
136
137 @Override
Jian Lib230e07c2020-12-21 11:25:12 +0900138 public KubevirtNode updateIntgBridge(DeviceId deviceId) {
139 return new Builder()
140 .hostname(hostname)
141 .clusterName(clusterName)
142 .type(type)
143 .intgBridge(deviceId)
Jian Li4fe40e52021-01-06 03:29:58 +0900144 .tunBridge(tunBridge)
145 .managementIp(managementIp)
146 .dataIp(dataIp)
147 .state(state)
148 .phyIntfs(phyIntfs)
149 .build();
150 }
151
152 @Override
153 public KubevirtNode updateTunBridge(DeviceId deviceId) {
154 return new Builder()
155 .hostname(hostname)
156 .clusterName(clusterName)
157 .type(type)
158 .intgBridge(intgBridge)
159 .tunBridge(deviceId)
Jian Lib230e07c2020-12-21 11:25:12 +0900160 .managementIp(managementIp)
161 .dataIp(dataIp)
162 .state(state)
163 .phyIntfs(phyIntfs)
164 .build();
165 }
166
167 @Override
Jian Li4eb0cf42020-12-19 04:01:49 +0900168 public Collection<KubevirtPhyInterface> phyIntfs() {
169 if (phyIntfs == null) {
170 return new ArrayList<>();
171 }
172
173 return phyIntfs;
174 }
175
176 /**
177 * Returns new builder instance.
178 *
179 * @return kubevirt node builder
180 */
181 public static Builder builder() {
182 return new Builder();
183 }
184
185 /**
186 * Returns new builder instance with the given node as a default value.
187 *
188 * @param node kubevirt node
189 * @return kubevirt node builder
190 */
191 public static Builder from(KubevirtNode node) {
192 return new Builder()
193 .hostname(node.hostname())
194 .clusterName(node.clusterName())
195 .type(node.type())
196 .intgBridge(node.intgBridge())
Jian Li4fe40e52021-01-06 03:29:58 +0900197 .tunBridge(node.tunBridge())
Jian Li4eb0cf42020-12-19 04:01:49 +0900198 .managementIp(node.managementIp())
199 .dataIp(node.dataIp())
Jian Li4fe40e52021-01-06 03:29:58 +0900200 .state(node.state())
201 .phyIntfs(node.phyIntfs());
Jian Li4eb0cf42020-12-19 04:01:49 +0900202 }
203
204 @Override
205 public boolean equals(Object o) {
206 if (this == o) {
207 return true;
208 }
209 if (o == null || getClass() != o.getClass()) {
210 return false;
211 }
212 DefaultKubevirtNode that = (DefaultKubevirtNode) o;
213 return clusterName.equals(that.clusterName) &&
214 hostname.equals(that.hostname) &&
215 type == that.type &&
216 intgBridge.equals(that.intgBridge) &&
Jian Li4fe40e52021-01-06 03:29:58 +0900217 tunBridge.equals(that.tunBridge) &&
Jian Li4eb0cf42020-12-19 04:01:49 +0900218 managementIp.equals(that.managementIp) &&
219 dataIp.equals(that.dataIp);
220 }
221
222 @Override
223 public int hashCode() {
Jian Li4fe40e52021-01-06 03:29:58 +0900224 return Objects.hash(clusterName, hostname, type, intgBridge, tunBridge,
Jian Li4eb0cf42020-12-19 04:01:49 +0900225 managementIp, dataIp);
226 }
227
228 @Override
229 public String toString() {
230 return MoreObjects.toStringHelper(this)
231 .add("clusterName", clusterName)
232 .add("hostname", hostname)
233 .add("type", type)
234 .add("intgBridge", intgBridge)
Jian Li4fe40e52021-01-06 03:29:58 +0900235 .add("tunBridge", tunBridge)
Jian Li4eb0cf42020-12-19 04:01:49 +0900236 .add("managementIp", managementIp)
237 .add("dataIp", dataIp)
238 .add("state", state)
Jian Li4fe40e52021-01-06 03:29:58 +0900239 .add("phyIntfs", phyIntfs)
Jian Li4eb0cf42020-12-19 04:01:49 +0900240 .toString();
241 }
242
243 public static final class Builder implements KubevirtNode.Builder {
244
245 private String clusterName;
246 private String hostname;
247 private Type type;
248 private DeviceId intgBridge;
Jian Li4fe40e52021-01-06 03:29:58 +0900249 private DeviceId tunBridge;
Jian Li4eb0cf42020-12-19 04:01:49 +0900250 private IpAddress managementIp;
251 private IpAddress dataIp;
252 private KubevirtNodeState state;
253 private Collection<KubevirtPhyInterface> phyIntfs;
254
255 // private constructor not intended to use from external
256 private Builder() {
257 }
258
259 @Override
260 public KubevirtNode build() {
261 checkArgument(hostname != null, NOT_NULL_MSG, "hostname");
262 checkArgument(type != null, NOT_NULL_MSG, "type");
263 checkArgument(state != null, NOT_NULL_MSG, "state");
264 checkArgument(managementIp != null, NOT_NULL_MSG, "management IP");
265
266 if (StringUtils.isEmpty(clusterName)) {
267 clusterName = DEFAULT_CLUSTER_NAME;
268 }
269
270 return new DefaultKubevirtNode(
271 clusterName,
272 hostname,
273 type,
274 intgBridge,
Jian Li4fe40e52021-01-06 03:29:58 +0900275 tunBridge,
Jian Li4eb0cf42020-12-19 04:01:49 +0900276 managementIp,
277 dataIp,
278 state,
279 phyIntfs
280 );
281 }
282
283 @Override
284 public Builder clusterName(String clusterName) {
285 this.clusterName = clusterName;
286 return this;
287 }
288
289 @Override
290 public Builder hostname(String hostname) {
291 this.hostname = hostname;
292 return this;
293 }
294
295 @Override
296 public Builder type(Type type) {
297 this.type = type;
298 return this;
299 }
300
301 @Override
302 public Builder intgBridge(DeviceId deviceId) {
303 this.intgBridge = deviceId;
304 return this;
305 }
306
307 @Override
Jian Li4fe40e52021-01-06 03:29:58 +0900308 public Builder tunBridge(DeviceId deviceId) {
309 this.tunBridge = deviceId;
310 return this;
311 }
312
313 @Override
Jian Li4eb0cf42020-12-19 04:01:49 +0900314 public Builder managementIp(IpAddress managementIp) {
315 this.managementIp = managementIp;
316 return this;
317 }
318
319 @Override
320 public Builder dataIp(IpAddress dataIp) {
321 this.dataIp = dataIp;
322 return this;
323 }
324
325 @Override
326 public Builder phyIntfs(Collection<KubevirtPhyInterface> phyIntfs) {
327 this.phyIntfs = phyIntfs;
328 return this;
329 }
330
331 @Override
332 public Builder state(KubevirtNodeState state) {
333 this.state = state;
334 return this;
335 }
336 }
337}