blob: 1b45ac5365105f3bdb6147ac9e33657c0e4744de [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 Hart335ef462014-10-16 08:20:46 -070016package org.onlab.onos.sdnip;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Objects;
21
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.IpPrefix;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Represents a route entry for an IP prefix.
29 */
30public class RouteEntry {
31 private final IpPrefix prefix; // The IP prefix
32 private final IpAddress nextHop; // Next-hop IP address
33
34 /**
35 * Class constructor.
36 *
37 * @param prefix the IP prefix of the route
38 * @param nextHop the next hop IP address for the route
39 */
40 public RouteEntry(IpPrefix prefix, IpAddress nextHop) {
41 this.prefix = checkNotNull(prefix);
42 this.nextHop = checkNotNull(nextHop);
43 }
44
45 /**
46 * Returns the IP prefix of the route.
47 *
48 * @return the IP prefix of the route
49 */
50 public IpPrefix prefix() {
51 return prefix;
52 }
53
54 /**
55 * Returns the next hop IP address for the route.
56 *
57 * @return the next hop IP address for the route
58 */
59 public IpAddress nextHop() {
60 return nextHop;
61 }
62
63 /**
64 * Creates the binary string representation of an IPv4 prefix.
65 * The string length is equal to the prefix length.
66 *
67 * @param ip4Prefix the IPv4 prefix to use
68 * @return the binary string representation
69 */
70 static String createBinaryString(IpPrefix ip4Prefix) {
71 if (ip4Prefix.prefixLength() == 0) {
72 return "";
73 }
74
75 StringBuilder result = new StringBuilder(ip4Prefix.prefixLength());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -070076 long value = ip4Prefix.toInt();
Jonathan Hart335ef462014-10-16 08:20:46 -070077 for (int i = 0; i < ip4Prefix.prefixLength(); i++) {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070078 long mask = 1 << (IpPrefix.MAX_INET_MASK_LENGTH - 1 - i);
Jonathan Hart335ef462014-10-16 08:20:46 -070079 result.append(((value & mask) == 0) ? "0" : "1");
80 }
81 return result.toString();
82 }
83
84 @Override
85 public boolean equals(Object other) {
86 if (this == other) {
87 return true;
88 }
89
90 //
91 // NOTE: Subclasses are considered as change of identity, hence
92 // equals() will return false if the class type doesn't match.
93 //
94 if (other == null || getClass() != other.getClass()) {
95 return false;
96 }
97
98 RouteEntry otherRoute = (RouteEntry) other;
99 return Objects.equals(this.prefix, otherRoute.prefix) &&
100 Objects.equals(this.nextHop, otherRoute.nextHop);
101 }
102
103 @Override
104 public int hashCode() {
105 return Objects.hash(prefix, nextHop);
106 }
107
108 @Override
109 public String toString() {
110 return MoreObjects.toStringHelper(getClass())
111 .add("prefix", prefix)
112 .add("nextHop", nextHop)
113 .toString();
114 }
115}