Bump upstream versions
[netconf.git] / netconf / mdsal-netconf-ssh / src / main / java / org / opendaylight / netconf / ssh / NetconfNorthboundSshServer.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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;
9
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.osgi.framework.ServiceReference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NetconfNorthboundSshServer {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundSshServer.class);
30
31     // Do not store unencrypted private key
32     private static final String DEFAULT_PRIVATE_KEY_PATH = null;
33     private static final String DEFAULT_ALGORITHM = "RSA";
34     private static final int DEFAULT_KEY_SIZE = 4096;
35
36     private final ChannelFuture localServer;
37     private final SshProxyServer sshProxyServer;
38
39     public NetconfNorthboundSshServer(final NetconfServerDispatcher netconfServerDispatcher,
40                                       final EventLoopGroup workerGroup,
41                                       final EventExecutor eventExecutor,
42                                       final String bindingAddress,
43                                       final String portNumber,
44                                       final AuthProvider authProvider) {
45
46         final LocalAddress localAddress = new LocalAddress(portNumber);
47
48         localServer = netconfServerDispatcher.createLocalServer(localAddress);
49         sshProxyServer = new SshProxyServer(Executors.newScheduledThreadPool(1), workerGroup, eventExecutor);
50
51         final InetSocketAddress inetAddress = getInetAddress(bindingAddress, portNumber);
52         final SshProxyServerConfigurationBuilder sshProxyServerConfigurationBuilder =
53                 new SshProxyServerConfigurationBuilder();
54         sshProxyServerConfigurationBuilder.setBindingAddress(inetAddress);
55         sshProxyServerConfigurationBuilder.setLocalAddress(localAddress);
56         sshProxyServerConfigurationBuilder.setAuthenticator(authProvider);
57         sshProxyServerConfigurationBuilder.setIdleTimeout(Integer.MAX_VALUE);
58         sshProxyServerConfigurationBuilder.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
59
60         localServer.addListener(future -> {
61             if (future.isDone() && !future.isCancelled()) {
62                 try {
63                     sshProxyServer.bind(sshProxyServerConfigurationBuilder.createSshProxyServerConfiguration());
64                     LOG.info("Netconf SSH endpoint started successfully at {}", bindingAddress);
65                 } catch (final IOException e) {
66                     throw new IllegalStateException("Unable to start SSH netconf server", e);
67                 }
68             } else {
69                 LOG.warn("Unable to start SSH netconf server at {}", bindingAddress, future.cause());
70                 throw new IllegalStateException("Unable to start SSH netconf server", future.cause());
71             }
72         });
73     }
74
75     private static InetSocketAddress getInetAddress(final String bindingAddress, final String portNumber) {
76         final IpAddress ipAddress = IetfInetUtil.ipAddressFor(bindingAddress);
77         final InetAddress inetAd = IetfInetUtil.INSTANCE.inetAddressFor(ipAddress);
78         return new InetSocketAddress(inetAd, Integer.parseInt(portNumber));
79     }
80
81     public void close() throws IOException {
82         sshProxyServer.close();
83
84         if (localServer.isDone()) {
85             localServer.channel().close();
86         } else {
87             localServer.cancel(true);
88         }
89     }
90
91     /*
92      * Called when the underlying reference to EventExecutor is about to be removed from the container allowing
93      * us to close the ssh server while it still exists.
94      */
95     public void unbind(final ServiceReference<?> reference) {
96         LOG.debug("EventExecutor is being removed, closing netconf ssh server. {}", reference);
97
98         try {
99             close();
100         } catch (final IOException e) {
101             LOG.error("Closing of ssh server failed while unbinding reference listener.", e);
102         }
103     }
104 }