blob: 5bd0b94ca3c0124d300ef1ebc8aeda3ed0d7e516 [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 */
psneha36b60f92018-08-29 08:56:08 -040071 UNDEFINED,
72
73 /**
74 * Route can from the DHCP-LQ route source.
75 */
76 DHCPLQ
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070077 }
78
79 private final Source source;
80 private final IpPrefix prefix;
81 private final IpAddress nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -070082 private final NodeId sourceNode;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070083
84 /**
85 * Creates a route.
86 *
87 * @param source route source
88 * @param prefix IP prefix
Jonathan Hart10dbafd2017-05-18 15:53:03 -070089 * @param nextHop next hop IP address
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070090 */
91 public Route(Source source, IpPrefix prefix, IpAddress nextHop) {
Jonathan Hart10dbafd2017-05-18 15:53:03 -070092 this(source, prefix, nextHop, UNDEFINED);
93 }
94
95 /**
96 * Creates a route.
97 *
98 * @param source route source
99 * @param prefix IP prefix
100 * @param nextHop next hop IP address
101 * @param sourceNode ONOS node the route was sourced from
102 */
103 public Route(Source source, IpPrefix prefix, IpAddress nextHop, NodeId sourceNode) {
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700104 checkNotNull(prefix);
Jonathan Hartfd176612016-04-11 10:42:10 -0700105 checkNotNull(nextHop);
106 checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700107
108 this.source = checkNotNull(source);
109 this.prefix = prefix;
110 this.nextHop = nextHop;
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700111 this.sourceNode = checkNotNull(sourceNode);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700112 }
113
114 /**
115 * Returns the route source.
116 *
117 * @return route source
118 */
119 public Source source() {
120 return source;
121 }
122
123 /**
124 * Returns the IP prefix of the route.
125 *
126 * @return IP prefix
127 */
128 public IpPrefix prefix() {
129 return prefix;
130 }
131
132 /**
133 * Returns the next hop IP address.
134 *
135 * @return next hop
136 */
137 public IpAddress nextHop() {
138 return nextHop;
139 }
140
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700141 /**
142 * Returns the ONOS node the route was sourced from.
143 *
144 * @return ONOS node ID
145 */
146 public NodeId sourceNode() {
147 return sourceNode;
148 }
149
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700150 @Override
151 public int hashCode() {
152 return Objects.hash(prefix, nextHop);
153 }
154
155 @Override
156 public boolean equals(Object other) {
157 if (this == other) {
158 return true;
159 }
160
161 if (!(other instanceof Route)) {
162 return false;
163 }
164
165 Route that = (Route) other;
166
167 return Objects.equals(this.prefix, that.prefix) &&
168 Objects.equals(this.nextHop, that.nextHop);
169 }
Jonathan Hartfd176612016-04-11 10:42:10 -0700170
171 @Override
172 public String toString() {
173 return toStringHelper(this)
174 .add("prefix", prefix)
175 .add("nextHop", nextHop)
176 .toString();
177 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700178}