2 * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bmp.impl;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import io.netty.bootstrap.ServerBootstrap;
14 import io.netty.buffer.PooledByteBufAllocator;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelInitializer;
18 import io.netty.channel.ChannelOption;
19 import io.netty.channel.EventLoopGroup;
20 import io.netty.channel.socket.nio.NioServerSocketChannel;
21 import java.net.InetSocketAddress;
22 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
23 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
24 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
25 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
26 import org.opendaylight.tcpmd5.api.KeyMapping;
27 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
28 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class BmpDispatcherImpl implements BmpDispatcher {
34 private static final Logger LOG = LoggerFactory.getLogger(BmpDispatcherImpl.class);
35 private static final int MAX_CONNECTIONS_COUNT = 128;
37 private final BmpHandlerFactory hf;
38 private final EventLoopGroup bossGroup;
39 private final EventLoopGroup workerGroup;
40 private final BmpSessionFactory sessionFactory;
41 private final Optional<MD5ServerChannelFactory<?>> scf;
43 public BmpDispatcherImpl(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final BmpMessageRegistry registry,
44 final BmpSessionFactory sessionFactory, final Optional<MD5ServerChannelFactory<?>> scf) {
45 this.bossGroup = Preconditions.checkNotNull(bossGroup);
46 this.workerGroup = Preconditions.checkNotNull(workerGroup);
47 this.hf = new BmpHandlerFactory(Preconditions.checkNotNull(registry));
48 this.sessionFactory = Preconditions.checkNotNull(sessionFactory);
53 public ChannelFuture createServer(final InetSocketAddress address, final BmpSessionListenerFactory slf, final Optional<KeyMapping> keys) {
54 Preconditions.checkNotNull(address);
55 Preconditions.checkNotNull(slf);
56 Preconditions.checkState(!keys.isPresent() || this.scf.isPresent(), "No key access instance available, cannot use key mapping.");
57 final ServerBootstrap b = new ServerBootstrap();
58 b.childHandler(new ChannelInitializer<Channel>() {
60 protected void initChannel(final Channel ch) throws Exception {
61 ch.pipeline().addLast(BmpDispatcherImpl.this.hf.getDecoders());
62 ch.pipeline().addLast(BmpDispatcherImpl.this.sessionFactory.getSession(ch, slf));
66 b.option(ChannelOption.SO_BACKLOG, MAX_CONNECTIONS_COUNT);
67 b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
69 if (keys.isPresent()) {
70 b.channelFactory(this.scf.get());
71 b.option(MD5ChannelOption.TCP_MD5SIG, keys.get());
72 LOG.debug("Adding MD5 keys {} to boostrap {}", keys.get(), b);
74 b.channel(NioServerSocketChannel.class);
76 b.group(this.bossGroup, this.workerGroup);
77 final ChannelFuture f = b.bind(address);
79 LOG.debug("Initiated BMP server {} at {}.", f, address);
86 this.workerGroup.shutdownGracefully();
88 this.bossGroup.shutdownGracefully();