blob: d53eb59c7766f716b2f59a8f2b50b03a9a34a88d [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip.bgp;
Jonathan Hart20d8e512014-10-16 11:05:52 -070017
18import static org.easymock.EasyMock.createMock;
19import static org.easymock.EasyMock.expect;
20import static org.easymock.EasyMock.replay;
21import static org.hamcrest.Matchers.is;
22import static org.hamcrest.Matchers.not;
23import static org.junit.Assert.assertThat;
24
25import java.util.ArrayList;
26
27import org.junit.Before;
28import org.junit.Test;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080029import org.onlab.packet.Ip4Address;
30import org.onlab.packet.Ip4Prefix;
Jonathan Hart20d8e512014-10-16 11:05:52 -070031
32/**
33 * Unit tests for the BgpRouteEntry class.
34 */
35public class BgpRouteEntryTest {
36 private BgpSession bgpSession;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080037 private static final Ip4Address BGP_SESSION_BGP_ID =
38 Ip4Address.valueOf("10.0.0.1");
39 private static final Ip4Address BGP_SESSION_IP_ADDRESS =
40 Ip4Address.valueOf("20.0.0.1");
Jonathan Hart20d8e512014-10-16 11:05:52 -070041
42 private BgpSession bgpSession2;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080043 private static final Ip4Address BGP_SESSION_BGP_ID2 =
44 Ip4Address.valueOf("10.0.0.2");
45 private static final Ip4Address BGP_SESSION_IP_ADDRESS2 =
46 Ip4Address.valueOf("20.0.0.1");
Jonathan Hart20d8e512014-10-16 11:05:52 -070047
48 private BgpSession bgpSession3;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080049 private static final Ip4Address BGP_SESSION_BGP_ID3 =
50 Ip4Address.valueOf("10.0.0.1");
51 private static final Ip4Address BGP_SESSION_IP_ADDRESS3 =
52 Ip4Address.valueOf("20.0.0.2");
Jonathan Hart20d8e512014-10-16 11:05:52 -070053
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080054 private final BgpSessionInfo localInfo = new BgpSessionInfo();
55 private final BgpSessionInfo remoteInfo = new BgpSessionInfo();
56
57 private final BgpSessionInfo localInfo2 = new BgpSessionInfo();
58 private final BgpSessionInfo remoteInfo2 = new BgpSessionInfo();
59
60 private final BgpSessionInfo localInfo3 = new BgpSessionInfo();
61 private final BgpSessionInfo remoteInfo3 = new BgpSessionInfo();
62
Jonathan Hart20d8e512014-10-16 11:05:52 -070063 @Before
64 public void setUp() throws Exception {
65 // Mock objects for testing
66 bgpSession = createMock(BgpSession.class);
67 bgpSession2 = createMock(BgpSession.class);
68 bgpSession3 = createMock(BgpSession.class);
69
70 // Setup the BGP Sessions
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080071 remoteInfo.setIp4Address(BGP_SESSION_IP_ADDRESS);
72 remoteInfo2.setIp4Address(BGP_SESSION_IP_ADDRESS2);
73 remoteInfo3.setIp4Address(BGP_SESSION_IP_ADDRESS3);
74 remoteInfo.setBgpId(BGP_SESSION_BGP_ID);
75 remoteInfo2.setBgpId(BGP_SESSION_BGP_ID2);
76 remoteInfo3.setBgpId(BGP_SESSION_BGP_ID3);
77
78 expect(bgpSession.localInfo()).andReturn(localInfo).anyTimes();
79 expect(bgpSession.remoteInfo()).andReturn(remoteInfo).anyTimes();
80 expect(bgpSession2.localInfo()).andReturn(localInfo2).anyTimes();
81 expect(bgpSession2.remoteInfo()).andReturn(remoteInfo2).anyTimes();
82 expect(bgpSession3.localInfo()).andReturn(localInfo3).anyTimes();
83 expect(bgpSession3.remoteInfo()).andReturn(remoteInfo3).anyTimes();
Jonathan Hart20d8e512014-10-16 11:05:52 -070084
85 replay(bgpSession);
86 replay(bgpSession2);
87 replay(bgpSession3);
88 }
89
90 /**
91 * Generates a BGP Route Entry.
92 *
93 * @return a generated BGP Route Entry
94 */
95 private BgpRouteEntry generateBgpRouteEntry() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080096 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
97 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -070098 byte origin = BgpConstants.Update.Origin.IGP;
99 // Setup the AS Path
100 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
101 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
102 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
103 segmentAsNumbers1.add((long) 1);
104 segmentAsNumbers1.add((long) 2);
105 segmentAsNumbers1.add((long) 3);
106 BgpRouteEntry.PathSegment pathSegment1 =
107 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
108 pathSegments.add(pathSegment1);
109 //
110 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
111 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
112 segmentAsNumbers2.add((long) 4);
113 segmentAsNumbers2.add((long) 5);
114 segmentAsNumbers2.add((long) 6);
115 BgpRouteEntry.PathSegment pathSegment2 =
116 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
117 pathSegments.add(pathSegment2);
118 //
119 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
120 //
121 long localPref = 100;
122 long multiExitDisc = 20;
123
124 BgpRouteEntry bgpRouteEntry =
125 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
126 localPref);
127 bgpRouteEntry.setMultiExitDisc(multiExitDisc);
128
129 return bgpRouteEntry;
130 }
131
132 /**
133 * Tests valid class constructor.
134 */
135 @Test
136 public void testConstructor() {
137 BgpRouteEntry bgpRouteEntry = generateBgpRouteEntry();
138
139 String expectedString =
140 "BgpRouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8, " +
Pavlin Radoslavove6015262014-11-07 13:08:53 -0800141 "bgpId=10.0.0.1, origin=IGP, asPath=AsPath{pathSegments=" +
142 "[PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}, " +
143 "PathSegment{type=AS_SET, segmentAsNumbers=[4, 5, 6]}]}, " +
Jonathan Hart20d8e512014-10-16 11:05:52 -0700144 "localPref=100, multiExitDisc=20}";
145 assertThat(bgpRouteEntry.toString(), is(expectedString));
146 }
147
148 /**
149 * Tests invalid class constructor for null BGP Session.
150 */
151 @Test(expected = NullPointerException.class)
152 public void testInvalidConstructorNullBgpSession() {
153 BgpSession bgpSessionNull = null;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800154 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
155 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -0700156 byte origin = BgpConstants.Update.Origin.IGP;
157 // Setup the AS Path
158 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
159 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
160 //
161 long localPref = 100;
162
163 new BgpRouteEntry(bgpSessionNull, prefix, nextHop, origin, asPath,
164 localPref);
165 }
166
167 /**
168 * Tests invalid class constructor for null AS Path.
169 */
170 @Test(expected = NullPointerException.class)
171 public void testInvalidConstructorNullAsPath() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800172 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
173 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -0700174 byte origin = BgpConstants.Update.Origin.IGP;
175 BgpRouteEntry.AsPath asPath = null;
176 long localPref = 100;
177
178 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
179 localPref);
180 }
181
182 /**
183 * Tests getting the fields of a BGP route entry.
184 */
185 @Test
186 public void testGetFields() {
187 // Create the fields to compare against
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800188 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
189 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -0700190 byte origin = BgpConstants.Update.Origin.IGP;
191 // Setup the AS Path
192 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
193 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
194 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
195 segmentAsNumbers1.add((long) 1);
196 segmentAsNumbers1.add((long) 2);
197 segmentAsNumbers1.add((long) 3);
198 BgpRouteEntry.PathSegment pathSegment1 =
199 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
200 pathSegments.add(pathSegment1);
201 //
202 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
203 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
204 segmentAsNumbers2.add((long) 4);
205 segmentAsNumbers2.add((long) 5);
206 segmentAsNumbers2.add((long) 6);
207 BgpRouteEntry.PathSegment pathSegment2 =
208 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
209 pathSegments.add(pathSegment2);
210 //
211 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
212 //
213 long localPref = 100;
214 long multiExitDisc = 20;
215
216 // Generate the entry to test
217 BgpRouteEntry bgpRouteEntry = generateBgpRouteEntry();
218
219 assertThat(bgpRouteEntry.prefix(), is(prefix));
220 assertThat(bgpRouteEntry.nextHop(), is(nextHop));
221 assertThat(bgpRouteEntry.getBgpSession(), is(bgpSession));
222 assertThat(bgpRouteEntry.getOrigin(), is(origin));
223 assertThat(bgpRouteEntry.getAsPath(), is(asPath));
224 assertThat(bgpRouteEntry.getLocalPref(), is(localPref));
225 assertThat(bgpRouteEntry.getMultiExitDisc(), is(multiExitDisc));
226 }
227
228 /**
229 * Tests whether a BGP route entry is a local route.
230 */
231 @Test
232 public void testIsLocalRoute() {
233 //
234 // Test non-local route
235 //
236 BgpRouteEntry bgpRouteEntry = generateBgpRouteEntry();
237 assertThat(bgpRouteEntry.isLocalRoute(), is(false));
238
239 //
240 // Test local route with AS Path that begins with AS_SET
241 //
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800242 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
243 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -0700244 byte origin = BgpConstants.Update.Origin.IGP;
245 // Setup the AS Path
246 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
247 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SET;
248 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
249 segmentAsNumbers1.add((long) 1);
250 segmentAsNumbers1.add((long) 2);
251 segmentAsNumbers1.add((long) 3);
252 BgpRouteEntry.PathSegment pathSegment1 =
253 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
254 pathSegments.add(pathSegment1);
255 //
256 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
257 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
258 segmentAsNumbers2.add((long) 4);
259 segmentAsNumbers2.add((long) 5);
260 segmentAsNumbers2.add((long) 6);
261 BgpRouteEntry.PathSegment pathSegment2 =
262 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
263 pathSegments.add(pathSegment2);
264 //
265 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
266 //
267 long localPref = 100;
268 long multiExitDisc = 20;
269 //
270 bgpRouteEntry =
271 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
272 localPref);
273 bgpRouteEntry.setMultiExitDisc(multiExitDisc);
274 assertThat(bgpRouteEntry.isLocalRoute(), is(true));
275
276 //
277 // Test local route with empty AS Path
278 //
279 pathSegments = new ArrayList<>();
280 asPath = new BgpRouteEntry.AsPath(pathSegments);
281 bgpRouteEntry =
282 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
283 localPref);
284 bgpRouteEntry.setMultiExitDisc(multiExitDisc);
285 assertThat(bgpRouteEntry.isLocalRoute(), is(true));
286 }
287
288 /**
289 * Tests getting the BGP Neighbor AS number for a route.
290 */
291 @Test
292 public void testGetNeighborAs() {
293 //
294 // Get neighbor AS for non-local route
295 //
296 BgpRouteEntry bgpRouteEntry = generateBgpRouteEntry();
297 assertThat(bgpRouteEntry.getNeighborAs(), is((long) 1));
298
299 //
300 // Get neighbor AS for a local route
301 //
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800302 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
303 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -0700304 byte origin = BgpConstants.Update.Origin.IGP;
305 // Setup the AS Path
306 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
307 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
308 //
309 long localPref = 100;
310 long multiExitDisc = 20;
311 //
312 bgpRouteEntry =
313 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
314 localPref);
315 bgpRouteEntry.setMultiExitDisc(multiExitDisc);
316 assertThat(bgpRouteEntry.getNeighborAs(), is(BgpConstants.BGP_AS_0));
317 }
318
319 /**
320 * Tests whether a BGP route entry has AS Path loop.
321 */
322 @Test
323 public void testHasAsPathLoop() {
324 BgpRouteEntry bgpRouteEntry = generateBgpRouteEntry();
325
326 // Test for loops: test each AS number in the interval [1, 6]
327 for (int i = 1; i <= 6; i++) {
328 assertThat(bgpRouteEntry.hasAsPathLoop(i), is(true));
329 }
330
331 // Test for non-loops
332 assertThat(bgpRouteEntry.hasAsPathLoop(500), is(false));
333 }
334
335 /**
336 * Tests the BGP Decision Process comparison of BGP routes.
337 */
338 @Test
339 public void testBgpDecisionProcessComparison() {
340 BgpRouteEntry bgpRouteEntry1 = generateBgpRouteEntry();
341 BgpRouteEntry bgpRouteEntry2 = generateBgpRouteEntry();
342
343 //
344 // Compare two routes that are same
345 //
346 assertThat(bgpRouteEntry1.isBetterThan(bgpRouteEntry2), is(true));
347 assertThat(bgpRouteEntry2.isBetterThan(bgpRouteEntry1), is(true));
348
349 //
350 // Compare two routes with different LOCAL_PREF
351 //
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800352 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
353 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -0700354 byte origin = BgpConstants.Update.Origin.IGP;
355 // Setup the AS Path
356 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
357 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
358 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
359 segmentAsNumbers1.add((long) 1);
360 segmentAsNumbers1.add((long) 2);
361 segmentAsNumbers1.add((long) 3);
362 BgpRouteEntry.PathSegment pathSegment1 =
363 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
364 pathSegments.add(pathSegment1);
365 //
366 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
367 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
368 segmentAsNumbers2.add((long) 4);
369 segmentAsNumbers2.add((long) 5);
370 segmentAsNumbers2.add((long) 6);
371 BgpRouteEntry.PathSegment pathSegment2 =
372 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
373 pathSegments.add(pathSegment2);
374 //
375 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
376 //
377 long localPref = 50; // Different
378 long multiExitDisc = 20;
379 bgpRouteEntry2 =
380 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
381 localPref);
382 bgpRouteEntry2.setMultiExitDisc(multiExitDisc);
383 //
384 assertThat(bgpRouteEntry1.isBetterThan(bgpRouteEntry2), is(true));
385 assertThat(bgpRouteEntry2.isBetterThan(bgpRouteEntry1), is(false));
386 localPref = bgpRouteEntry1.getLocalPref(); // Restore
387
388 //
389 // Compare two routes with different AS_PATH length
390 //
391 ArrayList<BgpRouteEntry.PathSegment> pathSegments2 = new ArrayList<>();
392 pathSegments2.add(pathSegment1);
393 // Different AS Path
394 BgpRouteEntry.AsPath asPath2 = new BgpRouteEntry.AsPath(pathSegments2);
395 bgpRouteEntry2 =
396 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath2,
397 localPref);
398 bgpRouteEntry2.setMultiExitDisc(multiExitDisc);
399 //
400 assertThat(bgpRouteEntry1.isBetterThan(bgpRouteEntry2), is(false));
401 assertThat(bgpRouteEntry2.isBetterThan(bgpRouteEntry1), is(true));
402
403 //
404 // Compare two routes with different ORIGIN
405 //
406 origin = BgpConstants.Update.Origin.EGP; // Different
407 bgpRouteEntry2 =
408 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
409 localPref);
410 bgpRouteEntry2.setMultiExitDisc(multiExitDisc);
411 //
412 assertThat(bgpRouteEntry1.isBetterThan(bgpRouteEntry2), is(true));
413 assertThat(bgpRouteEntry2.isBetterThan(bgpRouteEntry1), is(false));
414 origin = bgpRouteEntry1.getOrigin(); // Restore
415
416 //
417 // Compare two routes with different MULTI_EXIT_DISC
418 //
419 multiExitDisc = 10; // Different
420 bgpRouteEntry2 =
421 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
422 localPref);
423 bgpRouteEntry2.setMultiExitDisc(multiExitDisc);
424 //
425 assertThat(bgpRouteEntry1.isBetterThan(bgpRouteEntry2), is(true));
426 assertThat(bgpRouteEntry2.isBetterThan(bgpRouteEntry1), is(false));
427 multiExitDisc = bgpRouteEntry1.getMultiExitDisc(); // Restore
428
429 //
430 // Compare two routes with different BGP ID
431 //
432 bgpRouteEntry2 =
433 new BgpRouteEntry(bgpSession2, prefix, nextHop, origin, asPath,
434 localPref);
435 bgpRouteEntry2.setMultiExitDisc(multiExitDisc);
436 //
437 assertThat(bgpRouteEntry1.isBetterThan(bgpRouteEntry2), is(true));
438 assertThat(bgpRouteEntry2.isBetterThan(bgpRouteEntry1), is(false));
439
440 //
441 // Compare two routes with different BGP address
442 //
443 bgpRouteEntry2 =
444 new BgpRouteEntry(bgpSession3, prefix, nextHop, origin, asPath,
445 localPref);
446 bgpRouteEntry2.setMultiExitDisc(multiExitDisc);
447 //
448 assertThat(bgpRouteEntry1.isBetterThan(bgpRouteEntry2), is(true));
449 assertThat(bgpRouteEntry2.isBetterThan(bgpRouteEntry1), is(false));
450 }
451
452 /**
453 * Tests equality of {@link BgpRouteEntry}.
454 */
455 @Test
456 public void testEquality() {
457 BgpRouteEntry bgpRouteEntry1 = generateBgpRouteEntry();
458 BgpRouteEntry bgpRouteEntry2 = generateBgpRouteEntry();
459
460 assertThat(bgpRouteEntry1, is(bgpRouteEntry2));
461 }
462
463 /**
464 * Tests non-equality of {@link BgpRouteEntry}.
465 */
466 @Test
467 public void testNonEquality() {
468 BgpRouteEntry bgpRouteEntry1 = generateBgpRouteEntry();
469
470 // Setup BGP Route 2
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800471 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
472 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart20d8e512014-10-16 11:05:52 -0700473 byte origin = BgpConstants.Update.Origin.IGP;
474 // Setup the AS Path
475 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
476 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
477 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
478 segmentAsNumbers1.add((long) 1);
479 segmentAsNumbers1.add((long) 2);
480 segmentAsNumbers1.add((long) 3);
481 BgpRouteEntry.PathSegment pathSegment1 =
482 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
483 pathSegments.add(pathSegment1);
484 //
485 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
486 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
487 segmentAsNumbers2.add((long) 4);
488 segmentAsNumbers2.add((long) 5);
489 segmentAsNumbers2.add((long) 6);
490 BgpRouteEntry.PathSegment pathSegment2 =
491 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
492 pathSegments.add(pathSegment2);
493 //
494 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
495 //
496 long localPref = 500; // Different
497 long multiExitDisc = 20;
498 BgpRouteEntry bgpRouteEntry2 =
499 new BgpRouteEntry(bgpSession, prefix, nextHop, origin, asPath,
500 localPref);
501 bgpRouteEntry2.setMultiExitDisc(multiExitDisc);
502
503 assertThat(bgpRouteEntry1, is(not(bgpRouteEntry2)));
504 }
505
506 /**
507 * Tests object string representation.
508 */
509 @Test
510 public void testToString() {
511 BgpRouteEntry bgpRouteEntry = generateBgpRouteEntry();
512
513 String expectedString =
514 "BgpRouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8, " +
Pavlin Radoslavove6015262014-11-07 13:08:53 -0800515 "bgpId=10.0.0.1, origin=IGP, asPath=AsPath{pathSegments=" +
516 "[PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}, " +
517 "PathSegment{type=AS_SET, segmentAsNumbers=[4, 5, 6]}]}, " +
Jonathan Hart20d8e512014-10-16 11:05:52 -0700518 "localPref=100, multiExitDisc=20}";
519 assertThat(bgpRouteEntry.toString(), is(expectedString));
520 }
521}