- introduced netconf ssh wrapper bundle
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / SocketThread.java
1 package org.opendaylight.controller.netconf.ssh;
2
3
4 import ch.ethz.ssh2.AuthenticationResult;
5 import ch.ethz.ssh2.PtySettings;
6 import ch.ethz.ssh2.ServerAuthenticationCallback;
7 import ch.ethz.ssh2.ServerConnection;
8 import ch.ethz.ssh2.ServerConnectionCallback;
9 import ch.ethz.ssh2.ServerSession;
10 import ch.ethz.ssh2.ServerSessionCallback;
11 import ch.ethz.ssh2.SimpleServerSessionCallback;
12 import com.google.common.base.Optional;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import java.io.IOException;
15 import java.net.InetSocketAddress;
16 import java.net.Socket;
17 import java.nio.ByteBuffer;
18 import javax.net.ssl.SSLContext;
19 import org.opendaylight.controller.netconf.client.NetconfClient;
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.client.NetconfClientSession;
22 import org.opendaylight.controller.netconf.ssh.authentication.RSAKey;
23 import org.opendaylight.controller.netconf.ssh.handler.SSHChannelInboundHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 public class SocketThread implements Runnable, ServerAuthenticationCallback, ServerConnectionCallback
29 {
30
31     private Socket socket;
32     private static final String USER = "netconf";
33     private static final String PASSWORD = "netconf";
34     private NetconfClient netconfClient;
35     private static final InetSocketAddress clientAddress = new InetSocketAddress("127.0.0.1", 12023);
36     private static final Logger logger =  LoggerFactory.getLogger(SocketThread.class);
37
38
39     private static ServerConnection conn = null;
40
41     public static void start(Socket socket) throws IOException{
42         new Thread(new SocketThread(socket)).start();
43     }
44     private SocketThread(Socket socket) throws IOException {
45
46         this.socket = socket;
47
48         conn = new ServerConnection(socket);
49         RSAKey keyStore = new RSAKey();
50         conn.setRsaHostKey(keyStore.getPrivateKey());
51         conn.setAuthenticationCallback(this);
52         conn.setServerConnectionCallback(this);
53         conn.connect();
54     }
55
56     @Override
57     public void run() {
58         //noop
59     }
60     public ServerSessionCallback acceptSession(final ServerSession session)
61     {
62         SimpleServerSessionCallback cb = new SimpleServerSessionCallback()
63         {
64             @Override
65             public Runnable requestSubsystem(ServerSession ss, final String subsystem) throws IOException
66             {
67                 return new Runnable(){
68                     public void run()
69                     {
70                         if (subsystem.equals("netconf")){
71                             logger.info("netconf subsystem received");
72                             try {
73                                 NetconfClientDispatcher clientDispatcher = null;
74                                 NioEventLoopGroup nioGrup = new NioEventLoopGroup(1);
75                                 clientDispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent(), nioGrup, nioGrup);
76                                 logger.info("dispatcher created");
77                                 netconfClient = new NetconfClient("ssh_" + clientAddress.toString(),clientAddress,5000,clientDispatcher);
78                                 logger.info("netconf client created");
79                             } catch (Throwable t){
80                                 logger.error(t.getMessage(),t);
81                             }
82                         }
83                     }
84                 };
85             }
86             @Override
87             public Runnable requestPtyReq(final ServerSession ss, final PtySettings pty) throws IOException
88             {
89                 return new Runnable()
90                 {
91                     public void run()
92                     {
93                         System.out.println("Client requested " + pty.term + " pty");
94                     }
95                 };
96             }
97
98             @Override
99             public Runnable requestShell(final ServerSession ss) throws IOException
100             {
101                 return new Runnable()
102                 {
103                     public void run()
104                     {
105                         try
106                         {
107                             try (NetconfClientSession session = netconfClient.getClientSession())
108                             {
109                                 session.getChannel().pipeline().addLast(new SSHChannelInboundHandler(ss));
110                                 byte[] bytes = new byte[1024];
111                                 while (true)
112                                 {
113                                     int size = ss.getStdout().read(bytes);
114                                     if (size < 0)
115                                     {
116                                         System.err.println("SESSION EOF");
117                                         return;
118                                     }
119                                     session.getChannel().write(ByteBuffer.wrap(bytes,0,size));
120                                 }
121                             }
122
123                         }
124                         catch (IOException e)
125                         {
126                             System.err.println("SESSION DOWN");
127                             e.printStackTrace();
128                         }
129                     }
130                 };
131             }
132         };
133
134         return cb;
135     }
136
137     public String initAuthentication(ServerConnection sc)
138     {
139         return "";
140     }
141
142     public String[] getRemainingAuthMethods(ServerConnection sc)
143     {
144         return new String[] { ServerAuthenticationCallback.METHOD_PASSWORD,
145                 ServerAuthenticationCallback.METHOD_PUBLICKEY };
146     }
147
148     public AuthenticationResult authenticateWithNone(ServerConnection sc, String username)
149     {
150         return AuthenticationResult.FAILURE;
151     }
152
153     public AuthenticationResult authenticateWithPassword(ServerConnection sc, String username, String password)
154     {
155         if (USER.equals(username) && PASSWORD.equals(password))
156             return AuthenticationResult.SUCCESS;
157
158         return AuthenticationResult.FAILURE;
159     }
160
161     public AuthenticationResult authenticateWithPublicKey(ServerConnection sc, String username, String algorithm,
162             byte[] publickey, byte[] signature)
163     {
164         return AuthenticationResult.FAILURE;
165     }
166
167 }