X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2Fhandler%2Fssh%2Fclient%2FSshClient.java;fp=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2Fhandler%2Fssh%2Fclient%2FSshClient.java;h=3520fe029d41cbf01218cb6c8df9dd4500522be3;hb=c3108b4e80ec9f6ee6c8cf96df3009bb91dc8bc0;hp=0000000000000000000000000000000000000000;hpb=04a788d2df5303c60cdbcff02254291f411566bd;p=controller.git diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/SshClient.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/SshClient.java new file mode 100644 index 0000000000..3520fe029d --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/SshClient.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client; + +import ch.ethz.ssh2.Connection; +import ch.ethz.ssh2.Session; +import ch.ethz.ssh2.channel.Channel; +import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler; +import org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket.VirtualSocket; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * Wrapper class around GANYMED SSH java library. + */ +public class SshClient { + private final VirtualSocket socket; + private final Map openSessions = new HashMap<>(); + private final AuthenticationHandler authenticationHandler; + private Connection connection; + + public SshClient(VirtualSocket socket, AuthenticationHandler authenticationHandler) throws IOException { + this.socket = socket; + this.authenticationHandler = authenticationHandler; + } + + public SshSession openSession() throws IOException { + if (connection == null) { + connect(); + } + + Session session = connection.openSession(); + SshSession sshSession = new SshSession(session); + openSessions.put(openSessions.size(), sshSession); + + return sshSession; + } + + private void connect() throws IOException { + connection = new Connection(socket); + + connection.connect(); + authenticationHandler.authenticate(connection); + } + + public void closeSession(SshSession session) { + if (session.getState() == Channel.STATE_OPEN || session.getState() == Channel.STATE_OPENING) { + session.close(); + } + } + + public void close() { + for (SshSession session : openSessions.values()){ + closeSession(session); + } + + openSessions.clear(); + + if (connection != null) { + connection.close(); + } + } +}