Fix minor sonar issues
[bgpcep.git] / bgp / bmp-mock / src / main / java / org / opendaylight / protocol / bmp / mock / BmpMockDispatcher.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bmp.mock;
10
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;
24
25 public final class BmpMockDispatcher {
26
27     private static final Logger LOG = LoggerFactory.getLogger(BmpMockDispatcher.class);
28     private static final int CONNECT_TIMEOUT = 2000;
29
30     final BmpHandlerFactory hf;
31     private final BmpSessionFactory sessionFactory;
32
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);
37     }
38
39     public ChannelFuture createClient(final SocketAddress localAddress, final SocketAddress remoteAddress) {
40         final NioEventLoopGroup workergroup = new NioEventLoopGroup();
41         final Bootstrap b = new Bootstrap();
42
43         b.channel(NioSocketChannel.class);
44         b.option(ChannelOption.SO_KEEPALIVE, true);
45         b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT);
46         b.group(workergroup);
47
48         b.handler(new ChannelInitializer<NioSocketChannel>() {
49             @Override
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());
53             }
54         });
55         b.localAddress(localAddress);
56         b.remoteAddress(remoteAddress);
57         LOG.debug("BMP client {} <--> {} deployed", localAddress, remoteAddress);
58         return b.connect();
59     }
60 }