blob: 206220fbf63cbad9b10b2ee404d235f4560ba0ef [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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.api;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080017
18import com.google.common.base.MoreObjects;
Hyunsun Moon3ef52492016-06-15 14:56:31 -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;
23
24import java.util.Comparator;
25import java.util.Objects;
Hyunsun Moon3ef52492016-06-15 14:56:31 -070026import java.util.Optional;
27import java.util.Set;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080028
Hyunsun Moon3ef52492016-06-15 14:56:31 -070029import static com.google.common.base.Preconditions.checkArgument;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080030import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon3ef52492016-06-15 14:56:31 -070031import static org.opencord.cordvtn.api.Constants.DEFAULT_TUNNEL;
32import static org.opencord.cordvtn.api.Constants.OVSDB_PORT;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080033
34/**
35 * Representation of a compute infrastructure node for CORD VTN service.
36 */
37public final class CordVtnNode {
38
39 private final String hostname;
Hyunsun Moon126171d2016-02-09 01:55:48 -080040 private final NetworkAddress hostMgmtIp;
41 private final NetworkAddress localMgmtIp;
Hyunsun Moon3ef52492016-06-15 14:56:31 -070042 private final NetworkAddress dataIp;
43 private final Optional<TpPort> ovsdbPort;
Hyunsun Moon126171d2016-02-09 01:55:48 -080044 private final SshAccessInfo sshInfo;
Hyunsun Moon3ef52492016-06-15 14:56:31 -070045 private final DeviceId integrationBridgeId;
46 private final String dataIface;
47 private final Optional<String> hostMgmtIface;
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080048 private final CordVtnNodeState state;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080049
50 public static final Comparator<CordVtnNode> CORDVTN_NODE_COMPARATOR =
51 (node1, node2) -> node1.hostname().compareTo(node2.hostname());
52
53 /**
54 * Creates a new node.
55 *
56 * @param hostname hostname
Hyunsun Moon126171d2016-02-09 01:55:48 -080057 * @param hostMgmtIp host management network address
58 * @param localMgmtIp local management network address
Hyunsun Moon3ef52492016-06-15 14:56:31 -070059 * @param dataIp data network address
60 * @param ovsdbPort port number for ovsdb connection
61 * @param sshInfo ssh access information
62 * @param integrationBridgeId integration bridge identifier
63 * @param dataIface data plane interface name
64 * @param hostMgmtIface host management network interface
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080065 * @param state cordvtn node state
Hyunsun Moon4edb0172015-11-07 22:08:43 -080066 */
Hyunsun Moon3ef52492016-06-15 14:56:31 -070067 private CordVtnNode(String hostname,
68 NetworkAddress hostMgmtIp,
69 NetworkAddress localMgmtIp,
70 NetworkAddress dataIp,
71 Optional<TpPort> ovsdbPort,
72 SshAccessInfo sshInfo,
73 DeviceId integrationBridgeId,
74 String dataIface,
75 Optional<String> hostMgmtIface,
76 CordVtnNodeState state) {
77 this.hostname = hostname;
78 this.hostMgmtIp = hostMgmtIp;
79 this.localMgmtIp = localMgmtIp;
80 this.dataIp = dataIp;
81 this.ovsdbPort = ovsdbPort;
82 this.sshInfo = sshInfo;
83 this.integrationBridgeId = integrationBridgeId;
84 this.dataIface = dataIface;
85 this.hostMgmtIface = hostMgmtIface;
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080086 this.state = state;
87 }
88
89 /**
90 * Returns cordvtn node with new state.
91 *
92 * @param node cordvtn node
93 * @param state cordvtn node init state
94 * @return cordvtn node
95 */
96 public static CordVtnNode getUpdatedNode(CordVtnNode node, CordVtnNodeState state) {
97 return new CordVtnNode(node.hostname,
Hyunsun Moon3ef52492016-06-15 14:56:31 -070098 node.hostMgmtIp, node.localMgmtIp, node.dataIp,
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080099 node.ovsdbPort,
100 node.sshInfo,
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700101 node.integrationBridgeId,
102 node.dataIface, node.hostMgmtIface,
103 state);
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800104 }
105
106 /**
107 * Returns the hostname.
108 *
109 * @return hostname
110 */
111 public String hostname() {
112 return this.hostname;
113 }
114
115 /**
Hyunsun Moon126171d2016-02-09 01:55:48 -0800116 * Returns the host management network address.
117 *
118 * @return network address
119 */
120 public NetworkAddress hostMgmtIp() {
121 return this.hostMgmtIp;
122 }
123
124 /**
125 * Returns the local management network address.
126 *
127 * @return network address
128 */
129 public NetworkAddress localMgmtIp() {
130 return this.localMgmtIp;
131 }
132
133 /**
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700134 * Returns the data network address.
Hyunsun Moon126171d2016-02-09 01:55:48 -0800135 *
136 * @return network address
137 */
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700138 public NetworkAddress dataIp() {
139 return this.dataIp;
Hyunsun Moon126171d2016-02-09 01:55:48 -0800140 }
141
142 /**
143 * Returns the port number used for OVSDB connection.
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700144 * It returns default OVSDB port 6640, if it's not specified.
Hyunsun Moon126171d2016-02-09 01:55:48 -0800145 *
146 * @return port number
147 */
148 public TpPort ovsdbPort() {
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700149 if (this.ovsdbPort.isPresent()) {
150 return this.ovsdbPort.get();
151 } else {
152 return OVSDB_PORT;
153 }
Hyunsun Moon126171d2016-02-09 01:55:48 -0800154 }
155
156 /**
157 * Returns the SSH access information.
158 *
159 * @return ssh access information
160 */
161 public SshAccessInfo sshInfo() {
162 return this.sshInfo;
163 }
164
165 /**
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800166 * Returns the identifier of the integration bridge.
167 *
168 * @return device id
169 */
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700170 public DeviceId integrationBridgeId() {
171 return this.integrationBridgeId;
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800172 }
173
174 /**
175 * Returns the identifier of the OVSDB device.
176 *
177 * @return device id
178 */
179 public DeviceId ovsdbId() {
Hyunsun Moon126171d2016-02-09 01:55:48 -0800180 return DeviceId.deviceId("ovsdb:" + this.hostMgmtIp.ip().toString());
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800181 }
182
Hyunsun Moonde372572016-01-14 03:42:47 -0800183 /**
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700184 * Returns data network interface name.
Hyunsun Moonde372572016-01-14 03:42:47 -0800185 *
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700186 * @return data network interface name
Hyunsun Moonde372572016-01-14 03:42:47 -0800187 */
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700188 public String dataIface() {
189 return this.dataIface;
190 }
191
192 /**
193 * Returns host management network interface name.
194 *
195 * @return host management network interface name
196 */
197 public Optional<String> hostMgmtIface() {
198 return this.hostMgmtIface;
199 }
200
201 /**
202 * Returns a set of network interfaces for the VTN service to work properly.
203 *
204 * @return set of interface names
205 */
206 public Set<String> systemIfaces() {
207 Set<String> ifaces = Sets.newHashSet(DEFAULT_TUNNEL, dataIface);
208 if (hostMgmtIface.isPresent()) {
209 ifaces.add(hostMgmtIface.get());
210 }
211 return ifaces;
Hyunsun Moonde372572016-01-14 03:42:47 -0800212 }
213
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800214 /**
215 * Returns the state of the node.
216 *
217 * @return state
218 */
219 public CordVtnNodeState state() {
220 return this.state;
221 }
222
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800223 @Override
224 public boolean equals(Object obj) {
225 if (this == obj) {
226 return true;
227 }
228
229 if (obj instanceof CordVtnNode) {
230 CordVtnNode that = (CordVtnNode) obj;
Hyunsun Moonff55e812016-03-10 12:40:16 -0800231 if (Objects.equals(hostname, that.hostname) &&
232 Objects.equals(hostMgmtIp, that.hostMgmtIp) &&
233 Objects.equals(localMgmtIp, that.localMgmtIp) &&
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700234 Objects.equals(dataIp, that.dataIp) &&
Hyunsun Moonff55e812016-03-10 12:40:16 -0800235 Objects.equals(ovsdbPort, that.ovsdbPort) &&
236 Objects.equals(sshInfo, that.sshInfo) &&
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700237 Objects.equals(integrationBridgeId, that.integrationBridgeId) &&
238 Objects.equals(dataIface, that.dataIface) &&
239 Objects.equals(hostMgmtIface, that.hostMgmtIface)) {
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800240 return true;
241 }
242 }
243 return false;
244 }
245
246 @Override
247 public int hashCode() {
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700248 return Objects.hash(hostname,
249 hostMgmtIp,
250 localMgmtIp,
251 dataIp,
252 ovsdbPort,
253 sshInfo,
254 integrationBridgeId,
255 dataIface,
256 hostMgmtIface);
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800257 }
258
259 @Override
260 public String toString() {
261 return MoreObjects.toStringHelper(getClass())
Hyunsun Moon126171d2016-02-09 01:55:48 -0800262 .add("hostname", hostname)
263 .add("hostMgmtIp", hostMgmtIp)
264 .add("localMgmtIp", localMgmtIp)
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700265 .add("dataIp", dataIp)
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800266 .add("port", ovsdbPort)
Hyunsun Moon126171d2016-02-09 01:55:48 -0800267 .add("sshInfo", sshInfo)
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700268 .add("integrationBridgeId", integrationBridgeId)
269 .add("dataIface", dataIface)
270 .add("hostMgmtIface", hostMgmtIface)
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800271 .add("state", state)
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800272 .toString();
273 }
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700274
275 /**
276 * Returns new node builder instance.
277 *
278 * @return cordvtn node builder
279 */
280 public static Builder builder() {
281 return new Builder();
282 }
283
284 /**
285 * Builder of node entities.
286 */
287 public static final class Builder {
288 private String hostname;
289 private NetworkAddress hostMgmtIp;
290 private NetworkAddress localMgmtIp;
291 private NetworkAddress dataIp;
292 private Optional<TpPort> ovsdbPort = Optional.of(OVSDB_PORT);
293 private SshAccessInfo sshInfo;
294 private DeviceId integrationBridgeId;
295 private String dataIface;
296 private Optional<String> hostMgmtIface = Optional.empty();
297 private CordVtnNodeState state = CordVtnNodeState.noState();
298
299 private Builder() {
300 }
301
302 /**
303 * Builds an immutable cordvtn node.
304 *
305 * @return cordvtn node
306 */
307 public CordVtnNode build() {
308 // validate attributes
309 checkArgument(!Strings.isNullOrEmpty(hostname));
310 checkNotNull(hostMgmtIp);
311 checkNotNull(localMgmtIp);
312 checkNotNull(dataIp);
313 checkNotNull(ovsdbPort);
314 checkNotNull(sshInfo);
315 checkNotNull(integrationBridgeId);
316 checkNotNull(dataIface);
317 checkNotNull(hostMgmtIface);
318 return new CordVtnNode(hostname,
319 hostMgmtIp, localMgmtIp, dataIp,
320 ovsdbPort,
321 sshInfo,
322 integrationBridgeId,
323 dataIface,
324 hostMgmtIface,
325 state);
326 }
327
328 /**
329 * Returns cordvtn node builder with hostname.
330 *
331 * @param hostname hostname
332 * @return cordvtn node builder
333 */
334 public Builder hostname(String hostname) {
335 checkArgument(!Strings.isNullOrEmpty(hostname));
336 this.hostname = hostname;
337 return this;
338 }
339
340 /**
341 * Returns cordvtn node builder with host management network IP address.
342 *
343 * @param hostMgmtIp host management netework ip address
344 * @return cordvtn node builder
345 */
346 public Builder hostMgmtIp(NetworkAddress hostMgmtIp) {
347 checkNotNull(hostMgmtIp);
348 this.hostMgmtIp = hostMgmtIp;
349 return this;
350 }
351
352 /**
353 * Returns cordvtn node builder with host management network IP address.
354 *
355 * @param cidr string value of the host management network ip address
356 * @return cordvtn node builder
357 */
358 public Builder hostMgmtIp(String cidr) {
359 this.hostMgmtIp = NetworkAddress.valueOf(cidr);
360 return this;
361 }
362
363 /**
364 * Returns cordvtn node builder with local management network IP address.
365 *
366 * @param localMgmtIp local management network ip address
367 * @return cordvtn node builder
368 */
369 public Builder localMgmtIp(NetworkAddress localMgmtIp) {
370 checkNotNull(localMgmtIp);
371 this.localMgmtIp = localMgmtIp;
372 return this;
373 }
374
375 /**
376 * Returns cordvtn node builder with local management netework IP address.
377 *
378 * @param cidr string value of the local management network ip address
379 * @return cordvtn node builder
380 */
381 public Builder localMgmtIp(String cidr) {
382 this.localMgmtIp = NetworkAddress.valueOf(cidr);
383 return this;
384 }
385
386 /**
387 * Returns cordvtn node builder with data network IP address.
388 *
389 * @param dataIp data network ip address
390 * @return cordvtn node builder
391 */
392 public Builder dataIp(NetworkAddress dataIp) {
393 checkNotNull(dataIp);
394 this.dataIp = dataIp;
395 return this;
396 }
397
398 /**
399 * Returns cordvtn node builder with data network IP address.
400 *
401 * @param cidr string value of the data network ip address
402 * @return cordvtn node builder
403 */
404 public Builder dataIp(String cidr) {
405 this.dataIp = NetworkAddress.valueOf(cidr);
406 return this;
407 }
408
409 /**
410 * Returns cordvtn node builder with OVSDB server listen port number.
411 *
412 * @param port ovsdb server listen port number
413 * @return cordvtn node builder
414 */
415 public Builder ovsdbPort(TpPort port) {
416 checkNotNull(port);
417 this.ovsdbPort = Optional.of(port);
418 return this;
419 }
420
421 /**
422 * Returns cordvtn node builder with OVSDB server listen port number.
423 *
424 * @param port int value of the ovsdb server listen port number
425 * @return cordvtn node builder
426 */
427 public Builder ovsdbPort(int port) {
428 this.ovsdbPort = Optional.of(TpPort.tpPort(port));
429 return this;
430 }
431
432 /**
433 * Returns cordvtn node builder with SSH access information.
434 * @param sshInfo ssh access information
435 * @return cordvtn node builder
436 */
437 public Builder sshInfo(SshAccessInfo sshInfo) {
438 checkNotNull(sshInfo);
439 this.sshInfo = sshInfo;
440 return this;
441 }
442
443 /**
444 * Returns cordvtn node builder with integration bridge ID.
445 *
446 * @param deviceId device id of the integration bridge
447 * @return cordvtn node builder
448 */
449 public Builder integrationBridgeId(DeviceId deviceId) {
450 checkNotNull(deviceId);
451 this.integrationBridgeId = deviceId;
452 return this;
453 }
454
455 /**
456 * Returns cordvtn node builder with integration bridge ID.
457 *
458 * @param deviceId string value of the integration bridge device id
459 * @return cordvtn node builder
460 */
461 public Builder integrationBridgeId(String deviceId) {
462 this.integrationBridgeId = DeviceId.deviceId(deviceId);
463 return this;
464 }
465
466 /**
467 * Returns cordvtn node builder with data network interface name.
468 *
469 * @param dataIface data network interface name
470 * @return cordvtn node builder
471 */
472 public Builder dataIface(String dataIface) {
473 checkArgument(!Strings.isNullOrEmpty(dataIface));
474 this.dataIface = dataIface;
475 return this;
476 }
477
478 /**
479 * Returns cordvtn node builder with host management network interface.
480 *
481 * @param hostMgmtIface host management network interface name
482 * @return cordvtn node builder
483 */
484 public Builder hostMgmtIface(String hostMgmtIface) {
485 this.hostMgmtIface = Optional.ofNullable(hostMgmtIface);
486 return this;
487 }
488
489 /**
490 * Returns cordvtn node builder with init state.
491 *
492 * @param state init state
493 * @return cordvtn node builder
494 */
495 public Builder state(CordVtnNodeState state) {
496 checkNotNull(state);
497 this.state = state;
498 return this;
499 }
500 }
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800501}