blob: 0c82beea1e5937a06335c7702e3a55c6edfac990 [file] [log] [blame]
Priyanka Be25cefe82015-11-28 14:47:23 +05301/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
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
7 *
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.
15 */
16package org.onosproject.controller.impl;
17
Priyanka Be25cefe82015-11-28 14:47:23 +053018import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.IpAddress.Version;
Jonathan Hart51539b82015-10-29 09:53:04 -070023import org.onosproject.bgp.controller.impl.BgpSelectionAlgo;
Priyanka Be25cefe82015-11-28 14:47:23 +053024import org.onosproject.bgpio.exceptions.BgpParseException;
25import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;
26import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
27import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetailsLocalRib;
28import org.onosproject.bgpio.types.AsPath;
29import org.onosproject.bgpio.types.BgpValueType;
30import org.onosproject.bgpio.types.LocalPref;
31import org.onosproject.bgpio.types.Med;
32import org.onosproject.bgpio.types.Origin;
Jonathan Hart51539b82015-10-29 09:53:04 -070033
34import java.util.LinkedList;
35
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.core.Is.is;
Priyanka Be25cefe82015-11-28 14:47:23 +053038
39/**
40 * Test cases for BGP Selection Algorithm.
41 */
42public class BgpSelectionAlgoTest {
43
44 /**
45 * firstPathAttribute and secondPathAttribute has same AS count and firstPathAttribute
46 * has shortest Origin value than secondPathAttribute.
47 */
48 @Test
49 public void selectionAlgoTest1() throws BgpParseException {
50 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
51 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
52 BgpValueType pathAttribute1;
53 //origin with IGP
54 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
55 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
56 buffer.writeBytes(origin);
57 pathAttribute1 = Origin.read(buffer);
58 pathAttributes1.add(pathAttribute1);
59 //AsPath with AS_SEQ with one AS
60 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
61 (byte) 0xea };
62 buffer.writeBytes(asPath);
63 pathAttribute1 = AsPath.read(buffer);
64 pathAttributes1.add(pathAttribute1);
65
66 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
67 int bgpId = 168427777;
Jonathan Hart51539b82015-10-29 09:53:04 -070068 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +053069 boolean isIbgp = false;
70 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
71 attrList1.setIdentifier(0);
72 attrList1.setPathAttribute(pathAttributes1);
73 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
74 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -070075 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +053076
77 peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
78 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
79 BgpValueType pathAttribute2;
80 //origin with INCOMPLETE
81 origin = new byte[] {0x40, 0x01, 0x01, 0x02 };
82 buffer = ChannelBuffers.dynamicBuffer();
83 buffer.writeBytes(origin);
84 pathAttribute2 = Origin.read(buffer);
85 pathAttributes2.add(pathAttribute2);
86 //AsPath with AS_SEQ with one AS
87 asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
88 (byte) 0xe9 };
89 buffer.writeBytes(asPath);
90 pathAttribute2 = AsPath.read(buffer);
91 pathAttributes2.add(pathAttribute2);
92
93 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
94 bgpId = 536936448;
Jonathan Hart51539b82015-10-29 09:53:04 -070095 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +053096 isIbgp = true;
97 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
98 attrList2.setIdentifier(0);
99 attrList2.setPathAttribute(pathAttributes2);
100 attrList2.setProtocolID(ProtocolType.OSPF_V2);
101 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700102 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530103 BgpSelectionAlgo algo = new BgpSelectionAlgo();
104 int result = algo.compare(list1, list2);
105 assertThat(result, is(1));
106 }
107
108 /**
109 * firstPathAttribute has 1 AS count and secondPathAttribute has 2 AS count
110 * and firstPathAttribute has shortest Origin value than secondPathAttribute.
111 */
112 @Test
113 public void selectionAlgoTest2() throws BgpParseException {
114
115 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
116 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
117 BgpValueType pathAttribute1;
118 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
119 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
120 buffer.writeBytes(origin);
121 pathAttribute1 = Origin.read(buffer);
122 pathAttributes1.add(pathAttribute1);
123 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
124 (byte) 0xe9 };
125 buffer.writeBytes(asPath);
126 pathAttribute1 = AsPath.read(buffer);
127 pathAttributes1.add(pathAttribute1);
128
129 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
130 int bgpId = 168427777;
Jonathan Hart51539b82015-10-29 09:53:04 -0700131 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530132 boolean isIbgp = false;
133 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
134 attrList1.setIdentifier(0);
135 attrList1.setPathAttribute(pathAttributes1);
136 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
137 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700138 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530139
140 peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
141 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
142 BgpValueType pathAttribute2;
143 origin = new byte[] {0x40, 0x01, 0x01, 0x02 };
144 buffer = ChannelBuffers.dynamicBuffer();
145 buffer.writeBytes(origin);
146 pathAttribute2 = Origin.read(buffer);
147 pathAttributes2.add(pathAttribute2);
148 asPath = new byte[] {0x40, 0x02, 0x08, 0x02, 0x01, (byte) 0xfd,
149 (byte) 0xea, 0x02, 0x01, (byte) 0xfd, (byte) 0xea };
150 buffer.writeBytes(asPath);
151 pathAttribute2 = AsPath.read(buffer);
152 pathAttributes2.add(pathAttribute2);
153
154 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
155 bgpId = 536936448;
Jonathan Hart51539b82015-10-29 09:53:04 -0700156 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530157 isIbgp = true;
158 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
159 attrList2.setIdentifier(0);
160 attrList2.setPathAttribute(pathAttributes2);
161 attrList2.setProtocolID(ProtocolType.OSPF_V2);
162 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700163 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530164 BgpSelectionAlgo algo = new BgpSelectionAlgo();
165 int result = algo.compare(list1, list2);
166 assertThat(result, is(-1));
167 }
168
169 /**
170 * firstPathAttribute and secondPathAttribute has same AS value
171 * and firstPathAttribute has shortest Origin value than secondPathAttribute.
172 */
173 @Test
174 public void selectionAlgoTest3() throws BgpParseException {
175
176 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
177 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
178 BgpValueType pathAttribute1;
179 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
180 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
181 buffer.writeBytes(origin);
182 pathAttribute1 = Origin.read(buffer);
183 pathAttributes1.add(pathAttribute1);
184 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
185 (byte) 0xe9 };
186 buffer.writeBytes(asPath);
187 pathAttribute1 = AsPath.read(buffer);
188 pathAttributes1.add(pathAttribute1);
189
190 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
191 int bgpId = 168427777;
Jonathan Hart51539b82015-10-29 09:53:04 -0700192 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530193 boolean isIbgp = false;
194 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
195 attrList1.setIdentifier(0);
196 attrList1.setPathAttribute(pathAttributes1);
197 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
198 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700199 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530200
201 peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
202 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
203 BgpValueType pathAttribute2;
204 origin = new byte[] {0x40, 0x01, 0x01, 0x02 };
205 buffer = ChannelBuffers.dynamicBuffer();
206 buffer.writeBytes(origin);
207 pathAttribute2 = Origin.read(buffer);
208 pathAttributes2.add(pathAttribute2);
209 asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
210 (byte) 0xe9 };
211 buffer.writeBytes(asPath);
212 pathAttribute2 = AsPath.read(buffer);
213 pathAttributes2.add(pathAttribute2);
214
215 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
216 bgpId = 536936448;
Jonathan Hart51539b82015-10-29 09:53:04 -0700217 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530218 isIbgp = true;
219 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
220 attrList2.setIdentifier(0);
221 attrList2.setPathAttribute(pathAttributes2);
222 attrList2.setProtocolID(ProtocolType.OSPF_V2);
223 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700224 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530225 BgpSelectionAlgo algo = new BgpSelectionAlgo();
226 int result = algo.compare(list1, list2);
227 assertThat(result, is(1));
228 }
229
230 /**
231 * firstPathAttribute has lowest med than secondPathAttribute.
232 */
233 @Test
234 public void selectionAlgoTest4() throws BgpParseException {
235
236 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
237 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
238 BgpValueType pathAttribute1;
239 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
240 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
241 buffer.writeBytes(origin);
242 pathAttribute1 = Origin.read(buffer);
243 pathAttributes1.add(pathAttribute1);
244 byte[] med = new byte[] {(byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00,
245 0x00 };
246 buffer.writeBytes(med);
247 pathAttribute1 = Med.read(buffer);
248 pathAttributes1.add(pathAttribute1);
249 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
250 (byte) 0xe9 };
251 buffer.writeBytes(asPath);
252 pathAttribute1 = AsPath.read(buffer);
253 pathAttributes1.add(pathAttribute1);
254
255 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
256 int bgpId = 168427777;
Jonathan Hart51539b82015-10-29 09:53:04 -0700257 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530258 boolean isIbgp = false;
259 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
260 attrList1.setIdentifier(0);
261 attrList1.setPathAttribute(pathAttributes1);
262 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
263 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700264 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530265
266 peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
267 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
268 BgpValueType pathAttribute2;
269 origin = new byte[] {0x40, 0x01, 0x01, 0x02 };
270 buffer = ChannelBuffers.dynamicBuffer();
271 buffer.writeBytes(origin);
272 pathAttribute2 = Origin.read(buffer);
273 pathAttributes2.add(pathAttribute2);
274 med = new byte[] {(byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, 0x01 };
275 buffer.writeBytes(med);
276 pathAttribute2 = Med.read(buffer);
277 pathAttributes2.add(pathAttribute2);
278 asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
279 (byte) 0xe9 };
280 buffer.writeBytes(asPath);
281 pathAttribute2 = AsPath.read(buffer);
282 pathAttributes2.add(pathAttribute2);
283
284 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
285 bgpId = 536936448;
Jonathan Hart51539b82015-10-29 09:53:04 -0700286 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530287 isIbgp = true;
288 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
289 attrList2.setIdentifier(0);
290 attrList2.setPathAttribute(pathAttributes2);
291 attrList2.setProtocolID(ProtocolType.OSPF_V2);
292 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700293 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530294 BgpSelectionAlgo algo = new BgpSelectionAlgo();
295 int result = algo.compare(list1, list2);
296 assertThat(result, is(1));
297 }
298
299 /**
300 * secondPathAttribute has higher local preference than firstPathAttribute.
301 */
302 @Test
303 public void selectionAlgoTest5() throws BgpParseException {
304
305 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
306 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
307 BgpValueType pathAttribute1;
308 byte[] locPref = new byte[] {(byte) 0x00, 0x05, 0x04, 0x00, 0x00,
309 0x00, 0x01 };
310 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
311 buffer.writeBytes(locPref);
312 pathAttribute1 = LocalPref.read(buffer);
313 pathAttributes1.add(pathAttribute1);
314
315 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
316 int bgpId = 168427777;
Jonathan Hart51539b82015-10-29 09:53:04 -0700317 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530318 boolean isIbgp = false;
319 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
320 attrList1.setIdentifier(0);
321 attrList1.setPathAttribute(pathAttributes1);
322 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
323 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700324 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530325
326 peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
327 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
328 BgpValueType pathAttribute2;
329 locPref = new byte[] {(byte) 0x00, 0x05, 0x04, 0x00, 0x00, 0x00, 0x0a };
330 buffer = ChannelBuffers.dynamicBuffer();
331 buffer.writeBytes(locPref);
332 pathAttribute2 = LocalPref.read(buffer);
333 pathAttributes2.add(pathAttribute2);
334
335 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
336 bgpId = 536936448;
Jonathan Hart51539b82015-10-29 09:53:04 -0700337 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530338 isIbgp = true;
339 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
340 attrList2.setIdentifier(0);
341 attrList2.setPathAttribute(pathAttributes2);
342 attrList2.setProtocolID(ProtocolType.OSPF_V2);
343 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700344 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530345 BgpSelectionAlgo algo = new BgpSelectionAlgo();
346 int result = algo.compare(list1, list2);
347 assertThat(result, is(-1));
348 }
349
350 /**
351 * secondPathAttribute is EBGP than firstPathAttribute is IBGP.
352 */
353 @Test
354 public void selectionAlgoTest6() throws BgpParseException {
355
356 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
357 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
358 BgpValueType pathAttribute1;
359 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
360 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
361 buffer.writeBytes(origin);
362 pathAttribute1 = Origin.read(buffer);
363 pathAttributes1.add(pathAttribute1);
364 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
365 (byte) 0xe9 };
366 buffer.writeBytes(asPath);
367 pathAttribute1 = AsPath.read(buffer);
368 pathAttributes1.add(pathAttribute1);
369
370 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
371 int bgpId = 168427777;
Jonathan Hart51539b82015-10-29 09:53:04 -0700372 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530373 boolean isIbgp = true;
374 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
375 attrList1.setIdentifier(0);
376 attrList1.setPathAttribute(pathAttributes1);
377 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
378 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700379 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530380
381 peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
382 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
383 BgpValueType pathAttribute2;
384 origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
385 buffer = ChannelBuffers.dynamicBuffer();
386 buffer.writeBytes(origin);
387 pathAttribute2 = Origin.read(buffer);
388 pathAttributes2.add(pathAttribute2);
389 asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
390 (byte) 0xe9 };
391 buffer.writeBytes(asPath);
392 pathAttribute2 = AsPath.read(buffer);
393 pathAttributes2.add(pathAttribute2);
394
395 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
396 bgpId = 536936448;
Jonathan Hart51539b82015-10-29 09:53:04 -0700397 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530398 isIbgp = false;
399 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
400 attrList2.setIdentifier(0);
401 attrList2.setPathAttribute(pathAttributes2);
402 attrList2.setProtocolID(ProtocolType.OSPF_V2);
403 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700404 ipAddress, bgpId, locRibAsNum, false, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530405 BgpSelectionAlgo algo = new BgpSelectionAlgo();
406 int result = algo.compare(list1, list2);
407 assertThat(result, is(-1));
408 }
409
410 /**
411 * firstPathAttribute has lower BGPID than secondPathAttribute.
412 */
413 @Test
414 public void selectionAlgoTest7() throws BgpParseException {
415
416 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
417 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
418 BgpValueType pathAttribute1;
419 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
420 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
421 buffer.writeBytes(origin);
422 pathAttribute1 = Origin.read(buffer);
423 pathAttributes1.add(pathAttribute1);
424 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
425 (byte) 0xe9 };
426 buffer.writeBytes(asPath);
427 pathAttribute1 = AsPath.read(buffer);
428 pathAttributes1.add(pathAttribute1);
429
430 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
431 //A0A0A00
432 Integer bgpId = 168430080;
Jonathan Hart51539b82015-10-29 09:53:04 -0700433 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530434 boolean isIbgp = false;
435 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
436 attrList1.setIdentifier(0);
437 attrList1.setPathAttribute(pathAttributes1);
438 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
439 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700440 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530441
442 peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
443 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
444 BgpValueType pathAttribute2;
445 origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
446 buffer = ChannelBuffers.dynamicBuffer();
447 buffer.writeBytes(origin);
448 pathAttribute2 = Origin.read(buffer);
449 pathAttributes2.add(pathAttribute2);
450 asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
451 (byte) 0xe9 };
452 buffer.writeBytes(asPath);
453 pathAttribute2 = AsPath.read(buffer);
454 pathAttributes2.add(pathAttribute2);
455
456 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
457 //B0A0A00
458 bgpId = 185207296;
Jonathan Hart51539b82015-10-29 09:53:04 -0700459 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530460 isIbgp = false;
461 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
462 attrList2.setIdentifier(0);
463 attrList2.setPathAttribute(pathAttributes2);
464 attrList2.setProtocolID(ProtocolType.OSPF_V2);
465 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700466 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530467 BgpSelectionAlgo algo = new BgpSelectionAlgo();
468 int result = algo.compare(list1, list2);
469 assertThat(result, is(1));
470 }
471
472 /**
473 * secondPathAttribute has lowest peer address than firstPathAttribute.
474 */
475 @Test
476 public void selectionAlgoTest8() throws BgpParseException {
477
478 byte[] peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b };
479 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
480 BgpValueType pathAttribute1;
481 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
482 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
483 buffer.writeBytes(origin);
484 pathAttribute1 = Origin.read(buffer);
485 pathAttributes1.add(pathAttribute1);
486 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
487 (byte) 0xe9 };
488 buffer.writeBytes(asPath);
489 pathAttribute1 = AsPath.read(buffer);
490 pathAttributes1.add(pathAttribute1);
491
492 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
493 //A0A0A00
494 Integer bgpId = 168430080;
Jonathan Hart51539b82015-10-29 09:53:04 -0700495 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530496 boolean isIbgp = false;
497 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
498 attrList1.setIdentifier(0);
499 attrList1.setPathAttribute(pathAttributes1);
500 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
501 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700502 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530503
504 peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
505 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
506 BgpValueType pathAttribute2;
507 origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
508 buffer = ChannelBuffers.dynamicBuffer();
509 buffer.writeBytes(origin);
510 pathAttribute2 = Origin.read(buffer);
511 pathAttributes2.add(pathAttribute2);
512 asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
513 (byte) 0xe9 };
514 buffer.writeBytes(asPath);
515 pathAttribute2 = AsPath.read(buffer);
516 pathAttributes2.add(pathAttribute2);
517
518 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
519 //A0A0A00
520 bgpId = 168430080;
Jonathan Hart51539b82015-10-29 09:53:04 -0700521 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530522 isIbgp = false;
523 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
524 attrList2.setIdentifier(0);
525 attrList2.setPathAttribute(pathAttributes2);
526 attrList2.setProtocolID(ProtocolType.OSPF_V2);
527 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700528 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530529 BgpSelectionAlgo algo = new BgpSelectionAlgo();
530 int result = algo.compare(list1, list2);
531 assertThat(result, is(-1));
532 }
533
534 /**
535 * firstPathAttribute and secondPathAttribute are same.
536 */
537 @Test
538 public void selectionAlgoTest9() throws BgpParseException {
539
540 byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
541 LinkedList<BgpValueType> pathAttributes1 = new LinkedList<>();
542 BgpValueType pathAttribute1;
543 byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
544 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
545 buffer.writeBytes(origin);
546 pathAttribute1 = Origin.read(buffer);
547 pathAttributes1.add(pathAttribute1);
548 byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
549 (byte) 0xe9 };
550 buffer.writeBytes(asPath);
551 pathAttribute1 = AsPath.read(buffer);
552 pathAttributes1.add(pathAttribute1);
553
554 IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp);
555 //A0A0A00
556 Integer bgpId = 168430080;
Jonathan Hart51539b82015-10-29 09:53:04 -0700557 short locRibAsNum = 100;
Priyanka Be25cefe82015-11-28 14:47:23 +0530558 boolean isIbgp = false;
559 PathAttrNlriDetails attrList1 = new PathAttrNlriDetails();
560 attrList1.setIdentifier(0);
561 attrList1.setPathAttribute(pathAttributes1);
562 attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE);
563 PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700564 ipAddress, bgpId, locRibAsNum, isIbgp, attrList1);
Priyanka Be25cefe82015-11-28 14:47:23 +0530565
566 peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a };
567 LinkedList<BgpValueType> pathAttributes2 = new LinkedList<>();
568 BgpValueType pathAttribute2;
569 origin = new byte[] {0x40, 0x01, 0x01, 0x00 };
570 buffer = ChannelBuffers.dynamicBuffer();
571 buffer.writeBytes(origin);
572 pathAttribute2 = Origin.read(buffer);
573 pathAttributes2.add(pathAttribute2);
574 asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd,
575 (byte) 0xe9 };
576 buffer.writeBytes(asPath);
577 pathAttribute2 = AsPath.read(buffer);
578 pathAttributes2.add(pathAttribute2);
579
580 ipAddress = IpAddress.valueOf(Version.INET, peerIp);
581 //A0A0A00
582 bgpId = 168430080;
Jonathan Hart51539b82015-10-29 09:53:04 -0700583 locRibAsNum = 200;
Priyanka Be25cefe82015-11-28 14:47:23 +0530584 isIbgp = false;
585 PathAttrNlriDetails attrList2 = new PathAttrNlriDetails();
586 attrList2.setIdentifier(0);
587 attrList2.setPathAttribute(pathAttributes2);
588 attrList2.setProtocolID(ProtocolType.OSPF_V2);
589 PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib(
Jonathan Hart51539b82015-10-29 09:53:04 -0700590 ipAddress, bgpId, locRibAsNum, isIbgp, attrList2);
Priyanka Be25cefe82015-11-28 14:47:23 +0530591 BgpSelectionAlgo algo = new BgpSelectionAlgo();
592 int result = algo.compare(list1, list2);
593 assertThat(result, is(0));
594 }
595}