BUG-338 Allow incomming BGP connections.
[bgpcep.git] / bgp / rib-impl-config / src / main / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPPeerAcceptorModule.java
1 package org.opendaylight.controller.config.yang.bgp.rib.impl;
2
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;
12
13 /**
14 * BGP peer acceptor that handles incomming bgp connections.
15 */
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);
19     }
20
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);
23     }
24
25     @Override
26     public void customValidation() {
27         // Try to parse address
28         try {
29             getAddress();
30         } catch (final IllegalArgumentException e) {
31             throw new JmxAttributeValidationException("Unable to resolve configured address", e, Lists.newArrayList(bindingAddressJmxAttribute, bindingPortJmxAttribute));
32         }
33     }
34
35     @Override
36     public java.lang.AutoCloseable createInstance() {
37         final ChannelFuture future = getBgpDispatcherDependency().createServer(getPeerRegistryDependency(), getAddress(), new BGPServerSessionValidator());
38
39         // Validate future success
40         future.addListener(new GenericFutureListener<Future<Void>>() {
41             @Override
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());
45                 }
46             }
47         });
48
49         return new AutoCloseable() {
50             @Override
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
54                 future.cancel(true);
55                 future.channel().close();
56             }
57         };
58     }
59
60     private InetSocketAddress getAddress() {
61         final InetAddress inetAddr;
62         try {
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);
69         }
70         return new InetSocketAddress(inetAddr, getBindingPort().getValue());
71     }
72
73 }