From e63291850fd0795c5700e25e67e5dee89ba54c5f Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Tue, 1 Dec 2015 05:49:27 -0800 Subject: onos commit hash c2999f30c69e50df905a9d175ef80b3f23a98514 Change-Id: I2bb8562c4942b6d6a6d60b663db2e17540477b81 Signed-off-by: Ashlee Young --- .../org/onosproject/bgp/BgpControllerImplTest.java | 300 +++++++++++ .../onosproject/bgp/BgpPeerChannelHandlerTest.java | 107 ++++ .../onosproject/bgp/BgpPeerFrameDecoderTest.java | 168 ++++++ .../controller/impl/BgpSelectionAlgoTest.java | 595 +++++++++++++++++++++ 4 files changed, 1170 insertions(+) create mode 100755 framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java create mode 100755 framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerChannelHandlerTest.java create mode 100755 framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerFrameDecoderTest.java create mode 100644 framework/src/onos/bgp/ctl/src/test/java/org/onosproject/controller/impl/BgpSelectionAlgoTest.java (limited to 'framework/src/onos/bgp/ctl/src/test/java/org/onosproject') diff --git a/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java new file mode 100755 index 00000000..36b1d6fc --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java @@ -0,0 +1,300 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.bgp; + +import com.google.common.net.InetAddresses; +import org.jboss.netty.bootstrap.ClientBootstrap; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.ChannelPipelineFactory; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +import org.onlab.junit.TestUtils; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.LinkedList; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.onosproject.bgp.controller.BgpCfg; +import org.onosproject.bgp.controller.impl.BgpControllerImpl; +import org.onosproject.bgpio.types.BgpValueType; +import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv; +import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; + +/** + * Test case for BGPControllerImpl. + */ +public class BgpControllerImplTest { + + protected static final Logger log = LoggerFactory + .getLogger(BgpControllerImplTest.class); + + private static final String IP_LOOPBACK_ID1 = "127.0.0.1"; + + private static final int MESSAGE_TIMEOUT_MS = 3000; + public byte version; + public short asNumber; + public short holdTime; + public int bgpId = InetAddresses.coerceToInteger(InetAddresses.forString(IP_LOOPBACK_ID1)); + public boolean isLargeAsCapabilitySet = false; + public LinkedList capabilityTlv = new LinkedList<>(); + + @Before + public void setUp() throws Exception { + peer1 = new BgpPeerTest(version, asNumber, + holdTime, bgpId, isLargeAsCapabilitySet, + capabilityTlv); + + bgpControllerImpl = new BgpControllerImpl(); + + // NOTE: We use port 0 to bind on any available port + bgpControllerImpl.controller().setBgpPortNum(); + bgpControllerImpl.activate(); + + Channel serverChannel = TestUtils.getField(bgpControllerImpl.controller(), + "serverChannel"); + SocketAddress socketAddress = serverChannel.getLocalAddress(); + InetSocketAddress inetSocketAddress = + (InetSocketAddress) socketAddress; + InetAddress connectToAddress = InetAddresses.forString("127.0.0.1"); + connectToSocket = new InetSocketAddress(connectToAddress, + inetSocketAddress.getPort()); + + bgpControllerImpl.getConfig().setRouterId("1.1.1.1"); + bgpControllerImpl.getConfig().setAsNumber(200); + bgpControllerImpl.getConfig().setHoldTime((short) 120); + bgpControllerImpl.getConfig().setState(BgpCfg.State.IP_AS_CONFIGURED); + + bgpControllerImpl.getConfig().addPeer("127.0.0.1", 200); + } + + @After + public void tearDown() throws Exception { + bgpControllerImpl.deactivate(); + bgpControllerImpl = null; + } + + private BgpControllerImpl bgpControllerImpl; + + BgpPeerTest peer1; + + // The socket that the remote peers should connect to + private InetSocketAddress connectToSocket; + + @Test + public void bgpOpenMessageTest1() throws InterruptedException { + peer1.peerChannelHandler.asNumber = 200; + peer1.peerChannelHandler.version = 4; + peer1.peerChannelHandler.holdTime = 120; + peer1.connect(connectToSocket); + boolean result; + result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + } + + @Test + public void bgpOpenMessageTest2() throws InterruptedException { + // Open message with as number which is not configured at peer + peer1.peerChannelHandler.asNumber = 500; + peer1.peerChannelHandler.version = 4; + peer1.peerChannelHandler.holdTime = 120; + peer1.connect(connectToSocket); + + boolean result; + result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + } + + @Test + public void bgpOpenMessageTest3() throws InterruptedException { + // Open message with invalid hold time value + peer1.peerChannelHandler.asNumber = 200; + peer1.peerChannelHandler.version = 4; + peer1.peerChannelHandler.holdTime = 1; + peer1.connect(connectToSocket); + + boolean result; + result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + } + + @Test + public void bgpOpenMessageTest4() throws InterruptedException { + // Open message with invalid as number + peer1.peerChannelHandler.asNumber = 200; + peer1.peerChannelHandler.version = 4; + peer1.peerChannelHandler.holdTime = 120; + peer1.peerChannelHandler.isLargeAsCapabilitySet = true; + BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(766545); + peer1.peerChannelHandler.capabilityTlv.add(tempTlv); + peer1.connect(connectToSocket); + + boolean result; + result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + } + + @Test + public void bgpOpenMessageTest5() throws InterruptedException { + // Open message with LS capability + short afi = 16388; + byte res = 0; + byte safi = 71; + peer1.peerChannelHandler.asNumber = 200; + peer1.peerChannelHandler.version = 4; + peer1.peerChannelHandler.holdTime = 120; + bgpControllerImpl.getConfig().setLsCapability(true); + BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi); + peer1.peerChannelHandler.capabilityTlv.add(tempTlv1); + peer1.connect(connectToSocket); + + boolean result; + result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + } + + @Test + public void bgpOpenMessageTest6() throws InterruptedException { + // Open message with as4 capability + peer1.peerChannelHandler.asNumber = 200; + peer1.peerChannelHandler.version = 4; + peer1.peerChannelHandler.holdTime = 120; + peer1.peerChannelHandler.isLargeAsCapabilitySet = true; + bgpControllerImpl.getConfig().setLargeASCapability(true); + BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(200); + peer1.peerChannelHandler.capabilityTlv.add(tempTlv); + peer1.connect(connectToSocket); + + boolean result; + result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + + result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + } + + @Test + public void bgpOpenMessageTest7() throws InterruptedException { + // Open message with both LS capability and as4 capability + short afi = 16388; + byte res = 0; + byte safi = 71; + peer1.peerChannelHandler.asNumber = 200; + peer1.peerChannelHandler.version = 4; + peer1.peerChannelHandler.holdTime = 120; + + peer1.peerChannelHandler.isLargeAsCapabilitySet = true; + bgpControllerImpl.getConfig().setLargeASCapability(true); + BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(200); + peer1.peerChannelHandler.capabilityTlv.add(tempTlv); + + bgpControllerImpl.getConfig().setLsCapability(true); + BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi); + peer1.peerChannelHandler.capabilityTlv.add(tempTlv1); + peer1.connect(connectToSocket); + + boolean result; + result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await( + MESSAGE_TIMEOUT_MS, + TimeUnit.MILLISECONDS); + assertThat(result, is(true)); + } + + /** + * A class to capture the state for a BGP peer. + */ + private final class BgpPeerTest { + private ClientBootstrap peerBootstrap; + private BgpPeerFrameDecoderTest peerFrameDecoder = + new BgpPeerFrameDecoderTest(); + private BgpPeerChannelHandlerTest peerChannelHandler; + + private BgpPeerTest(byte version, short asNumber, + short holdTime, int bgpId, boolean isLargeAsCapabilitySet, + LinkedList capabilityTlv) { + peerChannelHandler = new BgpPeerChannelHandlerTest(version, + asNumber, holdTime, bgpId, isLargeAsCapabilitySet, capabilityTlv); + } + + /** + * Starts the BGP peer. + * + * @param connectToSocket the socket to connect to + */ + private void connect(InetSocketAddress connectToSocket) + throws InterruptedException { + + ChannelFactory channelFactory = + new NioClientSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool()); + ChannelPipelineFactory pipelineFactory = () -> { + ChannelPipeline pipeline = Channels.pipeline(); + pipeline.addLast("BgpPeerFrameDecoderTest", + peerFrameDecoder); + pipeline.addLast("BgpPeerChannelHandlerTest", + peerChannelHandler); + return pipeline; + }; + + peerBootstrap = new ClientBootstrap(channelFactory); + peerBootstrap.setOption("child.keepAlive", true); + peerBootstrap.setOption("child.tcpNoDelay", true); + peerBootstrap.setPipelineFactory(pipelineFactory); + peerBootstrap.connect(connectToSocket); + } + } +} \ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerChannelHandlerTest.java b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerChannelHandlerTest.java new file mode 100755 index 00000000..26ed36d8 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerChannelHandlerTest.java @@ -0,0 +1,107 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.bgp; + +import java.util.LinkedList; +import java.util.concurrent.TimeUnit; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelStateEvent; +import org.jboss.netty.channel.SimpleChannelHandler; +import org.onosproject.bgpio.protocol.ver4.BgpKeepaliveMsgVer4; +import org.onosproject.bgpio.protocol.ver4.BgpOpenMsgVer4; +import org.onosproject.bgpio.types.BgpHeader; +import org.onosproject.bgpio.types.BgpValueType; + +public class BgpPeerChannelHandlerTest extends SimpleChannelHandler { + public static final int OPEN_MSG_MINIMUM_LENGTH = 29; + public static final byte[] MARKER = new byte[] {(byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff}; + public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER, + (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01); + LinkedList capabilityTlv = new LinkedList<>(); + public byte version; + public short asNumber; + public short holdTime; + public int bgpId; + public boolean isLargeAsCapabilitySet; + + final BgpOpenMsgVer4 openMessage = new BgpOpenMsgVer4(); + ChannelHandlerContext savedCtx; + + /** + * Constructor to initialize all variables of BGP Open message. + * + * @param version BGP version in open message + * @param asNumber AS number in open message + * @param holdTime hold time in open message + * @param bgpId BGP identifier in open message + * @param capabilityTlv capabilities in open message + */ + public BgpPeerChannelHandlerTest(byte version, + short asNumber, + short holdTime, + int bgpId, + boolean isLargeAsCapabilitySet, + LinkedList capabilityTlv) { + this.version = version; + this.asNumber = asNumber; + this.holdTime = holdTime; + this.bgpId = bgpId; + this.isLargeAsCapabilitySet = isLargeAsCapabilitySet; + this.capabilityTlv = capabilityTlv; + } + + /** + * closes the channel. + */ + void closeChannel() { + savedCtx.getChannel().close(); + } + + @Override + public void channelConnected(ChannelHandlerContext ctx, + ChannelStateEvent channelEvent) throws InterruptedException { + this.savedCtx = ctx; + + BgpOpenMsgVer4 openMsg = new BgpOpenMsgVer4(DEFAULT_OPEN_HEADER, + this.version, + this.asNumber, + this.holdTime, + this.bgpId, + this.capabilityTlv); + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + openMsg.writeTo(buffer); + ctx.getChannel().write(buffer); + + TimeUnit.MILLISECONDS.sleep(100); + + BgpKeepaliveMsgVer4 keepaliveMsg = new BgpKeepaliveMsgVer4(); + ChannelBuffer buffer1 = ChannelBuffers.dynamicBuffer(); + keepaliveMsg.writeTo(buffer1); + ctx.getChannel().write(buffer1); + } + + @Override + public void channelDisconnected(ChannelHandlerContext ctx, + ChannelStateEvent channelEvent) { + //Do Nothing + } +} diff --git a/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerFrameDecoderTest.java b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerFrameDecoderTest.java new file mode 100755 index 00000000..7767053f --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/bgp/BgpPeerFrameDecoderTest.java @@ -0,0 +1,168 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.bgp; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.concurrent.CountDownLatch; + +/** + * Class to decode the message received. + */ +public class BgpPeerFrameDecoderTest extends FrameDecoder { + static final byte OPEN_MSG_TYPE = 0x1; + static final byte KEEPALIVE_MSG_TYPE = 0x4; + static final byte UPDATE_MSG_TYPE = 0x2; + static final byte NOTIFICATION_MSG_TYPE = 0x3; + static final int MINIMUM_COMMON_HEADER_LENGTH = 19; + static final int MINIMUM_OPEN_MSG_LENGTH = 29; + static final int MINIMUM_HEADER_MARKER_LENGTH = 16; + static final int HEADER_AND_MSG_LEN = 18; + + protected static final Logger log = LoggerFactory + .getLogger(BgpPeerFrameDecoderTest.class); + final CountDownLatch receivedOpenMessageLatch = new CountDownLatch(1); + final CountDownLatch receivedKeepaliveMessageLatch = new CountDownLatch(1); + final CountDownLatch receivedNotificationMessageLatch = new CountDownLatch(1); + + @Override + protected Object decode(ChannelHandlerContext ctx, + Channel channel, + ChannelBuffer cb) throws Exception { + + if (cb.readableBytes() < MINIMUM_COMMON_HEADER_LENGTH) { + log.debug("Error: Packet length is less then minimum length"); + return null; + } + + byte[] marker = new byte[MINIMUM_HEADER_MARKER_LENGTH]; + cb.readBytes(marker); + for (int i = 0; i < marker.length; i++) { + if (marker[i] != (byte) 0xff) { + log.debug("Error: Marker must be set all ones"); + ctx.getChannel().close(); + return null; + } + } + + short length = cb.readShort(); + if (length < MINIMUM_COMMON_HEADER_LENGTH) { + log.debug("Error: Bad message length"); + ctx.getChannel().close(); + return null; + } + + if (length != (cb.readableBytes() + HEADER_AND_MSG_LEN)) { + log.debug("Error: Bad message length"); + ctx.getChannel().close(); + return null; + } + + byte type = cb.readByte(); + int len = length - MINIMUM_COMMON_HEADER_LENGTH; + + ChannelBuffer message = cb.readBytes(len); + + switch (type) { + case OPEN_MSG_TYPE: + processBgpOpen(ctx, message); + break; + case UPDATE_MSG_TYPE: + break; + case NOTIFICATION_MSG_TYPE: + processBgpNotification(ctx, message); + break; + case KEEPALIVE_MSG_TYPE: + processBgpKeepalive(ctx, message); + break; + default: + ctx.getChannel().close(); + return null; + } + + return null; + } + + /** + * Processes BGP open message. + * + * @param ctx Channel handler context + * @param message open message + */ + private void processBgpOpen(ChannelHandlerContext ctx, + ChannelBuffer message) { + int minLength = + MINIMUM_OPEN_MSG_LENGTH - MINIMUM_COMMON_HEADER_LENGTH; + if (message.readableBytes() < minLength) { + log.debug("Error: Bad message length"); + ctx.getChannel().close(); + return; + } + + message.readByte(); // read version + message.readShort(); // read AS number + message.readShort(); // read Hold timer + message.readInt(); // read BGP Identifier + // Optional Parameters + int optParamLen = message.readUnsignedByte(); + if (message.readableBytes() < optParamLen) { + log.debug("Error: Bad message length"); + ctx.getChannel().close(); + return; + } + message.readBytes(optParamLen); + + // Open message received + receivedOpenMessageLatch.countDown(); + } + + /** + * Processes BGP keepalive message. + * + * @param ctx Channel handler context + * @param message keepalive message + */ + private void processBgpKeepalive(ChannelHandlerContext ctx, + ChannelBuffer message) { + + // Keepalive message received + receivedKeepaliveMessageLatch.countDown(); + } + + /** + * Processes BGP notification message. + * + * @param ctx Channel handler context + * @param message notification message + */ + private void processBgpNotification(ChannelHandlerContext ctx, + ChannelBuffer message) { + byte[] data; + message.readByte(); //read error code + message.readByte(); // read error sub code + if (message.readableBytes() > 0) { + data = new byte[message.readableBytes()]; + message.readBytes(data, 0, message.readableBytes()); + } + + // Notification message received + receivedNotificationMessageLatch.countDown(); + } +} \ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/controller/impl/BgpSelectionAlgoTest.java b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/controller/impl/BgpSelectionAlgoTest.java new file mode 100644 index 00000000..7c0fa417 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/test/java/org/onosproject/controller/impl/BgpSelectionAlgoTest.java @@ -0,0 +1,595 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.controller.impl; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import java.util.LinkedList; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.junit.Test; +import org.onlab.packet.IpAddress; +import org.onlab.packet.IpAddress.Version; +import org.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType; +import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails; +import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetailsLocalRib; +import org.onosproject.bgpio.types.AsPath; +import org.onosproject.bgpio.types.BgpValueType; +import org.onosproject.bgpio.types.LocalPref; +import org.onosproject.bgpio.types.Med; +import org.onosproject.bgpio.types.Origin; +import org.onosproject.bgp.controller.impl.BgpSelectionAlgo; + +/** + * Test cases for BGP Selection Algorithm. + */ +public class BgpSelectionAlgoTest { + + /** + * firstPathAttribute and secondPathAttribute has same AS count and firstPathAttribute + * has shortest Origin value than secondPathAttribute. + */ + @Test + public void selectionAlgoTest1() throws BgpParseException { + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + //origin with IGP + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + //AsPath with AS_SEQ with one AS + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xea }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + int bgpId = 168427777; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + //origin with INCOMPLETE + origin = new byte[] {0x40, 0x01, 0x01, 0x02 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + //AsPath with AS_SEQ with one AS + asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + bgpId = 536936448; + locRIBASNum = 200; + isIbgp = true; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(1)); + } + + /** + * firstPathAttribute has 1 AS count and secondPathAttribute has 2 AS count + * and firstPathAttribute has shortest Origin value than secondPathAttribute. + */ + @Test + public void selectionAlgoTest2() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + int bgpId = 168427777; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + origin = new byte[] {0x40, 0x01, 0x01, 0x02 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + asPath = new byte[] {0x40, 0x02, 0x08, 0x02, 0x01, (byte) 0xfd, + (byte) 0xea, 0x02, 0x01, (byte) 0xfd, (byte) 0xea }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + bgpId = 536936448; + locRIBASNum = 200; + isIbgp = true; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(-1)); + } + + /** + * firstPathAttribute and secondPathAttribute has same AS value + * and firstPathAttribute has shortest Origin value than secondPathAttribute. + */ + @Test + public void selectionAlgoTest3() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + int bgpId = 168427777; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + origin = new byte[] {0x40, 0x01, 0x01, 0x02 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + bgpId = 536936448; + locRIBASNum = 200; + isIbgp = true; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(1)); + } + + /** + * firstPathAttribute has lowest med than secondPathAttribute. + */ + @Test + public void selectionAlgoTest4() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] med = new byte[] {(byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, + 0x00 }; + buffer.writeBytes(med); + pathAttribute1 = Med.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + int bgpId = 168427777; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + origin = new byte[] {0x40, 0x01, 0x01, 0x02 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + med = new byte[] {(byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, 0x01 }; + buffer.writeBytes(med); + pathAttribute2 = Med.read(buffer); + pathAttributes2.add(pathAttribute2); + asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + bgpId = 536936448; + locRIBASNum = 200; + isIbgp = true; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(1)); + } + + /** + * secondPathAttribute has higher local preference than firstPathAttribute. + */ + @Test + public void selectionAlgoTest5() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] locPref = new byte[] {(byte) 0x00, 0x05, 0x04, 0x00, 0x00, + 0x00, 0x01 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(locPref); + pathAttribute1 = LocalPref.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + int bgpId = 168427777; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + locPref = new byte[] {(byte) 0x00, 0x05, 0x04, 0x00, 0x00, 0x00, 0x0a }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(locPref); + pathAttribute2 = LocalPref.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + bgpId = 536936448; + locRIBASNum = 200; + isIbgp = true; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(-1)); + } + + /** + * secondPathAttribute is EBGP than firstPathAttribute is IBGP. + */ + @Test + public void selectionAlgoTest6() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + int bgpId = 168427777; + short locRIBASNum = 100; + boolean isIbgp = true; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + bgpId = 536936448; + locRIBASNum = 200; + isIbgp = false; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, false, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(-1)); + } + + /** + * firstPathAttribute has lower BGPID than secondPathAttribute. + */ + @Test + public void selectionAlgoTest7() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + //A0A0A00 + Integer bgpId = 168430080; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + //B0A0A00 + bgpId = 185207296; + locRIBASNum = 200; + isIbgp = false; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(1)); + } + + /** + * secondPathAttribute has lowest peer address than firstPathAttribute. + */ + @Test + public void selectionAlgoTest8() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0b, 0x0b, 0x0b, 0x0b }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + //A0A0A00 + Integer bgpId = 168430080; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + //A0A0A00 + bgpId = 168430080; + locRIBASNum = 200; + isIbgp = false; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(-1)); + } + + /** + * firstPathAttribute and secondPathAttribute are same. + */ + @Test + public void selectionAlgoTest9() throws BgpParseException { + + byte[] peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes1 = new LinkedList<>(); + BgpValueType pathAttribute1; + byte[] origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute1 = Origin.read(buffer); + pathAttributes1.add(pathAttribute1); + byte[] asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute1 = AsPath.read(buffer); + pathAttributes1.add(pathAttribute1); + + IpAddress ipAddress = IpAddress.valueOf(Version.INET, peerIp); + //A0A0A00 + Integer bgpId = 168430080; + short locRIBASNum = 100; + boolean isIbgp = false; + PathAttrNlriDetails attrList1 = new PathAttrNlriDetails(); + attrList1.setIdentifier(0); + attrList1.setPathAttribute(pathAttributes1); + attrList1.setProtocolID(ProtocolType.ISIS_LEVEL_ONE); + PathAttrNlriDetailsLocalRib list1 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList1); + + peerIp = new byte[] {0x0a, 0x0a, 0x0a, 0x0a }; + LinkedList pathAttributes2 = new LinkedList<>(); + BgpValueType pathAttribute2; + origin = new byte[] {0x40, 0x01, 0x01, 0x00 }; + buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(origin); + pathAttribute2 = Origin.read(buffer); + pathAttributes2.add(pathAttribute2); + asPath = new byte[] {0x40, 0x02, 0x04, 0x02, 0x01, (byte) 0xfd, + (byte) 0xe9 }; + buffer.writeBytes(asPath); + pathAttribute2 = AsPath.read(buffer); + pathAttributes2.add(pathAttribute2); + + ipAddress = IpAddress.valueOf(Version.INET, peerIp); + //A0A0A00 + bgpId = 168430080; + locRIBASNum = 200; + isIbgp = false; + PathAttrNlriDetails attrList2 = new PathAttrNlriDetails(); + attrList2.setIdentifier(0); + attrList2.setPathAttribute(pathAttributes2); + attrList2.setProtocolID(ProtocolType.OSPF_V2); + PathAttrNlriDetailsLocalRib list2 = new PathAttrNlriDetailsLocalRib( + ipAddress, bgpId, locRIBASNum, isIbgp, attrList2); + BgpSelectionAlgo algo = new BgpSelectionAlgo(); + int result = algo.compare(list1, list2); + assertThat(result, is(0)); + } +} -- cgit 1.2.3-korg