Mohammad Shahid | aa7c123 | 2017-08-09 11:13:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
Ray Milkey | a95193c | 2017-08-10 15:35:36 -0700 | [diff] [blame] | 17 | package org.onosproject.evpnrouteservice; |
| 18 | |
| 19 | import java.util.List; |
| 20 | import java.util.Objects; |
Mohammad Shahid | aa7c123 | 2017-08-09 11:13:15 +0530 | [diff] [blame] | 21 | |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | |
Mohammad Shahid | aa7c123 | 2017-08-09 11:13:15 +0530 | [diff] [blame] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Represents a evpn route. |
| 31 | */ |
| 32 | public class EvpnRoute { |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Source of the route. |
| 37 | */ |
| 38 | public enum Source { |
| 39 | /** |
| 40 | * Route came from app source. |
| 41 | */ |
| 42 | LOCAL, |
| 43 | |
| 44 | /** |
| 45 | * Route came from remote bgp peer source. |
| 46 | */ |
| 47 | REMOTE, |
| 48 | } |
| 49 | |
| 50 | private final Source source; |
| 51 | private final MacAddress prefixMac; |
| 52 | private final IpPrefix prefix; |
| 53 | private final IpAddress nextHop; |
| 54 | private final RouteDistinguisher rd; |
| 55 | private List<VpnRouteTarget> importRtList; |
| 56 | private List<VpnRouteTarget> exportRtList; |
| 57 | private final Label label; |
| 58 | |
| 59 | /** |
| 60 | * Constructor to initialize the parameters. |
| 61 | * |
| 62 | * @param source route source |
| 63 | * @param prefixMac mac address |
| 64 | * @param prefix ip address |
Mohammad Shahid | aa7c123 | 2017-08-09 11:13:15 +0530 | [diff] [blame] | 65 | * @param nextHop evpn nexthop |
| 66 | * @param rd route distinguisher |
| 67 | * @param importRtList import route targets |
| 68 | * @param exportRtList export route targets |
| 69 | * @param label evpn route label |
| 70 | */ |
| 71 | public EvpnRoute(Source source, |
| 72 | MacAddress prefixMac, |
| 73 | IpPrefix prefix, |
| 74 | IpAddress nextHop, |
| 75 | RouteDistinguisher rd, |
| 76 | List<VpnRouteTarget> importRtList, |
| 77 | List<VpnRouteTarget> exportRtList, |
| 78 | Label label) { |
| 79 | |
| 80 | checkNotNull(prefixMac); |
| 81 | checkNotNull(prefix); |
| 82 | //checkNotNull(nextHop);//next hop can be null in case of MP un reach. |
| 83 | checkNotNull(rd); |
| 84 | checkNotNull(label); |
| 85 | this.source = checkNotNull(source); |
| 86 | this.prefix = prefix; |
| 87 | this.prefixMac = prefixMac; |
| 88 | this.nextHop = nextHop; |
| 89 | this.rd = rd; |
| 90 | this.importRtList = importRtList; |
| 91 | this.exportRtList = exportRtList; |
| 92 | this.label = label; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Constructor to initialize the parameters. |
| 97 | * |
| 98 | * @param source route source |
| 99 | * @param prefixMac mac address |
| 100 | * @param prefix ip address |
| 101 | * @param nextHop evpn nexthop |
| 102 | * @param rdToString route distinguisher |
| 103 | * @param importRtList import route targets |
| 104 | * @param exportRtList export route targets |
| 105 | * @param labelToInt evpn route label |
Mohammad Shahid | aa7c123 | 2017-08-09 11:13:15 +0530 | [diff] [blame] | 106 | */ |
| 107 | public EvpnRoute(Source source, |
| 108 | MacAddress prefixMac, |
| 109 | IpPrefix prefix, |
| 110 | IpAddress nextHop, |
| 111 | String rdToString, |
| 112 | List<VpnRouteTarget> importRtList, |
| 113 | List<VpnRouteTarget> exportRtList, |
| 114 | int labelToInt) { |
| 115 | checkNotNull(prefixMac); |
| 116 | checkNotNull(prefix); |
| 117 | //checkNotNull(nextHop); //next hop can be null in case of MP un reach. |
Mohammad Shahid | aa7c123 | 2017-08-09 11:13:15 +0530 | [diff] [blame] | 118 | this.source = checkNotNull(source); |
| 119 | this.prefix = prefix; |
| 120 | this.prefixMac = prefixMac; |
| 121 | this.nextHop = nextHop; |
| 122 | this.rd = RouteDistinguisher.routeDistinguisher(rdToString); |
| 123 | this.importRtList = importRtList; |
| 124 | this.exportRtList = exportRtList; |
| 125 | this.label = Label.label(labelToInt); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the route source. |
| 130 | * |
| 131 | * @return route source |
| 132 | */ |
| 133 | public Source source() { |
| 134 | return source; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns the address. |
| 139 | * |
| 140 | * @return MacAddress |
| 141 | */ |
| 142 | public MacAddress prefixMac() { |
| 143 | return prefixMac; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the IPv4 address. |
| 148 | * |
| 149 | * @return Ip4Address |
| 150 | */ |
| 151 | public IpPrefix prefixIp() { |
| 152 | return prefix; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the IPv4 address. |
| 157 | * |
| 158 | * @return Ip4Address |
| 159 | */ |
| 160 | public EvpnPrefix evpnPrefix() { |
| 161 | return new EvpnPrefix(rd, prefixMac, |
| 162 | prefix); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Returns the next hop IP address. |
| 168 | * |
| 169 | * @return Ip4Address |
| 170 | */ |
| 171 | public IpAddress ipNextHop() { |
| 172 | return nextHop; |
| 173 | } |
| 174 | |
| 175 | public EvpnNextHop nextHop() { |
| 176 | return EvpnNextHop.evpnNextHop(nextHop, |
| 177 | importRtList, |
| 178 | exportRtList, |
| 179 | label); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns the routeDistinguisher. |
| 184 | * |
| 185 | * @return RouteDistinguisher |
| 186 | */ |
| 187 | public RouteDistinguisher routeDistinguisher() { |
| 188 | return rd; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns the Route targets. |
| 193 | * |
| 194 | * @return RouteTarget List |
| 195 | */ |
| 196 | |
| 197 | public List<VpnRouteTarget> importRouteTarget() { |
| 198 | return importRtList; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns the Route targets. |
| 203 | * |
| 204 | * @return RouteTarget List |
| 205 | */ |
| 206 | public List<VpnRouteTarget> exportRouteTarget() { |
| 207 | return exportRtList; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Set import list. |
| 212 | * |
| 213 | * @param importRtList import list |
| 214 | */ |
| 215 | public void setImportRtList(List<VpnRouteTarget> importRtList) { |
| 216 | this.importRtList = importRtList; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Set export list. |
| 221 | * |
| 222 | * @param exportRtList export list |
| 223 | */ |
| 224 | public void setExportRtList(List<VpnRouteTarget> exportRtList) { |
| 225 | this.exportRtList = exportRtList; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns the label. |
| 230 | * |
| 231 | * @return Label |
| 232 | */ |
| 233 | public Label label() { |
| 234 | return label; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public int hashCode() { |
| 239 | return Objects.hash(prefixMac, |
| 240 | prefix, |
| 241 | nextHop, |
| 242 | rd, |
| 243 | importRtList, |
| 244 | exportRtList, |
| 245 | label); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public boolean equals(Object other) { |
| 250 | if (this == other) { |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | if (!(other instanceof EvpnRoute)) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | EvpnRoute that = (EvpnRoute) other; |
| 259 | |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 260 | return Objects.equals(prefixMac, that.prefixMac) |
Mohammad Shahid | aa7c123 | 2017-08-09 11:13:15 +0530 | [diff] [blame] | 261 | && Objects.equals(prefix, that.prefix) |
| 262 | && Objects.equals(nextHop, that.nextHop) |
| 263 | && Objects.equals(this.rd, that.rd) |
| 264 | && Objects.equals(this.importRtList, that.importRtList) |
| 265 | && Objects.equals(this.exportRtList, that.exportRtList) |
| 266 | && Objects.equals(this.label, that.label); |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public String toString() { |
| 271 | return toStringHelper(this) |
| 272 | .add("prefixMac", prefixMac) |
| 273 | .add("prefix", prefix) |
| 274 | .add("nextHop", nextHop) |
| 275 | .add("rd", this.rd) |
| 276 | .add("import rt", this.importRtList) |
| 277 | .add("export rt", this.exportRtList) |
| 278 | .add("label", this.label) |
| 279 | .toString(); |
| 280 | } |
| 281 | } |