Closed nested JSON writers
[netconf.git] / netconf / netconf-ssh / src / main / java / org / opendaylight / 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.netconf.ssh.osgi;
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.ssh.SshProxyServer;
22 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
23 import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
24 import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
25 import org.osgi.framework.BundleActivator;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.InvalidSyntaxException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class NetconfSSHActivator implements BundleActivator {
32     private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
33
34     private static final java.lang.String ALGORITHM = "RSA";
35     private static final int KEY_SIZE = 4096;
36     public static final int POOL_SIZE = 8;
37     private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
38
39     private ScheduledExecutorService minaTimerExecutor;
40     private NioEventLoopGroup clientGroup;
41     private ExecutorService nioExecutor;
42     private AuthProviderTracker authProviderTracker;
43
44     private SshProxyServer server;
45
46     @Override
47     public void start(final BundleContext bundleContext) throws IOException, InvalidSyntaxException {
48         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE,
49             runnable -> new Thread(runnable, "netconf-ssh-server-mina-timers"));
50         clientGroup = new NioEventLoopGroup();
51         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
52         server = startSSHServer(bundleContext);
53     }
54
55     @Override
56     public void stop(final BundleContext context) throws IOException {
57         if (server != null) {
58             server.close();
59         }
60
61         if (authProviderTracker != null) {
62             authProviderTracker.stop();
63         }
64
65         if (nioExecutor != null) {
66             nioExecutor.shutdownNow();
67         }
68
69         if (clientGroup != null) {
70             clientGroup.shutdownGracefully();
71         }
72
73         if (minaTimerExecutor != null) {
74             minaTimerExecutor.shutdownNow();
75         }
76     }
77
78     private SshProxyServer startSSHServer(final BundleContext bundleContext)
79             throws IOException, InvalidSyntaxException {
80         final NetconfConfiguration netconfConfiguration =
81                 NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
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         authProviderTracker = new AuthProviderTracker(bundleContext);
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(authProviderTracker)
102                         .setKeyPairProvider(keyPairProvider)
103                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
104                         .createSshProxyServerConfiguration());
105         return sshProxyServer;
106     }
107 }