Merge "Convert netconf-util, netconf-ssh, netconf-tcp to blueprint"
[netconf.git] / netconf / netconf-ssh / src / main / java / org / opendaylight / netconf / ssh / NetconfSSHProvider.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 package org.opendaylight.netconf.ssh;
9
10 import io.netty.channel.local.LocalAddress;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.InetSocketAddress;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.ScheduledExecutorService;
18 import org.apache.sshd.common.util.security.SecurityUtils;
19 import org.apache.sshd.common.util.threads.ThreadUtils;
20 import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
21 import org.opendaylight.netconf.auth.AuthProvider;
22 import org.opendaylight.netconf.util.NetconfConfiguration;
23 import org.osgi.framework.InvalidSyntaxException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NetconfSSHProvider {
28     private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHProvider.class);
29
30     private static final java.lang.String ALGORITHM = "RSA";
31     private static final int KEY_SIZE = 4096;
32     public static final int POOL_SIZE = 8;
33     private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
34
35     private final AuthProvider authProvider;
36     private final NetconfConfiguration netconfConfiguration;
37
38     private ScheduledExecutorService minaTimerExecutor;
39     private NioEventLoopGroup clientGroup;
40     private ExecutorService nioExecutor;
41
42     private SshProxyServer server;
43
44     public NetconfSSHProvider(final AuthProvider authProvider,
45                               final NetconfConfiguration netconfConfiguration) {
46
47         this.authProvider = authProvider;
48         this.netconfConfiguration = netconfConfiguration;
49     }
50
51     // Called via blueprint
52     @SuppressWarnings("unused")
53     public void init() throws IOException, InvalidSyntaxException {
54         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE,
55             runnable -> new Thread(runnable, "netconf-ssh-server-mina-timers"));
56         clientGroup = new NioEventLoopGroup();
57         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
58         server = startSSHServer();
59     }
60
61     // Called via blueprint
62     @SuppressWarnings("unused")
63     public void destroy() throws IOException {
64         if (server != null) {
65             server.close();
66         }
67
68         if (nioExecutor != null) {
69             nioExecutor.shutdownNow();
70         }
71
72         if (clientGroup != null) {
73             clientGroup.shutdownGracefully();
74         }
75
76         if (minaTimerExecutor != null) {
77             minaTimerExecutor.shutdownNow();
78         }
79     }
80
81     private SshProxyServer startSSHServer()
82             throws IOException, InvalidSyntaxException {
83
84         final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
85         LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
86
87         final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
88
89         final String path = netconfConfiguration.getPrivateKeyPath();
90         LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
91
92         final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
93         final AbstractGeneratorHostKeyProvider keyPairProvider = SecurityUtils.createGeneratorHostKeyProvider(null);
94         keyPairProvider.setAlgorithm(ALGORITHM);
95         keyPairProvider.setKeySize(KEY_SIZE);
96         keyPairProvider.setFile(new File(path));
97         sshProxyServer.bind(
98                 new SshProxyServerConfigurationBuilder()
99                         .setBindingAddress(sshSocketAddress)
100                         .setLocalAddress(localAddress)
101                         .setAuthenticator(authProvider)
102                         .setKeyPairProvider(keyPairProvider)
103                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
104                         .createSshProxyServerConfiguration());
105         return sshProxyServer;
106     }
107 }