Bug 1239 - Clean up and refactor netconf-ssh client
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / ssh / virtualsocket / VirtualSocket.java
1 /*
2  * Copyright (c) 2013 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.nettyutil.handler.ssh.virtualsocket;
10
11 import io.netty.channel.ChannelHandler;
12 import io.netty.channel.ChannelHandlerContext;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import java.net.Socket;
19 import java.net.SocketAddress;
20 import java.net.SocketException;
21 import java.nio.channels.SocketChannel;
22
23 /**
24  * Handler class providing Socket functionality to OIO client application. By using VirtualSocket user can
25  * use OIO application in asynchronous environment and NIO EventLoop. Using VirtualSocket OIO applications
26  * are able to use full potential of NIO environment.
27  */
28 //TODO: refactor - socket should be created when connection is established
29 public class VirtualSocket extends Socket implements ChannelHandler {
30     private static final String INPUT_STREAM = "inputStream";
31     private static final String OUTPUT_STREAM = "outputStream";
32
33     private final ChannelInputStream chais = new ChannelInputStream();
34     private final ChannelOutputStream chaos = new ChannelOutputStream();
35     private ChannelHandlerContext ctx;
36
37
38     public InputStream getInputStream() {
39         return this.chais;
40     }
41
42     public OutputStream getOutputStream() {
43         return this.chaos;
44     }
45
46     public void handlerAdded(ChannelHandlerContext ctx) {
47         this.ctx = ctx;
48
49         if (ctx.channel().pipeline().get(OUTPUT_STREAM) == null) {
50             ctx.channel().pipeline().addFirst(OUTPUT_STREAM, chaos);
51         }
52
53         if (ctx.channel().pipeline().get(INPUT_STREAM) == null) {
54             ctx.channel().pipeline().addFirst(INPUT_STREAM, chais);
55         }
56     }
57
58     public void handlerRemoved(ChannelHandlerContext ctx) {
59         if (ctx.channel().pipeline().get(OUTPUT_STREAM) != null) {
60             ctx.channel().pipeline().remove(OUTPUT_STREAM);
61         }
62
63         if (ctx.channel().pipeline().get(INPUT_STREAM) != null) {
64             ctx.channel().pipeline().remove(INPUT_STREAM);
65         }
66     }
67
68     public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
69         // TODO exceptionCaught is deprecated transform this handler
70         ctx.fireExceptionCaught(throwable);
71     }
72
73
74     @Override
75     public void connect(SocketAddress endpoint) throws IOException {}
76
77     @Override
78     public void connect(SocketAddress endpoint, int timeout) throws IOException {}
79
80     @Override
81     public void bind(SocketAddress bindpoint) throws IOException {}
82
83     @Override
84     public InetAddress getInetAddress() {
85         InetSocketAddress isa = getInetSocketAddress();
86         return isa.getAddress();
87     }
88
89     @Override
90     public InetAddress getLocalAddress() {return null;}
91
92     @Override
93     public int getPort() {
94         return getInetSocketAddress().getPort();
95     }
96
97     private InetSocketAddress getInetSocketAddress() {
98         return (InetSocketAddress)getRemoteSocketAddress();
99     }
100
101     @Override
102     public int getLocalPort() {return -1;}
103
104     @Override
105     public SocketAddress getRemoteSocketAddress() {
106         return this.ctx.channel().remoteAddress();
107     }
108
109     @Override
110     public SocketAddress getLocalSocketAddress() {
111         return this.ctx.channel().localAddress();
112     }
113
114     @Override
115     public SocketChannel getChannel() {return null;}
116
117     @Override
118     public void setTcpNoDelay(boolean on) throws SocketException {}
119
120     @Override
121     public boolean getTcpNoDelay() throws SocketException {return false;}
122
123     @Override
124     public void setSoLinger(boolean on, int linger) throws SocketException {}
125
126     @Override
127     public int getSoLinger() throws SocketException {return -1;}
128
129     @Override
130     public void sendUrgentData(int data) throws IOException {}
131
132     @Override
133     public void setOOBInline(boolean on) throws SocketException {}
134
135     @Override
136     public boolean getOOBInline() throws SocketException {return false;}
137
138     @Override
139     public synchronized void setSoTimeout(int timeout) throws SocketException {}
140
141     @Override
142     public synchronized int getSoTimeout() throws SocketException {return -1;}
143
144     @Override
145     public synchronized void setSendBufferSize(int size) throws SocketException {}
146
147     @Override
148     public synchronized int getSendBufferSize() throws SocketException {return -1;}
149
150     @Override
151     public synchronized void setReceiveBufferSize(int size) throws SocketException {}
152
153     @Override
154     public synchronized int getReceiveBufferSize() throws SocketException {return -1;}
155
156     @Override
157     public void setKeepAlive(boolean on) throws SocketException {}
158
159     @Override
160     public boolean getKeepAlive() throws SocketException {return false;}
161
162     @Override
163     public void setTrafficClass(int tc) throws SocketException {}
164
165     @Override
166     public int getTrafficClass() throws SocketException {return -1;}
167
168     @Override
169     public void setReuseAddress(boolean on) throws SocketException {}
170
171     @Override
172     public boolean getReuseAddress() throws SocketException {return false;}
173
174     @Override
175     public synchronized void close() throws IOException {}
176
177     @Override
178     public void shutdownInput() throws IOException {}
179
180     @Override
181     public void shutdownOutput() throws IOException {}
182
183     @Override
184     public String toString() {
185         return "VirtualSocket{" + getInetAddress() + ":" + getPort() + "}";
186     }
187
188     @Override
189     public boolean isConnected() {return false;}
190
191     @Override
192     public boolean isBound() {return false;}
193
194     @Override
195     public boolean isClosed() {return false;}
196
197     @Override
198     public boolean isInputShutdown() {return false;}
199
200     @Override
201     public boolean isOutputShutdown() {return false;}
202
203     @Override
204     public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {}
205 }