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