Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
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 | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 16 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.bgp; |
| 18 | |
| 19 | import org.hamcrest.Matchers; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import java.util.ArrayList; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 23 | |
| 24 | import static org.hamcrest.Matchers.is; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertThat; |
| 26 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Unit tests for the BgpRouteEntry.AsPath class. |
| 29 | */ |
| 30 | public class AsPathTest { |
| 31 | /** |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 32 | * Generates Path Segments. |
| 33 | * |
| 34 | * @return the generated Path Segments |
| 35 | */ |
| 36 | private ArrayList<BgpRouteEntry.PathSegment> generatePathSegments() { |
| 37 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
| 38 | byte pathSegmentType; |
| 39 | ArrayList<Long> segmentAsNumbers; |
| 40 | BgpRouteEntry.PathSegment pathSegment; |
| 41 | |
| 42 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_CONFED_SEQUENCE; |
| 43 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 44 | segmentAsNumbers.add(1L); |
| 45 | segmentAsNumbers.add(2L); |
| 46 | segmentAsNumbers.add(3L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 47 | pathSegment = |
| 48 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 49 | pathSegments.add(pathSegment); |
| 50 | // |
| 51 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_CONFED_SET; |
| 52 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 53 | segmentAsNumbers.add(4L); |
| 54 | segmentAsNumbers.add(5L); |
| 55 | segmentAsNumbers.add(6L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 56 | pathSegment = |
| 57 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 58 | pathSegments.add(pathSegment); |
| 59 | // |
| 60 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 61 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 62 | segmentAsNumbers.add(7L); |
| 63 | segmentAsNumbers.add(8L); |
| 64 | segmentAsNumbers.add(9L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 65 | pathSegment = |
| 66 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 67 | pathSegments.add(pathSegment); |
| 68 | // |
| 69 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SET; |
| 70 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 71 | segmentAsNumbers.add(10L); |
| 72 | segmentAsNumbers.add(11L); |
| 73 | segmentAsNumbers.add(12L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 74 | pathSegment = |
| 75 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 76 | pathSegments.add(pathSegment); |
| 77 | |
| 78 | return pathSegments; |
| 79 | } |
| 80 | |
| 81 | /** |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 82 | * Generates an AS Path. |
| 83 | * |
| 84 | * @return a generated AS Path |
| 85 | */ |
| 86 | private BgpRouteEntry.AsPath generateAsPath() { |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 87 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = |
| 88 | generatePathSegments(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 89 | BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments); |
| 90 | |
| 91 | return asPath; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests valid class constructor. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testConstructor() { |
| 99 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 100 | |
| 101 | String expectedString = |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 102 | "AsPath{pathSegments=[" + |
| 103 | "PathSegment{type=AS_CONFED_SEQUENCE, segmentAsNumbers=[1, 2, 3]}, " + |
| 104 | "PathSegment{type=AS_CONFED_SET, segmentAsNumbers=[4, 5, 6]}, " + |
| 105 | "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[7, 8, 9]}, " + |
| 106 | "PathSegment{type=AS_SET, segmentAsNumbers=[10, 11, 12]}]}"; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 107 | assertThat(asPath.toString(), is(expectedString)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests invalid class constructor for null Path Segments. |
| 112 | */ |
| 113 | @Test(expected = NullPointerException.class) |
| 114 | public void testInvalidConstructorNullPathSegments() { |
| 115 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = null; |
| 116 | new BgpRouteEntry.AsPath(pathSegments); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests getting the fields of an AS Path. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testGetFields() { |
| 124 | // Create the fields to compare against |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 125 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = |
| 126 | generatePathSegments(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 127 | |
| 128 | // Generate the entry to test |
| 129 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 130 | |
| 131 | assertThat(asPath.getPathSegments(), is(pathSegments)); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests getting the AS Path Length. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testGetAsPathLength() { |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 139 | // |
| 140 | // NOTE: |
| 141 | // - AS_CONFED_SEQUENCE and AS_CONFED_SET are excluded |
| 142 | // - AS_SET counts as a single hop |
| 143 | // |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 144 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 145 | assertThat(asPath.getAsPathLength(), is(4)); |
| 146 | |
| 147 | // Create an empty AS Path |
| 148 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
| 149 | asPath = new BgpRouteEntry.AsPath(pathSegments); |
| 150 | assertThat(asPath.getAsPathLength(), is(0)); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Tests equality of {@link BgpRouteEntry.AsPath}. |
| 155 | */ |
| 156 | @Test |
| 157 | public void testEquality() { |
| 158 | BgpRouteEntry.AsPath asPath1 = generateAsPath(); |
| 159 | BgpRouteEntry.AsPath asPath2 = generateAsPath(); |
| 160 | |
| 161 | assertThat(asPath1, is(asPath2)); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Tests non-equality of {@link BgpRouteEntry.AsPath}. |
| 166 | */ |
| 167 | @Test |
| 168 | public void testNonEquality() { |
| 169 | BgpRouteEntry.AsPath asPath1 = generateAsPath(); |
| 170 | |
| 171 | // Setup AS Path 2 |
| 172 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 173 | byte pathSegmentType; |
| 174 | ArrayList<Long> segmentAsNumbers; |
| 175 | BgpRouteEntry.PathSegment pathSegment; |
| 176 | |
| 177 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_CONFED_SEQUENCE; |
| 178 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 179 | segmentAsNumbers.add(1L); |
| 180 | segmentAsNumbers.add(2L); |
| 181 | segmentAsNumbers.add(3L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 182 | pathSegment = |
| 183 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 184 | pathSegments.add(pathSegment); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 185 | // |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 186 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_CONFED_SET; |
| 187 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 188 | segmentAsNumbers.add(4L); |
| 189 | segmentAsNumbers.add(5L); |
| 190 | segmentAsNumbers.add(6L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 191 | pathSegment = |
| 192 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 193 | pathSegments.add(pathSegment); |
| 194 | // |
| 195 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 196 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 197 | segmentAsNumbers.add(7L); |
| 198 | segmentAsNumbers.add(8L); |
| 199 | segmentAsNumbers.add(9L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 200 | pathSegment = |
| 201 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 202 | pathSegments.add(pathSegment); |
| 203 | // |
| 204 | pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SET; |
| 205 | segmentAsNumbers = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 206 | segmentAsNumbers.add(10L); |
| 207 | segmentAsNumbers.add(111L); // Different |
| 208 | segmentAsNumbers.add(12L); |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 209 | pathSegment = |
| 210 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 211 | pathSegments.add(pathSegment); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 212 | // |
| 213 | BgpRouteEntry.AsPath asPath2 = new BgpRouteEntry.AsPath(pathSegments); |
| 214 | |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 215 | assertThat(asPath1, Matchers.is(Matchers.not(asPath2))); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Tests object string representation. |
| 220 | */ |
| 221 | @Test |
| 222 | public void testToString() { |
| 223 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 224 | |
| 225 | String expectedString = |
Pavlin Radoslavov | 49eb64d | 2014-11-10 17:03:19 -0800 | [diff] [blame] | 226 | "AsPath{pathSegments=[" + |
| 227 | "PathSegment{type=AS_CONFED_SEQUENCE, segmentAsNumbers=[1, 2, 3]}, " + |
| 228 | "PathSegment{type=AS_CONFED_SET, segmentAsNumbers=[4, 5, 6]}, " + |
| 229 | "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[7, 8, 9]}, " + |
| 230 | "PathSegment{type=AS_SET, segmentAsNumbers=[10, 11, 12]}]}"; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 231 | assertThat(asPath.toString(), is(expectedString)); |
| 232 | } |
| 233 | } |