BUG-3888 : refactoring, removed unused session validators
[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.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;
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
20     private static final int PRIVILEGED_PORTS = 1024;
21
22     public BGPPeerAcceptorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
23         super(identifier, dependencyResolver);
24     }
25
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);
28     }
29
30     @Override
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.");
35         }
36         // Try to parse address
37         try {
38             getAddress();
39         } catch (final IllegalArgumentException e) {
40             throw new JmxAttributeValidationException("Unable to resolve configured address", e, Lists.newArrayList(bindingAddressJmxAttribute, bindingPortJmxAttribute));
41         }
42     }
43
44     @Override
45     public java.lang.AutoCloseable createInstance() {
46         final ChannelFuture future = getAcceptingBgpDispatcherDependency().createServer(getAcceptingPeerRegistryDependency(), getAddress());
47
48         // Validate future success
49         future.addListener(new GenericFutureListener<Future<Void>>() {
50             @Override
51             public void operationComplete(final Future<Void> future) {
52                 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", getAddress(), future.cause());
53             }
54         });
55
56         return new AutoCloseable() {
57             @Override
58             public void close() {
59                 // This closes the acceptor and no new bgp connections will be accepted
60                 // Connections already established will be preserved
61                 future.cancel(true);
62                 future.channel().close();
63             }
64         };
65     }
66
67     private InetSocketAddress getAddress() {
68         final InetAddress inetAddr;
69         try {
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);
76         }
77         return new InetSocketAddress(inetAddr, getBindingPort().getValue());
78     }
79
80 }