Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.routing.fpm; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | |
| 21 | import java.net.InetSocketAddress; |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | |
| 26 | /** |
| 27 | * Represents an FPM peer. |
| 28 | */ |
| 29 | public class FpmPeer { |
| 30 | |
| 31 | private final IpAddress address; |
| 32 | private final int port; |
| 33 | |
| 34 | /** |
| 35 | * Creates a new FPM peer. |
| 36 | * |
| 37 | * @param address peer IP address |
| 38 | * @param port peer TCP port number |
| 39 | */ |
| 40 | public FpmPeer(IpAddress address, int port) { |
| 41 | this.address = address; |
| 42 | this.port = port; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the peers IP address. |
| 47 | * |
| 48 | * @return IP address |
| 49 | */ |
| 50 | public IpAddress address() { |
| 51 | return address; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the peer port number. |
| 56 | * |
| 57 | * @return port number |
| 58 | */ |
| 59 | public int port() { |
| 60 | return port; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int hashCode() { |
| 65 | return Objects.hash(address, port); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean equals(Object other) { |
| 70 | if (this == other) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | if (!(other instanceof FpmPeer)) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | FpmPeer that = (FpmPeer) other; |
| 79 | |
| 80 | return Objects.equals(this.address, that.address) && |
| 81 | Objects.equals(this.port, that.port); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String toString() { |
| 86 | return toStringHelper(this) |
| 87 | .add("address", address) |
| 88 | .add("port", port) |
| 89 | .toString(); |
| 90 | } |
| 91 | |
| 92 | public static FpmPeer fromSocketAddress(InetSocketAddress address) { |
| 93 | return new FpmPeer(IpAddress.valueOf(address.getAddress()), address.getPort()); |
| 94 | } |
| 95 | } |