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