blob: d9a11fc661491db5276dd6048c1638f0fc22fa19 [file] [log] [blame]
Jonathan Hartb10f1e72017-05-02 16:36:26 -07001/*
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
17package org.onosproject.routing.fpm;
18
19import org.onlab.packet.IpAddress;
20
21import java.net.InetSocketAddress;
22import java.util.Objects;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25
26/**
27 * Represents an FPM peer.
28 */
29public 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}