import io.netty.util.concurrent.Promise;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.GuardedBy;
-import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
import org.opendaylight.protocol.bgp.rib.spi.SessionNegotiator;
import org.opendaylight.protocol.util.Values;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
private synchronized void startNegotiation() {
// Open can be sent first either from ODL (IDLE) or from peer (OPEN_CONFIRM)
Preconditions.checkState(this.state == State.IDLE || this.state == State.OPEN_CONFIRM);
+ final IpAddress remoteIp = getRemoteIp();
// Check if peer is configured in registry before retrieving preferences
- if (!this.registry.isPeerConfigured(getRemoteIp())) {
+ if (!this.registry.isPeerConfigured(remoteIp)) {
final BGPDocumentedException cause = new BGPDocumentedException(
- "BGP peer with ip: " + getRemoteIp()
- + " not configured, check configured peers in : "
- + this.registry, BGPError.CONNECTION_REJECTED);
+ String.format("BGP peer with ip: %s not configured, check configured peers in : %s", remoteIp, this.registry), BGPError.CONNECTION_REJECTED);
negotiationFailed(cause);
return;
}
- final BGPSessionPreferences preferences = getPreferences();
+ final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
int as = preferences.getMyAs().getValue().intValue();
// Set as AS_TRANS if the value is bigger than 2B
}
}
- private BGPSessionPreferences getPreferences() {
- return this.registry.getPeerPreferences(getRemoteIp());
- }
-
private IpAddress getRemoteIp() {
return StrictBGPPeerRegistry.getIpAddress(this.channel.remoteAddress());
}
}
private void handleOpen(final Open openObj) {
+ final IpAddress remoteIp = getRemoteIp();
+ final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
try {
- final BGPSessionListener peer = this.registry.getPeer(getRemoteIp(), getSourceId(openObj, getPreferences()), getDestinationId(openObj, getPreferences()), getAsNumber(openObj, getPreferences()), openObj);
+ final BGPSessionListener peer = this.registry.getPeer(remoteIp, getSourceId(openObj, preferences), getDestinationId(openObj, preferences), openObj);
sendMessage(new KeepaliveBuilder().build());
- this.session = new BGPSessionImpl(peer, this.channel, openObj, getPreferences(), this.registry);
+ this.session = new BGPSessionImpl(peer, this.channel, openObj, preferences, this.registry);
this.state = State.OPEN_CONFIRM;
LOG.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, openObj);
} catch (final BGPDocumentedException e) {
*/
protected abstract Ipv4Address getSourceId(final Open openMsg, final BGPSessionPreferences preferences);
- /**
- * @param openMsg Open message received from remote BGP speaker
- * @param preferences Local BGP speaker preferences
- * @return AS Number of device that initiate connection
- */
- protected AsNumber getAsNumber(final Open openMsg, final BGPSessionPreferences preferences) {
- return AsNumberUtil.advertizedAsNumber(openMsg);
- }
-
public synchronized State getState() {
return this.state;
}
public class StrictBGPPeerRegistryTest {
- private StrictBGPPeerRegistry droppingBGPSessionRegistry;
+ private StrictBGPPeerRegistry peerRegistry;
private BGPSessionPreferences mockPreferences;
- private final AsNumber AS1 = new AsNumber(1234L);
+ private static final AsNumber AS1 = new AsNumber(1234L);
private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
@Before
public void setUp() throws Exception {
- this.droppingBGPSessionRegistry = new StrictBGPPeerRegistry();
- this.mockPreferences = getMockPreferences(this.AS1);
+ this.peerRegistry = new StrictBGPPeerRegistry();
+ this.mockPreferences = getMockPreferences(AS1);
}
@Test
final Ipv4Address to = new Ipv4Address("255.255.255.255");
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
try {
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
} catch (final BGPDocumentedException e) {
assertEquals(BGPError.CEASE, e.getError());
return;
final Ipv4Address to = new Ipv4Address("255.255.255.255");
try {
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
} catch (final IllegalStateException e) {
return;
}
final IpAddress remoteIp2 = new IpAddress(to2);
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
final ReusableBGPPeer session2 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
- final BGPSessionListener returnedSession1 = this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+ final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
assertSame(session1, returnedSession1);
- final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2, this.AS1, createOpen(to, this.AS1));
+ final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, from, to2, createOpen(to, AS1));
assertSame(session2, returnedSession2);
Mockito.verifyZeroInteractions(session1);
final IpAddress remoteIp = new IpAddress(lower);
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
- this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, this.AS1, createOpen(lower, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
try {
- this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, this.AS1, createOpen(higher, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
} catch (final BGPDocumentedException e) {
assertEquals(BGPError.CEASE, e.getError());
return;
final IpAddress remoteIp = new IpAddress(lower);
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
- this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, this.AS1, createOpen(higher, this.AS1));
- this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, this.AS1, createOpen(lower, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
+ this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
Mockito.verify(session1).releaseConnection();
}
final Ipv4Address to = new Ipv4Address("255.255.255.255");
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
try {
- this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to, this.AS1, createOpen(to, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, to, to, createOpen(to, AS1));
} catch (final BGPDocumentedException e) {
assertEquals(BGPError.CEASE, e.getError());
return;
final AsNumber as2 = new AsNumber(1235L);
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
Mockito.verify(session1).releaseConnection();
}
final AsNumber as2 = new AsNumber(3L);
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
try {
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
} catch (final BGPDocumentedException e) {
assertEquals(BGPError.CEASE, e.getError());
return;
final AsNumber as2 = new AsNumber(3L);
final ReusableBGPPeer session1 = getMockSession();
- this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+ this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
try {
- this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
+ this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
} catch (final BGPDocumentedException e) {
assertEquals(BGPError.BAD_PEER_AS, e.getError());
return;
}
public BGPSessionPreferences getMockPreferences(final AsNumber remoteAs) {
- return new BGPSessionPreferences(this.AS1, 1, new Ipv4Address("0.0.0.1"), remoteAs, Collections.<BgpParameters> emptyList());
+ return new BGPSessionPreferences(AS1, 1, new Ipv4Address("0.0.0.1"), remoteAs, Collections.<BgpParameters> emptyList());
}
}