import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.acceptor.config.rev161003.BgpPeerAcceptorConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class BGPPeerAcceptorImpl implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(BGPPeerAcceptorImpl.class);
private static final int PRIVILEGED_PORTS = 1024;
- private final ChannelFuture futureChannel;
+ private final BGPPeerRegistry peerRegistry;
+ private final BGPDispatcher bgpDispatcher;
+ private final InetSocketAddress address;
+ private ChannelFuture futureChannel;
private AutoCloseable listenerRegistration;
- public BGPPeerAcceptorImpl(final BgpPeerAcceptorConfig config, final BGPPeerRegistry peerRegistry, final BGPDispatcher bgpDispatcher) {
- final PortNumber portNumber = config.getBindingPort();
- final IpAddress bindingAddress = config.getBindingAddress();
- LOG.debug("Instantiating BGP Peer Acceptor : {}/{}", bindingAddress, portNumber);
-
- if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && portNumber.getValue() < PRIVILEGED_PORTS) {
- throw new AccessControlException("Unable to bind port " + portNumber.getValue() + " while running as non-root user.");
+ public BGPPeerAcceptorImpl(final IpAddress bindingAddress, final PortNumber portNumber,
+ final BGPPeerRegistry peerRegistry, final BGPDispatcher bgpDispatcher) {
+ this.peerRegistry = Preconditions.checkNotNull(peerRegistry);
+ this.bgpDispatcher = Preconditions.checkNotNull(bgpDispatcher);
+ this.address = getAddress(Preconditions.checkNotNull(bindingAddress), Preconditions.checkNotNull(portNumber));
+ if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot()
+ && portNumber.getValue() < PRIVILEGED_PORTS) {
+ throw new AccessControlException("Unable to bind port " + portNumber.getValue() +
+ " while running as non-root user.");
}
+ }
- this.futureChannel = bgpDispatcher.createServer(peerRegistry, getAddress(bindingAddress, portNumber));
+ public void start() {
+ LOG.debug("Instantiating BGP Peer Acceptor : {}", this.address);
+ this.futureChannel = this.bgpDispatcher.createServer(this.peerRegistry, this.address);
// Validate future success
this.futureChannel.addListener(future -> {
- Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", getAddress(bindingAddress, portNumber), future.cause());
+ Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s",
+ this.address, future.cause());
final Channel channel = this.futureChannel.channel();
if (Epoll.isAvailable()) {
- BGPPeerAcceptorImpl.this.listenerRegistration = peerRegistry.registerPeerRegisterListener(new BGPPeerAcceptorImpl.PeerRegistryListenerImpl(channel.config()));
+ this.listenerRegistration = this.peerRegistry.registerPeerRegisterListener(
+ new BGPPeerAcceptorImpl.PeerRegistryListenerImpl(channel.config()));
}
});
}
+ private InetSocketAddress getAddress(final IpAddress ipAddress, final PortNumber portNumber) {
+ final InetAddress inetAddr;
+ try {
+ inetAddr = InetAddress.getByName(ipAddress.getIpv4Address() != null ?
+ ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue());
+ } catch (final UnknownHostException e) {
+ throw new IllegalArgumentException("Illegal binding address " + ipAddress, e);
+ }
+ return new InetSocketAddress(inetAddr, portNumber.getValue());
+ }
+
+ /**
+ * This closes the acceptor and no new bgp connections will be accepted
+ * Connections already established will be preserved
+ *
+ * @throws Exception
+ */
+ @Override
+ public void close() throws Exception {
+ this.futureChannel.cancel(true);
+ this.futureChannel.channel().close();
+ if (this.listenerRegistration != null) {
+ this.listenerRegistration.close();
+ }
+ }
+
private static final class PeerRegistryListenerImpl implements PeerRegistryListener {
private final ChannelConfig channelConfig;
private final KeyMapping keys;
}
}
}
-
- private InetSocketAddress getAddress(final IpAddress ipAddress, final PortNumber portNumber) {
- final InetAddress inetAddr;
- try {
- inetAddr = InetAddress.getByName(ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue());
- } catch (final UnknownHostException e) {
- throw new IllegalArgumentException("Illegal binding address " + ipAddress, e);
- }
- return new InetSocketAddress(inetAddr, portNumber.getValue());
- }
-
- /**
- * This closes the acceptor and no new bgp connections will be accepted
- * Connections already established will be preserved
- *
- * @throws Exception
- */
- @Override
- public void close() throws Exception {
- this.futureChannel.cancel(true);
- this.futureChannel.channel().close();
- if (BGPPeerAcceptorImpl.this.listenerRegistration != null) {
- BGPPeerAcceptorImpl.this.listenerRegistration.close();
- }
- }
}
<reference id="BGPDispatcher" interface="org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher"/>
<odl:clustered-app-config id="bgpPeerAcceptorConfig"
- binding-class="org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.acceptor.config.rev161003.BgpPeerAcceptorConfig"
+ binding-class="org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.peer.acceptor.config.rev161003.BgpPeerAcceptorConfig"
list-key-value="default">
<odl:default-config>
<![CDATA[
- <bgp-peer-acceptor-config xmlns="urn:opendaylight:params:xml:ns:yang:bgp-peer-acceptor-config">
+ <bgp-peer-acceptor-config xmlns="urn:opendaylight:params:xml:ns:yang:odl-bgp-peer-acceptor-config">
<config-name>default</config-name>
<binding-port>1790</binding-port>
</bgp-peer-acceptor-config>
</odl:default-config>
</odl:clustered-app-config>
- <bean id="bgpPeerAcceptor" class="org.opendaylight.protocol.bgp.peer.acceptor.BGPPeerAcceptorImpl">
- <argument ref="bgpPeerAcceptorConfig"/>
+ <bean id="bgpPeerAcceptor" class="org.opendaylight.protocol.bgp.peer.acceptor.BGPPeerAcceptorImpl"
+ init-method="start" destroy-method="close">
+ <argument>
+ <bean factory-ref="bgpPeerAcceptorConfig" factory-method="getBindingAddress"/>
+ </argument>
+ <argument>
+ <bean factory-ref="bgpPeerAcceptorConfig" factory-method="getBindingPort"/>
+ </argument>
<argument ref="BGPPeerRegistry"/>
<argument ref="BGPDispatcher"/>
</bean>
import com.google.common.collect.Sets;
import io.netty.util.concurrent.Future;
import java.net.InetSocketAddress;
-import java.util.concurrent.ExecutionException;
import org.junit.Assert;
import org.junit.Test;
import org.opendaylight.protocol.bgp.rib.impl.AbstractBGPDispatcherTest;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.acceptor.config.rev161003.BgpPeerAcceptorConfig;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.acceptor.config.rev161003.BgpPeerAcceptorConfigBuilder;
public class BGPPeerAcceptorImplTest extends AbstractBGPDispatcherTest {
@Test
- public void testBGPPeerAcceptorImpl() throws InterruptedException, ExecutionException {
+ public void testBGPPeerAcceptorImpl() throws Exception {
final InetSocketAddress inetServerAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
final IpAddress serverIpAddress = new IpAddress(new Ipv4Address(InetSocketAddressUtil.toHostAndPort(inetServerAddress).getHostText()));
- final BgpPeerAcceptorConfig config = new BgpPeerAcceptorConfigBuilder().setBindingAddress(serverIpAddress)
- .setBindingPort(new PortNumber(InetSocketAddressUtil.toHostAndPort(inetServerAddress).getPort())).build();
+ final PortNumber portNumber = new PortNumber(InetSocketAddressUtil.toHostAndPort(inetServerAddress).getPort());
this.registry.addPeer(serverIpAddress, this.serverListener, createPreferences(inetServerAddress));
- final BGPPeerAcceptorImpl bgpPeerAcceptor = new BGPPeerAcceptorImpl(config, this.registry, this.serverDispatcher);
- final Future<BGPSessionImpl> futureClient = this.clientDispatcher.createClient(this.clientAddress, inetServerAddress, this.registry, 2, true);
+ final BGPPeerAcceptorImpl bgpPeerAcceptor = new BGPPeerAcceptorImpl(serverIpAddress, portNumber,
+ this.registry, this.serverDispatcher);
+ bgpPeerAcceptor.start();
+ final Future<BGPSessionImpl> futureClient = this.clientDispatcher
+ .createClient(this.clientAddress, inetServerAddress, this.registry, 2, true);
waitFutureSuccess(futureClient);
final BGPSessionImpl session = futureClient.get();
Assert.assertEquals(State.UP, this.clientListener.getState());
Assert.assertEquals(Sets.newHashSet(IPV_4_TT), session.getAdvertisedTableTypes());
session.close();
checkIdleState(this.clientListener);
+
+ bgpPeerAcceptor.close();
}
}
\ No newline at end of file