Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.incubator.net.routing; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Represents a route. |
| 29 | */ |
| 30 | public 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 Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 69 | * @param nextHop net hop IP address |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 70 | */ |
| 71 | public Route(Source source, IpPrefix prefix, IpAddress nextHop) { |
| 72 | checkNotNull(prefix); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 73 | checkArgument(nextHop == null || prefix.version().equals(nextHop.version()), VERSION_MISMATCH); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 74 | |
| 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 | } |