blob: 3815ff3c48dda64b961b01f99c27876deee52686 [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;
42 private final IpAddress managementIp;
43 private final IpAddress dataIp;
44 private final KubevirtNodeState state;
45 private final Collection<KubevirtPhyInterface> phyIntfs;
46
47 /**
48 * A default constructor of kubevirt node.
49 *
50 * @param clusterName clusterName
51 * @param hostname hostname
52 * @param type node type
53 * @param intgBridge integration bridge
54 * @param managementIp management IP address
55 * @param dataIp data IP address
56 * @param state node state
57 * @param phyIntfs physical interfaces
58 */
59 protected DefaultKubevirtNode(String clusterName, String hostname, Type type,
60 DeviceId intgBridge, IpAddress managementIp,
61 IpAddress dataIp, KubevirtNodeState state,
62 Collection<KubevirtPhyInterface> phyIntfs) {
63 this.clusterName = clusterName;
64 this.hostname = hostname;
65 this.type = type;
66 this.intgBridge = intgBridge;
67 this.managementIp = managementIp;
68 this.dataIp = dataIp;
69 this.state = state;
70 this.phyIntfs = phyIntfs;
71 }
72
73 @Override
74 public String clusterName() {
75 return clusterName;
76 }
77
78 @Override
79 public String hostname() {
80 return hostname;
81 }
82
83 @Override
84 public Type type() {
85 return type;
86 }
87
88 @Override
89 public DeviceId ovsdb() {
90 return DeviceId.deviceId(OVSDB + managementIp().toString());
91 }
92
93 @Override
94 public DeviceId intgBridge() {
95 return intgBridge;
96 }
97
98 @Override
99 public IpAddress managementIp() {
100 return managementIp;
101 }
102
103 @Override
104 public IpAddress dataIp() {
105 return dataIp;
106 }
107
108 @Override
109 public KubevirtNodeState state() {
110 return state;
111 }
112
113 @Override
114 public KubevirtNode updateState(KubevirtNodeState newState) {
115 return new Builder()
116 .hostname(hostname)
117 .clusterName(clusterName)
118 .type(type)
119 .intgBridge(intgBridge)
120 .managementIp(managementIp)
121 .dataIp(dataIp)
122 .state(newState)
123 .phyIntfs(phyIntfs)
124 .build();
125 }
126
127 @Override
Jian Lib230e07c2020-12-21 11:25:12 +0900128 public KubevirtNode updateIntgBridge(DeviceId deviceId) {
129 return new Builder()
130 .hostname(hostname)
131 .clusterName(clusterName)
132 .type(type)
133 .intgBridge(deviceId)
134 .managementIp(managementIp)
135 .dataIp(dataIp)
136 .state(state)
137 .phyIntfs(phyIntfs)
138 .build();
139 }
140
141 @Override
Jian Li4eb0cf42020-12-19 04:01:49 +0900142 public Collection<KubevirtPhyInterface> phyIntfs() {
143 if (phyIntfs == null) {
144 return new ArrayList<>();
145 }
146
147 return phyIntfs;
148 }
149
150 /**
151 * Returns new builder instance.
152 *
153 * @return kubevirt node builder
154 */
155 public static Builder builder() {
156 return new Builder();
157 }
158
159 /**
160 * Returns new builder instance with the given node as a default value.
161 *
162 * @param node kubevirt node
163 * @return kubevirt node builder
164 */
165 public static Builder from(KubevirtNode node) {
166 return new Builder()
167 .hostname(node.hostname())
168 .clusterName(node.clusterName())
169 .type(node.type())
170 .intgBridge(node.intgBridge())
171 .managementIp(node.managementIp())
172 .dataIp(node.dataIp())
173 .state(node.state());
174 }
175
176 @Override
177 public boolean equals(Object o) {
178 if (this == o) {
179 return true;
180 }
181 if (o == null || getClass() != o.getClass()) {
182 return false;
183 }
184 DefaultKubevirtNode that = (DefaultKubevirtNode) o;
185 return clusterName.equals(that.clusterName) &&
186 hostname.equals(that.hostname) &&
187 type == that.type &&
188 intgBridge.equals(that.intgBridge) &&
189 managementIp.equals(that.managementIp) &&
190 dataIp.equals(that.dataIp);
191 }
192
193 @Override
194 public int hashCode() {
195 return Objects.hash(clusterName, hostname, type, intgBridge,
196 managementIp, dataIp);
197 }
198
199 @Override
200 public String toString() {
201 return MoreObjects.toStringHelper(this)
202 .add("clusterName", clusterName)
203 .add("hostname", hostname)
204 .add("type", type)
205 .add("intgBridge", intgBridge)
206 .add("managementIp", managementIp)
207 .add("dataIp", dataIp)
208 .add("state", state)
209 .toString();
210 }
211
212 public static final class Builder implements KubevirtNode.Builder {
213
214 private String clusterName;
215 private String hostname;
216 private Type type;
217 private DeviceId intgBridge;
218 private IpAddress managementIp;
219 private IpAddress dataIp;
220 private KubevirtNodeState state;
221 private Collection<KubevirtPhyInterface> phyIntfs;
222
223 // private constructor not intended to use from external
224 private Builder() {
225 }
226
227 @Override
228 public KubevirtNode build() {
229 checkArgument(hostname != null, NOT_NULL_MSG, "hostname");
230 checkArgument(type != null, NOT_NULL_MSG, "type");
231 checkArgument(state != null, NOT_NULL_MSG, "state");
232 checkArgument(managementIp != null, NOT_NULL_MSG, "management IP");
233
234 if (StringUtils.isEmpty(clusterName)) {
235 clusterName = DEFAULT_CLUSTER_NAME;
236 }
237
238 return new DefaultKubevirtNode(
239 clusterName,
240 hostname,
241 type,
242 intgBridge,
243 managementIp,
244 dataIp,
245 state,
246 phyIntfs
247 );
248 }
249
250 @Override
251 public Builder clusterName(String clusterName) {
252 this.clusterName = clusterName;
253 return this;
254 }
255
256 @Override
257 public Builder hostname(String hostname) {
258 this.hostname = hostname;
259 return this;
260 }
261
262 @Override
263 public Builder type(Type type) {
264 this.type = type;
265 return this;
266 }
267
268 @Override
269 public Builder intgBridge(DeviceId deviceId) {
270 this.intgBridge = deviceId;
271 return this;
272 }
273
274 @Override
275 public Builder managementIp(IpAddress managementIp) {
276 this.managementIp = managementIp;
277 return this;
278 }
279
280 @Override
281 public Builder dataIp(IpAddress dataIp) {
282 this.dataIp = dataIp;
283 return this;
284 }
285
286 @Override
287 public Builder phyIntfs(Collection<KubevirtPhyInterface> phyIntfs) {
288 this.phyIntfs = phyIntfs;
289 return this;
290 }
291
292 @Override
293 public Builder state(KubevirtNodeState state) {
294 this.state = state;
295 return this;
296 }
297 }
298}