Added missing headers
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPPeerAcceptorModule.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yang.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.util.concurrent.Future;
14 import io.netty.util.concurrent.GenericFutureListener;
15 import io.netty.util.internal.PlatformDependent;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import java.net.UnknownHostException;
19 import java.security.AccessControlException;
20 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
21
22 /**
23 * BGP peer acceptor that handles incoming bgp connections.
24 */
25 public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerAcceptorModule {
26
27     private static final int PRIVILEGED_PORTS = 1024;
28
29     public BGPPeerAcceptorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
30         super(identifier, dependencyResolver);
31     }
32
33     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) {
34         super(identifier, dependencyResolver, oldModule, oldInstance);
35     }
36
37     @Override
38     public void customValidation() {
39         // check if unix root user
40         if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && getBindingPort().getValue() < PRIVILEGED_PORTS) {
41             throw new AccessControlException("Unable to bind port " + getBindingPort().getValue() + " while running as non-root user.");
42         }
43         // Try to parse address
44         try {
45             getAddress();
46         } catch (final IllegalArgumentException e) {
47             throw new JmxAttributeValidationException("Unable to resolve configured address", e, Lists.newArrayList(bindingAddressJmxAttribute, bindingPortJmxAttribute));
48         }
49     }
50
51     @Override
52     public java.lang.AutoCloseable createInstance() {
53         final ChannelFuture future = getAcceptingBgpDispatcherDependency().createServer(getAcceptingPeerRegistryDependency(), getAddress());
54
55         // Validate future success
56         future.addListener(new GenericFutureListener<Future<Void>>() {
57             @Override
58             public void operationComplete(final Future<Void> future) {
59                 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", getAddress(), future.cause());
60             }
61         });
62
63         return new AutoCloseable() {
64             @Override
65             public void close() {
66                 // This closes the acceptor and no new bgp connections will be accepted
67                 // Connections already established will be preserved
68                 future.cancel(true);
69                 future.channel().close();
70             }
71         };
72     }
73
74     private InetSocketAddress getAddress() {
75         final InetAddress inetAddr;
76         try {
77             inetAddr = InetAddress.getByName(getBindingAddress()
78                     .getIpv4Address() != null ? getBindingAddress()
79                     .getIpv4Address().getValue() : getBindingAddress()
80                     .getIpv6Address().getValue());
81         } catch (final UnknownHostException e) {
82             throw new IllegalArgumentException("Illegal binding address " + getBindingAddress(), e);
83         }
84         return new InetSocketAddress(inetAddr, getBindingPort().getValue());
85     }
86
87 }