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.northbound;
10 import java.io.IOException;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.netconf.auth.AuthProvider;
14 import org.opendaylight.netconf.server.ServerChannelInitializer;
15 import org.opendaylight.netconf.server.ServerTransportInitializer;
16 import org.opendaylight.netconf.shaded.sshd.server.auth.password.UserAuthPasswordFactory;
17 import org.opendaylight.netconf.shaded.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
18 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
19 import org.opendaylight.netconf.transport.ssh.SSHServer;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.server.rev230417.netconf.server.listen.stack.grouping.transport.ssh.ssh.TcpServerParametersBuilder;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev230417.TcpServerGrouping;
24 import org.opendaylight.yangtools.yang.common.Uint16;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Deactivate;
28 import org.osgi.service.component.annotations.Reference;
29 import org.osgi.service.metatype.annotations.AttributeDefinition;
30 import org.osgi.service.metatype.annotations.Designate;
31 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * NETCONF server for MD-SAL (listening by default on port 2830).
38 @Component(service = { }, configurationPid = "org.opendaylight.netconf.ssh")
39 @Designate(ocd = SshServerTransport.Configuration.class)
40 public final class SshServerTransport implements AutoCloseable {
41 @ObjectClassDefinition
42 public @interface Configuration {
44 String bindingAddress() default "0.0.0.0";
45 @AttributeDefinition(min = "1", max = "65535")
46 int portNumber() default 2830;
49 private static final Logger LOG = LoggerFactory.getLogger(SshServerTransport.class);
51 private final SSHServer sshServer;
54 public SshServerTransport(@Reference final TransportFactoryHolder factoryHolder,
55 @Reference final OSGiNetconfServer backend,
56 @Reference(target = "(type=netconf-auth-provider)") final AuthProvider authProvider,
57 final Configuration configuration) {
58 this(factoryHolder, backend.serverChannelInitializer(), authProvider, new TcpServerParametersBuilder()
59 .setLocalAddress(IetfInetUtil.ipAddressFor(configuration.bindingAddress()))
60 .setLocalPort(new PortNumber(Uint16.valueOf(configuration.portNumber())))
64 public SshServerTransport(final TransportFactoryHolder factoryHolder, final ServerChannelInitializer initializer,
65 final AuthProvider authProvider, final TcpServerGrouping listenParams) {
66 final var localAddr = listenParams.requireLocalAddress().stringValue();
67 final var localPort = listenParams.requireLocalPort().getValue();
70 sshServer = factoryHolder.factory().listenServer("netconf", new ServerTransportInitializer(initializer),
71 listenParams, null, factoryMgr -> {
72 factoryMgr.setUserAuthFactories(List.of(UserAuthPasswordFactory.INSTANCE));
73 factoryMgr.setPasswordAuthenticator(
74 (username, password, session) -> authProvider.authenticated(username, password));
75 factoryMgr.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
78 } catch (UnsupportedConfigurationException | ExecutionException | InterruptedException e) {
79 LOG.warn("Could not start SSH NETCONF server at {}:{}", localAddr, localPort, e);
80 throw new IllegalStateException("Unable to start SSH netconf server", e);
83 LOG.info("SSH NETCONF server at {}:{} started", localAddr, localPort);
88 public void close() throws IOException {
90 sshServer.shutdown().get();
91 } catch (ExecutionException | InterruptedException e) {
92 LOG.warn("Could not stop SSH NETCONF server {}", sshServer, e);