Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
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 | |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 18 | import org.onlab.packet.IpAddress; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import static com.google.common.base.MoreObjects.toStringHelper; |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Default implementation of a controller instance descriptor. |
| 27 | */ |
| 28 | public class DefaultControllerNode implements ControllerNode { |
| 29 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 30 | public static final int DEFAULT_PORT = 9876; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 31 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 32 | private final NodeId id; |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 33 | private final IpAddress ip; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 34 | private final int tcpPort; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 35 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 36 | // For serialization |
| 37 | private DefaultControllerNode() { |
| 38 | this.id = null; |
| 39 | this.ip = null; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 40 | this.tcpPort = 0; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 41 | } |
| 42 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Creates a new instance with the specified id and IP address. |
| 45 | * |
| 46 | * @param id instance identifier |
| 47 | * @param ip instance IP address |
| 48 | */ |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 49 | public DefaultControllerNode(NodeId id, IpAddress ip) { |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 50 | this(id, ip, DEFAULT_PORT); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a new instance with the specified id and IP address and TCP port. |
| 55 | * |
| 56 | * @param id instance identifier |
| 57 | * @param ip instance IP address |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 58 | * @param tcpPort TCP port |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 59 | */ |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 60 | public DefaultControllerNode(NodeId id, IpAddress ip, int tcpPort) { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 61 | this.id = checkNotNull(id); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 62 | this.ip = ip; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 63 | this.tcpPort = tcpPort; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public NodeId id() { |
| 68 | return id; |
| 69 | } |
| 70 | |
| 71 | @Override |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 72 | public IpAddress ip() { |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 73 | return ip; |
| 74 | } |
| 75 | |
| 76 | @Override |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 77 | public int tcpPort() { |
| 78 | return tcpPort; |
| 79 | } |
| 80 | |
| 81 | @Override |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 82 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 83 | return id.hashCode(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean equals(Object o) { |
| 88 | if (this == o) { |
| 89 | return true; |
| 90 | } |
| 91 | if (o instanceof DefaultControllerNode) { |
| 92 | DefaultControllerNode that = (DefaultControllerNode) o; |
| 93 | return Objects.equals(this.id, that.id); |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 100 | return toStringHelper(this).add("id", id) |
| 101 | .add("ip", ip).add("tcpPort", tcpPort).toString(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } |