Merge "Bug 1073: Added Transaction Chain support to InMemoryDataTreeModification."
[controller.git] / opendaylight / netconf / netconf-tcp / src / main / java / org / opendaylight / controller / netconf / tcp / netty / ProxyServer.java
1 /*
2  * Copyright (c) 2014 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.controller.netconf.tcp.netty;
10
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelInitializer;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.local.LocalAddress;
17 import io.netty.channel.local.LocalChannel;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.SocketChannel;
20 import io.netty.channel.socket.nio.NioServerSocketChannel;
21 import io.netty.handler.logging.LogLevel;
22 import io.netty.handler.logging.LoggingHandler;
23 import java.net.InetSocketAddress;
24
25 public class ProxyServer implements AutoCloseable {
26     private final EventLoopGroup bossGroup = new NioEventLoopGroup();
27     private final EventLoopGroup workerGroup = new NioEventLoopGroup();
28     private final ChannelFuture channelFuture;
29
30     public ProxyServer(InetSocketAddress address, final LocalAddress localAddress) {
31         // Configure the server.
32         final Bootstrap clientBootstrap = new Bootstrap();
33         clientBootstrap.group(bossGroup).channel(LocalChannel.class);
34
35         ServerBootstrap serverBootstrap = new ServerBootstrap();
36         serverBootstrap.group(bossGroup, workerGroup)
37                 .channel(NioServerSocketChannel.class)
38                 .handler(new LoggingHandler(LogLevel.DEBUG))
39                 .childHandler(new ChannelInitializer<SocketChannel>() {
40                     @Override
41                     public void initChannel(SocketChannel ch) throws Exception {
42                         ch.pipeline().addLast(new ProxyServerHandler(clientBootstrap, localAddress));
43                     }
44                 });
45
46         // Start the server.
47         channelFuture = serverBootstrap.bind(address).syncUninterruptibly();
48     }
49
50     @Override
51     public void close() {
52         channelFuture.channel().close();
53         bossGroup.shutdownGracefully();
54         workerGroup.shutdownGracefully();
55     }
56 }