Add blueprint wiring for netconf-ssh
[netconf.git] / netconf / 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.net.UnknownHostException;
18 import java.util.concurrent.Executors;
19 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
20 import org.opendaylight.netconf.api.NetconfServerDispatcher;
21 import org.opendaylight.netconf.auth.AuthProvider;
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;
26
27 public class NetconfNorthboundSshServer {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundSshServer.class);
30
31     private final ChannelFuture localServer;
32     private final SshProxyServer sshProxyServer;
33
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) {
40
41         final LocalAddress localAddress = new LocalAddress(portNumber);
42
43         localServer = netconfServerDispatcher.createLocalServer(localAddress);
44         sshProxyServer = new SshProxyServer(Executors.newScheduledThreadPool(1), workerGroup, eventExecutor);
45
46         final InetSocketAddress inetAddress = getInetAddress(bindingAddress, portNumber);
47         final SshProxyServerConfigurationBuilder sshProxyServerConfigurationBuilder = new SshProxyServerConfigurationBuilder();
48         sshProxyServerConfigurationBuilder.setBindingAddress(inetAddress);
49         sshProxyServerConfigurationBuilder.setLocalAddress(localAddress);
50         sshProxyServerConfigurationBuilder.setAuthenticator(authProvider);
51         sshProxyServerConfigurationBuilder.setIdleTimeout(Integer.MAX_VALUE);
52         sshProxyServerConfigurationBuilder.setKeyPairProvider(new PEMGeneratorHostKeyProvider());
53
54         localServer.addListener(future -> {
55             if (future.isDone() && !future.isCancelled()) {
56                 try {
57                     sshProxyServer.bind(sshProxyServerConfigurationBuilder.createSshProxyServerConfiguration());
58                     LOG.info("Netconf SSH endpoint started successfully at {}", bindingAddress);
59                 } catch (final IOException e) {
60                     throw new RuntimeException("Unable to start SSH netconf server", e);
61                 }
62             } else {
63                 LOG.warn("Unable to start SSH netconf server at {}", bindingAddress, future.cause());
64                 throw new RuntimeException("Unable to start SSH netconf server", future.cause());
65             }
66         });
67     }
68
69     private InetSocketAddress getInetAddress(final String bindingAddress, final String portNumber) {
70         try {
71             IpAddress ipAddress= IpAddressBuilder.getDefaultInstance(bindingAddress);
72             final InetAddress inetAd = InetAddress.getByName(ipAddress.getIpv4Address() == null ? ipAddress.getIpv6Address().getValue() : ipAddress.getIpv4Address().getValue());
73             return new InetSocketAddress(inetAd, Integer.valueOf(portNumber));
74         } catch (final UnknownHostException e) {
75             throw new IllegalArgumentException("Unable to bind netconf endpoint to address " + bindingAddress, e);
76         }
77     }
78
79     public void close() {
80         sshProxyServer.close();
81
82         if (localServer.isDone()) {
83             localServer.channel().close();
84         } else {
85             localServer.cancel(true);
86         }
87     }
88 }