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