2 * Copyright (c) 2013 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client;
11 import ch.ethz.ssh2.Connection;
12 import ch.ethz.ssh2.Session;
13 import ch.ethz.ssh2.channel.Channel;
14 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
15 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket.VirtualSocket;
17 import java.io.IOException;
18 import java.util.HashMap;
22 * Wrapper class around GANYMED SSH java library.
24 public class SshClient {
25 private final VirtualSocket socket;
26 private final Map<Integer, SshSession> openSessions = new HashMap<>();
27 private final AuthenticationHandler authenticationHandler;
28 private Connection connection;
30 public SshClient(VirtualSocket socket, AuthenticationHandler authenticationHandler) throws IOException {
32 this.authenticationHandler = authenticationHandler;
35 public SshSession openSession() throws IOException {
36 if (connection == null) {
40 Session session = connection.openSession();
41 SshSession sshSession = new SshSession(session);
42 openSessions.put(openSessions.size(), sshSession);
47 private void connect() throws IOException {
48 connection = new Connection(socket);
51 authenticationHandler.authenticate(connection);
54 public void closeSession(SshSession session) {
55 if (session.getState() == Channel.STATE_OPEN || session.getState() == Channel.STATE_OPENING) {
61 for (SshSession session : openSessions.values()){
62 closeSession(session);
67 if (connection != null) {