blob: 153eed3fc072635e7c213c5ca654afc8124a5965 [file] [log] [blame]
Hyunsun Moon4edb0172015-11-07 22:08:43 -08001/*
Brian O'Connor8e57fd52016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Hyunsun Moon4edb0172015-11-07 22:08:43 -08003 *
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 */
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070016package org.opencord.cordvtn.api.node;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080017
18import com.google.common.base.MoreObjects;
Hyunsun Moon81a13562016-08-04 13:48:08 -070019import com.google.common.base.Strings;
20import com.google.common.collect.Sets;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080021import org.onlab.packet.TpPort;
22import org.onosproject.net.DeviceId;
Hyunsun Moon187bf532017-01-19 10:57:40 +090023import org.opencord.cordvtn.api.net.CidrAddr;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080024
25import java.util.Comparator;
26import java.util.Objects;
Hyunsun Moon81a13562016-08-04 13:48:08 -070027import java.util.Optional;
28import java.util.Set;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080029
Hyunsun Moon81a13562016-08-04 13:48:08 -070030import static com.google.common.base.Preconditions.checkArgument;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080031import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon3fc0cbc2016-11-22 18:29:12 -080032import static org.opencord.cordvtn.api.Constants.DEFAULT_OVSDB_PORT;
Hyunsun Moon81a13562016-08-04 13:48:08 -070033import static org.opencord.cordvtn.api.Constants.DEFAULT_TUNNEL;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080034
35/**
36 * Representation of a compute infrastructure node for CORD VTN service.
37 */
38public final class CordVtnNode {
39
40 private final String hostname;
Hyunsun Moon187bf532017-01-19 10:57:40 +090041 private final CidrAddr hostMgmtIp;
42 private final CidrAddr localMgmtIp;
43 private final CidrAddr dataIp;
Hyunsun Moon81a13562016-08-04 13:48:08 -070044 private final Optional<TpPort> ovsdbPort;
Hyunsun Moon126171d2016-02-09 01:55:48 -080045 private final SshAccessInfo sshInfo;
Hyunsun Moon81a13562016-08-04 13:48:08 -070046 private final DeviceId integrationBridgeId;
47 private final String dataIface;
48 private final Optional<String> hostMgmtIface;
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080049 private final CordVtnNodeState state;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080050
51 public static final Comparator<CordVtnNode> CORDVTN_NODE_COMPARATOR =
52 (node1, node2) -> node1.hostname().compareTo(node2.hostname());
53
54 /**
55 * Creates a new node.
56 *
57 * @param hostname hostname
Hyunsun Moon126171d2016-02-09 01:55:48 -080058 * @param hostMgmtIp host management network address
59 * @param localMgmtIp local management network address
Hyunsun Moon81a13562016-08-04 13:48:08 -070060 * @param dataIp data network address
61 * @param ovsdbPort port number for ovsdb connection
62 * @param sshInfo ssh access information
63 * @param integrationBridgeId integration bridge identifier
64 * @param dataIface data plane interface name
65 * @param hostMgmtIface host management network interface
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080066 * @param state cordvtn node state
Hyunsun Moon4edb0172015-11-07 22:08:43 -080067 */
Hyunsun Moon81a13562016-08-04 13:48:08 -070068 private CordVtnNode(String hostname,
Hyunsun Moon187bf532017-01-19 10:57:40 +090069 CidrAddr hostMgmtIp,
70 CidrAddr localMgmtIp,
71 CidrAddr dataIp,
Hyunsun Moon81a13562016-08-04 13:48:08 -070072 Optional<TpPort> ovsdbPort,
73 SshAccessInfo sshInfo,
74 DeviceId integrationBridgeId,
75 String dataIface,
76 Optional<String> hostMgmtIface,
77 CordVtnNodeState state) {
78 this.hostname = hostname;
79 this.hostMgmtIp = hostMgmtIp;
80 this.localMgmtIp = localMgmtIp;
81 this.dataIp = dataIp;
82 this.ovsdbPort = ovsdbPort;
83 this.sshInfo = sshInfo;
84 this.integrationBridgeId = integrationBridgeId;
85 this.dataIface = dataIface;
86 this.hostMgmtIface = hostMgmtIface;
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080087 this.state = state;
88 }
89
90 /**
91 * Returns cordvtn node with new state.
92 *
93 * @param node cordvtn node
94 * @param state cordvtn node init state
95 * @return cordvtn node
96 */
97 public static CordVtnNode getUpdatedNode(CordVtnNode node, CordVtnNodeState state) {
98 return new CordVtnNode(node.hostname,
Hyunsun Moon81a13562016-08-04 13:48:08 -070099 node.hostMgmtIp, node.localMgmtIp, node.dataIp,
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800100 node.ovsdbPort,
101 node.sshInfo,
Hyunsun Moon81a13562016-08-04 13:48:08 -0700102 node.integrationBridgeId,
103 node.dataIface, node.hostMgmtIface,
104 state);
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800105 }
106
107 /**
108 * Returns the hostname.
109 *
110 * @return hostname
111 */
112 public String hostname() {
113 return this.hostname;
114 }
115
116 /**
Hyunsun Moon126171d2016-02-09 01:55:48 -0800117 * Returns the host management network address.
118 *
119 * @return network address
120 */
Hyunsun Moon187bf532017-01-19 10:57:40 +0900121 public CidrAddr hostMgmtIp() {
Hyunsun Moon126171d2016-02-09 01:55:48 -0800122 return this.hostMgmtIp;
123 }
124
125 /**
126 * Returns the local management network address.
127 *
128 * @return network address
129 */
Hyunsun Moon187bf532017-01-19 10:57:40 +0900130 public CidrAddr localMgmtIp() {
Hyunsun Moon126171d2016-02-09 01:55:48 -0800131 return this.localMgmtIp;
132 }
133
134 /**
Hyunsun Moon81a13562016-08-04 13:48:08 -0700135 * Returns the data network address.
Hyunsun Moon126171d2016-02-09 01:55:48 -0800136 *
137 * @return network address
138 */
Hyunsun Moon187bf532017-01-19 10:57:40 +0900139 public CidrAddr dataIp() {
Hyunsun Moon81a13562016-08-04 13:48:08 -0700140 return this.dataIp;
Hyunsun Moon126171d2016-02-09 01:55:48 -0800141 }
142
143 /**
144 * Returns the port number used for OVSDB connection.
Hyunsun Moon81a13562016-08-04 13:48:08 -0700145 * It returns default OVSDB port 6640, if it's not specified.
Hyunsun Moon126171d2016-02-09 01:55:48 -0800146 *
147 * @return port number
148 */
149 public TpPort ovsdbPort() {
Hyunsun Moon81a13562016-08-04 13:48:08 -0700150 if (this.ovsdbPort.isPresent()) {
151 return this.ovsdbPort.get();
152 } else {
Hyunsun Moon3fc0cbc2016-11-22 18:29:12 -0800153 return TpPort.tpPort(DEFAULT_OVSDB_PORT);
Hyunsun Moon81a13562016-08-04 13:48:08 -0700154 }
Hyunsun Moon126171d2016-02-09 01:55:48 -0800155 }
156
157 /**
158 * Returns the SSH access information.
159 *
160 * @return ssh access information
161 */
162 public SshAccessInfo sshInfo() {
163 return this.sshInfo;
164 }
165
166 /**
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800167 * Returns the identifier of the integration bridge.
168 *
169 * @return device id
170 */
Hyunsun Moon81a13562016-08-04 13:48:08 -0700171 public DeviceId integrationBridgeId() {
172 return this.integrationBridgeId;
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800173 }
174
175 /**
176 * Returns the identifier of the OVSDB device.
177 *
178 * @return device id
179 */
180 public DeviceId ovsdbId() {
Hyunsun Moon126171d2016-02-09 01:55:48 -0800181 return DeviceId.deviceId("ovsdb:" + this.hostMgmtIp.ip().toString());
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800182 }
183
Hyunsun Moonde372572016-01-14 03:42:47 -0800184 /**
Hyunsun Moon81a13562016-08-04 13:48:08 -0700185 * Returns data network interface name.
Hyunsun Moonde372572016-01-14 03:42:47 -0800186 *
Hyunsun Moon81a13562016-08-04 13:48:08 -0700187 * @return data network interface name
Hyunsun Moonde372572016-01-14 03:42:47 -0800188 */
Hyunsun Moon81a13562016-08-04 13:48:08 -0700189 public String dataIface() {
190 return this.dataIface;
191 }
192
193 /**
194 * Returns host management network interface name.
195 *
196 * @return host management network interface name
197 */
198 public Optional<String> hostMgmtIface() {
199 return this.hostMgmtIface;
200 }
201
202 /**
203 * Returns a set of network interfaces for the VTN service to work properly.
204 *
205 * @return set of interface names
206 */
207 public Set<String> systemIfaces() {
208 Set<String> ifaces = Sets.newHashSet(DEFAULT_TUNNEL, dataIface);
209 if (hostMgmtIface.isPresent()) {
210 ifaces.add(hostMgmtIface.get());
211 }
212 return ifaces;
Hyunsun Moonde372572016-01-14 03:42:47 -0800213 }
214
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800215 /**
216 * Returns the state of the node.
217 *
218 * @return state
219 */
220 public CordVtnNodeState state() {
221 return this.state;
222 }
223
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800224 @Override
225 public boolean equals(Object obj) {
226 if (this == obj) {
227 return true;
228 }
229
230 if (obj instanceof CordVtnNode) {
231 CordVtnNode that = (CordVtnNode) obj;
Hyunsun Moonff55e812016-03-10 12:40:16 -0800232 if (Objects.equals(hostname, that.hostname) &&
233 Objects.equals(hostMgmtIp, that.hostMgmtIp) &&
234 Objects.equals(localMgmtIp, that.localMgmtIp) &&
Hyunsun Moon81a13562016-08-04 13:48:08 -0700235 Objects.equals(dataIp, that.dataIp) &&
Hyunsun Moonff55e812016-03-10 12:40:16 -0800236 Objects.equals(ovsdbPort, that.ovsdbPort) &&
237 Objects.equals(sshInfo, that.sshInfo) &&
Hyunsun Moon81a13562016-08-04 13:48:08 -0700238 Objects.equals(integrationBridgeId, that.integrationBridgeId) &&
239 Objects.equals(dataIface, that.dataIface) &&
240 Objects.equals(hostMgmtIface, that.hostMgmtIface)) {
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800241 return true;
242 }
243 }
244 return false;
245 }
246
247 @Override
248 public int hashCode() {
Hyunsun Moon81a13562016-08-04 13:48:08 -0700249 return Objects.hash(hostname,
250 hostMgmtIp,
251 localMgmtIp,
252 dataIp,
253 ovsdbPort,
254 sshInfo,
255 integrationBridgeId,
256 dataIface,
257 hostMgmtIface);
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800258 }
259
260 @Override
261 public String toString() {
262 return MoreObjects.toStringHelper(getClass())
Hyunsun Moon126171d2016-02-09 01:55:48 -0800263 .add("hostname", hostname)
264 .add("hostMgmtIp", hostMgmtIp)
265 .add("localMgmtIp", localMgmtIp)
Hyunsun Moon81a13562016-08-04 13:48:08 -0700266 .add("dataIp", dataIp)
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800267 .add("port", ovsdbPort)
Hyunsun Moon126171d2016-02-09 01:55:48 -0800268 .add("sshInfo", sshInfo)
Hyunsun Moon81a13562016-08-04 13:48:08 -0700269 .add("integrationBridgeId", integrationBridgeId)
270 .add("dataIface", dataIface)
271 .add("hostMgmtIface", hostMgmtIface)
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800272 .add("state", state)
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800273 .toString();
274 }
Hyunsun Moon81a13562016-08-04 13:48:08 -0700275
276 /**
277 * Returns new node builder instance.
278 *
279 * @return cordvtn node builder
280 */
281 public static Builder builder() {
282 return new Builder();
283 }
284
285 /**
286 * Builder of node entities.
287 */
288 public static final class Builder {
289 private String hostname;
Hyunsun Moon187bf532017-01-19 10:57:40 +0900290 private CidrAddr hostMgmtIp;
291 private CidrAddr localMgmtIp;
292 private CidrAddr dataIp;
Hyunsun Moon3fc0cbc2016-11-22 18:29:12 -0800293 private Optional<TpPort> ovsdbPort =
294 Optional.of(TpPort.tpPort(DEFAULT_OVSDB_PORT));
Hyunsun Moon81a13562016-08-04 13:48:08 -0700295 private SshAccessInfo sshInfo;
296 private DeviceId integrationBridgeId;
297 private String dataIface;
298 private Optional<String> hostMgmtIface = Optional.empty();
299 private CordVtnNodeState state = CordVtnNodeState.noState();
300
301 private Builder() {
302 }
303
304 /**
305 * Builds an immutable cordvtn node.
306 *
307 * @return cordvtn node
308 */
309 public CordVtnNode build() {
310 // validate attributes
311 checkArgument(!Strings.isNullOrEmpty(hostname));
312 checkNotNull(hostMgmtIp);
313 checkNotNull(localMgmtIp);
314 checkNotNull(dataIp);
315 checkNotNull(ovsdbPort);
316 checkNotNull(sshInfo);
317 checkNotNull(integrationBridgeId);
318 checkNotNull(dataIface);
319 checkNotNull(hostMgmtIface);
320 return new CordVtnNode(hostname,
321 hostMgmtIp, localMgmtIp, dataIp,
322 ovsdbPort,
323 sshInfo,
324 integrationBridgeId,
325 dataIface,
326 hostMgmtIface,
327 state);
328 }
329
330 /**
331 * Returns cordvtn node builder with hostname.
332 *
333 * @param hostname hostname
334 * @return cordvtn node builder
335 */
336 public Builder hostname(String hostname) {
337 checkArgument(!Strings.isNullOrEmpty(hostname));
338 this.hostname = hostname;
339 return this;
340 }
341
342 /**
343 * Returns cordvtn node builder with host management network IP address.
344 *
345 * @param hostMgmtIp host management netework ip address
346 * @return cordvtn node builder
347 */
Hyunsun Moon187bf532017-01-19 10:57:40 +0900348 public Builder hostMgmtIp(CidrAddr hostMgmtIp) {
Hyunsun Moon81a13562016-08-04 13:48:08 -0700349 checkNotNull(hostMgmtIp);
350 this.hostMgmtIp = hostMgmtIp;
351 return this;
352 }
353
354 /**
355 * Returns cordvtn node builder with host management network IP address.
356 *
357 * @param cidr string value of the host management network ip address
358 * @return cordvtn node builder
359 */
360 public Builder hostMgmtIp(String cidr) {
Hyunsun Moon187bf532017-01-19 10:57:40 +0900361 this.hostMgmtIp = CidrAddr.valueOf(cidr);
Hyunsun Moon81a13562016-08-04 13:48:08 -0700362 return this;
363 }
364
365 /**
366 * Returns cordvtn node builder with local management network IP address.
367 *
368 * @param localMgmtIp local management network ip address
369 * @return cordvtn node builder
370 */
Hyunsun Moon187bf532017-01-19 10:57:40 +0900371 public Builder localMgmtIp(CidrAddr localMgmtIp) {
Hyunsun Moon81a13562016-08-04 13:48:08 -0700372 checkNotNull(localMgmtIp);
373 this.localMgmtIp = localMgmtIp;
374 return this;
375 }
376
377 /**
378 * Returns cordvtn node builder with local management netework IP address.
379 *
380 * @param cidr string value of the local management network ip address
381 * @return cordvtn node builder
382 */
383 public Builder localMgmtIp(String cidr) {
Hyunsun Moon187bf532017-01-19 10:57:40 +0900384 this.localMgmtIp = CidrAddr.valueOf(cidr);
Hyunsun Moon81a13562016-08-04 13:48:08 -0700385 return this;
386 }
387
388 /**
389 * Returns cordvtn node builder with data network IP address.
390 *
391 * @param dataIp data network ip address
392 * @return cordvtn node builder
393 */
Hyunsun Moon187bf532017-01-19 10:57:40 +0900394 public Builder dataIp(CidrAddr dataIp) {
Hyunsun Moon81a13562016-08-04 13:48:08 -0700395 checkNotNull(dataIp);
396 this.dataIp = dataIp;
397 return this;
398 }
399
400 /**
401 * Returns cordvtn node builder with data network IP address.
402 *
403 * @param cidr string value of the data network ip address
404 * @return cordvtn node builder
405 */
406 public Builder dataIp(String cidr) {
Hyunsun Moon187bf532017-01-19 10:57:40 +0900407 this.dataIp = CidrAddr.valueOf(cidr);
Hyunsun Moon81a13562016-08-04 13:48:08 -0700408 return this;
409 }
410
411 /**
412 * Returns cordvtn node builder with OVSDB server listen port number.
413 *
414 * @param port ovsdb server listen port number
415 * @return cordvtn node builder
416 */
417 public Builder ovsdbPort(TpPort port) {
418 checkNotNull(port);
419 this.ovsdbPort = Optional.of(port);
420 return this;
421 }
422
423 /**
424 * Returns cordvtn node builder with OVSDB server listen port number.
425 *
426 * @param port int value of the ovsdb server listen port number
427 * @return cordvtn node builder
428 */
429 public Builder ovsdbPort(int port) {
430 this.ovsdbPort = Optional.of(TpPort.tpPort(port));
431 return this;
432 }
433
434 /**
435 * Returns cordvtn node builder with SSH access information.
436 * @param sshInfo ssh access information
437 * @return cordvtn node builder
438 */
439 public Builder sshInfo(SshAccessInfo sshInfo) {
440 checkNotNull(sshInfo);
441 this.sshInfo = sshInfo;
442 return this;
443 }
444
445 /**
446 * Returns cordvtn node builder with integration bridge ID.
447 *
448 * @param deviceId device id of the integration bridge
449 * @return cordvtn node builder
450 */
451 public Builder integrationBridgeId(DeviceId deviceId) {
452 checkNotNull(deviceId);
453 this.integrationBridgeId = deviceId;
454 return this;
455 }
456
457 /**
458 * Returns cordvtn node builder with integration bridge ID.
459 *
460 * @param deviceId string value of the integration bridge device id
461 * @return cordvtn node builder
462 */
463 public Builder integrationBridgeId(String deviceId) {
464 this.integrationBridgeId = DeviceId.deviceId(deviceId);
465 return this;
466 }
467
468 /**
469 * Returns cordvtn node builder with data network interface name.
470 *
471 * @param dataIface data network interface name
472 * @return cordvtn node builder
473 */
474 public Builder dataIface(String dataIface) {
475 checkArgument(!Strings.isNullOrEmpty(dataIface));
476 this.dataIface = dataIface;
477 return this;
478 }
479
480 /**
481 * Returns cordvtn node builder with host management network interface.
482 *
483 * @param hostMgmtIface host management network interface name
484 * @return cordvtn node builder
485 */
486 public Builder hostMgmtIface(String hostMgmtIface) {
487 this.hostMgmtIface = Optional.ofNullable(hostMgmtIface);
488 return this;
489 }
490
491 /**
492 * Returns cordvtn node builder with init state.
493 *
494 * @param state init state
495 * @return cordvtn node builder
496 */
497 public Builder state(CordVtnNodeState state) {
498 checkNotNull(state);
499 this.state = state;
500 return this;
501 }
502 }
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800503}