BUG-1612 Implement mina ssh netconf server endpoint
[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.util.osgi.NetconfConfigUtil;
27 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
28 import org.osgi.framework.BundleActivator;
29 import org.osgi.framework.BundleContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class NetconfSSHActivator implements BundleActivator {
34     private static final Logger logger = LoggerFactory.getLogger(NetconfSSHActivator.class);
35
36     private static final java.lang.String ALGORITHM = "RSA";
37     private static final int KEY_SIZE = 4096;
38     public static final int POOL_SIZE = 8;
39
40     private ScheduledExecutorService minaTimerExecutor;
41     private NioEventLoopGroup clientGroup;
42     private ExecutorService nioExecutor;
43     private AuthProviderTracker authProviderTracker;
44
45     private SshProxyServer server;
46
47     @Override
48     public void start(final BundleContext bundleContext) throws IOException {
49         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
50             @Override
51             public Thread newThread(final Runnable r) {
52                 return new Thread(r, "netconf-ssh-server-mina-timers");
53             }
54         });
55         clientGroup = new NioEventLoopGroup();
56         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
57         server = startSSHServer(bundleContext);
58     }
59
60     @Override
61     public void stop(final BundleContext context) throws IOException {
62         if (server != null) {
63             server.close();
64         }
65
66         if(authProviderTracker != null) {
67             authProviderTracker.stop();
68         }
69
70         if(nioExecutor!=null) {
71             nioExecutor.shutdownNow();
72         }
73
74         if(clientGroup != null) {
75             clientGroup.shutdownGracefully();
76         }
77
78         if(minaTimerExecutor != null) {
79             minaTimerExecutor.shutdownNow();
80         }
81     }
82
83     private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
84         final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
85
86         if (maybeSshSocketAddress.isPresent() == false) {
87             logger.trace("SSH bridge not configured");
88             return null;
89         }
90
91         final InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
92         logger.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
93
94         final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
95
96         authProviderTracker = new AuthProviderTracker(bundleContext);
97
98         final String path = FilenameUtils.separatorsToSystem(NetconfConfigUtil.getPrivateKeyPath(bundleContext));
99         checkState(!Strings.isNullOrEmpty(path), "Path to ssh private key is blank. Reconfigure %s",
100                 NetconfConfigUtil.getPrivateKeyKey());
101
102         final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
103         sshProxyServer.bind(sshSocketAddress, localAddress, authProviderTracker, new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE));
104         return sshProxyServer;
105     }
106
107 }