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