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
9 package org.opendaylight.protocol.bmp.mock;
11 import com.google.common.base.Preconditions;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelInitializer;
15 import io.netty.channel.ChannelOption;
16 import io.netty.channel.nio.NioEventLoopGroup;
17 import io.netty.channel.socket.nio.NioSocketChannel;
18 import java.net.SocketAddress;
19 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
20 import org.opendaylight.protocol.bmp.impl.BmpHandlerFactory;
21 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public final class BmpMockDispatcher {
27 private static final Logger LOG = LoggerFactory.getLogger(BmpMockDispatcher.class);
28 private static final int CONNECT_TIMEOUT = 2000;
30 final BmpHandlerFactory hf;
31 private final BmpSessionFactory sessionFactory;
33 public BmpMockDispatcher(final BmpMessageRegistry registry, final BmpSessionFactory sessionFactory) {
34 this.sessionFactory = Preconditions.checkNotNull(sessionFactory);
35 Preconditions.checkNotNull(registry);
36 this.hf = new BmpHandlerFactory(registry);
39 public ChannelFuture createClient(final SocketAddress localAddress, final SocketAddress remoteAddress) {
40 final NioEventLoopGroup workergroup = new NioEventLoopGroup();
41 final Bootstrap b = new Bootstrap();
43 b.channel(NioSocketChannel.class);
44 b.option(ChannelOption.SO_KEEPALIVE, true);
45 b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT);
48 b.handler(new ChannelInitializer<NioSocketChannel>() {
50 protected void initChannel(final NioSocketChannel ch) throws Exception {
51 ch.pipeline().addLast(BmpMockDispatcher.this.sessionFactory.getSession(ch, null));
52 ch.pipeline().addLast(BmpMockDispatcher.this.hf.getEncoders());
55 b.localAddress(localAddress);
56 b.remoteAddress(remoteAddress);
57 LOG.debug("BMP client {} <--> {} deployed", localAddress, remoteAddress);