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