From 106fdfe560a0e76c28780eceb3362cd78b06ebf2 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Fri, 30 Jun 2017 13:11:22 +0200 Subject: HONEYCOMB-374: move BGP to minimal-distribution Separate BGP distribution was temporary workaround for HONEYCOMB-358 and (partially) HONEYCOMB-360. BGP northbound interface is disabled by default. Change-Id: I72a0f83f40ea96bee478be7d6560f2112611d80f Signed-off-by: Marek Gradzki --- .../fd/honeycomb/infra/bgp/BgpServerProvider.java | 107 +++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 infra/northbound/bgp/src/main/java/io/fd/honeycomb/infra/bgp/BgpServerProvider.java (limited to 'infra/northbound/bgp/src/main/java/io/fd/honeycomb/infra/bgp/BgpServerProvider.java') diff --git a/infra/northbound/bgp/src/main/java/io/fd/honeycomb/infra/bgp/BgpServerProvider.java b/infra/northbound/bgp/src/main/java/io/fd/honeycomb/infra/bgp/BgpServerProvider.java new file mode 100644 index 000000000..b972ee297 --- /dev/null +++ b/infra/northbound/bgp/src/main/java/io/fd/honeycomb/infra/bgp/BgpServerProvider.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.infra.bgp; + +import com.google.common.base.Preconditions; +import com.google.inject.Inject; +import io.fd.honeycomb.infra.distro.ProviderTrait; +import io.netty.channel.Channel; +import io.netty.channel.ChannelConfig; +import io.netty.channel.ChannelFuture; +import io.netty.channel.epoll.Epoll; +import io.netty.channel.epoll.EpollChannelOption; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher; +import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry; +import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences; +import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistryListener; +import org.opendaylight.protocol.concepts.KeyMapping; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class BgpServerProvider extends ProviderTrait { + private static final Logger LOG = LoggerFactory.getLogger(BgpServerProvider.class); + @Inject + private BgpConfiguration cfg; + @Inject + private BGPPeerRegistry peerRegistry; + @Inject + private BGPDispatcher dispatcher; + + @Override + protected BgpServer create() { + // code based on org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerAcceptorModule from Boron-SR3 + final InetAddress bindingAddress; + try { + bindingAddress = InetAddress.getByName(cfg.bgpBindingAddress.get()); + } catch (UnknownHostException e) { + throw new IllegalArgumentException("Illegal BGP binding address", e); + } + final InetSocketAddress address = new InetSocketAddress(bindingAddress, cfg.bgpPort.get()); + LOG.debug("Creating BgpServer for {}", address); + final ChannelFuture localServer = dispatcher.createServer(peerRegistry, address); + localServer.addListener(future -> { + Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", address, future.cause()); + final Channel channel = localServer.channel(); + if (Epoll.isAvailable()) { + peerRegistry.registerPeerRegisterListener(new PeerRegistryListenerImpl(channel.config())); + } + }); + final BgpServer server = new BgpServer(localServer); + LOG.debug("BgpServer successfully started."); + return server; + } + + public static final class BgpServer { + private ChannelFuture localServer; + + BgpServer(final ChannelFuture localServer) { + this.localServer = localServer; + } + + public ChannelFuture getLocalServer() { + return localServer; + } + } + + private static final class PeerRegistryListenerImpl implements PeerRegistryListener { + private final ChannelConfig channelConfig; + private final KeyMapping keys; + + PeerRegistryListenerImpl(final ChannelConfig channelConfig) { + this.channelConfig = channelConfig; + this.keys = KeyMapping.getKeyMapping(); + } + @Override + public void onPeerAdded(final IpAddress ip, final BGPSessionPreferences prefs) { + if (prefs.getMd5Password().isPresent()) { + this.keys.put(IetfInetUtil.INSTANCE.inetAddressFor(ip), prefs.getMd5Password().get()); + this.channelConfig.setOption(EpollChannelOption.TCP_MD5SIG, this.keys); + } + } + @Override + public void onPeerRemoved(final IpAddress ip) { + if (this.keys.remove(IetfInetUtil.INSTANCE.inetAddressFor(ip)) != null) { + this.channelConfig.setOption(EpollChannelOption.TCP_MD5SIG, this.keys); + } + } + } +} -- cgit 1.2.3-korg