Merge "creating a default subnet"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / 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.util.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 public class VirtualSocket extends Socket implements ChannelHandler {
29     private final ChannelInputStream chis = new ChannelInputStream();
30     private final ChannelOutputStream chos = new ChannelOutputStream();
31     private ChannelHandlerContext ctx;
32
33
34     public InputStream getInputStream() {
35         return this.chis;
36     }
37
38     public OutputStream getOutputStream() {
39         return this.chos;
40     }
41
42     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
43         this.ctx = ctx;
44
45         if (ctx.channel().pipeline().get("outputStream") == null) {
46             ctx.channel().pipeline().addFirst("outputStream", chos);
47         }
48
49         if (ctx.channel().pipeline().get("inputStream") == null) {
50             ctx.channel().pipeline().addFirst("inputStream", chis);
51         }
52     }
53
54     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
55         if (ctx.channel().pipeline().get("outputStream") != null) {
56             ctx.channel().pipeline().remove("outputStream");
57         }
58
59         if (ctx.channel().pipeline().get("inputStream") != null) {
60             ctx.channel().pipeline().remove("inputStream");
61         }
62     }
63
64     public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) throws Exception {
65         ctx.fireExceptionCaught(throwable);
66     }
67
68     public VirtualSocket() {super();}
69
70     @Override
71     public void connect(SocketAddress endpoint) throws IOException {}
72
73     @Override
74     public void connect(SocketAddress endpoint, int timeout) throws IOException {}
75
76     @Override
77     public void bind(SocketAddress bindpoint) throws IOException {}
78
79     @Override
80     public InetAddress getInetAddress() {
81         InetSocketAddress isa = getInetSocketAddress();
82
83         if (isa == null) throw new VirtualSocketException();
84
85         return getInetSocketAddress().getAddress();
86     }
87
88     @Override
89     public InetAddress getLocalAddress() {return null;}
90
91     @Override
92     public int getPort() {
93         return getInetSocketAddress().getPort();
94     }
95
96     private InetSocketAddress getInetSocketAddress() {
97         return (InetSocketAddress)getRemoteSocketAddress();
98     }
99
100     @Override
101     public int getLocalPort() {return -1;}
102
103     @Override
104     public SocketAddress getRemoteSocketAddress() {
105         return this.ctx.channel().remoteAddress();
106     }
107
108     @Override
109     public SocketAddress getLocalSocketAddress() {
110         return this.ctx.channel().localAddress();
111     }
112
113     @Override
114     public SocketChannel getChannel() {return null;}
115
116     @Override
117     public void setTcpNoDelay(boolean on) throws SocketException {}
118
119     @Override
120     public boolean getTcpNoDelay() throws SocketException {return false;}
121
122     @Override
123     public void setSoLinger(boolean on, int linger) throws SocketException {}
124
125     @Override
126     public int getSoLinger() throws SocketException {return -1;}
127
128     @Override
129     public void sendUrgentData(int data) throws IOException {}
130
131     @Override
132     public void setOOBInline(boolean on) throws SocketException {}
133
134     @Override
135     public boolean getOOBInline() throws SocketException {return false;}
136
137     @Override
138     public synchronized void setSoTimeout(int timeout) throws SocketException {}
139
140     @Override
141     public synchronized int getSoTimeout() throws SocketException {return -1;}
142
143     @Override
144     public synchronized void setSendBufferSize(int size) throws SocketException {}
145
146     @Override
147     public synchronized int getSendBufferSize() throws SocketException {return -1;}
148
149     @Override
150     public synchronized void setReceiveBufferSize(int size) throws SocketException {}
151
152     @Override
153     public synchronized int getReceiveBufferSize() throws SocketException {return -1;}
154
155     @Override
156     public void setKeepAlive(boolean on) throws SocketException {}
157
158     @Override
159     public boolean getKeepAlive() throws SocketException {return false;}
160
161     @Override
162     public void setTrafficClass(int tc) throws SocketException {}
163
164     @Override
165     public int getTrafficClass() throws SocketException {return -1;}
166
167     @Override
168     public void setReuseAddress(boolean on) throws SocketException {}
169
170     @Override
171     public boolean getReuseAddress() throws SocketException {return false;}
172
173     @Override
174     public synchronized void close() throws IOException {}
175
176     @Override
177     public void shutdownInput() throws IOException {}
178
179     @Override
180     public void shutdownOutput() throws IOException {}
181
182     @Override
183     public String toString() {
184         return "Virtual socket InetAdress["+getInetAddress()+"], Port["+getPort()+"]";
185     }
186
187     @Override
188     public boolean isConnected() {return false;}
189
190     @Override
191     public boolean isBound() {return false;}
192
193     @Override
194     public boolean isClosed() {return false;}
195
196     @Override
197     public boolean isInputShutdown() {return false;}
198
199     @Override
200     public boolean isOutputShutdown() {return false;}
201
202     @Override
203     public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {}
204 }