blob: 555634ed6005ecb8d1101224e1d31cead41f91d8 [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
Mohammad Shahidaa7c1232017-08-09 11:13:15 +053065 * @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 Shahidaa7c1232017-08-09 11:13:15 +0530106 */
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.
118 checkNotNull(labelToInt);
119 this.source = checkNotNull(source);
120 this.prefix = prefix;
121 this.prefixMac = prefixMac;
122 this.nextHop = nextHop;
123 this.rd = RouteDistinguisher.routeDistinguisher(rdToString);
124 this.importRtList = importRtList;
125 this.exportRtList = exportRtList;
126 this.label = Label.label(labelToInt);
127 }
128
129 /**
130 * Returns the route source.
131 *
132 * @return route source
133 */
134 public Source source() {
135 return source;
136 }
137
138 /**
139 * Returns the address.
140 *
141 * @return MacAddress
142 */
143 public MacAddress prefixMac() {
144 return prefixMac;
145 }
146
147 /**
148 * Returns the IPv4 address.
149 *
150 * @return Ip4Address
151 */
152 public IpPrefix prefixIp() {
153 return prefix;
154 }
155
156 /**
157 * Returns the IPv4 address.
158 *
159 * @return Ip4Address
160 */
161 public EvpnPrefix evpnPrefix() {
162 return new EvpnPrefix(rd, prefixMac,
163 prefix);
164 }
165
166
167 /**
168 * Returns the next hop IP address.
169 *
170 * @return Ip4Address
171 */
172 public IpAddress ipNextHop() {
173 return nextHop;
174 }
175
176 public EvpnNextHop nextHop() {
177 return EvpnNextHop.evpnNextHop(nextHop,
178 importRtList,
179 exportRtList,
180 label);
181 }
182
183 /**
184 * Returns the routeDistinguisher.
185 *
186 * @return RouteDistinguisher
187 */
188 public RouteDistinguisher routeDistinguisher() {
189 return rd;
190 }
191
192 /**
193 * Returns the Route targets.
194 *
195 * @return RouteTarget List
196 */
197
198 public List<VpnRouteTarget> importRouteTarget() {
199 return importRtList;
200 }
201
202 /**
203 * Returns the Route targets.
204 *
205 * @return RouteTarget List
206 */
207 public List<VpnRouteTarget> exportRouteTarget() {
208 return exportRtList;
209 }
210
211 /**
212 * Set import list.
213 *
214 * @param importRtList import list
215 */
216 public void setImportRtList(List<VpnRouteTarget> importRtList) {
217 this.importRtList = importRtList;
218 }
219
220 /**
221 * Set export list.
222 *
223 * @param exportRtList export list
224 */
225 public void setExportRtList(List<VpnRouteTarget> exportRtList) {
226 this.exportRtList = exportRtList;
227 }
228
229 /**
230 * Returns the label.
231 *
232 * @return Label
233 */
234 public Label label() {
235 return label;
236 }
237
238 @Override
239 public int hashCode() {
240 return Objects.hash(prefixMac,
241 prefix,
242 nextHop,
243 rd,
244 importRtList,
245 exportRtList,
246 label);
247 }
248
249 @Override
250 public boolean equals(Object other) {
251 if (this == other) {
252 return true;
253 }
254
255 if (!(other instanceof EvpnRoute)) {
256 return false;
257 }
258
259 EvpnRoute that = (EvpnRoute) other;
260
261 return Objects.equals(prefixMac, prefixMac)
262 && Objects.equals(prefix, that.prefix)
263 && Objects.equals(nextHop, that.nextHop)
264 && Objects.equals(this.rd, that.rd)
265 && Objects.equals(this.importRtList, that.importRtList)
266 && Objects.equals(this.exportRtList, that.exportRtList)
267 && Objects.equals(this.label, that.label);
268 }
269
270 @Override
271 public String toString() {
272 return toStringHelper(this)
273 .add("prefixMac", prefixMac)
274 .add("prefix", prefix)
275 .add("nextHop", nextHop)
276 .add("rd", this.rd)
277 .add("import rt", this.importRtList)
278 .add("export rt", this.exportRtList)
279 .add("label", this.label)
280 .toString();
281 }
282}