blob: 2baf6866aa9960e632582d63a7c0977e5a8997de [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.
Jonathan Hart888eaa02016-06-22 15:56:21 -070028 *
29 * @deprecated use RouteService instead
Jonathan Hart335ef462014-10-16 08:20:46 -070030 */
Jonathan Hart888eaa02016-06-22 15:56:21 -070031@Deprecated
Jonathan Hart335ef462014-10-16 08:20:46 -070032public class RouteEntry {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080033 private final IpPrefix prefix; // The IP prefix
34 private final IpAddress nextHop; // Next-hop IP address
Jonathan Hart335ef462014-10-16 08:20:46 -070035
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 Radoslavov3a0a52e2015-01-06 17:41:37 -080042 public RouteEntry(IpPrefix prefix, IpAddress nextHop) {
Jonathan Hart335ef462014-10-16 08:20:46 -070043 this.prefix = checkNotNull(prefix);
44 this.nextHop = checkNotNull(nextHop);
45 }
46
47 /**
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080048 * 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 Radoslavov87dd9302015-03-10 13:53:24 -070057 * 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 Hart335ef462014-10-16 08:20:46 -070075 * Returns the IP prefix of the route.
76 *
77 * @return the IP prefix of the route
78 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080079 public IpPrefix prefix() {
Jonathan Hart335ef462014-10-16 08:20:46 -070080 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 Radoslavov3a0a52e2015-01-06 17:41:37 -080088 public IpAddress nextHop() {
Jonathan Hart335ef462014-10-16 08:20:46 -070089 return nextHop;
90 }
91
92 /**
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080093 * Creates the binary string representation of an IP prefix.
94 * The prefix can be either IPv4 or IPv6.
Pingping Lin92ca4912015-11-19 16:41:54 -080095 * 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 Hart335ef462014-10-16 08:20:46 -070099 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800100 * @param ipPrefix the IP prefix to use
Jonathan Hart335ef462014-10-16 08:20:46 -0700101 * @return the binary string representation
102 */
Jonathan Hart41349e92015-02-09 14:14:02 -0800103 public static String createBinaryString(IpPrefix ipPrefix) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800104 if (ipPrefix.prefixLength() == 0) {
Pingping Lin92ca4912015-11-19 16:41:54 -0800105 return "0";
Jonathan Hart335ef462014-10-16 08:20:46 -0700106 }
107
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800108 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 Hart335ef462014-10-16 08:20:46 -0700117 }
Pingping Lin92ca4912015-11-19 16:41:54 -0800118
119 return "0" + result.toString();
Jonathan Hart335ef462014-10-16 08:20:46 -0700120 }
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}