Priyanka B
Committed by Gerrit Code Review

[ONOS] BGP Selection Algo and Test

Change-Id: I9d9e8c699dea46fde07b7cba526b3c7fd1f3bd34
1 +/*
2 + * Copyright 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 + */
16 +package org.onosproject.bgp.controller.impl;
17 +
18 +import java.util.Comparator;
19 +import java.util.List;
20 +import java.util.ListIterator;
21 +
22 +import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetailsLocalRib;
23 +import org.onosproject.bgpio.types.AsPath;
24 +import org.onosproject.bgpio.types.BgpValueType;
25 +import org.onosproject.bgpio.types.LocalPref;
26 +import org.onosproject.bgpio.types.Med;
27 +import org.onosproject.bgpio.types.Origin;
28 +import org.onosproject.bgpio.types.Origin.ORIGINTYPE;
29 +import org.slf4j.Logger;
30 +import org.slf4j.LoggerFactory;
31 +
32 +/**
33 + * Implementation of BGP best path Selection process.
34 + */
35 +public final class BgpSelectionAlgo implements Comparator<PathAttrNlriDetailsLocalRib> {
36 + private static final Logger log = LoggerFactory.getLogger(BgpSelectionAlgo.class);
37 + LocalPref obj1LocPref = null;
38 + AsPath obj1Aspath = null;
39 + Origin obj1Origin = null;
40 + Med obj1Med = null;
41 + LocalPref obj2LocPref = null;
42 + AsPath obj2Aspath = null;
43 + Origin obj2Origin = null;
44 + Med obj2Med = null;
45 +
46 + @Override
47 + public int compare(PathAttrNlriDetailsLocalRib pathNlriDetails1, PathAttrNlriDetailsLocalRib pathNlriDetails2) {
48 + if (pathNlriDetails1 == null) {
49 + return -1;
50 + }
51 + if (pathNlriDetails2 == null) {
52 + return 1;
53 + }
54 + if (pathNlriDetails1.equals(pathNlriDetails2)) {
55 + return 0;
56 + }
57 +
58 + List<BgpValueType> o1 = pathNlriDetails1.localRibNlridetails().pathAttributes();
59 + List<BgpValueType> o2 = pathNlriDetails2.localRibNlridetails().pathAttributes();
60 + ListIterator<BgpValueType> listIteratorObj1 = o1.listIterator();
61 + ListIterator<BgpValueType> listIteratorObj2 = o2.listIterator();
62 + storeAttr(listIteratorObj1, listIteratorObj2);
63 +
64 + // prefer attribute with higher local preference
65 + if (obj1LocPref != null || obj2LocPref != null && (obj1LocPref != null && !obj1LocPref.equals(obj2LocPref))) {
66 + return compareLocalPref(obj1LocPref, obj2LocPref);
67 + }
68 +
69 + // prefer attribute with shortest Aspath
70 + if (!obj1Aspath.equals(obj2Aspath)) {
71 + Integer obj1Size = countASSize(obj1Aspath);
72 + Integer obj2Size = countASSize(obj2Aspath);
73 + if (obj1Size != obj2Size) {
74 + return compareAsPath(obj1Size, obj2Size);
75 + }
76 + }
77 +
78 + // prefer attribute with lowest origin type
79 + if (!obj1Origin.equals(obj2Origin)) {
80 + return compareOrigin(obj1Origin, obj2Origin);
81 + }
82 +
83 + // prefer attribute with lowest MED
84 + if (obj1Med != null || obj2Med != null && (obj1Med != null && !obj1Med.equals(obj2Med))) {
85 + return compareMed(obj1Med, obj2Med);
86 + }
87 +
88 + if ((pathNlriDetails1 != null || pathNlriDetails2 != null) && (pathNlriDetails1 != null && !pathNlriDetails1
89 + .equals(pathNlriDetails2))) {
90 + return comparePeerDetails(pathNlriDetails1, pathNlriDetails2);
91 + }
92 + return 0;
93 + }
94 +
95 + /**
96 + * Compares local preference of two objects and returns object with higher preference.
97 + *
98 + * @param obj1LocPref local preference object1
99 + * @param obj2LocPref local preference object2
100 + * @return object with higher preference
101 + */
102 + int compareLocalPref(LocalPref obj1LocPref, LocalPref obj2LocPref) {
103 + return ((Integer) (obj1LocPref.localPref())).compareTo((Integer) (obj2LocPref.localPref()));
104 + }
105 +
106 + /**
107 + * Compares AsPath of two objects and returns object with shortest AsPath.
108 + *
109 + * @param obj1Size object1 AS count
110 + * @param obj2Size object2 AS count
111 + * @return
112 + */
113 + int compareAsPath(Integer obj1Size, Integer obj2Size) {
114 + return obj1Size.compareTo(obj2Size);
115 + }
116 +
117 + /**
118 + * Compare Origin of two objects and returns object with lowest origin value.
119 + *
120 + * @param obj1Origin Origin object1
121 + * @param obj2Origin Origin object1
122 + * @return object with lowest origin value
123 + */
124 + int compareOrigin(Origin obj1Origin, Origin obj2Origin) {
125 + if (obj1Origin.origin() == ORIGINTYPE.IGP) {
126 + return 1;
127 + }
128 + if (obj2Origin.origin() == ORIGINTYPE.IGP) {
129 + return -1;
130 + }
131 + if (obj1Origin.origin() == ORIGINTYPE.EGP) {
132 + return 1;
133 + } else {
134 + return -1;
135 + }
136 + }
137 +
138 + /**
139 + * Compare Med of two objects and returns object with lowestMed value.
140 + *
141 + * @param obj1Med Med object1
142 + * @param obj2Med Med object2
143 + * @return returns object with lowestMed value
144 + */
145 + int compareMed(Med obj1Med, Med obj2Med) {
146 + return ((Integer) (obj2Med.med())).compareTo((Integer) (obj1Med.med()));
147 + }
148 +
149 + /**
150 + * Compares EBGP over IBGP, BGP identifier value and peer address.
151 + *
152 + * @param pathNlriDetails1 PathAttrNlriDetailsLocalRib object1
153 + * @param pathNlriDetails2 PathAttrNlriDetailsLocalRib object2
154 + * @return object which as EBGP over IBGP, lowest BGP identifier value and lowest peer address
155 + */
156 + int comparePeerDetails(PathAttrNlriDetailsLocalRib pathNlriDetails1, PathAttrNlriDetailsLocalRib pathNlriDetails2) {
157 + // consider EBGP over IBGP
158 + if (pathNlriDetails1.isLocalRibIbgpSession() != pathNlriDetails2.isLocalRibIbgpSession()) {
159 + if (pathNlriDetails1 == null || pathNlriDetails1.isLocalRibIbgpSession()) {
160 + return -1;
161 + }
162 + if (pathNlriDetails2 == null || pathNlriDetails2.isLocalRibIbgpSession()) {
163 + return 1;
164 + }
165 + }
166 + // prefer lowest BGP identifier value.
167 + if (pathNlriDetails1.localRibIdentifier() != pathNlriDetails2.localRibIdentifier()) {
168 + return ((Integer) pathNlriDetails2.localRibIdentifier())
169 + .compareTo(pathNlriDetails1.localRibIdentifier());
170 + }
171 + //prefer lowest peer address
172 + if (pathNlriDetails1.localRibIpAddress() != pathNlriDetails2.localRibIpAddress()) {
173 + return pathNlriDetails2.localRibIpAddress().compareTo(pathNlriDetails1.localRibIpAddress());
174 + }
175 + return 0;
176 + }
177 +
178 + /**
179 + * Returns ASes count of AsPath attribute , if AS_SET is present then count as 1.
180 + *
181 + * @param aspath object of AsPath
182 + * @return count of ASes
183 + */
184 + Integer countASSize(AsPath aspath) {
185 + boolean isASSet = false;
186 + int count = 0;
187 + if (!aspath.asPathSet().isEmpty()) {
188 + isASSet = true;
189 + }
190 + if (!aspath.asPathSeq().isEmpty()) {
191 + count = aspath.asPathSeq().size();
192 + }
193 + return isASSet ? ++count : count;
194 + }
195 +
196 + /**
197 + * Stores BGP basic attributes of two objects.
198 + *
199 + * @param listIteratorObj1 list iterator of object1
200 + * @param listIteratorObj2 list iterator of object2
201 + */
202 + void storeAttr(ListIterator<BgpValueType> listIteratorObj1, ListIterator<BgpValueType> listIteratorObj2) {
203 + while (listIteratorObj1.hasNext()) {
204 + BgpValueType pathAttributeObj1 = listIteratorObj1.next();
205 + switch (pathAttributeObj1.getType()) {
206 + case LocalPref.LOCAL_PREF_TYPE:
207 + obj1LocPref = (LocalPref) pathAttributeObj1;
208 + break;
209 + case AsPath.ASPATH_TYPE:
210 + obj1Aspath = (AsPath) pathAttributeObj1;
211 + break;
212 + case Origin.ORIGIN_TYPE:
213 + obj1Origin = (Origin) pathAttributeObj1;
214 + break;
215 + case Med.MED_TYPE:
216 + obj1Med = (Med) pathAttributeObj1;
217 + break;
218 + default:
219 + log.debug("Got other type, Not required: " + pathAttributeObj1.getType());
220 + }
221 + }
222 + while (listIteratorObj2.hasNext()) {
223 + BgpValueType pathAttributeObj2 = listIteratorObj2.next();
224 + switch (pathAttributeObj2.getType()) {
225 + case LocalPref.LOCAL_PREF_TYPE:
226 + obj2LocPref = (LocalPref) pathAttributeObj2;
227 + break;
228 + case AsPath.ASPATH_TYPE:
229 + obj2Aspath = (AsPath) pathAttributeObj2;
230 + break;
231 + case Origin.ORIGIN_TYPE:
232 + obj2Origin = (Origin) pathAttributeObj2;
233 + break;
234 + case Med.MED_TYPE:
235 + obj2Med = (Med) pathAttributeObj2;
236 + break;
237 + default:
238 + log.debug("Got other type, Not required: " + pathAttributeObj2.getType());
239 + }
240 + }
241 + }
242 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 + */
16 +package org.onosproject.controller.impl;
17 +
18 +import static org.hamcrest.MatcherAssert.assertThat;
19 +import static org.hamcrest.core.Is.is;
20 +
21 +import java.util.LinkedList;
22 +
23 +import org.jboss.netty.buffer.ChannelBuffer;
24 +import org.jboss.netty.buffer.ChannelBuffers;
25 +import org.junit.Test;
26 +import org.onlab.packet.IpAddress;
27 +import org.onlab.packet.IpAddress.Version;
28 +import org.onosproject.bgpio.exceptions.BgpParseException;
29 +import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;
30 +import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
31 +import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetailsLocalRib;
32 +import org.onosproject.bgpio.types.AsPath;
33 +import org.onosproject.bgpio.types.BgpValueType;
34 +import org.onosproject.bgpio.types.LocalPref;
35 +import org.onosproject.bgpio.types.Med;
36 +import org.onosproject.bgpio.types.Origin;
37 +import org.onosproject.bgp.controller.impl.BgpSelectionAlgo;
38 +
39 +/**
40 + * Test cases for BGP Selection Algorithm.
41 + */
42 +public 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;
68 + short locRIBASNum = 100;
69 + 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(
75 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
76 +
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;
95 + locRIBASNum = 200;
96 + 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(
102 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
103 + 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;
131 + short locRIBASNum = 100;
132 + 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(
138 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
139 +
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;
156 + locRIBASNum = 200;
157 + 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(
163 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
164 + 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;
192 + short locRIBASNum = 100;
193 + 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(
199 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
200 +
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;
217 + locRIBASNum = 200;
218 + 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(
224 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
225 + 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;
257 + short locRIBASNum = 100;
258 + 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(
264 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
265 +
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;
286 + locRIBASNum = 200;
287 + 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(
293 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
294 + 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;
317 + short locRIBASNum = 100;
318 + 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(
324 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
325 +
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;
337 + locRIBASNum = 200;
338 + 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(
344 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
345 + 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;
372 + short locRIBASNum = 100;
373 + 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(
379 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
380 +
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;
397 + locRIBASNum = 200;
398 + 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(
404 + ipAddress, bgpId, locRIBASNum, false, attrList2);
405 + 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;
433 + short locRIBASNum = 100;
434 + 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(
440 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
441 +
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;
459 + locRIBASNum = 200;
460 + 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(
466 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
467 + 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;
495 + short locRIBASNum = 100;
496 + 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(
502 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
503 +
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;
521 + locRIBASNum = 200;
522 + 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(
528 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
529 + 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;
557 + short locRIBASNum = 100;
558 + 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(
564 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1);
565 +
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;
583 + locRIBASNum = 200;
584 + 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(
590 + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2);
591 + BgpSelectionAlgo algo = new BgpSelectionAlgo();
592 + int result = algo.compare(list1, list2);
593 + assertThat(result, is(0));
594 + }
595 +}