blob: 19407de05f2a81098cc7079cdf58227a8839218c [file] [log] [blame]
Jonathan Hart7d7e2f52016-03-29 16:22:49 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart7d7e2f52016-03-29 16:22:49 -07003 *
4 * 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
7 *
8 * 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.
15 */
16
Ray Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070018
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
Jonathan Hart10dbafd2017-05-18 15:53:03 -070021import org.onosproject.cluster.NodeId;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070022
23import java.util.Objects;
24
Jonathan Hartfd176612016-04-11 10:42:10 -070025import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070026import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Represents a route.
31 */
32public class Route {
33
34 private static final String VERSION_MISMATCH =
35 "Prefix and next hop must be in the same address family";
36
Jonathan Hart10dbafd2017-05-18 15:53:03 -070037 private static final NodeId UNDEFINED = new NodeId("-");
38
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070039 /**
40 * Source of the route.
41 */
42 public enum Source {
43 /**
44 * Route came from the iBGP route source.
45 */
46 BGP,
47
48 /**
49 * Route came from the FPM route source.
50 */
51 FPM,
52
53 /**
Kalhee Kimb9082122018-03-29 18:57:21 +000054 * Route came from the RIP route source.
55 */
56 RIP,
57
58 /**
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070059 * Route can from the static route source.
60 */
61 STATIC,
62
63 /**
Kalhee Kim54a97c92018-04-02 21:23:46 +000064 * Route can from the DHCP route source.
65 */
66 DHCP,
67
68 /**
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070069 * Route source was not defined.
70 */
71 UNDEFINED
72 }
73
74 private final Source source;
75 private final IpPrefix prefix;
76 private final IpAddress nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -070077 private final NodeId sourceNode;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070078
79 /**
80 * Creates a route.
81 *
82 * @param source route source
83 * @param prefix IP prefix
Jonathan Hart10dbafd2017-05-18 15:53:03 -070084 * @param nextHop next hop IP address
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070085 */
86 public Route(Source source, IpPrefix prefix, IpAddress nextHop) {
Jonathan Hart10dbafd2017-05-18 15:53:03 -070087 this(source, prefix, nextHop, UNDEFINED);
88 }
89
90 /**
91 * Creates a route.
92 *
93 * @param source route source
94 * @param prefix IP prefix
95 * @param nextHop next hop IP address
96 * @param sourceNode ONOS node the route was sourced from
97 */
98 public Route(Source source, IpPrefix prefix, IpAddress nextHop, NodeId sourceNode) {
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070099 checkNotNull(prefix);
Jonathan Hartfd176612016-04-11 10:42:10 -0700100 checkNotNull(nextHop);
101 checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700102
103 this.source = checkNotNull(source);
104 this.prefix = prefix;
105 this.nextHop = nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700106 this.sourceNode = checkNotNull(sourceNode);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700107 }
108
109 /**
110 * Returns the route source.
111 *
112 * @return route source
113 */
114 public Source source() {
115 return source;
116 }
117
118 /**
119 * Returns the IP prefix of the route.
120 *
121 * @return IP prefix
122 */
123 public IpPrefix prefix() {
124 return prefix;
125 }
126
127 /**
128 * Returns the next hop IP address.
129 *
130 * @return next hop
131 */
132 public IpAddress nextHop() {
133 return nextHop;
134 }
135
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700136 /**
137 * Returns the ONOS node the route was sourced from.
138 *
139 * @return ONOS node ID
140 */
141 public NodeId sourceNode() {
142 return sourceNode;
143 }
144
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700145 @Override
146 public int hashCode() {
147 return Objects.hash(prefix, nextHop);
148 }
149
150 @Override
151 public boolean equals(Object other) {
152 if (this == other) {
153 return true;
154 }
155
156 if (!(other instanceof Route)) {
157 return false;
158 }
159
160 Route that = (Route) other;
161
162 return Objects.equals(this.prefix, that.prefix) &&
163 Objects.equals(this.nextHop, that.nextHop);
164 }
Jonathan Hartfd176612016-04-11 10:42:10 -0700165
166 @Override
167 public String toString() {
168 return toStringHelper(this)
169 .add("prefix", prefix)
170 .add("nextHop", nextHop)
171 .toString();
172 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700173}