Replace Preconditions.CheckNotNull per RequireNonNull
[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 static java.util.Objects.requireNonNull;
12
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.bootstrap.ServerBootstrap;
15 import io.netty.buffer.PooledByteBufAllocator;
16 import io.netty.channel.Channel;
17 import io.netty.channel.ChannelFuture;
18 import io.netty.channel.ChannelInitializer;
19 import io.netty.channel.ChannelOption;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import io.netty.channel.socket.nio.NioServerSocketChannel;
23 import io.netty.channel.socket.nio.NioSocketChannel;
24 import java.net.InetSocketAddress;
25 import java.net.SocketAddress;
26 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
27 import org.opendaylight.protocol.bmp.impl.BmpHandlerFactory;
28 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 final class BmpMockDispatcher {
33
34     private static final Logger LOG = LoggerFactory.getLogger(BmpMockDispatcher.class);
35     private static final int CONNECT_TIMEOUT = 2000;
36     private static final int MAX_CONNECTIONS_COUNT = 128;
37
38     private final BmpHandlerFactory hf;
39     private final BmpSessionFactory sessionFactory;
40
41     private final EventLoopGroup bossGroup = new NioEventLoopGroup();
42     private final EventLoopGroup workerGroup = new NioEventLoopGroup();
43
44     BmpMockDispatcher(final BmpMessageRegistry registry, final BmpSessionFactory sessionFactory) {
45         this.sessionFactory = requireNonNull(sessionFactory);
46         requireNonNull(registry);
47         this.hf = new BmpHandlerFactory(registry);
48     }
49
50     private Bootstrap createClientInstance(final SocketAddress localAddress) {
51         final NioEventLoopGroup workergroup = new NioEventLoopGroup();
52         final Bootstrap bootstrap = new Bootstrap();
53
54         bootstrap.channel(NioSocketChannel.class);
55         bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
56         bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT);
57         bootstrap.option(ChannelOption.SO_REUSEADDR, true);
58         bootstrap.group(workergroup);
59
60         bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
61             @Override
62             protected void initChannel(final NioSocketChannel ch) throws Exception {
63                 ch.pipeline().addLast(BmpMockDispatcher.this.sessionFactory.getSession(ch, null));
64                 ch.pipeline().addLast(BmpMockDispatcher.this.hf.getEncoders());
65             }
66         });
67         bootstrap.localAddress(localAddress);
68         return bootstrap;
69     }
70
71     ChannelFuture createClient(final SocketAddress localAddress, final SocketAddress remoteAddress) {
72         requireNonNull(localAddress);
73         requireNonNull(remoteAddress);
74
75         // ideally we should use Bootstrap clones here
76         final Bootstrap bootstrap = createClientInstance(localAddress);
77         final ChannelFuture channelFuture = bootstrap.connect(remoteAddress);
78         LOG.info("BMP client {} <--> {} deployed", localAddress, remoteAddress);
79         return channelFuture;
80     }
81
82     private ServerBootstrap createServerInstance() {
83         final ServerBootstrap serverBootstrap = new ServerBootstrap();
84         serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
85             @Override
86             protected void initChannel(final Channel ch) throws Exception {
87                 ch.pipeline().addLast(BmpMockDispatcher.this.sessionFactory.getSession(ch, null));
88                 ch.pipeline().addLast(BmpMockDispatcher.this.hf.getEncoders());
89             }
90         });
91
92         serverBootstrap.option(ChannelOption.SO_BACKLOG, MAX_CONNECTIONS_COUNT);
93         serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
94         serverBootstrap.channel(NioServerSocketChannel.class);
95         serverBootstrap.group(this.bossGroup, this.workerGroup);
96         return serverBootstrap;
97     }
98
99     ChannelFuture createServer(final InetSocketAddress localAddress) {
100         requireNonNull(localAddress);
101         final ServerBootstrap serverBootstrap = createServerInstance();
102         final ChannelFuture channelFuture = serverBootstrap.bind(localAddress);
103         LOG.info("Initiated BMP server at {}.", localAddress);
104         return channelFuture;
105     }
106 }