blob: cdeacba0054d75f1052881de0ae485bfd5e98a44 [file] [log] [blame]
Madan Jampani0cb00672015-02-27 00:27:22 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampani0cb00672015-02-27 00:27:22 -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 */
16package org.onosproject.store.cluster.impl;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19
20import java.util.Objects;
21
22import org.onosproject.cluster.ControllerNode;
23
24/**
25 * Node info read from configuration files during bootstrap.
26 */
27public final class NodeInfo {
28 private final String id;
29 private final String ip;
30 private final int tcpPort;
31
32 private NodeInfo(String id, String ip, int port) {
33 this.id = id;
34 this.ip = ip;
35 this.tcpPort = port;
36 }
37
Madan Jampani137b5fc2015-02-27 13:10:53 -080038 /*
39 * Needed for serialization.
40 */
41 private NodeInfo() {
42 id = null;
43 ip = null;
44 tcpPort = 0;
45 }
46
Madan Jampani0cb00672015-02-27 00:27:22 -080047 /**
48 * Creates a new instance.
49 * @param id node id
50 * @param ip node ip address
51 * @param port tcp port
52 * @return NodeInfo
53 */
54 public static NodeInfo from(String id, String ip, int port) {
55 NodeInfo node = new NodeInfo(id, ip, port);
56 return node;
57 }
58
59 /**
60 * Returns the NodeInfo for a controller node.
61 * @param node controller node
62 * @return NodeInfo
63 */
64 public static NodeInfo of(ControllerNode node) {
65 return NodeInfo.from(node.id().toString(), node.ip().toString(), node.tcpPort());
66 }
67
68 /**
69 * Returns node id.
70 * @return node id
71 */
72 public String getId() {
73 return id;
74 }
75
76 /**
77 * Returns node ip.
78 * @return node ip
79 */
80 public String getIp() {
81 return ip;
82 }
83
84 /**
85 * Returns node port.
86 * @return port
87 */
88 public int getTcpPort() {
89 return tcpPort;
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hash(id, ip, tcpPort);
95 }
96
97 @Override
98 public boolean equals(Object o) {
99 if (this == o) {
100 return true;
101 }
102 if (o instanceof NodeInfo) {
103 NodeInfo that = (NodeInfo) o;
104 return Objects.equals(this.id, that.id) &&
105 Objects.equals(this.ip, that.ip) &&
106 Objects.equals(this.tcpPort, that.tcpPort);
107 }
108 return false;
109 }
110
111 @Override
112 public String toString() {
113 return toStringHelper(this)
114 .add("id", id)
115 .add("ip", ip)
116 .add("tcpPort", tcpPort).toString();
117 }
118}