Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 16 | |
Jonathan Hart | c9e36c5 | 2017-01-05 09:53:33 +1300 | [diff] [blame] | 17 | package org.onosproject.routing.bgp; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 18 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 22 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Represents a route entry for an IP prefix. |
| 29 | */ |
| 30 | public class RouteEntry { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 31 | private final IpPrefix prefix; // The IP prefix |
| 32 | private final IpAddress nextHop; // Next-hop IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 33 | |
| 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 Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 40 | public RouteEntry(IpPrefix prefix, IpAddress nextHop) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 41 | this.prefix = checkNotNull(prefix); |
| 42 | this.nextHop = checkNotNull(nextHop); |
| 43 | } |
| 44 | |
| 45 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 46 | * 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 Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 55 | * 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 73 | * Returns the IP prefix of the route. |
| 74 | * |
| 75 | * @return the IP prefix of the route |
| 76 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 77 | public IpPrefix prefix() { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 78 | 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 Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 86 | public IpAddress nextHop() { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 87 | return nextHop; |
| 88 | } |
| 89 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 90 | @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 | } |