Remove commons-io dependency in netconf-ssh
[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.sshd.common.util.ThreadUtils;
23 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
24 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
25 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
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 LOG = 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     private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
40
41     private ScheduledExecutorService minaTimerExecutor;
42     private NioEventLoopGroup clientGroup;
43     private ExecutorService nioExecutor;
44     private AuthProviderTracker authProviderTracker;
45
46     private SshProxyServer server;
47
48     @Override
49     public void start(final BundleContext bundleContext) throws IOException {
50         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
51             @Override
52             public Thread newThread(final Runnable r) {
53                 return new Thread(r, "netconf-ssh-server-mina-timers");
54             }
55         });
56         clientGroup = new NioEventLoopGroup();
57         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
58         server = startSSHServer(bundleContext);
59     }
60
61     @Override
62     public void stop(final BundleContext context) throws IOException {
63         if (server != null) {
64             server.close();
65         }
66
67         if(authProviderTracker != null) {
68             authProviderTracker.stop();
69         }
70
71         if(nioExecutor!=null) {
72             nioExecutor.shutdownNow();
73         }
74
75         if(clientGroup != null) {
76             clientGroup.shutdownGracefully();
77         }
78
79         if(minaTimerExecutor != null) {
80             minaTimerExecutor.shutdownNow();
81         }
82     }
83
84     private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
85         final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
86
87         if (!maybeSshSocketAddress.isPresent()) {
88             LOG.trace("SSH bridge not configured");
89             return null;
90         }
91
92         final InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
93         LOG.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
94
95         final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
96
97         authProviderTracker = new AuthProviderTracker(bundleContext);
98
99         final String path = NetconfConfigUtil.getPrivateKeyPath(bundleContext);
100
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 }