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 java.net.InetAddress;
8 import java.net.InetSocketAddress;
9 import java.net.UnknownHostException;
10 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
11 import org.opendaylight.protocol.bgp.rib.impl.server.BGPServerSessionValidator;
14 * BGP peer acceptor that handles incomming bgp connections.
16 public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerAcceptorModule {
17 public BGPPeerAcceptorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
18 super(identifier, dependencyResolver);
21 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) {
22 super(identifier, dependencyResolver, oldModule, oldInstance);
26 public void customValidation() {
27 // Try to parse address
30 } catch (final IllegalArgumentException e) {
31 throw new JmxAttributeValidationException("Unable to resolve configured address", e, Lists.newArrayList(bindingAddressJmxAttribute, bindingPortJmxAttribute));
36 public java.lang.AutoCloseable createInstance() {
37 final ChannelFuture future = getBgpDispatcherDependency().createServer(getPeerRegistryDependency(), getAddress(), new BGPServerSessionValidator());
39 // Validate future success
40 future.addListener(new GenericFutureListener<Future<Void>>() {
42 public void operationComplete(Future<Void> future) throws Exception {
43 if(future.isSuccess() == false) {
44 throw new IllegalStateException(String.format("Unable to start bgp server on %s", getAddress()), future.cause());
49 return new AutoCloseable() {
51 public void close() throws Exception {
52 // This closes the acceptor and no new bgp connections will be accepted
53 // Connections already established will be preserved
55 future.channel().close();
60 private InetSocketAddress getAddress() {
61 final InetAddress inetAddr;
63 inetAddr = InetAddress.getByName(getBindingAddress()
64 .getIpv4Address() != null ? getBindingAddress()
65 .getIpv4Address().getValue() : getBindingAddress()
66 .getIpv6Address().getValue());
67 } catch (final UnknownHostException e) {
68 throw new IllegalArgumentException("Illegal binding address " + getBindingAddress(), e);
70 return new InetSocketAddress(inetAddr, getBindingPort().getValue());