Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 17 | |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 18 | import java.net.InetAddress; |
| 19 | import java.net.UnknownHostException; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Default implementation of a controller instance descriptor. |
| 29 | */ |
| 30 | public class DefaultControllerNode implements ControllerNode { |
| 31 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 32 | public static final int DEFAULT_PORT = 9876; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 33 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 34 | private final NodeId id; |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 35 | private final String host; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 36 | private final int tcpPort; |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 37 | private transient volatile IpAddress ip; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 38 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 39 | // For serialization |
| 40 | private DefaultControllerNode() { |
| 41 | this.id = null; |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 42 | this.host = null; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 43 | this.tcpPort = 0; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 44 | } |
| 45 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Creates a new instance with the specified id and IP address. |
| 48 | * |
| 49 | * @param id instance identifier |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 50 | * @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 |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 73 | * @param ip instance IP address |
| 74 | */ |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 75 | public DefaultControllerNode(NodeId id, IpAddress ip) { |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 76 | this(id, ip != null ? ip.toString() : null, DEFAULT_PORT); |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 80 | * Creates a new instance with the specified id and IP address. |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 81 | * |
| 82 | * @param id instance identifier |
| 83 | * @param ip instance IP address |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 84 | * @param tcpPort TCP port |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 85 | */ |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 86 | public DefaultControllerNode(NodeId id, IpAddress ip, int tcpPort) { |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 87 | this(id, ip != null ? ip.toString() : null, tcpPort); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public NodeId id() { |
| 92 | return id; |
| 93 | } |
| 94 | |
| 95 | @Override |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 96 | 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 | } |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 114 | return ip; |
| 115 | } |
| 116 | |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 117 | private IpAddress resolveIp() { |
| 118 | try { |
| 119 | return IpAddress.valueOf(InetAddress.getByName(host)); |
| 120 | } catch (UnknownHostException e) { |
| 121 | return null; |
| 122 | } |
| 123 | } |
| 124 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 125 | @Override |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 126 | public int tcpPort() { |
| 127 | return tcpPort; |
| 128 | } |
| 129 | |
| 130 | @Override |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 131 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 132 | return id.hashCode(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 133 | } |
| 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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 149 | return toStringHelper(this) |
| 150 | .add("id", id) |
| 151 | .add("host", host) |
| 152 | .add("tcpPort", tcpPort) |
| 153 | .toString(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } |