1 package org.opendaylight.controller.config.yang.bgp.rib.impl;
3 import com.google.common.base.Preconditions;
4 import com.google.common.collect.Lists;
5 import io.netty.channel.ChannelFuture;
6 import io.netty.util.concurrent.Future;
7 import io.netty.util.concurrent.GenericFutureListener;
8 import io.netty.util.internal.PlatformDependent;
9 import java.net.InetAddress;
10 import java.net.InetSocketAddress;
11 import java.net.UnknownHostException;
12 import java.security.AccessControlException;
13 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
16 * BGP peer acceptor that handles incoming bgp connections.
18 public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerAcceptorModule {
20 private static final int PRIVILEGED_PORTS = 1024;
22 public BGPPeerAcceptorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
23 super(identifier, dependencyResolver);
26 public BGPPeerAcceptorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerAcceptorModule oldModule, final java.lang.AutoCloseable oldInstance) {
27 super(identifier, dependencyResolver, oldModule, oldInstance);
31 public void customValidation() {
32 // check if unix root user
33 if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && getBindingPort().getValue() < PRIVILEGED_PORTS) {
34 throw new AccessControlException("Unable to bind port " + getBindingPort().getValue() + " while running as non-root user.");
36 // Try to parse address
39 } catch (final IllegalArgumentException e) {
40 throw new JmxAttributeValidationException("Unable to resolve configured address", e, Lists.newArrayList(bindingAddressJmxAttribute, bindingPortJmxAttribute));
45 public java.lang.AutoCloseable createInstance() {
46 final ChannelFuture future = getAcceptingBgpDispatcherDependency().createServer(getAcceptingPeerRegistryDependency(), getAddress());
48 // Validate future success
49 future.addListener(new GenericFutureListener<Future<Void>>() {
51 public void operationComplete(final Future<Void> future) {
52 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", getAddress(), future.cause());
56 return new AutoCloseable() {
59 // This closes the acceptor and no new bgp connections will be accepted
60 // Connections already established will be preserved
62 future.channel().close();
67 private InetSocketAddress getAddress() {
68 final InetAddress inetAddr;
70 inetAddr = InetAddress.getByName(getBindingAddress()
71 .getIpv4Address() != null ? getBindingAddress()
72 .getIpv4Address().getValue() : getBindingAddress()
73 .getIpv6Address().getValue());
74 } catch (final UnknownHostException e) {
75 throw new IllegalArgumentException("Illegal binding address " + getBindingAddress(), e);
77 return new InetSocketAddress(inetAddr, getBindingPort().getValue());