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 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Represents a route. |
| 30 | */ |
| 31 | public 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 Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 70 | * @param nextHop net hop IP address |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 71 | */ |
| 72 | public Route(Source source, IpPrefix prefix, IpAddress nextHop) { |
| 73 | checkNotNull(prefix); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 74 | checkNotNull(nextHop); |
| 75 | checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 76 | |
| 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 Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return toStringHelper(this) |
| 133 | .add("prefix", prefix) |
| 134 | .add("nextHop", nextHop) |
| 135 | .toString(); |
| 136 | } |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 137 | } |