Convert mdsal-netconf-ssh to OSGi DS
[netconf.git] / netconf / mdsal-netconf-ssh / src / test / java / org / opendaylight / netconf / ssh / ProxyServerHandler.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.ssh;
9
10 import io.netty.bootstrap.Bootstrap;
11 import io.netty.buffer.Unpooled;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.local.LocalAddress;
19 import io.netty.channel.local.LocalChannel;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ProxyServerHandler extends ChannelInboundHandlerAdapter {
24     private static final Logger LOG = LoggerFactory.getLogger(ProxyServerHandler.class);
25     private final Bootstrap clientBootstrap;
26     private final LocalAddress localAddress;
27
28
29     private Channel clientChannel;
30
31     public ProxyServerHandler(final EventLoopGroup bossGroup, final LocalAddress localAddress) {
32         clientBootstrap = new Bootstrap();
33         clientBootstrap.group(bossGroup).channel(LocalChannel.class);
34         this.localAddress = localAddress;
35     }
36
37     @Override
38     public void channelActive(final ChannelHandlerContext remoteCtx) {
39         final ProxyClientHandler clientHandler = new ProxyClientHandler(remoteCtx);
40         clientBootstrap.handler(new ChannelInitializer<LocalChannel>() {
41             @Override
42             public void initChannel(final LocalChannel ch) {
43                 ch.pipeline().addLast(clientHandler);
44             }
45         });
46         ChannelFuture clientChannelFuture = clientBootstrap.connect(localAddress).awaitUninterruptibly();
47         clientChannel = clientChannelFuture.channel();
48         clientChannel.writeAndFlush(Unpooled.copiedBuffer("connected\n".getBytes()));
49     }
50
51     @Override
52     public void channelInactive(final ChannelHandlerContext ctx) {
53         LOG.info("channelInactive - closing client connection");
54         clientChannel.close();
55     }
56
57     @Override
58     public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
59         LOG.debug("Writing to client {}", msg);
60         clientChannel.write(msg);
61     }
62
63     @Override
64     public void channelReadComplete(final ChannelHandlerContext ctx) {
65         LOG.debug("flushing");
66         clientChannel.flush();
67     }
68
69     @Override
70     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
71         // Close the connection when an exception is raised.
72         LOG.warn("Unexpected exception from downstream.", cause);
73         ctx.close();
74     }
75 }
76