blob: c4647e15083106f17ef781f6bf5bf6c8ffdb08d2 [file] [log] [blame]
Jonathan Hart7d7e2f52016-03-29 16:22:49 -07001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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
69 * @param nextHop
70 */
71 public Route(Source source, IpPrefix prefix, IpAddress nextHop) {
72 checkNotNull(prefix);
73 checkNotNull(nextHop);
74 checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH);
75
76 this.source = checkNotNull(source);
77 this.prefix = prefix;
78 this.nextHop = nextHop;
79 }
80
81 /**
82 * Returns the route source.
83 *
84 * @return route source
85 */
86 public Source source() {
87 return source;
88 }
89
90 /**
91 * Returns the IP prefix of the route.
92 *
93 * @return IP prefix
94 */
95 public IpPrefix prefix() {
96 return prefix;
97 }
98
99 /**
100 * Returns the next hop IP address.
101 *
102 * @return next hop
103 */
104 public IpAddress nextHop() {
105 return nextHop;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(prefix, nextHop);
111 }
112
113 @Override
114 public boolean equals(Object other) {
115 if (this == other) {
116 return true;
117 }
118
119 if (!(other instanceof Route)) {
120 return false;
121 }
122
123 Route that = (Route) other;
124
125 return Objects.equals(this.prefix, that.prefix) &&
126 Objects.equals(this.nextHop, that.nextHop);
127 }
128}