blob: 31fe8b73fc8e43180b50c5989a83ca076bdd420e [file] [log] [blame]
Jonathan Hart7d7e2f52016-03-29 16:22:49 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.incubator.net.routing;
18
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 /**
54 * Route can from the static route source.
55 */
56 STATIC,
57
58 /**
59 * Route source was not defined.
60 */
61 UNDEFINED
62 }
63
64 private final Source source;
65 private final IpPrefix prefix;
66 private final IpAddress nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -070067 private final NodeId sourceNode;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070068
69 /**
70 * Creates a route.
71 *
72 * @param source route source
73 * @param prefix IP prefix
Jonathan Hart10dbafd2017-05-18 15:53:03 -070074 * @param nextHop next hop IP address
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070075 */
76 public Route(Source source, IpPrefix prefix, IpAddress nextHop) {
Jonathan Hart10dbafd2017-05-18 15:53:03 -070077 this(source, prefix, nextHop, UNDEFINED);
78 }
79
80 /**
81 * Creates a route.
82 *
83 * @param source route source
84 * @param prefix IP prefix
85 * @param nextHop next hop IP address
86 * @param sourceNode ONOS node the route was sourced from
87 */
88 public Route(Source source, IpPrefix prefix, IpAddress nextHop, NodeId sourceNode) {
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070089 checkNotNull(prefix);
Jonathan Hartfd176612016-04-11 10:42:10 -070090 checkNotNull(nextHop);
91 checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070092
93 this.source = checkNotNull(source);
94 this.prefix = prefix;
95 this.nextHop = nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -070096 this.sourceNode = checkNotNull(sourceNode);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070097 }
98
99 /**
100 * Returns the route source.
101 *
102 * @return route source
103 */
104 public Source source() {
105 return source;
106 }
107
108 /**
109 * Returns the IP prefix of the route.
110 *
111 * @return IP prefix
112 */
113 public IpPrefix prefix() {
114 return prefix;
115 }
116
117 /**
118 * Returns the next hop IP address.
119 *
120 * @return next hop
121 */
122 public IpAddress nextHop() {
123 return nextHop;
124 }
125
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700126 /**
127 * Returns the ONOS node the route was sourced from.
128 *
129 * @return ONOS node ID
130 */
131 public NodeId sourceNode() {
132 return sourceNode;
133 }
134
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700135 @Override
136 public int hashCode() {
137 return Objects.hash(prefix, nextHop);
138 }
139
140 @Override
141 public boolean equals(Object other) {
142 if (this == other) {
143 return true;
144 }
145
146 if (!(other instanceof Route)) {
147 return false;
148 }
149
150 Route that = (Route) other;
151
152 return Objects.equals(this.prefix, that.prefix) &&
153 Objects.equals(this.nextHop, that.nextHop);
154 }
Jonathan Hartfd176612016-04-11 10:42:10 -0700155
156 @Override
157 public String toString() {
158 return toStringHelper(this)
159 .add("prefix", prefix)
160 .add("nextHop", nextHop)
161 .toString();
162 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700163}