Pavlin Radoslavov

ONOS-866: Refactor the storing and handling of BgpSession info.

Moved the local and remote BGP session info to a separate class BgpSessionInfo.
No functional changes.

Also, removed methods TestBgpPeerChannelHandler.prepareBgpKeepalive(),
prepareBgpNotification() and prepareBgpMessage() from the unit tests.
Instead, use the corresponding methods in the BGP implementation itself
to generate the BGP messages.

Change-Id: I7f4b6ad4f6995c242cd8a9848ea527b1fcac9c11
......@@ -51,7 +51,7 @@ final class BgpKeepalive {
BgpConstants.BGP_KEEPALIVE_EXPECTED_LENGTH) {
log.debug("BGP RX KEEPALIVE Error from {}: " +
"Invalid total message length {}. Expected {}",
bgpSession.getRemoteAddress(),
bgpSession.remoteInfo().address(),
message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH,
BgpConstants.BGP_KEEPALIVE_EXPECTED_LENGTH);
//
......@@ -70,7 +70,7 @@ final class BgpKeepalive {
// Parse the KEEPALIVE message: nothing to do
//
log.trace("BGP RX KEEPALIVE message from {}",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
// Start the Session Timeout timer
bgpSession.restartSessionTimeoutTimer(ctx);
......
......@@ -53,8 +53,8 @@ final class BgpNotification {
if (message.readableBytes() < minLength) {
log.debug("BGP RX NOTIFICATION Error from {}: " +
"Message length {} too short. Must be at least {}",
bgpSession.getRemoteAddress(), message.readableBytes(),
minLength);
bgpSession.remoteInfo().address(),
message.readableBytes(), minLength);
//
// ERROR: Bad Message Length
//
......@@ -71,7 +71,7 @@ final class BgpNotification {
log.debug("BGP RX NOTIFICATION message from {}: Error Code {} " +
"Error Subcode {} Data Length {}",
bgpSession.getRemoteAddress(), errorCode, errorSubcode,
bgpSession.remoteInfo().address(), errorCode, errorSubcode,
dataLength);
//
......
......@@ -258,16 +258,16 @@ public class BgpRouteEntry extends RouteEntry {
}
// Compare the peer BGP ID: lower is better
Ip4Address peerBgpId = getBgpSession().getRemoteBgpId();
Ip4Address otherPeerBgpId = other.getBgpSession().getRemoteBgpId();
Ip4Address peerBgpId = getBgpSession().remoteInfo().bgpId();
Ip4Address otherPeerBgpId = other.getBgpSession().remoteInfo().bgpId();
if (!peerBgpId.equals(otherPeerBgpId)) {
return (peerBgpId.compareTo(otherPeerBgpId) < 0);
}
// Compare the peer BGP address: lower is better
Ip4Address peerAddress = getBgpSession().getRemoteIp4Address();
Ip4Address peerAddress = getBgpSession().remoteInfo().ip4Address();
Ip4Address otherPeerAddress =
other.getBgpSession().getRemoteIp4Address();
other.getBgpSession().remoteInfo().ip4Address();
if (!peerAddress.equals(otherPeerAddress)) {
return (peerAddress.compareTo(otherPeerAddress) < 0);
}
......@@ -486,7 +486,7 @@ public class BgpRouteEntry extends RouteEntry {
return MoreObjects.toStringHelper(getClass())
.add("prefix", prefix())
.add("nextHop", nextHop())
.add("bgpId", bgpSession.getRemoteBgpId())
.add("bgpId", bgpSession.remoteInfo().bgpId())
.add("origin", Update.Origin.typeToString(origin))
.add("asPath", asPath)
.add("localPref", localPref)
......
/*
* Copyright 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.sdnip.bgp;
import java.net.SocketAddress;
import org.onlab.packet.Ip4Address;
/**
* Class for keeping information about a BGP session.
*
* There are two instances per each BGP peer session: one to keep the local
* information about the BGP session, and another to keep information about
* the remote BGP peer.
*/
public class BgpSessionInfo {
private SocketAddress address; // IP addr/port
private Ip4Address ip4Address; // IPv4 address
private int bgpVersion; // 1 octet
private long asNumber; // AS number: 2 octets
private long as4Number; // AS4 number: 4 octets
private long holdtime; // 2 octets
private Ip4Address bgpId; // 4 octets -> IPv4 address
private boolean mpExtensions; // Multiprotocol Extensions
// enabled: RFC 4760
private boolean ipv4Unicast; // IPv4/UNICAST AFI/SAFI
private boolean ipv4Multicast; // IPv4/MULTICAST AFI/SAFI
private boolean ipv6Unicast; // IPv6/UNICAST AFI/SAFI
private boolean ipv6Multicast; // IPv6/MULTICAST AFI/SAFI
private boolean as4OctetCapability; // AS 4 octet path capability
/**
* Gets the BGP session address: local or remote.
*
* @return the BGP session address
*/
public SocketAddress address() {
return this.address;
}
/**
* Sets the BGP session address: local or remote.
*
* @param address the BGP session address to set
*/
public void setAddress(SocketAddress address) {
this.address = address;
}
/**
* Gets the BGP session IPv4 address: local or remote.
*
* @return the BGP session IPv4 address
*/
public Ip4Address ip4Address() {
return this.ip4Address;
}
/**
* Sets the BGP session IPv4 address: local or remote.
*
* @param ip4Address the BGP session IPv4 address to set
*/
public void setIp4Address(Ip4Address ip4Address) {
this.ip4Address = ip4Address;
}
/**
* Gets the BGP session BGP version: local or remote.
*
* @return the BGP session BGP version
*/
public int bgpVersion() {
return this.bgpVersion;
}
/**
* Sets the BGP session BGP version: local or remote.
*
* @param bgpVersion the BGP session BGP version to set
*/
public void setBgpVersion(int bgpVersion) {
this.bgpVersion = bgpVersion;
}
/**
* Gets the BGP session AS number: local or remote.
*
* @return the BGP session AS number
*/
public long asNumber() {
return this.asNumber;
}
/**
* Sets the BGP session AS number: local or remote.
*
* @param asNumber the BGP session AS number to set
*/
public void setAsNumber(long asNumber) {
this.asNumber = asNumber;
}
/**
* Gets the BGP session AS4 number: local or remote.
*
* @return the BGP session AS4 number
*/
public long as4Number() {
return this.as4Number;
}
/**
* Sets the BGP session AS4 number: local or remote.
*
* @param as4Number the BGP session AS4 number to set
*/
public void setAs4Number(long as4Number) {
this.as4Number = as4Number;
}
/**
* Gets the BGP session holdtime: local or remote.
*
* @return the BGP session holdtime
*/
public long holdtime() {
return this.holdtime;
}
/**
* Sets the BGP session holdtime: local or remote.
*
* @param holdtime the BGP session holdtime to set
*/
public void setHoldtime(long holdtime) {
this.holdtime = holdtime;
}
/**
* Gets the BGP session BGP Identifier as an IPv4 address: local or remote.
*
* @return the BGP session BGP Identifier as an IPv4 address
*/
public Ip4Address bgpId() {
return this.bgpId;
}
/**
* Sets the BGP session BGP Identifier as an IPv4 address: local or remote.
*
* @param bgpId the BGP session BGP Identifier to set
*/
public void setBgpId(Ip4Address bgpId) {
this.bgpId = bgpId;
}
/**
* Gets the BGP Multiprotocol Extensions: local or remote.
*
* @return true if the BGP Multiprotocol Extensions are enabled, otherwise
* false
*/
public boolean mpExtensions() {
return this.mpExtensions;
}
/**
* Gets the BGP session AFI/SAFI configuration for IPv4 unicast: local or
* remote.
*
* @return the BGP session AFI/SAFI configuration for IPv4 unicast
*/
public boolean ipv4Unicast() {
return ipv4Unicast;
}
/**
* Sets the BGP session AFI/SAFI configuration for IPv4 unicast: local or
* remote.
*/
public void setIpv4Unicast() {
this.mpExtensions = true;
this.ipv4Unicast = true;
}
/**
* Gets the BGP session AFI/SAFI configuration for IPv4 multicast: local or
* remote.
*
* @return the BGP session AFI/SAFI configuration for IPv4 multicast
*/
public boolean ipv4Multicast() {
return ipv4Multicast;
}
/**
* Sets the BGP session AFI/SAFI configuration for IPv4 multicast: local or
* remote.
*/
public void setIpv4Multicast() {
this.mpExtensions = true;
this.ipv4Multicast = true;
}
/**
* Gets the BGP session AFI/SAFI configuration for IPv6 unicast: local or
* remote.
*
* @return the BGP session AFI/SAFI configuration for IPv6 unicast
*/
public boolean ipv6Unicast() {
return ipv6Unicast;
}
/**
* Sets the BGP session AFI/SAFI configuration for IPv6 unicast: local or
* remote.
*/
void setIpv6Unicast() {
this.mpExtensions = true;
this.ipv6Unicast = true;
}
/**
* Gets the BGP session AFI/SAFI configuration for IPv6 multicast: local or
* remote.
*
* @return the BGP session AFI/SAFI configuration for IPv6 multicast
*/
public boolean ipv6Multicast() {
return ipv6Multicast;
}
/**
* Sets the BGP session AFI/SAFI configuration for IPv6 multicast: local or
* remote.
*/
public void setIpv6Multicast() {
this.mpExtensions = true;
this.ipv6Multicast = true;
}
/**
* Gets the BGP session 4 octet AS path capability: local or remote.
*
* @return true when the BGP session has 4 octet AS path capability
*/
public boolean as4OctetCapability() {
return this.as4OctetCapability;
}
/**
* Sets the BGP session 4 octet AS path capability.
*/
public void setAs4OctetCapability() {
this.as4OctetCapability = true;
}
}
......@@ -189,18 +189,18 @@ public class BgpSessionManager {
boolean peerConnected(BgpSession bgpSession) {
// Test whether there is already a session from the same remote
if (bgpSessions.get(bgpSession.getRemoteAddress()) != null) {
if (bgpSessions.get(bgpSession.remoteInfo().address()) != null) {
return false; // Duplicate BGP session
}
bgpSessions.put(bgpSession.getRemoteAddress(), bgpSession);
bgpSessions.put(bgpSession.remoteInfo().address(), bgpSession);
//
// If the first connection, set my BGP ID to the local address
// of the socket.
//
if (bgpSession.getLocalAddress() instanceof InetSocketAddress) {
if (bgpSession.localInfo().address() instanceof InetSocketAddress) {
InetAddress inetAddr =
((InetSocketAddress) bgpSession.getLocalAddress()).getAddress();
((InetSocketAddress) bgpSession.localInfo().address()).getAddress();
Ip4Address ip4Address = Ip4Address.valueOf(inetAddr.getAddress());
updateMyBgpId(ip4Address);
}
......@@ -213,7 +213,7 @@ public class BgpSessionManager {
* @param bgpSession the BGP session for the peer
*/
void peerDisconnected(BgpSession bgpSession) {
bgpSessions.remove(bgpSession.getRemoteAddress());
bgpSessions.remove(bgpSession.remoteInfo().address());
}
/**
......
......@@ -68,8 +68,8 @@ final class BgpUpdate {
if (message.readableBytes() < minLength) {
log.debug("BGP RX UPDATE Error from {}: " +
"Message length {} too short. Must be at least {}",
bgpSession.getRemoteAddress(), message.readableBytes(),
minLength);
bgpSession.remoteInfo().address(),
message.readableBytes(), minLength);
//
// ERROR: Bad Message Length
//
......@@ -83,7 +83,7 @@ final class BgpUpdate {
}
log.debug("BGP RX UPDATE message from {}",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
//
// Parse the UPDATE message
......@@ -105,13 +105,13 @@ final class BgpUpdate {
} catch (BgpParseException e) {
// ERROR: Invalid Network Field
log.debug("Exception parsing Withdrawn Prefixes from BGP peer {}: ",
bgpSession.getRemoteBgpId(), e);
bgpSession.remoteInfo().bgpId(), e);
actionsBgpUpdateInvalidNetworkField(bgpSession, ctx);
return;
}
for (Ip4Prefix prefix : withdrawnPrefixes) {
log.debug("BGP RX UPDATE message WITHDRAWN from {}: {}",
bgpSession.getRemoteAddress(), prefix);
bgpSession.remoteInfo().address(), prefix);
BgpRouteEntry bgpRouteEntry = bgpSession.findBgpRoute(prefix);
if (bgpRouteEntry != null) {
decodedBgpRoutes.deletedUnicastRoutes4.put(prefix,
......@@ -126,7 +126,7 @@ final class BgpUpdate {
parsePathAttributes(bgpSession, ctx, message, decodedBgpRoutes);
} catch (BgpParseException e) {
log.debug("Exception parsing Path Attributes from BGP peer {}: ",
bgpSession.getRemoteBgpId(), e);
bgpSession.remoteInfo().bgpId(), e);
// NOTE: The session was already closed, so nothing else to do
return;
}
......@@ -366,7 +366,7 @@ final class BgpUpdate {
// Skip the data from the unrecognized attribute
log.debug("BGP RX UPDATE message from {}: " +
"Unrecognized Attribute Type {}",
bgpSession.getRemoteAddress(), attrTypeCode);
bgpSession.remoteInfo().address(), attrTypeCode);
message.skipBytes(attrLen);
break;
}
......@@ -384,7 +384,7 @@ final class BgpUpdate {
} catch (BgpParseException e) {
// ERROR: Invalid Network Field
log.debug("Exception parsing NLRI from BGP peer {}: ",
bgpSession.getRemoteBgpId(), e);
bgpSession.remoteInfo().bgpId(), e);
actionsBgpUpdateInvalidNetworkField(bgpSession, ctx);
// Rethrow the exception
throw e;
......@@ -433,15 +433,15 @@ final class BgpUpdate {
new BgpRouteEntry(bgpSession, prefix, mpNlri.nextHop4,
origin.byteValue(), asPath, localPref);
bgpRouteEntry.setMultiExitDisc(multiExitDisc);
if (bgpRouteEntry.hasAsPathLoop(bgpSession.getLocalAs())) {
if (bgpRouteEntry.hasAsPathLoop(bgpSession.localInfo().asNumber())) {
log.debug("BGP RX UPDATE message IGNORED from {}: {} " +
"nextHop {}: contains AS Path loop",
bgpSession.getRemoteAddress(), prefix,
bgpSession.remoteInfo().address(), prefix,
mpNlri.nextHop4);
continue;
} else {
log.debug("BGP RX UPDATE message ADDED from {}: {} nextHop {}",
bgpSession.getRemoteAddress(), prefix,
bgpSession.remoteInfo().address(), prefix,
mpNlri.nextHop4);
}
// Remove from the collection of deleted routes
......@@ -456,15 +456,15 @@ final class BgpUpdate {
new BgpRouteEntry(bgpSession, prefix, mpNlri.nextHop6,
origin.byteValue(), asPath, localPref);
bgpRouteEntry.setMultiExitDisc(multiExitDisc);
if (bgpRouteEntry.hasAsPathLoop(bgpSession.getLocalAs())) {
if (bgpRouteEntry.hasAsPathLoop(bgpSession.localInfo().asNumber())) {
log.debug("BGP RX UPDATE message IGNORED from {}: {} " +
"nextHop {}: contains AS Path loop",
bgpSession.getRemoteAddress(), prefix,
bgpSession.remoteInfo().address(), prefix,
mpNlri.nextHop6);
continue;
} else {
log.debug("BGP RX UPDATE message ADDED from {}: {} nextHop {}",
bgpSession.getRemoteAddress(), prefix,
bgpSession.remoteInfo().address(), prefix,
mpNlri.nextHop6);
}
// Remove from the collection of deleted routes
......@@ -507,7 +507,7 @@ final class BgpUpdate {
// Multiprotocol Extensions are not enabled, even if the UPDATE
// message doesn't contain the legacy NLRI (per RFC 4271).
//
if (!bgpSession.getMpExtensions()) {
if (!bgpSession.mpExtensions()) {
hasNlri = true;
hasLegacyNlri = true;
} else {
......@@ -838,7 +838,7 @@ final class BgpUpdate {
// Here we check only (a), because (b) doesn't apply for us: all our
// peers are iBGP.
//
if (nextHopAddress.equals(bgpSession.getLocalIp4Address())) {
if (nextHopAddress.equals(bgpSession.localInfo().ip4Address())) {
// ERROR: Invalid NEXT_HOP Attribute
message.resetReaderIndex();
actionsBgpUpdateInvalidNextHopAttribute(
......@@ -1297,7 +1297,7 @@ final class BgpUpdate {
BgpSession bgpSession,
ChannelHandlerContext ctx) {
log.debug("BGP RX UPDATE Error from {}: Invalid Network Field",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
//
// ERROR: Invalid Network Field
......@@ -1323,7 +1323,7 @@ final class BgpUpdate {
BgpSession bgpSession,
ChannelHandlerContext ctx) {
log.debug("BGP RX UPDATE Error from {}: Malformed Attribute List",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
//
// ERROR: Malformed Attribute List
......@@ -1352,7 +1352,7 @@ final class BgpUpdate {
ChannelHandlerContext ctx,
int missingAttrTypeCode) {
log.debug("BGP RX UPDATE Error from {}: Missing Well-known Attribute: {}",
bgpSession.getRemoteAddress(), missingAttrTypeCode);
bgpSession.remoteInfo().address(), missingAttrTypeCode);
//
// ERROR: Missing Well-known Attribute
......@@ -1390,7 +1390,7 @@ final class BgpUpdate {
ChannelBuffer message,
short origin) {
log.debug("BGP RX UPDATE Error from {}: Invalid ORIGIN Attribute",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
//
// ERROR: Invalid ORIGIN Attribute
......@@ -1427,7 +1427,7 @@ final class BgpUpdate {
int attrFlags,
ChannelBuffer message) {
log.debug("BGP RX UPDATE Error from {}: Attribute Flags Error",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
//
// ERROR: Attribute Flags Error
......@@ -1467,7 +1467,7 @@ final class BgpUpdate {
ChannelBuffer message,
Ip4Address nextHop) {
log.debug("BGP RX UPDATE Error from {}: Invalid NEXT_HOP Attribute {}",
bgpSession.getRemoteAddress(), nextHop);
bgpSession.remoteInfo().address(), nextHop);
//
// ERROR: Invalid NEXT_HOP Attribute
......@@ -1506,7 +1506,7 @@ final class BgpUpdate {
ChannelBuffer message) {
log.debug("BGP RX UPDATE Error from {}: " +
"Unrecognized Well-known Attribute Error: {}",
bgpSession.getRemoteAddress(), attrTypeCode);
bgpSession.remoteInfo().address(), attrTypeCode);
//
// ERROR: Unrecognized Well-known Attribute
......@@ -1545,7 +1545,7 @@ final class BgpUpdate {
int attrFlags,
ChannelBuffer message) {
log.debug("BGP RX UPDATE Error from {}: Optional Attribute Error: {}",
bgpSession.getRemoteAddress(), attrTypeCode);
bgpSession.remoteInfo().address(), attrTypeCode);
//
// ERROR: Optional Attribute Error
......@@ -1583,7 +1583,7 @@ final class BgpUpdate {
int attrFlags,
ChannelBuffer message) {
log.debug("BGP RX UPDATE Error from {}: Attribute Length Error",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
//
// ERROR: Attribute Length Error
......@@ -1612,7 +1612,7 @@ final class BgpUpdate {
BgpSession bgpSession,
ChannelHandlerContext ctx) {
log.debug("BGP RX UPDATE Error from {}: Malformed AS Path",
bgpSession.getRemoteAddress());
bgpSession.remoteInfo().address());
//
// ERROR: Malformed AS_PATH
......
......@@ -60,7 +60,7 @@ public class BgpNeighborsListCommand extends AbstractShellCommand {
// Print a single neighbor (if found)
BgpSession foundBgpSession = null;
for (BgpSession bgpSession : bgpSessions) {
if (bgpSession.getRemoteBgpId().toString().equals(bgpNeighbor)) {
if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) {
foundBgpSession = bgpSession;
break;
}
......@@ -99,33 +99,34 @@ public class BgpNeighborsListCommand extends AbstractShellCommand {
*/
private void printNeighbor(BgpSession bgpSession) {
print(FORMAT_NEIGHBOR_LINE1,
bgpSession.getRemoteBgpId().toString(),
bgpSession.getRemoteAs(),
bgpSession.getLocalAs());
bgpSession.remoteInfo().bgpId().toString(),
bgpSession.remoteInfo().asNumber(),
bgpSession.localInfo().asNumber());
print(FORMAT_NEIGHBOR_LINE2,
bgpSession.getRemoteBgpId().toString(),
bgpSession.getRemoteAddress().toString(),
bgpSession.getRemoteBgpVersion(),
bgpSession.getRemoteHoldtime());
bgpSession.remoteInfo().bgpId().toString(),
bgpSession.remoteInfo().address().toString(),
bgpSession.remoteInfo().bgpVersion(),
bgpSession.remoteInfo().holdtime());
print(FORMAT_NEIGHBOR_LINE3,
bgpSession.getRemoteIpv4Unicast() ? "YES" : "NO",
bgpSession.getRemoteIpv4Multicast() ? "YES" : "NO",
bgpSession.getRemoteIpv6Unicast() ? "YES" : "NO",
bgpSession.getRemoteIpv6Multicast() ? "YES" : "NO");
bgpSession.remoteInfo().ipv4Unicast() ? "YES" : "NO",
bgpSession.remoteInfo().ipv4Multicast() ? "YES" : "NO",
bgpSession.remoteInfo().ipv6Unicast() ? "YES" : "NO",
bgpSession.remoteInfo().ipv6Multicast() ? "YES" : "NO");
print(FORMAT_NEIGHBOR_LINE4,
bgpSession.getLocalBgpId().toString(),
bgpSession.getLocalAddress().toString(),
bgpSession.getLocalBgpVersion(),
bgpSession.getLocalHoldtime());
bgpSession.localInfo().bgpId().toString(),
bgpSession.localInfo().address().toString(),
bgpSession.localInfo().bgpVersion(),
bgpSession.localInfo().holdtime());
print(FORMAT_NEIGHBOR_LINE5,
bgpSession.getLocalIpv4Unicast() ? "YES" : "NO",
bgpSession.getLocalIpv4Multicast() ? "YES" : "NO",
bgpSession.getLocalIpv6Unicast() ? "YES" : "NO",
bgpSession.getLocalIpv6Multicast() ? "YES" : "NO");
if (bgpSession.getLocalAs4OctetCapability() || bgpSession.getRemoteAs4OctetCapability()) {
bgpSession.localInfo().ipv4Unicast() ? "YES" : "NO",
bgpSession.localInfo().ipv4Multicast() ? "YES" : "NO",
bgpSession.localInfo().ipv6Unicast() ? "YES" : "NO",
bgpSession.localInfo().ipv6Multicast() ? "YES" : "NO");
if (bgpSession.localInfo().as4OctetCapability() ||
bgpSession.remoteInfo().as4OctetCapability()) {
print(FORMAT_NEIGHBOR_LINE6,
bgpSession.getLocalAs4OctetCapability() ? "Advertised" : "",
bgpSession.getRemoteAs4OctetCapability() ? "Received" : "");
bgpSession.localInfo().as4OctetCapability() ? "Advertised" : "",
bgpSession.remoteInfo().as4OctetCapability() ? "Received" : "");
}
}
......@@ -155,25 +156,27 @@ public class BgpNeighborsListCommand extends AbstractShellCommand {
private ObjectNode json(ObjectMapper mapper, BgpSession bgpSession) {
ObjectNode result = mapper.createObjectNode();
result.put("remoteAddress", bgpSession.getRemoteAddress().toString());
result.put("remoteBgpVersion", bgpSession.getRemoteBgpVersion());
result.put("remoteAs", bgpSession.getRemoteAs());
result.put("remoteHoldtime", bgpSession.getRemoteHoldtime());
result.put("remoteBgpId", bgpSession.getRemoteBgpId().toString());
result.put("remoteIpv4Unicast", bgpSession.getRemoteIpv4Unicast());
result.put("remoteIpv4Multicast", bgpSession.getRemoteIpv4Multicast());
result.put("remoteIpv6Unicast", bgpSession.getRemoteIpv6Unicast());
result.put("remoteIpv6Multicast", bgpSession.getRemoteIpv6Multicast());
result.put("remoteAddress", bgpSession.remoteInfo().address().toString());
result.put("remoteBgpVersion", bgpSession.remoteInfo().bgpVersion());
result.put("remoteAs", bgpSession.remoteInfo().asNumber());
result.put("remoteAs4", bgpSession.remoteInfo().as4Number());
result.put("remoteHoldtime", bgpSession.remoteInfo().holdtime());
result.put("remoteBgpId", bgpSession.remoteInfo().bgpId().toString());
result.put("remoteIpv4Unicast", bgpSession.remoteInfo().ipv4Unicast());
result.put("remoteIpv4Multicast", bgpSession.remoteInfo().ipv4Multicast());
result.put("remoteIpv6Unicast", bgpSession.remoteInfo().ipv6Unicast());
result.put("remoteIpv6Multicast", bgpSession.remoteInfo().ipv6Multicast());
//
result.put("localAddress", bgpSession.getLocalAddress().toString());
result.put("localBgpVersion", bgpSession.getLocalBgpVersion());
result.put("localAs", bgpSession.getLocalAs());
result.put("localHoldtime", bgpSession.getLocalHoldtime());
result.put("localBgpId", bgpSession.getLocalBgpId().toString());
result.put("localIpv4Unicast", bgpSession.getLocalIpv4Unicast());
result.put("localIpv4Multicast", bgpSession.getLocalIpv4Multicast());
result.put("localIpv6Unicast", bgpSession.getLocalIpv6Unicast());
result.put("localIpv6Multicast", bgpSession.getLocalIpv6Multicast());
result.put("localAddress", bgpSession.localInfo().address().toString());
result.put("localBgpVersion", bgpSession.localInfo().bgpVersion());
result.put("localAs", bgpSession.localInfo().asNumber());
result.put("localAs4", bgpSession.localInfo().as4Number());
result.put("localHoldtime", bgpSession.localInfo().holdtime());
result.put("localBgpId", bgpSession.localInfo().bgpId().toString());
result.put("localIpv4Unicast", bgpSession.localInfo().ipv4Unicast());
result.put("localIpv4Multicast", bgpSession.localInfo().ipv4Multicast());
result.put("localIpv6Unicast", bgpSession.localInfo().ipv6Unicast());
result.put("localIpv6Multicast", bgpSession.localInfo().ipv6Multicast());
return result;
}
......
......@@ -71,7 +71,7 @@ public class BgpRoutesListCommand extends AbstractShellCommand {
if (bgpNeighbor != null) {
// Print the routes from a single neighbor (if found)
for (BgpSession bgpSession : service.getBgpSessions()) {
if (bgpSession.getRemoteBgpId().toString().equals(bgpNeighbor)) {
if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) {
foundBgpSession = bgpSession;
break;
}
......@@ -152,7 +152,7 @@ public class BgpRoutesListCommand extends AbstractShellCommand {
print(FORMAT_ROUTE_LINE1, route.prefix(), route.nextHop(),
Update.Origin.typeToString(route.getOrigin()),
route.getLocalPref(), route.getMultiExitDisc(),
route.getBgpSession().getRemoteBgpId());
route.getBgpSession().remoteInfo().bgpId());
print(FORMAT_ROUTE_LINE2, asPath4Cli(route.getAsPath()));
}
}
......@@ -245,7 +245,8 @@ public class BgpRoutesListCommand extends AbstractShellCommand {
result.put("prefix", route.prefix().toString());
result.put("nextHop", route.nextHop().toString());
result.put("bgpId", route.getBgpSession().getRemoteBgpId().toString());
result.put("bgpId",
route.getBgpSession().remoteInfo().bgpId().toString());
result.put("origin", Update.Origin.typeToString(route.getOrigin()));
result.put("asPath", json(mapper, route.getAsPath()));
result.put("localPref", route.getLocalPref());
......
......@@ -51,6 +51,15 @@ public class BgpRouteEntryTest {
private static final Ip4Address BGP_SESSION_IP_ADDRESS3 =
Ip4Address.valueOf("20.0.0.2");
private final BgpSessionInfo localInfo = new BgpSessionInfo();
private final BgpSessionInfo remoteInfo = new BgpSessionInfo();
private final BgpSessionInfo localInfo2 = new BgpSessionInfo();
private final BgpSessionInfo remoteInfo2 = new BgpSessionInfo();
private final BgpSessionInfo localInfo3 = new BgpSessionInfo();
private final BgpSessionInfo remoteInfo3 = new BgpSessionInfo();
@Before
public void setUp() throws Exception {
// Mock objects for testing
......@@ -59,20 +68,19 @@ public class BgpRouteEntryTest {
bgpSession3 = createMock(BgpSession.class);
// Setup the BGP Sessions
expect(bgpSession.getRemoteBgpId())
.andReturn(BGP_SESSION_BGP_ID).anyTimes();
expect(bgpSession.getRemoteIp4Address())
.andReturn(BGP_SESSION_IP_ADDRESS).anyTimes();
//
expect(bgpSession2.getRemoteBgpId())
.andReturn(BGP_SESSION_BGP_ID2).anyTimes();
expect(bgpSession2.getRemoteIp4Address())
.andReturn(BGP_SESSION_IP_ADDRESS2).anyTimes();
//
expect(bgpSession3.getRemoteBgpId())
.andReturn(BGP_SESSION_BGP_ID3).anyTimes();
expect(bgpSession3.getRemoteIp4Address())
.andReturn(BGP_SESSION_IP_ADDRESS3).anyTimes();
remoteInfo.setIp4Address(BGP_SESSION_IP_ADDRESS);
remoteInfo2.setIp4Address(BGP_SESSION_IP_ADDRESS2);
remoteInfo3.setIp4Address(BGP_SESSION_IP_ADDRESS3);
remoteInfo.setBgpId(BGP_SESSION_BGP_ID);
remoteInfo2.setBgpId(BGP_SESSION_BGP_ID2);
remoteInfo3.setBgpId(BGP_SESSION_BGP_ID3);
expect(bgpSession.localInfo()).andReturn(localInfo).anyTimes();
expect(bgpSession.remoteInfo()).andReturn(remoteInfo).anyTimes();
expect(bgpSession2.localInfo()).andReturn(localInfo2).anyTimes();
expect(bgpSession2.remoteInfo()).andReturn(remoteInfo2).anyTimes();
expect(bgpSession3.localInfo()).andReturn(localInfo3).anyTimes();
expect(bgpSession3.remoteInfo()).andReturn(remoteInfo3).anyTimes();
replay(bgpSession);
replay(bgpSession2);
......
......@@ -179,13 +179,13 @@ public class BgpSessionManagerTest {
assertThat(result, is(true));
for (BgpSession bgpSession : bgpSessionManager.getBgpSessions()) {
if (bgpSession.getRemoteBgpId().equals(BGP_PEER1_ID)) {
if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER1_ID)) {
bgpSession1 = bgpSession;
}
if (bgpSession.getRemoteBgpId().equals(BGP_PEER2_ID)) {
if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER2_ID)) {
bgpSession2 = bgpSession;
}
if (bgpSession.getRemoteBgpId().equals(BGP_PEER3_ID)) {
if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER3_ID)) {
bgpSession3 = bgpSession;
}
}
......@@ -394,7 +394,7 @@ public class BgpSessionManagerTest {
assertThat(bgpSession2, notNullValue());
assertThat(bgpSession3, notNullValue());
for (BgpSession bgpSession : bgpSessionManager.getBgpSessions()) {
long sessionAs = TestUtils.getField(bgpSession, "localAs");
long sessionAs = bgpSession.localInfo().asNumber();
assertThat(sessionAs, is(TestBgpPeerChannelHandler.PEER_AS));
}
}
......
......@@ -59,7 +59,7 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
ctx.getChannel().write(message);
// Prepare and transmit BGP KEEPALIVE message
message = prepareBgpKeepalive();
message = BgpKeepalive.prepareBgpKeepalive();
ctx.getChannel().write(message);
}
......@@ -82,7 +82,8 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
message.writeShort(PEER_HOLDTIME);
message.writeInt(bgpId.toInt());
message.writeByte(0); // No Optional Parameters
return prepareBgpMessage(BgpConstants.BGP_TYPE_OPEN, message);
return BgpMessage.prepareBgpMessage(BgpConstants.BGP_TYPE_OPEN,
message);
}
/**
......@@ -155,7 +156,8 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
message.writeBytes(pathAttributes);
message.writeBytes(encodedPrefixes);
return prepareBgpMessage(BgpConstants.BGP_TYPE_UPDATE, message);
return BgpMessage.prepareBgpMessage(BgpConstants.BGP_TYPE_UPDATE,
message);
}
/**
......@@ -214,64 +216,4 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
return message;
}
/**
* Prepares BGP KEEPALIVE message.
*
* @return the message to transmit (BGP header included)
*/
ChannelBuffer prepareBgpKeepalive() {
ChannelBuffer message =
ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH);
return prepareBgpMessage(BgpConstants.BGP_TYPE_KEEPALIVE, message);
}
/**
* Prepares BGP NOTIFICATION message.
*
* @param errorCode the BGP NOTIFICATION Error Code
* @param errorSubcode the BGP NOTIFICATION Error Subcode if applicable,
* otherwise BgpConstants.Notifications.ERROR_SUBCODE_UNSPECIFIC
* @param payload the BGP NOTIFICATION Data if applicable, otherwise null
* @return the message to transmit (BGP header included)
*/
ChannelBuffer prepareBgpNotification(int errorCode, int errorSubcode,
ChannelBuffer data) {
ChannelBuffer message =
ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH);
// Prepare the NOTIFICATION message payload
message.writeByte(errorCode);
message.writeByte(errorSubcode);
if (data != null) {
message.writeBytes(data);
}
return prepareBgpMessage(BgpConstants.BGP_TYPE_NOTIFICATION, message);
}
/**
* Prepares BGP message.
*
* @param type the BGP message type
* @param payload the message payload to transmit (BGP header excluded)
* @return the message to transmit (BGP header included)
*/
private ChannelBuffer prepareBgpMessage(int type, ChannelBuffer payload) {
ChannelBuffer message =
ChannelBuffers.buffer(BgpConstants.BGP_HEADER_LENGTH +
payload.readableBytes());
// Write the marker
for (int i = 0; i < BgpConstants.BGP_HEADER_MARKER_LENGTH; i++) {
message.writeByte(0xff);
}
// Write the rest of the BGP header
message.writeShort(BgpConstants.BGP_HEADER_LENGTH +
payload.readableBytes());
message.writeByte(type);
// Write the payload
message.writeBytes(payload);
return message;
}
}
......