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