Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 16 | package org.onosproject.routing; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 21 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Represents a route entry for an IP prefix. |
Jonathan Hart | 888eaa0 | 2016-06-22 15:56:21 -0700 | [diff] [blame] | 28 | * |
| 29 | * @deprecated use RouteService instead |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 30 | */ |
Jonathan Hart | 888eaa0 | 2016-06-22 15:56:21 -0700 | [diff] [blame] | 31 | @Deprecated |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 32 | public class RouteEntry { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 33 | private final IpPrefix prefix; // The IP prefix |
| 34 | private final IpAddress nextHop; // Next-hop IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Class constructor. |
| 38 | * |
| 39 | * @param prefix the IP prefix of the route |
| 40 | * @param nextHop the next hop IP address for the route |
| 41 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 42 | public RouteEntry(IpPrefix prefix, IpAddress nextHop) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 43 | this.prefix = checkNotNull(prefix); |
| 44 | this.nextHop = checkNotNull(nextHop); |
| 45 | } |
| 46 | |
| 47 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 48 | * Returns the IP version of the route. |
| 49 | * |
| 50 | * @return the IP version of the route |
| 51 | */ |
| 52 | public IpAddress.Version version() { |
| 53 | return nextHop.version(); |
| 54 | } |
| 55 | |
| 56 | /** |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 57 | * Tests whether the IP version of this address is IPv4. |
| 58 | * |
| 59 | * @return true if the IP version of this address is IPv4, otherwise false. |
| 60 | */ |
| 61 | public boolean isIp4() { |
| 62 | return nextHop.isIp4(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Tests whether the IP version of this address is IPv6. |
| 67 | * |
| 68 | * @return true if the IP version of this address is IPv6, otherwise false. |
| 69 | */ |
| 70 | public boolean isIp6() { |
| 71 | return nextHop.isIp6(); |
| 72 | } |
| 73 | |
| 74 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 75 | * Returns the IP prefix of the route. |
| 76 | * |
| 77 | * @return the IP prefix of the route |
| 78 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 79 | public IpPrefix prefix() { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 80 | return prefix; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the next hop IP address for the route. |
| 85 | * |
| 86 | * @return the next hop IP address for the route |
| 87 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 88 | public IpAddress nextHop() { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 89 | return nextHop; |
| 90 | } |
| 91 | |
| 92 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 93 | * Creates the binary string representation of an IP prefix. |
| 94 | * The prefix can be either IPv4 or IPv6. |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 95 | * The string length is equal to the prefix length + 1. |
| 96 | * |
| 97 | * For each string, we put a extra "0" in the front. The purpose of |
| 98 | * doing this is to store the default route inside InvertedRadixTree. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 99 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 100 | * @param ipPrefix the IP prefix to use |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 101 | * @return the binary string representation |
| 102 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 103 | public static String createBinaryString(IpPrefix ipPrefix) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 104 | if (ipPrefix.prefixLength() == 0) { |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 105 | return "0"; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 108 | byte[] octets = ipPrefix.address().toOctets(); |
| 109 | StringBuilder result = new StringBuilder(ipPrefix.prefixLength()); |
| 110 | for (int i = 0; i < ipPrefix.prefixLength(); i++) { |
| 111 | int byteOffset = i / Byte.SIZE; |
| 112 | int bitOffset = i % Byte.SIZE; |
| 113 | int mask = 1 << (Byte.SIZE - 1 - bitOffset); |
| 114 | byte value = octets[byteOffset]; |
| 115 | boolean isSet = ((value & mask) != 0); |
| 116 | result.append(isSet ? "1" : "0"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 117 | } |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 118 | |
| 119 | return "0" + result.toString(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean equals(Object other) { |
| 124 | if (this == other) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | // |
| 129 | // NOTE: Subclasses are considered as change of identity, hence |
| 130 | // equals() will return false if the class type doesn't match. |
| 131 | // |
| 132 | if (other == null || getClass() != other.getClass()) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | RouteEntry otherRoute = (RouteEntry) other; |
| 137 | return Objects.equals(this.prefix, otherRoute.prefix) && |
| 138 | Objects.equals(this.nextHop, otherRoute.nextHop); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int hashCode() { |
| 143 | return Objects.hash(prefix, nextHop); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public String toString() { |
| 148 | return MoreObjects.toStringHelper(getClass()) |
| 149 | .add("prefix", prefix) |
| 150 | .add("nextHop", nextHop) |
| 151 | .toString(); |
| 152 | } |
| 153 | } |