blob: 8053ecd4d2be6ebcc8105d8a95cbcd26a73773a1 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Jonathan Hartc9e36c52017-01-05 09:53:33 +13002 * Copyright 2017-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 Hartc9e36c52017-01-05 09:53:33 +130016package org.onosproject.routing.bgp;
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
Jonathan Hart335ef462014-10-16 08:20:46 -070089 @Override
90 public boolean equals(Object other) {
91 if (this == other) {
92 return true;
93 }
94
95 //
96 // NOTE: Subclasses are considered as change of identity, hence
97 // equals() will return false if the class type doesn't match.
98 //
99 if (other == null || getClass() != other.getClass()) {
100 return false;
101 }
102
103 RouteEntry otherRoute = (RouteEntry) other;
104 return Objects.equals(this.prefix, otherRoute.prefix) &&
105 Objects.equals(this.nextHop, otherRoute.nextHop);
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(prefix, nextHop);
111 }
112
113 @Override
114 public String toString() {
115 return MoreObjects.toStringHelper(getClass())
116 .add("prefix", prefix)
117 .add("nextHop", nextHop)
118 .toString();
119 }
120}