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