Add JSONRestconfService impl for the restconf Draft18 impl
[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.IOException;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.ThreadFactory;
18 import org.apache.sshd.common.util.ThreadUtils;
19 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
20 import org.opendaylight.netconf.ssh.SshProxyServer;
21 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
22 import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
23 import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
24 import org.osgi.framework.BundleActivator;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.InvalidSyntaxException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class NetconfSSHActivator implements BundleActivator {
31     private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
32
33     private static final java.lang.String ALGORITHM = "RSA";
34     private static final int KEY_SIZE = 4096;
35     public static final int POOL_SIZE = 8;
36     private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
37
38     private ScheduledExecutorService minaTimerExecutor;
39     private NioEventLoopGroup clientGroup;
40     private ExecutorService nioExecutor;
41     private AuthProviderTracker authProviderTracker;
42
43     private SshProxyServer server;
44
45     @Override
46     public void start(final BundleContext bundleContext) throws IOException, InvalidSyntaxException {
47         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
48             @Override
49             public Thread newThread(final Runnable runnable) {
50                 return new Thread(runnable, "netconf-ssh-server-mina-timers");
51             }
52         });
53         clientGroup = new NioEventLoopGroup();
54         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
55         server = startSSHServer(bundleContext);
56     }
57
58     @Override
59     public void stop(final BundleContext context) throws IOException {
60         if (server != null) {
61             server.close();
62         }
63
64         if (authProviderTracker != null) {
65             authProviderTracker.stop();
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(final BundleContext bundleContext)
82             throws IOException, InvalidSyntaxException {
83         final NetconfConfiguration netconfConfiguration =
84                 NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
85
86         final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
87         LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
88
89         final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
90         authProviderTracker = new AuthProviderTracker(bundleContext);
91
92         final String path = netconfConfiguration.getPrivateKeyPath();
93         LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
94
95         final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
96         sshProxyServer.bind(
97                 new SshProxyServerConfigurationBuilder()
98                         .setBindingAddress(sshSocketAddress)
99                         .setLocalAddress(localAddress)
100                         .setAuthenticator(authProviderTracker)
101                         .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
102                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
103                         .createSshProxyServerConfiguration());
104         return sshProxyServer;
105     }
106 }