blob: 13ee240abed513586ab9dcc9d6082a6f4dba2508 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Jonathan Hart335ef462014-10-16 08:20:46 -070017
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Objects;
21
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080022import org.onlab.packet.Ip4Address;
23import org.onlab.packet.Ip4Prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070024
25import com.google.common.base.MoreObjects;
26
27/**
28 * Represents a route entry for an IP prefix.
29 */
30public class RouteEntry {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080031 private final Ip4Prefix prefix; // The IP prefix
32 private final Ip4Address nextHop; // Next-hop IP address
Jonathan Hart335ef462014-10-16 08:20:46 -070033
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 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080040 public RouteEntry(Ip4Prefix prefix, Ip4Address nextHop) {
Jonathan Hart335ef462014-10-16 08:20:46 -070041 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 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080050 public Ip4Prefix prefix() {
Jonathan Hart335ef462014-10-16 08:20:46 -070051 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 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080059 public Ip4Address nextHop() {
Jonathan Hart335ef462014-10-16 08:20:46 -070060 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 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080070 static String createBinaryString(Ip4Prefix ip4Prefix) {
Jonathan Hart335ef462014-10-16 08:20:46 -070071 if (ip4Prefix.prefixLength() == 0) {
72 return "";
73 }
74
75 StringBuilder result = new StringBuilder(ip4Prefix.prefixLength());
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070076 long value = ip4Prefix.address().toInt() & 0xffffffffL;
Jonathan Hart335ef462014-10-16 08:20:46 -070077 for (int i = 0; i < ip4Prefix.prefixLength(); i++) {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080078 long mask = 1 << (Ip4Prefix.MAX_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}