Fix checkstyle warnings in netconf-ssh.
[controller.git] / opendaylight / netconf / netconf-ssh / src / test / java / org / opendaylight / controller / netconf / netty / 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
9 package org.opendaylight.controller.netconf.netty;
10
11 import com.google.common.base.Charsets;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelHandlerContext;
18 import io.netty.channel.ChannelInboundHandlerAdapter;
19 import io.netty.channel.ChannelInitializer;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.local.LocalAddress;
22 import io.netty.channel.local.LocalChannel;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class ProxyServerHandler extends ChannelInboundHandlerAdapter {
27     private static final Logger LOG = LoggerFactory.getLogger(ProxyServerHandler.class);
28     private final Bootstrap clientBootstrap;
29     private final LocalAddress localAddress;
30
31
32     private Channel clientChannel;
33
34     public ProxyServerHandler(EventLoopGroup bossGroup, LocalAddress localAddress) {
35         clientBootstrap = new Bootstrap();
36         clientBootstrap.group(bossGroup).channel(LocalChannel.class);
37         this.localAddress = localAddress;
38     }
39
40     @Override
41     public void channelActive(ChannelHandlerContext remoteCtx) {
42         final ProxyClientHandler clientHandler = new ProxyClientHandler(remoteCtx);
43         clientBootstrap.handler(new ChannelInitializer<LocalChannel>() {
44             @Override
45             public void initChannel(LocalChannel ch) throws Exception {
46                 ch.pipeline().addLast(clientHandler);
47             }
48         });
49         ChannelFuture clientChannelFuture = clientBootstrap.connect(localAddress).awaitUninterruptibly();
50         clientChannel = clientChannelFuture.channel();
51         clientChannel.writeAndFlush(Unpooled.copiedBuffer("connected\n".getBytes()));
52     }
53
54     @Override
55     public void channelInactive(ChannelHandlerContext ctx) {
56         LOG.info("channelInactive - closing client connection");
57         clientChannel.close();
58     }
59
60     @Override
61     public void channelRead(ChannelHandlerContext ctx, final Object msg) {
62         LOG.debug("Writing to client {}", msg);
63         clientChannel.write(msg);
64     }
65
66     @Override
67     public void channelReadComplete(ChannelHandlerContext ctx) {
68         LOG.debug("flushing");
69         clientChannel.flush();
70     }
71
72     @Override
73     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
74         // Close the connection when an exception is raised.
75         LOG.warn("Unexpected exception from downstream.", cause);
76         ctx.close();
77     }
78 }
79
80 class ProxyClientHandler extends ChannelInboundHandlerAdapter {
81     private static final Logger LOG = LoggerFactory.getLogger(ProxyClientHandler.class);
82
83     private final ChannelHandlerContext remoteCtx;
84
85
86     public ProxyClientHandler(ChannelHandlerContext remoteCtx) {
87         this.remoteCtx = remoteCtx;
88     }
89
90     @Override
91     public void channelActive(ChannelHandlerContext ctx) {
92         LOG.info("client active");
93     }
94
95     @Override
96     public void channelRead(ChannelHandlerContext ctx, Object msg) {
97         ByteBuf bb = (ByteBuf) msg;
98         LOG.info(">{}", bb.toString(Charsets.UTF_8));
99         remoteCtx.write(msg);
100     }
101
102     @Override
103     public void channelReadComplete(ChannelHandlerContext ctx) {
104         LOG.debug("Flushing server ctx");
105         remoteCtx.flush();
106     }
107
108     @Override
109     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
110         // Close the connection when an exception is raised.
111         LOG.warn("Unexpected exception from downstream", cause);
112         ctx.close();
113     }
114
115     // called both when local or remote connection dies
116     @Override
117     public void channelInactive(ChannelHandlerContext ctx) {
118         LOG.debug("channelInactive() called, closing remote client ctx");
119         remoteCtx.close();
120     }
121 }