Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.Objects; |
| 24 | |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Represents a route. |
| 31 | */ |
| 32 | public class Route { |
| 33 | |
| 34 | private static final String VERSION_MISMATCH = |
| 35 | "Prefix and next hop must be in the same address family"; |
| 36 | |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 37 | private static final NodeId UNDEFINED = new NodeId("-"); |
| 38 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Source of the route. |
| 41 | */ |
| 42 | public enum Source { |
| 43 | /** |
| 44 | * Route came from the iBGP route source. |
| 45 | */ |
| 46 | BGP, |
| 47 | |
| 48 | /** |
| 49 | * Route came from the FPM route source. |
| 50 | */ |
| 51 | FPM, |
| 52 | |
| 53 | /** |
| 54 | * Route can from the static route source. |
| 55 | */ |
| 56 | STATIC, |
| 57 | |
| 58 | /** |
| 59 | * Route source was not defined. |
| 60 | */ |
| 61 | UNDEFINED |
| 62 | } |
| 63 | |
| 64 | private final Source source; |
| 65 | private final IpPrefix prefix; |
| 66 | private final IpAddress nextHop; |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 67 | private final NodeId sourceNode; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Creates a route. |
| 71 | * |
| 72 | * @param source route source |
| 73 | * @param prefix IP prefix |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 74 | * @param nextHop next hop IP address |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 75 | */ |
| 76 | public Route(Source source, IpPrefix prefix, IpAddress nextHop) { |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 77 | this(source, prefix, nextHop, UNDEFINED); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates a route. |
| 82 | * |
| 83 | * @param source route source |
| 84 | * @param prefix IP prefix |
| 85 | * @param nextHop next hop IP address |
| 86 | * @param sourceNode ONOS node the route was sourced from |
| 87 | */ |
| 88 | public Route(Source source, IpPrefix prefix, IpAddress nextHop, NodeId sourceNode) { |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 89 | checkNotNull(prefix); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 90 | checkNotNull(nextHop); |
| 91 | checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 92 | |
| 93 | this.source = checkNotNull(source); |
| 94 | this.prefix = prefix; |
| 95 | this.nextHop = nextHop; |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 96 | this.sourceNode = checkNotNull(sourceNode); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the route source. |
| 101 | * |
| 102 | * @return route source |
| 103 | */ |
| 104 | public Source source() { |
| 105 | return source; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the IP prefix of the route. |
| 110 | * |
| 111 | * @return IP prefix |
| 112 | */ |
| 113 | public IpPrefix prefix() { |
| 114 | return prefix; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the next hop IP address. |
| 119 | * |
| 120 | * @return next hop |
| 121 | */ |
| 122 | public IpAddress nextHop() { |
| 123 | return nextHop; |
| 124 | } |
| 125 | |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Returns the ONOS node the route was sourced from. |
| 128 | * |
| 129 | * @return ONOS node ID |
| 130 | */ |
| 131 | public NodeId sourceNode() { |
| 132 | return sourceNode; |
| 133 | } |
| 134 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public int hashCode() { |
| 137 | return Objects.hash(prefix, nextHop); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean equals(Object other) { |
| 142 | if (this == other) { |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | if (!(other instanceof Route)) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | Route that = (Route) other; |
| 151 | |
| 152 | return Objects.equals(this.prefix, that.prefix) && |
| 153 | Objects.equals(this.nextHop, that.nextHop); |
| 154 | } |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 155 | |
| 156 | @Override |
| 157 | public String toString() { |
| 158 | return toStringHelper(this) |
| 159 | .add("prefix", prefix) |
| 160 | .add("nextHop", nextHop) |
| 161 | .toString(); |
| 162 | } |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 163 | } |