2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.netconf.ssh;
10 import io.netty.channel.ChannelFuture;
11 import io.netty.channel.EventLoopGroup;
12 import io.netty.channel.local.LocalAddress;
13 import io.netty.util.concurrent.EventExecutor;
14 import java.io.IOException;
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.Executors;
18 import org.opendaylight.netconf.api.NetconfServerDispatcher;
19 import org.opendaylight.netconf.auth.AuthProvider;
20 import org.opendaylight.netconf.shaded.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
24 import org.osgi.framework.ServiceReference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class NetconfNorthboundSshServer {
30 private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundSshServer.class);
32 // Do not store unencrypted private key
33 private static final String DEFAULT_PRIVATE_KEY_PATH = null;
34 private static final String DEFAULT_ALGORITHM = "RSA";
35 private static final int DEFAULT_KEY_SIZE = 4096;
37 private final ChannelFuture localServer;
38 private final SshProxyServer sshProxyServer;
40 public NetconfNorthboundSshServer(final NetconfServerDispatcher netconfServerDispatcher,
41 final EventLoopGroup workerGroup,
42 final EventExecutor eventExecutor,
43 final String bindingAddress,
44 final String portNumber,
45 final AuthProvider authProvider) {
47 final LocalAddress localAddress = new LocalAddress(portNumber);
49 localServer = netconfServerDispatcher.createLocalServer(localAddress);
50 sshProxyServer = new SshProxyServer(Executors.newScheduledThreadPool(1), workerGroup, eventExecutor);
52 final InetSocketAddress inetAddress = getInetAddress(bindingAddress, portNumber);
53 final SshProxyServerConfigurationBuilder sshProxyServerConfigurationBuilder =
54 new SshProxyServerConfigurationBuilder();
55 sshProxyServerConfigurationBuilder.setBindingAddress(inetAddress);
56 sshProxyServerConfigurationBuilder.setLocalAddress(localAddress);
57 sshProxyServerConfigurationBuilder.setAuthenticator(authProvider);
58 sshProxyServerConfigurationBuilder.setIdleTimeout(Integer.MAX_VALUE);
59 sshProxyServerConfigurationBuilder.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
61 localServer.addListener(future -> {
62 if (future.isDone() && !future.isCancelled()) {
64 sshProxyServer.bind(sshProxyServerConfigurationBuilder.createSshProxyServerConfiguration());
65 LOG.info("Netconf SSH endpoint started successfully at {}", bindingAddress);
66 } catch (final IOException e) {
67 throw new RuntimeException("Unable to start SSH netconf server", e);
70 LOG.warn("Unable to start SSH netconf server at {}", bindingAddress, future.cause());
71 throw new RuntimeException("Unable to start SSH netconf server", future.cause());
76 private static InetSocketAddress getInetAddress(final String bindingAddress, final String portNumber) {
77 IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(bindingAddress);
78 final InetAddress inetAd = IetfInetUtil.INSTANCE.inetAddressFor(ipAddress);
79 return new InetSocketAddress(inetAd, Integer.parseInt(portNumber));
82 public void close() throws IOException {
83 sshProxyServer.close();
85 if (localServer.isDone()) {
86 localServer.channel().close();
88 localServer.cancel(true);
93 * Called when the underlying reference to EventExecutor is about to be removed from the container allowing
94 * us to close the ssh server while it still exists.
96 public void unbind(final ServiceReference<?> reference) {
97 LOG.debug("EventExecutor is being removed, closing netconf ssh server. {}", reference);
101 } catch (final IOException e) {
102 LOG.error("Closing of ssh server failed while unbinding reference listener.", e);