blob: cd653ce934a118e02c8831bee21f4e21b520c3f2 [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
128 public Collection<KubevirtPhyInterface> phyIntfs() {
129 if (phyIntfs == null) {
130 return new ArrayList<>();
131 }
132
133 return phyIntfs;
134 }
135
136 /**
137 * Returns new builder instance.
138 *
139 * @return kubevirt node builder
140 */
141 public static Builder builder() {
142 return new Builder();
143 }
144
145 /**
146 * Returns new builder instance with the given node as a default value.
147 *
148 * @param node kubevirt node
149 * @return kubevirt node builder
150 */
151 public static Builder from(KubevirtNode node) {
152 return new Builder()
153 .hostname(node.hostname())
154 .clusterName(node.clusterName())
155 .type(node.type())
156 .intgBridge(node.intgBridge())
157 .managementIp(node.managementIp())
158 .dataIp(node.dataIp())
159 .state(node.state());
160 }
161
162 @Override
163 public boolean equals(Object o) {
164 if (this == o) {
165 return true;
166 }
167 if (o == null || getClass() != o.getClass()) {
168 return false;
169 }
170 DefaultKubevirtNode that = (DefaultKubevirtNode) o;
171 return clusterName.equals(that.clusterName) &&
172 hostname.equals(that.hostname) &&
173 type == that.type &&
174 intgBridge.equals(that.intgBridge) &&
175 managementIp.equals(that.managementIp) &&
176 dataIp.equals(that.dataIp);
177 }
178
179 @Override
180 public int hashCode() {
181 return Objects.hash(clusterName, hostname, type, intgBridge,
182 managementIp, dataIp);
183 }
184
185 @Override
186 public String toString() {
187 return MoreObjects.toStringHelper(this)
188 .add("clusterName", clusterName)
189 .add("hostname", hostname)
190 .add("type", type)
191 .add("intgBridge", intgBridge)
192 .add("managementIp", managementIp)
193 .add("dataIp", dataIp)
194 .add("state", state)
195 .toString();
196 }
197
198 public static final class Builder implements KubevirtNode.Builder {
199
200 private String clusterName;
201 private String hostname;
202 private Type type;
203 private DeviceId intgBridge;
204 private IpAddress managementIp;
205 private IpAddress dataIp;
206 private KubevirtNodeState state;
207 private Collection<KubevirtPhyInterface> phyIntfs;
208
209 // private constructor not intended to use from external
210 private Builder() {
211 }
212
213 @Override
214 public KubevirtNode build() {
215 checkArgument(hostname != null, NOT_NULL_MSG, "hostname");
216 checkArgument(type != null, NOT_NULL_MSG, "type");
217 checkArgument(state != null, NOT_NULL_MSG, "state");
218 checkArgument(managementIp != null, NOT_NULL_MSG, "management IP");
219
220 if (StringUtils.isEmpty(clusterName)) {
221 clusterName = DEFAULT_CLUSTER_NAME;
222 }
223
224 return new DefaultKubevirtNode(
225 clusterName,
226 hostname,
227 type,
228 intgBridge,
229 managementIp,
230 dataIp,
231 state,
232 phyIntfs
233 );
234 }
235
236 @Override
237 public Builder clusterName(String clusterName) {
238 this.clusterName = clusterName;
239 return this;
240 }
241
242 @Override
243 public Builder hostname(String hostname) {
244 this.hostname = hostname;
245 return this;
246 }
247
248 @Override
249 public Builder type(Type type) {
250 this.type = type;
251 return this;
252 }
253
254 @Override
255 public Builder intgBridge(DeviceId deviceId) {
256 this.intgBridge = deviceId;
257 return this;
258 }
259
260 @Override
261 public Builder managementIp(IpAddress managementIp) {
262 this.managementIp = managementIp;
263 return this;
264 }
265
266 @Override
267 public Builder dataIp(IpAddress dataIp) {
268 this.dataIp = dataIp;
269 return this;
270 }
271
272 @Override
273 public Builder phyIntfs(Collection<KubevirtPhyInterface> phyIntfs) {
274 this.phyIntfs = phyIntfs;
275 return this;
276 }
277
278 @Override
279 public Builder state(KubevirtNodeState state) {
280 this.state = state;
281 return this;
282 }
283 }
284}