Changed lookup of Controller IP to the following:
[netvirt.git] / ovsdb / src / test / java / org / opendaylight / ovsdb / lib / jsonrpc / NettyBootStrapper.java
1 package org.opendaylight.ovsdb.lib.jsonrpc;
2
3 import io.netty.bootstrap.ServerBootstrap;
4 import io.netty.channel.*;
5 import io.netty.channel.nio.NioEventLoopGroup;
6 import io.netty.channel.socket.SocketChannel;
7 import io.netty.channel.socket.nio.NioServerSocketChannel;
8
9 import java.util.concurrent.TimeUnit;
10
11 public class NettyBootStrapper {
12
13     EventLoopGroup bossGroup = null;
14     EventLoopGroup workerGroup = null;
15     ChannelFuture f = null;
16
17     public ChannelFuture startServer(int localPort, final ChannelHandler... handlers) throws Exception {
18         // Configure the server.
19         bossGroup = new NioEventLoopGroup();
20         workerGroup = new NioEventLoopGroup();
21         ServerBootstrap b = new ServerBootstrap();
22         b.group(bossGroup, workerGroup)
23                 .channel(NioServerSocketChannel.class)
24                 .option(ChannelOption.SO_BACKLOG, 100)
25                 .localAddress(localPort)
26                 .childOption(ChannelOption.TCP_NODELAY, true)
27                 .childHandler(new ChannelInitializer<SocketChannel>() {
28
29                     @Override
30                     public void initChannel(SocketChannel ch) throws Exception {
31                         for (ChannelHandler handler : handlers) {
32                             ch.pipeline().addLast(handler);
33                         }
34                     }
35                 });
36
37         // Start the server.
38         f = b.bind().sync();
39         return f;
40     }
41
42     public void stopServer() throws InterruptedException {
43         try {
44
45             ChannelFuture channelFuture = f.channel().closeFuture();
46             channelFuture.get(1000, TimeUnit.MILLISECONDS);
47             if (!channelFuture.isDone()) {
48                 f.channel().unsafe().closeForcibly();
49             }
50
51             bossGroup.shutdownGracefully();
52             workerGroup.shutdownGracefully();
53
54             // Wait until all threads are terminated.
55             bossGroup.terminationFuture().sync();
56             workerGroup.terminationFuture().sync();
57         } catch (Exception e) {
58             //ignore
59         }
60     }
61
62 }