org.apache.sshd.client.future.OpenFuture Java Examples

The following examples show how to use org.apache.sshd.client.future.OpenFuture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NetconfSessionMinaImpl.java    From onos with Apache License 2.0 6 votes vote down vote up
@Deprecated
private void openChannel() throws IOException {
    channel = session.createSubsystemChannel("netconf");
    OpenFuture channelFuture = channel.open();
    if (channelFuture.await(connectTimeout, TimeUnit.SECONDS)) {
        if (channelFuture.isOpened()) {
            streamHandler = new NetconfStreamThread(channel.getInvertedOut(), channel.getInvertedIn(),
                    channel.getInvertedErr(), deviceInfo,
                    new NetconfSessionDelegateImpl(), replies);
            primaryListeners.forEach(l -> streamHandler.addDeviceEventListener(l));
        } else {
            throw new NetconfException("Failed to open channel with device " +
                    deviceInfo);
        }
        sendHello();
    }
}
 
Example #2
Source File: OpenstackNetworkingUtil.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Sends flow trace string to node.
 *
 * @param requestString reqeust string
 * @param node src node
 * @return flow trace result in string format
 */
public static String sendTraceRequestToNode(String requestString,
                                            OpenstackNode node) {
    String traceResult = null;
    OpenstackSshAuth sshAuth = node.sshAuthInfo();

    try (SshClient client = SshClient.setUpDefaultClient()) {
        client.start();

        try (ClientSession session = client
                .connect(sshAuth.id(), node.managementIp().getIp4Address().toString(), SSH_PORT)
                .verify(TIMEOUT_MS, TimeUnit.SECONDS).getSession()) {
            session.addPasswordIdentity(sshAuth.password());
            session.auth().verify(TIMEOUT_MS, TimeUnit.SECONDS);


            try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) {

                log.debug("requestString: {}", requestString);
                final InputStream inputStream =
                        new ByteArrayInputStream(requestString.getBytes(Charsets.UTF_8));

                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                OutputStream errStream = new ByteArrayOutputStream();

                channel.setIn(new NoCloseInputStream(inputStream));
                channel.setErr(errStream);
                channel.setOut(outputStream);

                Collection<ClientChannelEvent> eventList = Lists.newArrayList();
                eventList.add(ClientChannelEvent.OPENED);

                OpenFuture channelFuture = channel.open();

                if (channelFuture.await(TIMEOUT_MS, TimeUnit.SECONDS)) {

                    long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;

                    while (!channelFuture.isOpened()) {
                        if ((timeoutExpiredMs - System.currentTimeMillis()) <= 0) {
                            log.error("Failed to open channel");
                            return null;
                        }
                    }
                    TimeUnit.SECONDS.sleep(WAIT_OUTPUT_STREAM_SECOND);

                    traceResult = outputStream.toString(Charsets.UTF_8.name());

                    channel.close();
                }
            } finally {
                session.close();
            }
        } finally {
            client.stop();
        }

    } catch (Exception e) {
        log.error("Exception occurred because of {}", e);
    }

    return traceResult;
}
 
Example #3
Source File: OpenstackNetworkingUiMessageHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private String sendTraceRequestToNode(String requestString,
                                            OpenstackNode node) {
    String traceResult = null;
    OpenstackSshAuth sshAuth = node.sshAuthInfo();

    try (SshClient client = SshClient.setUpDefaultClient()) {
        client.start();

        try (ClientSession session = client
                .connect(sshAuth.id(), node.managementIp().getIp4Address().toString(), SSH_PORT)
                .verify(TIMEOUT_MS, TimeUnit.SECONDS).getSession()) {
            session.addPasswordIdentity(sshAuth.password());
            session.auth().verify(TIMEOUT_MS, TimeUnit.SECONDS);


            try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) {

                log.debug("requestString: {}", requestString);
                final InputStream inputStream =
                        new ByteArrayInputStream(requestString.getBytes());

                OutputStream outputStream = new ByteArrayOutputStream();
                OutputStream errStream = new ByteArrayOutputStream();

                channel.setIn(new NoCloseInputStream(inputStream));
                channel.setErr(errStream);
                channel.setOut(outputStream);

                Collection<ClientChannelEvent> eventList = Lists.newArrayList();
                eventList.add(ClientChannelEvent.OPENED);

                OpenFuture channelFuture = channel.open();

                if (channelFuture.await(TIMEOUT_MS, TimeUnit.SECONDS)) {

                    long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;

                    while (!channelFuture.isOpened()) {
                        if ((timeoutExpiredMs - System.currentTimeMillis()) <= 0) {
                            log.error("Failed to open channel");
                            return null;
                        }
                    }
                    TimeUnit.SECONDS.sleep(WAIT_OUTPUT_STREAM_SECOND);

                    traceResult = ((ByteArrayOutputStream) outputStream).toString(Charsets.UTF_8.name());

                    channel.close();
                }
            } finally {
                session.close();
            }
        } finally {
            client.stop();
        }

    } catch (Exception e) {
        log.error("Exception occurred because of {}", e.toString());
    }

    return traceResult;
}