blob: 4149164d3cc83b12c2c64e4872ad70b1dade8780 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-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 Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing;
Jonathan Hart335ef462014-10-16 08:20:46 -070017
Jonathan Hart41349e92015-02-09 14:14:02 -080018import com.google.common.base.MoreObjects;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080019import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070021
Jonathan Hart41349e92015-02-09 14:14:02 -080022import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hart335ef462014-10-16 08:20:46 -070025
26/**
27 * Represents a route entry for an IP prefix.
28 */
29public class RouteEntry {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080030 private final IpPrefix prefix; // The IP prefix
31 private final IpAddress nextHop; // Next-hop IP address
Jonathan Hart335ef462014-10-16 08:20:46 -070032
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 Radoslavov3a0a52e2015-01-06 17:41:37 -080039 public RouteEntry(IpPrefix prefix, IpAddress nextHop) {
Jonathan Hart335ef462014-10-16 08:20:46 -070040 this.prefix = checkNotNull(prefix);
41 this.nextHop = checkNotNull(nextHop);
42 }
43
44 /**
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080045 * 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 /**
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -070054 * Tests whether the IP version of this address is IPv4.
55 *
56 * @return true if the IP version of this address is IPv4, otherwise false.
57 */
58 public boolean isIp4() {
59 return nextHop.isIp4();
60 }
61
62 /**
63 * Tests whether the IP version of this address is IPv6.
64 *
65 * @return true if the IP version of this address is IPv6, otherwise false.
66 */
67 public boolean isIp6() {
68 return nextHop.isIp6();
69 }
70
71 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070072 * Returns the IP prefix of the route.
73 *
74 * @return the IP prefix of the route
75 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080076 public IpPrefix prefix() {
Jonathan Hart335ef462014-10-16 08:20:46 -070077 return prefix;
78 }
79
80 /**
81 * Returns the next hop IP address for the route.
82 *
83 * @return the next hop IP address for the route
84 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080085 public IpAddress nextHop() {
Jonathan Hart335ef462014-10-16 08:20:46 -070086 return nextHop;
87 }
88
89 /**
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080090 * Creates the binary string representation of an IP prefix.
91 * The prefix can be either IPv4 or IPv6.
Pingping Lin92ca4912015-11-19 16:41:54 -080092 * The string length is equal to the prefix length + 1.
93 *
94 * For each string, we put a extra "0" in the front. The purpose of
95 * doing this is to store the default route inside InvertedRadixTree.
Jonathan Hart335ef462014-10-16 08:20:46 -070096 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080097 * @param ipPrefix the IP prefix to use
Jonathan Hart335ef462014-10-16 08:20:46 -070098 * @return the binary string representation
99 */
Jonathan Hart41349e92015-02-09 14:14:02 -0800100 public static String createBinaryString(IpPrefix ipPrefix) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800101 if (ipPrefix.prefixLength() == 0) {
Pingping Lin92ca4912015-11-19 16:41:54 -0800102 return "0";
Jonathan Hart335ef462014-10-16 08:20:46 -0700103 }
104
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800105 byte[] octets = ipPrefix.address().toOctets();
106 StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
107 for (int i = 0; i < ipPrefix.prefixLength(); i++) {
108 int byteOffset = i / Byte.SIZE;
109 int bitOffset = i % Byte.SIZE;
110 int mask = 1 << (Byte.SIZE - 1 - bitOffset);
111 byte value = octets[byteOffset];
112 boolean isSet = ((value & mask) != 0);
113 result.append(isSet ? "1" : "0");
Jonathan Hart335ef462014-10-16 08:20:46 -0700114 }
Pingping Lin92ca4912015-11-19 16:41:54 -0800115
116 return "0" + result.toString();
Jonathan Hart335ef462014-10-16 08:20:46 -0700117 }
118
119 @Override
120 public boolean equals(Object other) {
121 if (this == other) {
122 return true;
123 }
124
125 //
126 // NOTE: Subclasses are considered as change of identity, hence
127 // equals() will return false if the class type doesn't match.
128 //
129 if (other == null || getClass() != other.getClass()) {
130 return false;
131 }
132
133 RouteEntry otherRoute = (RouteEntry) other;
134 return Objects.equals(this.prefix, otherRoute.prefix) &&
135 Objects.equals(this.nextHop, otherRoute.nextHop);
136 }
137
138 @Override
139 public int hashCode() {
140 return Objects.hash(prefix, nextHop);
141 }
142
143 @Override
144 public String toString() {
145 return MoreObjects.toStringHelper(getClass())
146 .add("prefix", prefix)
147 .add("nextHop", nextHop)
148 .toString();
149 }
150}