Search in sources :

Example 1 with SshProxyServer

use of org.opendaylight.netconf.ssh.SshProxyServer in project netconf by opendaylight.

the class SSHTest method test.

@Test
public void test() throws Exception {
    File sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
    sshKeyPair.deleteOnExit();
    new Thread(new EchoServer(), "EchoServer").start();
    final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 10831);
    final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerEx, nettyGroup, nioExec);
    sshProxyServer.bind(new SshProxyServerConfigurationBuilder().setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS).setAuthenticator((username, password) -> true).setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
    final EchoClientHandler echoClientHandler = connectClient(addr);
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
        Thread.sleep(500);
    }
    assertTrue(echoClientHandler.isConnected());
    LOG.info("connected, writing to client");
    echoClientHandler.write(AHOJ);
    // check that server sent back the same string
    stopwatch = stopwatch.reset().start();
    while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
        Thread.sleep(500);
    }
    try {
        final String read = echoClientHandler.read();
        assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
    } finally {
        LOG.info("Closing socket");
        sshProxyServer.close();
    }
}
Also used : SshProxyServer(org.opendaylight.netconf.ssh.SshProxyServer) SshProxyServerConfigurationBuilder(org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder) InetSocketAddress(java.net.InetSocketAddress) Stopwatch(com.google.common.base.Stopwatch) File(java.io.File) Test(org.junit.Test)

Example 2 with SshProxyServer

use of org.opendaylight.netconf.ssh.SshProxyServer in project netconf by opendaylight.

the class SSHServerTest method setUp.

@Before
public void setUp() throws Exception {
    sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
    sshKeyPair.deleteOnExit();
    LOG.info("Creating SSH server");
    final InetSocketAddress addr = InetSocketAddress.createUnresolved(HOST, PORT);
    server = new SshProxyServer(minaTimerEx, clientGroup, nioExec);
    server.bind(new SshProxyServerConfigurationBuilder().setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS).setAuthenticator((username, password) -> true).setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
    LOG.info("SSH server started on {}", PORT);
}
Also used : SshProxyServer(org.opendaylight.netconf.ssh.SshProxyServer) SshProxyServerConfigurationBuilder(org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder) InetSocketAddress(java.net.InetSocketAddress) Before(org.junit.Before)

Example 3 with SshProxyServer

use of org.opendaylight.netconf.ssh.SshProxyServer in project netconf by opendaylight.

the class NetconfDeviceSimulator method close.

@Override
public void close() {
    for (final SshProxyServer sshWrapper : sshWrappers) {
        try {
            sshWrapper.close();
        } catch (final IOException e) {
            LOG.debug("Wrapper {} failed to close", sshWrapper, e);
        }
    }
    for (final Channel deviceCh : devicesChannels) {
        deviceCh.close();
    }
    nettyThreadgroup.shutdownGracefully();
    minaTimerExecutor.shutdownNow();
    nioExecutor.shutdownNow();
}
Also used : SshProxyServer(org.opendaylight.netconf.ssh.SshProxyServer) Channel(io.netty.channel.Channel) IOException(java.io.IOException)

Example 4 with SshProxyServer

use of org.opendaylight.netconf.ssh.SshProxyServer in project netconf by opendaylight.

the class NetconfDeviceSimulator method start.

public List<Integer> start() {
    LOG.info("Starting {}, {} simulated devices starting on port {}", configuration.getDeviceCount(), configuration.isSsh() ? "SSH" : "TCP", configuration.getStartingPort());
    final SharedSchemaRepository schemaRepo = new SharedSchemaRepository("netconf-simulator");
    final Set<Capability> capabilities = parseSchemasToModuleCapabilities(schemaRepo);
    final NetconfServerDispatcherImpl dispatcher = createDispatcher(capabilities, sourceIdentifier -> schemaRepo.getSchemaSource(sourceIdentifier, YangTextSchemaSource.class));
    int currentPort = configuration.getStartingPort();
    final List<Integer> openDevices = new ArrayList<>();
    // Generate key to temp folder
    final KeyPairProvider keyPairProvider = new VirtualKeyPairProvider();
    final AsynchronousChannelGroup group;
    try {
        group = AsynchronousChannelGroup.withThreadPool(nioExecutor);
    } catch (final IOException e) {
        throw new IllegalStateException("Failed to create group", e);
    }
    for (int i = 0; i < configuration.getDeviceCount(); i++) {
        if (currentPort > 65535) {
            LOG.warn("Port cannot be greater than 65535, stopping further attempts.");
            break;
        }
        final InetSocketAddress address = getAddress(configuration.getIp(), currentPort);
        final ChannelFuture server;
        if (configuration.isSsh()) {
            final InetSocketAddress bindingAddress = InetSocketAddress.createUnresolved("0.0.0.0", currentPort);
            final LocalAddress tcpLocalAddress = new LocalAddress(address.toString());
            server = dispatcher.createLocalServer(tcpLocalAddress);
            try {
                final SshProxyServer sshServer = new SshProxyServer(minaTimerExecutor, nettyThreadgroup, group);
                sshServer.bind(getSshConfiguration(bindingAddress, tcpLocalAddress, keyPairProvider));
                sshWrappers.add(sshServer);
            } catch (final BindException e) {
                LOG.warn("Cannot start simulated device on {}, port already in use. Skipping.", address);
                // Close local server and continue
                server.cancel(true);
                if (server.isDone()) {
                    server.channel().close();
                }
                continue;
            } catch (final IOException e) {
                LOG.warn("Cannot start simulated device on {} due to IOException.", address, e);
                break;
            } finally {
                currentPort++;
            }
            try {
                server.get();
            } catch (final InterruptedException e) {
                throw new RuntimeException(e);
            } catch (final ExecutionException e) {
                LOG.warn("Cannot start ssh simulated device on {}, skipping", address, e);
                continue;
            }
            LOG.debug("Simulated SSH device started on {}", address);
        } else {
            server = dispatcher.createServer(address);
            currentPort++;
            try {
                server.get();
            } catch (final InterruptedException e) {
                throw new RuntimeException(e);
            } catch (final ExecutionException e) {
                LOG.warn("Cannot start tcp simulated device on {}, skipping", address, e);
                continue;
            }
            LOG.debug("Simulated TCP device started on {}", server.channel().localAddress());
        }
        devicesChannels.add(server.channel());
        openDevices.add(currentPort - 1);
    }
    if (openDevices.size() == configuration.getDeviceCount()) {
        LOG.info("All simulated devices started successfully from port {} to {}", configuration.getStartingPort(), currentPort - 1);
    } else if (openDevices.size() == 0) {
        LOG.warn("No simulated devices started.");
    } else {
        LOG.warn("Not all simulated devices started successfully. Started devices ar on ports {}", openDevices);
    }
    return openDevices;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) Capability(org.opendaylight.netconf.api.capability.Capability) YangModuleCapability(org.opendaylight.netconf.api.capability.YangModuleCapability) BasicCapability(org.opendaylight.netconf.api.capability.BasicCapability) YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) SshProxyServer(org.opendaylight.netconf.ssh.SshProxyServer) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) BindException(java.net.BindException) IOException(java.io.IOException) NetconfServerDispatcherImpl(org.opendaylight.netconf.impl.NetconfServerDispatcherImpl) KeyPairProvider(org.opendaylight.netconf.shaded.sshd.common.keyprovider.KeyPairProvider) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) ExecutionException(java.util.concurrent.ExecutionException) SharedSchemaRepository(org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository)

Aggregations

SshProxyServer (org.opendaylight.netconf.ssh.SshProxyServer)4 InetSocketAddress (java.net.InetSocketAddress)3 IOException (java.io.IOException)2 SshProxyServerConfigurationBuilder (org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder)2 Stopwatch (com.google.common.base.Stopwatch)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 LocalAddress (io.netty.channel.local.LocalAddress)1 File (java.io.File)1 BindException (java.net.BindException)1 AsynchronousChannelGroup (java.nio.channels.AsynchronousChannelGroup)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Before (org.junit.Before)1 Test (org.junit.Test)1 BasicCapability (org.opendaylight.netconf.api.capability.BasicCapability)1 Capability (org.opendaylight.netconf.api.capability.Capability)1 YangModuleCapability (org.opendaylight.netconf.api.capability.YangModuleCapability)1 NetconfServerDispatcherImpl (org.opendaylight.netconf.impl.NetconfServerDispatcherImpl)1 KeyPairProvider (org.opendaylight.netconf.shaded.sshd.common.keyprovider.KeyPairProvider)1