blob: 4b8c2a05048bd931a6ee12408eb7675fd5a58cfc [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 /**
64 * Route source was not defined.
65 */
66 UNDEFINED
67 }
68
69 private final Source source;
70 private final IpPrefix prefix;
71 private final IpAddress nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -070072 private final NodeId sourceNode;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070073
74 /**
75 * Creates a route.
76 *
77 * @param source route source
78 * @param prefix IP prefix
Jonathan Hart10dbafd2017-05-18 15:53:03 -070079 * @param nextHop next hop IP address
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070080 */
81 public Route(Source source, IpPrefix prefix, IpAddress nextHop) {
Jonathan Hart10dbafd2017-05-18 15:53:03 -070082 this(source, prefix, nextHop, UNDEFINED);
83 }
84
85 /**
86 * Creates a route.
87 *
88 * @param source route source
89 * @param prefix IP prefix
90 * @param nextHop next hop IP address
91 * @param sourceNode ONOS node the route was sourced from
92 */
93 public Route(Source source, IpPrefix prefix, IpAddress nextHop, NodeId sourceNode) {
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070094 checkNotNull(prefix);
Jonathan Hartfd176612016-04-11 10:42:10 -070095 checkNotNull(nextHop);
96 checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070097
98 this.source = checkNotNull(source);
99 this.prefix = prefix;
100 this.nextHop = nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700101 this.sourceNode = checkNotNull(sourceNode);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700102 }
103
104 /**
105 * Returns the route source.
106 *
107 * @return route source
108 */
109 public Source source() {
110 return source;
111 }
112
113 /**
114 * Returns the IP prefix of the route.
115 *
116 * @return IP prefix
117 */
118 public IpPrefix prefix() {
119 return prefix;
120 }
121
122 /**
123 * Returns the next hop IP address.
124 *
125 * @return next hop
126 */
127 public IpAddress nextHop() {
128 return nextHop;
129 }
130
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700131 /**
132 * Returns the ONOS node the route was sourced from.
133 *
134 * @return ONOS node ID
135 */
136 public NodeId sourceNode() {
137 return sourceNode;
138 }
139
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700140 @Override
141 public int hashCode() {
142 return Objects.hash(prefix, nextHop);
143 }
144
145 @Override
146 public boolean equals(Object other) {
147 if (this == other) {
148 return true;
149 }
150
151 if (!(other instanceof Route)) {
152 return false;
153 }
154
155 Route that = (Route) other;
156
157 return Objects.equals(this.prefix, that.prefix) &&
158 Objects.equals(this.nextHop, that.nextHop);
159 }
Jonathan Hartfd176612016-04-11 10:42:10 -0700160
161 @Override
162 public String toString() {
163 return toStringHelper(this)
164 .add("prefix", prefix)
165 .add("nextHop", nextHop)
166 .toString();
167 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700168}