Refresh IETF client/server models
[netconf.git] / apps / netconf-nb / src / main / java / org / opendaylight / netconf / northbound / SshServerTransport.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.northbound;
9
10 import java.io.IOException;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.netconf.api.TransportConstants;
14 import org.opendaylight.netconf.auth.AuthProvider;
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.rev240208.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.rev240208.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;
34
35 /**
36  * NETCONF server for MD-SAL (listening by default on port 2830).
37  */
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 {
43         @AttributeDefinition
44         String bindingAddress() default "0.0.0.0";
45         // NOTE: default is not TransportConstants.SSH_TCP_PORT to allow unprivileged execution
46         @AttributeDefinition(min = "1", max = "65535")
47         int portNumber() default 2830;
48     }
49
50     private static final Logger LOG = LoggerFactory.getLogger(SshServerTransport.class);
51
52     private final SSHServer sshServer;
53
54     @Activate
55     public SshServerTransport(@Reference final TransportFactoryHolder factoryHolder,
56             @Reference final OSGiNetconfServer backend,
57             @Reference(target = "(type=netconf-auth-provider)") final AuthProvider authProvider,
58             final Configuration configuration) {
59         this(factoryHolder, backend.serverTransportInitializer(), authProvider, new TcpServerParametersBuilder()
60             .setLocalAddress(IetfInetUtil.ipAddressFor(configuration.bindingAddress()))
61             .setLocalPort(new PortNumber(Uint16.valueOf(configuration.portNumber())))
62             .build());
63     }
64
65     public SshServerTransport(final TransportFactoryHolder factoryHolder, final ServerTransportInitializer initializer,
66             final AuthProvider authProvider, final TcpServerGrouping listenParams) {
67         final var localAddr = listenParams.requireLocalAddress().stringValue();
68         final var localPort = listenParams.requireLocalPort().getValue();
69
70         try {
71             sshServer = factoryHolder.factory().listenServer(TransportConstants.SSH_SUBSYSTEM, initializer,
72                 listenParams, null, factoryMgr -> {
73                     factoryMgr.setUserAuthFactories(List.of(UserAuthPasswordFactory.INSTANCE));
74                     factoryMgr.setPasswordAuthenticator(
75                         (username, password, session) -> authProvider.authenticated(username, password));
76                     factoryMgr.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
77                 })
78                 .get();
79         } catch (UnsupportedConfigurationException | ExecutionException | InterruptedException e) {
80             LOG.warn("Could not start SSH NETCONF server at {}:{}", localAddr, localPort, e);
81             throw new IllegalStateException("Unable to start SSH netconf server", e);
82         }
83
84         LOG.info("SSH NETCONF server at {}:{} started", localAddr, localPort);
85     }
86
87     @Deactivate
88     @Override
89     public void close() throws IOException {
90         try {
91             sshServer.shutdown().get();
92         } catch (ExecutionException | InterruptedException e) {
93             LOG.warn("Could not stop SSH NETCONF server {}", sshServer, e);
94         }
95     }
96 }