2 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.infra.bgp;
19 import com.google.common.base.Preconditions;
20 import com.google.inject.Inject;
21 import io.fd.honeycomb.infra.distro.ProviderTrait;
22 import io.netty.channel.Channel;
23 import io.netty.channel.ChannelConfig;
24 import io.netty.channel.ChannelFuture;
25 import io.netty.channel.epoll.Epoll;
26 import io.netty.channel.epoll.EpollChannelOption;
27 import java.net.InetAddress;
28 import java.net.InetSocketAddress;
29 import java.net.UnknownHostException;
30 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistryListener;
34 import org.opendaylight.protocol.concepts.KeyMapping;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class BgpServerProvider extends ProviderTrait<BgpServerProvider.BgpServer> {
41 private static final Logger LOG = LoggerFactory.getLogger(BgpServerProvider.class);
43 private BgpConfiguration cfg;
45 private BGPPeerRegistry peerRegistry;
47 private BGPDispatcher dispatcher;
50 protected BgpServer create() {
51 // code based on org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerAcceptorModule from Boron-SR3
52 final InetAddress bindingAddress;
54 bindingAddress = InetAddress.getByName(cfg.bgpBindingAddress.get());
55 } catch (UnknownHostException e) {
56 throw new IllegalArgumentException("Illegal BGP binding address", e);
58 final InetSocketAddress address = new InetSocketAddress(bindingAddress, cfg.bgpPort.get());
59 LOG.debug("Creating BgpServer for {}", address);
60 final ChannelFuture localServer = dispatcher.createServer(peerRegistry, address);
61 localServer.addListener(future -> {
62 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", address, future.cause());
63 final Channel channel = localServer.channel();
64 if (Epoll.isAvailable()) {
65 peerRegistry.registerPeerRegisterListener(new PeerRegistryListenerImpl(channel.config()));
68 final BgpServer server = new BgpServer(localServer);
69 LOG.debug("BgpServer successfully started.");
73 public static final class BgpServer {
74 private ChannelFuture localServer;
76 BgpServer(final ChannelFuture localServer) {
77 this.localServer = localServer;
80 public ChannelFuture getLocalServer() {
85 private static final class PeerRegistryListenerImpl implements PeerRegistryListener {
86 private final ChannelConfig channelConfig;
87 private final KeyMapping keys;
89 PeerRegistryListenerImpl(final ChannelConfig channelConfig) {
90 this.channelConfig = channelConfig;
91 this.keys = KeyMapping.getKeyMapping();
94 public void onPeerAdded(final IpAddress ip, final BGPSessionPreferences prefs) {
95 if (prefs.getMd5Password().isPresent()) {
96 this.keys.put(IetfInetUtil.INSTANCE.inetAddressFor(ip), prefs.getMd5Password().get());
97 this.channelConfig.setOption(EpollChannelOption.TCP_MD5SIG, this.keys);
101 public void onPeerRemoved(final IpAddress ip) {
102 if (this.keys.remove(IetfInetUtil.INSTANCE.inetAddressFor(ip)) != null) {
103 this.channelConfig.setOption(EpollChannelOption.TCP_MD5SIG, this.keys);