2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.bgp.rib.impl;
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;
23 * BGP peer acceptor that handles incoming bgp connections.
25 public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerAcceptorModule {
27 private static final int PRIVILEGED_PORTS = 1024;
29 public BGPPeerAcceptorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
30 super(identifier, dependencyResolver);
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);
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.");
43 // Try to parse address
46 } catch (final IllegalArgumentException e) {
47 throw new JmxAttributeValidationException("Unable to resolve configured address", e, Lists.newArrayList(bindingAddressJmxAttribute, bindingPortJmxAttribute));
52 public java.lang.AutoCloseable createInstance() {
53 final ChannelFuture future = getAcceptingBgpDispatcherDependency().createServer(getAcceptingPeerRegistryDependency(), getAddress());
55 // Validate future success
56 future.addListener(new GenericFutureListener<Future<Void>>() {
58 public void operationComplete(final Future<Void> future) {
59 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", getAddress(), future.cause());
63 return new AutoCloseable() {
66 // This closes the acceptor and no new bgp connections will be accepted
67 // Connections already established will be preserved
69 future.channel().close();
74 private InetSocketAddress getAddress() {
75 final InetAddress inetAddr;
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);
84 return new InetSocketAddress(inetAddr, getBindingPort().getValue());