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 | /** |
Kalhee Kim | b908212 | 2018-03-29 18:57:21 +0000 | [diff] [blame] | 54 | * Route came from the RIP route source. |
| 55 | */ |
| 56 | RIP, |
| 57 | |
| 58 | /** |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 59 | * Route can from the static route source. |
| 60 | */ |
| 61 | STATIC, |
| 62 | |
| 63 | /** |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 64 | * Route can from the DHCP route source. |
| 65 | */ |
| 66 | DHCP, |
| 67 | |
| 68 | /** |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 69 | * Route source was not defined. |
| 70 | */ |
psneha | 36b60f9 | 2018-08-29 08:56:08 -0400 | [diff] [blame] | 71 | UNDEFINED, |
| 72 | |
| 73 | /** |
| 74 | * Route can from the DHCP-LQ route source. |
| 75 | */ |
| 76 | DHCPLQ |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private final Source source; |
| 80 | private final IpPrefix prefix; |
| 81 | private final IpAddress nextHop; |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 82 | private final NodeId sourceNode; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Creates a route. |
| 86 | * |
| 87 | * @param source route source |
| 88 | * @param prefix IP prefix |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 89 | * @param nextHop next hop IP address |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 90 | */ |
| 91 | public Route(Source source, IpPrefix prefix, IpAddress nextHop) { |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 92 | this(source, prefix, nextHop, UNDEFINED); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a route. |
| 97 | * |
| 98 | * @param source route source |
| 99 | * @param prefix IP prefix |
| 100 | * @param nextHop next hop IP address |
| 101 | * @param sourceNode ONOS node the route was sourced from |
| 102 | */ |
| 103 | public Route(Source source, IpPrefix prefix, IpAddress nextHop, NodeId sourceNode) { |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 104 | checkNotNull(prefix); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 105 | checkNotNull(nextHop); |
| 106 | checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 107 | |
| 108 | this.source = checkNotNull(source); |
| 109 | this.prefix = prefix; |
| 110 | this.nextHop = nextHop; |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 111 | this.sourceNode = checkNotNull(sourceNode); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the route source. |
| 116 | * |
| 117 | * @return route source |
| 118 | */ |
| 119 | public Source source() { |
| 120 | return source; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the IP prefix of the route. |
| 125 | * |
| 126 | * @return IP prefix |
| 127 | */ |
| 128 | public IpPrefix prefix() { |
| 129 | return prefix; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the next hop IP address. |
| 134 | * |
| 135 | * @return next hop |
| 136 | */ |
| 137 | public IpAddress nextHop() { |
| 138 | return nextHop; |
| 139 | } |
| 140 | |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Returns the ONOS node the route was sourced from. |
| 143 | * |
| 144 | * @return ONOS node ID |
| 145 | */ |
| 146 | public NodeId sourceNode() { |
| 147 | return sourceNode; |
| 148 | } |
| 149 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public int hashCode() { |
| 152 | return Objects.hash(prefix, nextHop); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object other) { |
| 157 | if (this == other) { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | if (!(other instanceof Route)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | Route that = (Route) other; |
| 166 | |
| 167 | return Objects.equals(this.prefix, that.prefix) && |
| 168 | Objects.equals(this.nextHop, that.nextHop); |
| 169 | } |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 170 | |
| 171 | @Override |
| 172 | public String toString() { |
| 173 | return toStringHelper(this) |
| 174 | .add("prefix", prefix) |
| 175 | .add("nextHop", nextHop) |
| 176 | .toString(); |
| 177 | } |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 178 | } |