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