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