01a1261849b09299db03d68d7f3c278f552b48f2
[netconf.git] / netconf / mdsal-netconf-ssh / src / test / java / org / opendaylight / netconf / 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 package org.opendaylight.netconf.netty;
9
10 import io.netty.bootstrap.ServerBootstrap;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.ChannelHandler;
13 import io.netty.channel.ChannelInitializer;
14 import io.netty.channel.ChannelOption;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.local.LocalAddress;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import io.netty.channel.socket.SocketChannel;
19 import io.netty.channel.socket.nio.NioServerSocketChannel;
20 import io.netty.handler.logging.LogLevel;
21 import io.netty.handler.logging.LoggingHandler;
22 import java.net.InetSocketAddress;
23 import org.opendaylight.netconf.util.NetconfConfiguration;
24
25 public class ProxyServer implements Runnable {
26     private final ProxyHandlerFactory proxyHandlerFactory;
27
28     public ProxyServer(final ProxyHandlerFactory proxyHandlerFactory) {
29         this.proxyHandlerFactory = proxyHandlerFactory;
30     }
31
32     @Override
33     public void run() {
34         // Configure the server.
35         final EventLoopGroup bossGroup = new NioEventLoopGroup();
36         EventLoopGroup workerGroup = new NioEventLoopGroup();
37         try {
38             final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
39             ServerBootstrap serverBootstrap = new ServerBootstrap();
40             serverBootstrap.group(bossGroup, workerGroup)
41                     .channel(NioServerSocketChannel.class)
42                     .option(ChannelOption.SO_BACKLOG, 100)
43                     .handler(new LoggingHandler(LogLevel.INFO))
44                     .childHandler(new ChannelInitializer<SocketChannel>() {
45                         @Override
46                         public void initChannel(final SocketChannel ch) throws Exception {
47                             ch.pipeline().addLast(proxyHandlerFactory.create(bossGroup, localAddress));
48                         }
49                     });
50
51             // Start the server.
52             InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8080);
53             ChannelFuture future = serverBootstrap.bind(address).sync();
54
55             // Wait until the server socket is closed.
56             future.channel().closeFuture().sync();
57         } catch (InterruptedException e) {
58             throw new RuntimeException(e);
59         } finally {
60             // Shut down all event loops to terminate all threads.
61             bossGroup.shutdownGracefully();
62             workerGroup.shutdownGracefully();
63         }
64     }
65
66     public interface ProxyHandlerFactory {
67         ChannelHandler create(EventLoopGroup bossGroup, LocalAddress localAddress);
68     }
69
70     public static void main(final String[] args) {
71         ProxyHandlerFactory proxyHandlerFactory = ProxyServerHandler::new;
72         start(proxyHandlerFactory);
73     }
74
75     public static void start(final ProxyHandlerFactory proxyHandlerFactory) {
76         new Thread(new EchoServer()).start();
77         new Thread(new ProxyServer(proxyHandlerFactory)).start();
78     }
79 }