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