Shashikanth VH
Committed by Gerrit Code Review

BGP system test issue fix

Change-Id: I36064e33146444531cdef6deab0d55b89efae8a7
......@@ -376,24 +376,10 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
throw new IOException("Invalid peer connection.");
}
// Connection should establish only if local ip and Autonomous system number is configured.
if (bgpconfig.getState() != BgpCfg.State.IP_AS_CONFIGURED) {
sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null);
channel.close();
log.info("BGP local AS and router ID not configured");
return;
}
inetAddress = (InetSocketAddress) address;
peerAddr = IpAddress.valueOf(inetAddress.getAddress()).toString();
// if peer is not configured disconnect session
if (!bgpconfig.isPeerConfigured(peerAddr)) {
log.debug("Peer is not configured {}", peerAddr);
sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null);
channel.close();
return;
}
// if connection is already established close channel
if (peerManager.isPeerConnected(BgpId.bgpId(IpAddress.valueOf(peerAddr)))) {
......@@ -662,8 +648,10 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
private void sendHandshakeOpenMessage() throws IOException, BgpParseException {
int bgpId;
bgpId = Ip4Address.valueOf(bgpconfig.getRouterId()).toInt();
BgpMessage msg = factory4.openMessageBuilder().setAsNumber((short) bgpconfig.getAsNumber())
InetSocketAddress localAddress = (InetSocketAddress) channel.getLocalAddress();
bgpId = Ip4Address.valueOf(IpAddress.valueOf(localAddress.getAddress()).toString()).toInt();
BgpMessage msg = factory4.openMessageBuilder().setAsNumber((short) peerAsNum)
.setHoldTime(bgpconfig.getHoldTime()).setBgpId(bgpId).setLsCapabilityTlv(bgpconfig.getLsCapability())
.setLargeAsCapabilityTlv(bgpconfig.getLargeASCapability()).build();
log.debug("Sending open message to {}", channel.getRemoteAddress());
......@@ -734,11 +722,6 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_BGP_IDENTIFIER, null);
}
// Validate AS number
result = asNumberValidation(h, openMsg);
if (!result) {
throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_PEER_AS, null);
}
// Validate hold timer
if ((openMsg.getHoldTime() != 0) && (openMsg.getHoldTime() < BGP_MIN_HOLDTIME)) {
......
......@@ -67,6 +67,7 @@ public class BgpConfig implements BgpCfg {
this.holdTime = DEFAULT_HOLD_TIMER;
this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
this.lsCapability = true;
}
@Override
......
......@@ -110,9 +110,15 @@ public class BgpControllerImplTest {
@Test
public void bgpOpenMessageTest1() throws InterruptedException {
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(
......@@ -128,16 +134,26 @@ public class BgpControllerImplTest {
@Test
public void bgpOpenMessageTest2() throws InterruptedException {
// Open message with as number which is not configured at peer
short afi = 16388;
byte res = 0;
byte safi = 71;
peer1.peerChannelHandler.asNumber = 500;
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));
result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await(
MESSAGE_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
assertThat(result, is(true));
assertThat(result, is(false));
}
@Test
......@@ -201,6 +217,9 @@ public class BgpControllerImplTest {
@Test
public void bgpOpenMessageTest6() throws InterruptedException {
// Open message with as4 capability
short afi = 16388;
byte res = 0;
byte safi = 71;
peer1.peerChannelHandler.asNumber = 200;
peer1.peerChannelHandler.version = 4;
peer1.peerChannelHandler.holdTime = 120;
......@@ -208,6 +227,9 @@ public class BgpControllerImplTest {
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;
......