HONEYCOMB-374: move BGP to minimal-distribution
[honeycomb.git] / infra / northbound / bgp / src / main / java / io / fd / honeycomb / infra / bgp / BgpServerProvider.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.infra.bgp;
18
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;
39
40 public final class BgpServerProvider  extends ProviderTrait<BgpServerProvider.BgpServer> {
41     private static final Logger LOG = LoggerFactory.getLogger(BgpServerProvider.class);
42     @Inject
43     private BgpConfiguration cfg;
44     @Inject
45     private BGPPeerRegistry peerRegistry;
46     @Inject
47     private BGPDispatcher dispatcher;
48
49     @Override
50     protected BgpServer create() {
51         // code based on org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerAcceptorModule from Boron-SR3
52         final InetAddress bindingAddress;
53         try {
54             bindingAddress = InetAddress.getByName(cfg.bgpBindingAddress.get());
55         } catch (UnknownHostException e) {
56             throw new IllegalArgumentException("Illegal BGP binding address", e);
57         }
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()));
66             }
67         });
68         final BgpServer server = new BgpServer(localServer);
69         LOG.debug("BgpServer successfully started.");
70         return server;
71     }
72
73     public static final class BgpServer {
74         private ChannelFuture localServer;
75
76         BgpServer(final ChannelFuture localServer) {
77             this.localServer = localServer;
78         }
79
80         public ChannelFuture getLocalServer() {
81             return localServer;
82         }
83     }
84
85     private static final class PeerRegistryListenerImpl implements PeerRegistryListener {
86         private final ChannelConfig channelConfig;
87         private final KeyMapping keys;
88
89         PeerRegistryListenerImpl(final ChannelConfig channelConfig) {
90             this.channelConfig = channelConfig;
91             this.keys = KeyMapping.getKeyMapping();
92         }
93         @Override
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);
98             }
99         }
100         @Override
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);
104             }
105         }
106     }
107 }