blob: 20d12985571331a716b89f9878c8c805f1ec28dd [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Jonathan Hartc9e36c52017-01-05 09:53:33 +13002 * Copyright 2017-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hartf4bd0482017-01-27 15:11:18 -080016
Jonathan Hartc9e36c52017-01-05 09:53:33 +130017package org.onosproject.routing.bgp;
Jonathan Hart335ef462014-10-16 08:20:46 -070018
Jonathan Hart41349e92015-02-09 14:14:02 -080019import com.google.common.base.MoreObjects;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080020import org.onlab.packet.IpAddress;
21import org.onlab.packet.IpPrefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070022
Jonathan Hart41349e92015-02-09 14:14:02 -080023import java.util.Objects;
24
25import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hart335ef462014-10-16 08:20:46 -070026
27/**
28 * Represents a route entry for an IP prefix.
29 */
30public class RouteEntry {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080031 private final IpPrefix prefix; // The IP prefix
32 private final IpAddress nextHop; // Next-hop IP address
Jonathan Hart335ef462014-10-16 08:20:46 -070033
34 /**
35 * Class constructor.
36 *
37 * @param prefix the IP prefix of the route
38 * @param nextHop the next hop IP address for the route
39 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080040 public RouteEntry(IpPrefix prefix, IpAddress nextHop) {
Jonathan Hart335ef462014-10-16 08:20:46 -070041 this.prefix = checkNotNull(prefix);
42 this.nextHop = checkNotNull(nextHop);
43 }
44
45 /**
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080046 * Returns the IP version of the route.
47 *
48 * @return the IP version of the route
49 */
50 public IpAddress.Version version() {
51 return nextHop.version();
52 }
53
54 /**
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -070055 * Tests whether the IP version of this address is IPv4.
56 *
57 * @return true if the IP version of this address is IPv4, otherwise false.
58 */
59 public boolean isIp4() {
60 return nextHop.isIp4();
61 }
62
63 /**
64 * Tests whether the IP version of this address is IPv6.
65 *
66 * @return true if the IP version of this address is IPv6, otherwise false.
67 */
68 public boolean isIp6() {
69 return nextHop.isIp6();
70 }
71
72 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070073 * Returns the IP prefix of the route.
74 *
75 * @return the IP prefix of the route
76 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080077 public IpPrefix prefix() {
Jonathan Hart335ef462014-10-16 08:20:46 -070078 return prefix;
79 }
80
81 /**
82 * Returns the next hop IP address for the route.
83 *
84 * @return the next hop IP address for the route
85 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080086 public IpAddress nextHop() {
Jonathan Hart335ef462014-10-16 08:20:46 -070087 return nextHop;
88 }
89
Jonathan Hart335ef462014-10-16 08:20:46 -070090 @Override
91 public boolean equals(Object other) {
92 if (this == other) {
93 return true;
94 }
95
96 //
97 // NOTE: Subclasses are considered as change of identity, hence
98 // equals() will return false if the class type doesn't match.
99 //
100 if (other == null || getClass() != other.getClass()) {
101 return false;
102 }
103
104 RouteEntry otherRoute = (RouteEntry) other;
105 return Objects.equals(this.prefix, otherRoute.prefix) &&
106 Objects.equals(this.nextHop, otherRoute.nextHop);
107 }
108
109 @Override
110 public int hashCode() {
111 return Objects.hash(prefix, nextHop);
112 }
113
114 @Override
115 public String toString() {
116 return MoreObjects.toStringHelper(getClass())
117 .add("prefix", prefix)
118 .add("nextHop", nextHop)
119 .toString();
120 }
121}