51a9a9c4de4d6994d7e4613b5f3d10b97962ad60
[bgpcep.git] / bgp / rib-impl / 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 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;
14
15 /**
16 * BGP peer acceptor that handles incoming bgp connections.
17 */
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);
21     }
22
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);
25     }
26
27     @Override
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.");
32         }
33         // Try to parse address
34         try {
35             getAddress();
36         } catch (final IllegalArgumentException e) {
37             throw new JmxAttributeValidationException("Unable to resolve configured address", e, Lists.newArrayList(bindingAddressJmxAttribute, bindingPortJmxAttribute));
38         }
39     }
40
41     @Override
42     public java.lang.AutoCloseable createInstance() {
43         final ChannelFuture future = getAcceptingBgpDispatcherDependency().createServer(getAcceptingPeerRegistryDependency(), getAddress(), new BGPServerSessionValidator());
44
45         // Validate future success
46         future.addListener(new GenericFutureListener<Future<Void>>() {
47             @Override
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());
51                 }
52             }
53         });
54
55         return new AutoCloseable() {
56             @Override
57             public void close() {
58                 // This closes the acceptor and no new bgp connections will be accepted
59                 // Connections already established will be preserved
60                 future.cancel(true);
61                 future.channel().close();
62             }
63         };
64     }
65
66     private InetSocketAddress getAddress() {
67         final InetAddress inetAddr;
68         try {
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);
75         }
76         return new InetSocketAddress(inetAddr, getBindingPort().getValue());
77     }
78
79 }