Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 19 | package org.onlab.onos.sdnip; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | |
| 30 | /** |
| 31 | * Represents a route entry for an IP prefix. |
| 32 | */ |
| 33 | public class RouteEntry { |
| 34 | private final IpPrefix prefix; // The IP prefix |
| 35 | private final IpAddress nextHop; // Next-hop IP address |
| 36 | |
| 37 | /** |
| 38 | * Class constructor. |
| 39 | * |
| 40 | * @param prefix the IP prefix of the route |
| 41 | * @param nextHop the next hop IP address for the route |
| 42 | */ |
| 43 | public RouteEntry(IpPrefix prefix, IpAddress nextHop) { |
| 44 | this.prefix = checkNotNull(prefix); |
| 45 | this.nextHop = checkNotNull(nextHop); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns the IP prefix of the route. |
| 50 | * |
| 51 | * @return the IP prefix of the route |
| 52 | */ |
| 53 | public IpPrefix prefix() { |
| 54 | return prefix; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the next hop IP address for the route. |
| 59 | * |
| 60 | * @return the next hop IP address for the route |
| 61 | */ |
| 62 | public IpAddress nextHop() { |
| 63 | return nextHop; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates the binary string representation of an IPv4 prefix. |
| 68 | * The string length is equal to the prefix length. |
| 69 | * |
| 70 | * @param ip4Prefix the IPv4 prefix to use |
| 71 | * @return the binary string representation |
| 72 | */ |
| 73 | static String createBinaryString(IpPrefix ip4Prefix) { |
| 74 | if (ip4Prefix.prefixLength() == 0) { |
| 75 | return ""; |
| 76 | } |
| 77 | |
| 78 | StringBuilder result = new StringBuilder(ip4Prefix.prefixLength()); |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 79 | long value = ip4Prefix.toInt(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 80 | for (int i = 0; i < ip4Prefix.prefixLength(); i++) { |
| 81 | long mask = 1 << (IpAddress.MAX_INET_MASK - 1 - i); |
| 82 | result.append(((value & mask) == 0) ? "0" : "1"); |
| 83 | } |
| 84 | return result.toString(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean equals(Object other) { |
| 89 | if (this == other) { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | // |
| 94 | // NOTE: Subclasses are considered as change of identity, hence |
| 95 | // equals() will return false if the class type doesn't match. |
| 96 | // |
| 97 | if (other == null || getClass() != other.getClass()) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | RouteEntry otherRoute = (RouteEntry) other; |
| 102 | return Objects.equals(this.prefix, otherRoute.prefix) && |
| 103 | Objects.equals(this.nextHop, otherRoute.nextHop); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return Objects.hash(prefix, nextHop); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String toString() { |
| 113 | return MoreObjects.toStringHelper(getClass()) |
| 114 | .add("prefix", prefix) |
| 115 | .add("nextHop", nextHop) |
| 116 | .toString(); |
| 117 | } |
| 118 | } |