blob: 0ae4952de0fbd8ac6209398490f442e50dd58e2e [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;
21
22import java.util.Objects;
23
Jonathan Hartfd176612016-04-11 10:42:10 -070024import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070025import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Represents a route.
30 */
31public class Route {
32
33 private static final String VERSION_MISMATCH =
34 "Prefix and next hop must be in the same address family";
35
36 /**
37 * Source of the route.
38 */
39 public enum Source {
40 /**
41 * Route came from the iBGP route source.
42 */
43 BGP,
44
45 /**
46 * Route came from the FPM route source.
47 */
48 FPM,
49
50 /**
51 * Route can from the static route source.
52 */
53 STATIC,
54
55 /**
56 * Route source was not defined.
57 */
58 UNDEFINED
59 }
60
61 private final Source source;
62 private final IpPrefix prefix;
63 private final IpAddress nextHop;
64
65 /**
66 * Creates a route.
67 *
68 * @param source route source
69 * @param prefix IP prefix
Ray Milkeyd4334db2016-04-05 17:39:44 -070070 * @param nextHop net hop IP address
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070071 */
72 public Route(Source source, IpPrefix prefix, IpAddress nextHop) {
73 checkNotNull(prefix);
Jonathan Hartfd176612016-04-11 10:42:10 -070074 checkNotNull(nextHop);
75 checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH);
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070076
77 this.source = checkNotNull(source);
78 this.prefix = prefix;
79 this.nextHop = nextHop;
80 }
81
82 /**
83 * Returns the route source.
84 *
85 * @return route source
86 */
87 public Source source() {
88 return source;
89 }
90
91 /**
92 * Returns the IP prefix of the route.
93 *
94 * @return IP prefix
95 */
96 public IpPrefix prefix() {
97 return prefix;
98 }
99
100 /**
101 * Returns the next hop IP address.
102 *
103 * @return next hop
104 */
105 public IpAddress nextHop() {
106 return nextHop;
107 }
108
109 @Override
110 public int hashCode() {
111 return Objects.hash(prefix, nextHop);
112 }
113
114 @Override
115 public boolean equals(Object other) {
116 if (this == other) {
117 return true;
118 }
119
120 if (!(other instanceof Route)) {
121 return false;
122 }
123
124 Route that = (Route) other;
125
126 return Objects.equals(this.prefix, that.prefix) &&
127 Objects.equals(this.nextHop, that.nextHop);
128 }
Jonathan Hartfd176612016-04-11 10:42:10 -0700129
130 @Override
131 public String toString() {
132 return toStringHelper(this)
133 .add("prefix", prefix)
134 .add("nextHop", nextHop)
135 .toString();
136 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700137}