blob: 8bc4757d9262f75e9b142d64ba7a4ff7237471c6 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cluster;
tome4729872014-09-23 00:37:37 -070017
Jordan Haltermane458f002018-09-18 17:42:05 -070018import java.net.InetAddress;
19import java.net.UnknownHostException;
tome4729872014-09-23 00:37:37 -070020import java.util.Objects;
21
Jordan Haltermane458f002018-09-18 17:42:05 -070022import org.onlab.packet.IpAddress;
23
tome4729872014-09-23 00:37:37 -070024import static com.google.common.base.MoreObjects.toStringHelper;
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070025import static com.google.common.base.Preconditions.checkNotNull;
tome4729872014-09-23 00:37:37 -070026
27/**
28 * Default implementation of a controller instance descriptor.
29 */
30public class DefaultControllerNode implements ControllerNode {
31
Thomas Vachuskade563cf2015-04-01 00:28:50 -070032 public static final int DEFAULT_PORT = 9876;
tomee49c372014-09-26 15:14:50 -070033
tome4729872014-09-23 00:37:37 -070034 private final NodeId id;
Jordan Haltermane458f002018-09-18 17:42:05 -070035 private final String host;
tomee49c372014-09-26 15:14:50 -070036 private final int tcpPort;
Jordan Haltermane458f002018-09-18 17:42:05 -070037 private transient volatile IpAddress ip;
tome4729872014-09-23 00:37:37 -070038
tom2d7c65f2014-09-23 01:09:35 -070039 // For serialization
40 private DefaultControllerNode() {
41 this.id = null;
Jordan Haltermane458f002018-09-18 17:42:05 -070042 this.host = null;
tomee49c372014-09-26 15:14:50 -070043 this.tcpPort = 0;
tom2d7c65f2014-09-23 01:09:35 -070044 }
45
tome4729872014-09-23 00:37:37 -070046 /**
47 * Creates a new instance with the specified id and IP address.
48 *
49 * @param id instance identifier
Jordan Haltermane458f002018-09-18 17:42:05 -070050 * @param host instance hostname
51 */
52 public DefaultControllerNode(NodeId id, String host) {
53 this(id, host, DEFAULT_PORT);
54 }
55
56 /**
57 * Creates a new instance with the specified id and IP address and TCP port.
58 *
59 * @param id instance identifier
60 * @param host instance host name
61 * @param tcpPort TCP port
62 */
63 public DefaultControllerNode(NodeId id, String host, int tcpPort) {
64 this.id = checkNotNull(id);
65 this.host = host;
66 this.tcpPort = tcpPort;
67 }
68
69 /**
70 * Creates a new instance with the specified id and IP address.
71 *
72 * @param id instance identifier
tome4729872014-09-23 00:37:37 -070073 * @param ip instance IP address
74 */
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070075 public DefaultControllerNode(NodeId id, IpAddress ip) {
Jordan Haltermane458f002018-09-18 17:42:05 -070076 this(id, ip != null ? ip.toString() : null, DEFAULT_PORT);
tomee49c372014-09-26 15:14:50 -070077 }
78
79 /**
Jordan Haltermane458f002018-09-18 17:42:05 -070080 * Creates a new instance with the specified id and IP address.
tomee49c372014-09-26 15:14:50 -070081 *
82 * @param id instance identifier
83 * @param ip instance IP address
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080084 * @param tcpPort TCP port
tomee49c372014-09-26 15:14:50 -070085 */
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070086 public DefaultControllerNode(NodeId id, IpAddress ip, int tcpPort) {
Jordan Haltermane458f002018-09-18 17:42:05 -070087 this(id, ip != null ? ip.toString() : null, tcpPort);
tome4729872014-09-23 00:37:37 -070088 }
89
90 @Override
91 public NodeId id() {
92 return id;
93 }
94
95 @Override
Jordan Haltermane458f002018-09-18 17:42:05 -070096 public String host() {
97 return host;
98 }
99
100 @Override
101 public IpAddress ip(boolean resolve) {
102 if (resolve) {
103 ip = resolveIp();
104 return ip;
105 }
106
107 if (ip == null) {
108 synchronized (this) {
109 if (ip == null) {
110 ip = resolveIp();
111 }
112 }
113 }
tome4729872014-09-23 00:37:37 -0700114 return ip;
115 }
116
Jordan Haltermane458f002018-09-18 17:42:05 -0700117 private IpAddress resolveIp() {
118 try {
119 return IpAddress.valueOf(InetAddress.getByName(host));
120 } catch (UnknownHostException e) {
121 return null;
122 }
123 }
124
tome4729872014-09-23 00:37:37 -0700125 @Override
tomee49c372014-09-26 15:14:50 -0700126 public int tcpPort() {
127 return tcpPort;
128 }
129
130 @Override
tome4729872014-09-23 00:37:37 -0700131 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700132 return id.hashCode();
tome4729872014-09-23 00:37:37 -0700133 }
134
135 @Override
136 public boolean equals(Object o) {
137 if (this == o) {
138 return true;
139 }
140 if (o instanceof DefaultControllerNode) {
141 DefaultControllerNode that = (DefaultControllerNode) o;
142 return Objects.equals(this.id, that.id);
143 }
144 return false;
145 }
146
147 @Override
148 public String toString() {
Jordan Haltermane458f002018-09-18 17:42:05 -0700149 return toStringHelper(this)
150 .add("id", id)
151 .add("host", host)
152 .add("tcpPort", tcpPort)
153 .toString();
tome4729872014-09-23 00:37:37 -0700154 }
155
156}