Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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. |
| 28 | */ |
| 29 | public class RouteEntry { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 30 | private final IpPrefix prefix; // The IP prefix |
| 31 | private final IpAddress nextHop; // Next-hop IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Class constructor. |
| 35 | * |
| 36 | * @param prefix the IP prefix of the route |
| 37 | * @param nextHop the next hop IP address for the route |
| 38 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 39 | public RouteEntry(IpPrefix prefix, IpAddress nextHop) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 40 | this.prefix = checkNotNull(prefix); |
| 41 | this.nextHop = checkNotNull(nextHop); |
| 42 | } |
| 43 | |
| 44 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 45 | * Returns the IP version of the route. |
| 46 | * |
| 47 | * @return the IP version of the route |
| 48 | */ |
| 49 | public IpAddress.Version version() { |
| 50 | return nextHop.version(); |
| 51 | } |
| 52 | |
| 53 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 54 | * Returns the IP prefix of the route. |
| 55 | * |
| 56 | * @return the IP prefix of the route |
| 57 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 58 | public IpPrefix prefix() { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 59 | return prefix; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the next hop IP address for the route. |
| 64 | * |
| 65 | * @return the next hop IP address for the route |
| 66 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 67 | public IpAddress nextHop() { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 68 | return nextHop; |
| 69 | } |
| 70 | |
| 71 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 72 | * Creates the binary string representation of an IP prefix. |
| 73 | * The prefix can be either IPv4 or IPv6. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 74 | * The string length is equal to the prefix length. |
| 75 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 76 | * @param ipPrefix the IP prefix to use |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 77 | * @return the binary string representation |
| 78 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 79 | public static String createBinaryString(IpPrefix ipPrefix) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 80 | if (ipPrefix.prefixLength() == 0) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 81 | return ""; |
| 82 | } |
| 83 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 84 | byte[] octets = ipPrefix.address().toOctets(); |
| 85 | StringBuilder result = new StringBuilder(ipPrefix.prefixLength()); |
| 86 | for (int i = 0; i < ipPrefix.prefixLength(); i++) { |
| 87 | int byteOffset = i / Byte.SIZE; |
| 88 | int bitOffset = i % Byte.SIZE; |
| 89 | int mask = 1 << (Byte.SIZE - 1 - bitOffset); |
| 90 | byte value = octets[byteOffset]; |
| 91 | boolean isSet = ((value & mask) != 0); |
| 92 | result.append(isSet ? "1" : "0"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 93 | } |
| 94 | return result.toString(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean equals(Object other) { |
| 99 | if (this == other) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | // |
| 104 | // NOTE: Subclasses are considered as change of identity, hence |
| 105 | // equals() will return false if the class type doesn't match. |
| 106 | // |
| 107 | if (other == null || getClass() != other.getClass()) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | RouteEntry otherRoute = (RouteEntry) other; |
| 112 | return Objects.equals(this.prefix, otherRoute.prefix) && |
| 113 | Objects.equals(this.nextHop, otherRoute.nextHop); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int hashCode() { |
| 118 | return Objects.hash(prefix, nextHop); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public String toString() { |
| 123 | return MoreObjects.toStringHelper(getClass()) |
| 124 | .add("prefix", prefix) |
| 125 | .add("nextHop", nextHop) |
| 126 | .toString(); |
| 127 | } |
| 128 | } |