Pavlin Radoslavov

* Add unit tests for the BGP route preference comparison mechanism

  This addresses ONOS-134
* Refactor existing BGP unit tests

Change-Id: Ia0ad7954632ec6b856d1a8a972f1837179121abb
...@@ -32,20 +32,15 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler { ...@@ -32,20 +32,15 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
32 static final long PEER_AS = 65001; 32 static final long PEER_AS = 65001;
33 static final int PEER_HOLDTIME = 120; // 120 seconds 33 static final int PEER_HOLDTIME = 120; // 120 seconds
34 final Ip4Address bgpId; // The BGP ID 34 final Ip4Address bgpId; // The BGP ID
35 - final long localPref; // Local preference for routes
36 - final long multiExitDisc = 20; // MED value
37 -
38 ChannelHandlerContext savedCtx; 35 ChannelHandlerContext savedCtx;
39 36
40 /** 37 /**
41 * Constructor for given BGP ID. 38 * Constructor for given BGP ID.
42 * 39 *
43 * @param bgpId the BGP ID to use 40 * @param bgpId the BGP ID to use
44 - * @param localPref the local preference for the routes to use
45 */ 41 */
46 - TestBgpPeerChannelHandler(Ip4Address bgpId, long localPref) { 42 + TestBgpPeerChannelHandler(Ip4Address bgpId) {
47 this.bgpId = bgpId; 43 this.bgpId = bgpId;
48 - this.localPref = localPref;
49 } 44 }
50 45
51 /** 46 /**
...@@ -94,11 +89,17 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler { ...@@ -94,11 +89,17 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
94 * Prepares BGP UPDATE message. 89 * Prepares BGP UPDATE message.
95 * 90 *
96 * @param nextHopRouter the next-hop router address for the routes to add 91 * @param nextHopRouter the next-hop router address for the routes to add
92 + * @param localPref the local preference for the routes to use
93 + * @param multiExitDisc the MED value
94 + * @param asPath the AS path for the routes to add
97 * @param addedRoutes the routes to add 95 * @param addedRoutes the routes to add
98 * @param withdrawnRoutes the routes to withdraw 96 * @param withdrawnRoutes the routes to withdraw
99 * @return the message to transmit (BGP header included) 97 * @return the message to transmit (BGP header included)
100 */ 98 */
101 ChannelBuffer prepareBgpUpdate(Ip4Address nextHopRouter, 99 ChannelBuffer prepareBgpUpdate(Ip4Address nextHopRouter,
100 + long localPref,
101 + long multiExitDisc,
102 + BgpRouteEntry.AsPath asPath,
102 Collection<Ip4Prefix> addedRoutes, 103 Collection<Ip4Prefix> addedRoutes,
103 Collection<Ip4Prefix> withdrawnRoutes) { 104 Collection<Ip4Prefix> withdrawnRoutes) {
104 int attrFlags; 105 int attrFlags;
...@@ -119,23 +120,14 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler { ...@@ -119,23 +120,14 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
119 pathAttributes.writeByte(BgpConstants.Update.Origin.TYPE); 120 pathAttributes.writeByte(BgpConstants.Update.Origin.TYPE);
120 pathAttributes.writeByte(1); // Data length 121 pathAttributes.writeByte(1); // Data length
121 pathAttributes.writeByte(BgpConstants.Update.Origin.IGP); 122 pathAttributes.writeByte(BgpConstants.Update.Origin.IGP);
122 - // AS_PATH: Two Path Segments of 3 ASes each 123 +
124 + // AS_PATH: asPath
123 attrFlags = 0x40; // Transitive flag 125 attrFlags = 0x40; // Transitive flag
124 pathAttributes.writeByte(attrFlags); 126 pathAttributes.writeByte(attrFlags);
125 pathAttributes.writeByte(BgpConstants.Update.AsPath.TYPE); 127 pathAttributes.writeByte(BgpConstants.Update.AsPath.TYPE);
126 - pathAttributes.writeByte(16); // Data length 128 + ChannelBuffer encodedAsPath = encodeAsPath(asPath);
127 - byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; 129 + pathAttributes.writeByte(encodedAsPath.readableBytes()); // Data length
128 - pathAttributes.writeByte(pathSegmentType1); 130 + pathAttributes.writeBytes(encodedAsPath);
129 - pathAttributes.writeByte(3); // Three ASes
130 - pathAttributes.writeShort(65010); // AS=65010
131 - pathAttributes.writeShort(65020); // AS=65020
132 - pathAttributes.writeShort(65030); // AS=65030
133 - byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
134 - pathAttributes.writeByte(pathSegmentType2);
135 - pathAttributes.writeByte(3); // Three ASes
136 - pathAttributes.writeShort(65041); // AS=65041
137 - pathAttributes.writeShort(65042); // AS=65042
138 - pathAttributes.writeShort(65043); // AS=65043
139 // NEXT_HOP: nextHopRouter 131 // NEXT_HOP: nextHopRouter
140 attrFlags = 0x40; // Transitive flag 132 attrFlags = 0x40; // Transitive flag
141 pathAttributes.writeByte(attrFlags); 133 pathAttributes.writeByte(attrFlags);
...@@ -203,6 +195,27 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler { ...@@ -203,6 +195,27 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
203 } 195 }
204 196
205 /** 197 /**
198 + * Encodes an AS path.
199 + *
200 + * @param asPath the AS path to encode
201 + * @return the buffer with the encoded AS path
202 + */
203 + private ChannelBuffer encodeAsPath(BgpRouteEntry.AsPath asPath) {
204 + ChannelBuffer message =
205 + ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH);
206 +
207 + for (BgpRouteEntry.PathSegment pathSegment : asPath.getPathSegments()) {
208 + message.writeByte(pathSegment.getType());
209 + message.writeByte(pathSegment.getSegmentAsNumbers().size());
210 + for (Long asNumber : pathSegment.getSegmentAsNumbers()) {
211 + message.writeShort(asNumber.intValue());
212 + }
213 + }
214 +
215 + return message;
216 + }
217 +
218 + /**
206 * Prepares BGP KEEPALIVE message. 219 * Prepares BGP KEEPALIVE message.
207 * 220 *
208 * @return the message to transmit (BGP header included) 221 * @return the message to transmit (BGP header included)
......