Increase IDLE timeout for ssh netconf server
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / osgi / NetconfSSHActivator.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.controller.netconf.ssh.osgi;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.base.Optional;
13 import com.google.common.base.Strings;
14 import io.netty.channel.local.LocalAddress;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ThreadFactory;
22 import org.apache.commons.io.FilenameUtils;
23 import org.apache.sshd.common.util.ThreadUtils;
24 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
25 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
26 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
27 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
28 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
29 import org.osgi.framework.BundleActivator;
30 import org.osgi.framework.BundleContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class NetconfSSHActivator implements BundleActivator {
35     private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
36
37     private static final java.lang.String ALGORITHM = "RSA";
38     private static final int KEY_SIZE = 4096;
39     public static final int POOL_SIZE = 8;
40     private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
41
42     private ScheduledExecutorService minaTimerExecutor;
43     private NioEventLoopGroup clientGroup;
44     private ExecutorService nioExecutor;
45     private AuthProviderTracker authProviderTracker;
46
47     private SshProxyServer server;
48
49     @Override
50     public void start(final BundleContext bundleContext) throws IOException {
51         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
52             @Override
53             public Thread newThread(final Runnable r) {
54                 return new Thread(r, "netconf-ssh-server-mina-timers");
55             }
56         });
57         clientGroup = new NioEventLoopGroup();
58         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
59         server = startSSHServer(bundleContext);
60     }
61
62     @Override
63     public void stop(final BundleContext context) throws IOException {
64         if (server != null) {
65             server.close();
66         }
67
68         if(authProviderTracker != null) {
69             authProviderTracker.stop();
70         }
71
72         if(nioExecutor!=null) {
73             nioExecutor.shutdownNow();
74         }
75
76         if(clientGroup != null) {
77             clientGroup.shutdownGracefully();
78         }
79
80         if(minaTimerExecutor != null) {
81             minaTimerExecutor.shutdownNow();
82         }
83     }
84
85     private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
86         final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
87
88         if (maybeSshSocketAddress.isPresent() == false) {
89             LOG.trace("SSH bridge not configured");
90             return null;
91         }
92
93         final InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
94         LOG.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
95
96         final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
97
98         authProviderTracker = new AuthProviderTracker(bundleContext);
99
100         final String path = FilenameUtils.separatorsToSystem(NetconfConfigUtil.getPrivateKeyPath(bundleContext));
101         checkState(!Strings.isNullOrEmpty(path), "Path to ssh private key is blank. Reconfigure %s",
102                 NetconfConfigUtil.getPrivateKeyKey());
103
104         final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
105         sshProxyServer.bind(
106                 new SshProxyServerConfigurationBuilder()
107                         .setBindingAddress(sshSocketAddress)
108                         .setLocalAddress(localAddress)
109                         .setAuthenticator(authProviderTracker)
110                         .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
111                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
112                         .createSshProxyServerConfiguration());
113         return sshProxyServer;
114     }
115
116 }