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.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
19 import org.opendaylight.netconf.api.NetconfServerDispatcher;
20 import org.opendaylight.netconf.auth.AuthProvider;
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.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class NetconfNorthboundSshServer {
29 private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundSshServer.class);
31 private final ChannelFuture localServer;
32 private final SshProxyServer sshProxyServer;
34 public NetconfNorthboundSshServer(final NetconfServerDispatcher netconfServerDispatcher,
35 final EventLoopGroup workerGroup,
36 final EventExecutor eventExecutor,
37 final String bindingAddress,
38 final String portNumber,
39 final AuthProvider authProvider) {
41 final LocalAddress localAddress = new LocalAddress(portNumber);
43 localServer = netconfServerDispatcher.createLocalServer(localAddress);
44 sshProxyServer = new SshProxyServer(Executors.newScheduledThreadPool(1), workerGroup, eventExecutor);
46 final InetSocketAddress inetAddress = getInetAddress(bindingAddress, portNumber);
47 final SshProxyServerConfigurationBuilder sshProxyServerConfigurationBuilder =
48 new SshProxyServerConfigurationBuilder();
49 sshProxyServerConfigurationBuilder.setBindingAddress(inetAddress);
50 sshProxyServerConfigurationBuilder.setLocalAddress(localAddress);
51 sshProxyServerConfigurationBuilder.setAuthenticator(authProvider);
52 sshProxyServerConfigurationBuilder.setIdleTimeout(Integer.MAX_VALUE);
53 sshProxyServerConfigurationBuilder.setKeyPairProvider(new PEMGeneratorHostKeyProvider());
55 localServer.addListener(future -> {
56 if (future.isDone() && !future.isCancelled()) {
58 sshProxyServer.bind(sshProxyServerConfigurationBuilder.createSshProxyServerConfiguration());
59 LOG.info("Netconf SSH endpoint started successfully at {}", bindingAddress);
60 } catch (final IOException e) {
61 throw new RuntimeException("Unable to start SSH netconf server", e);
64 LOG.warn("Unable to start SSH netconf server at {}", bindingAddress, future.cause());
65 throw new RuntimeException("Unable to start SSH netconf server", future.cause());
70 private static InetSocketAddress getInetAddress(final String bindingAddress, final String portNumber) {
71 IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(bindingAddress);
72 final InetAddress inetAd = IetfInetUtil.INSTANCE.inetAddressFor(ipAddress);
73 return new InetSocketAddress(inetAd, Integer.parseInt(portNumber));
77 sshProxyServer.close();
79 if (localServer.isDone()) {
80 localServer.channel().close();
82 localServer.cancel(true);