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.util.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.util.handler.ssh.authentication.AuthenticationHandler;
15 import org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket.VirtualSocket;
17 import java.io.IOException;
18 import java.util.HashMap;
23 * Wrapper class around GANYMED SSH java library.
25 public class SshClient {
26 private final VirtualSocket socket;
27 private final Map<Integer, SshSession> openSessions = new HashMap();
28 private final AuthenticationHandler authenticationHandler;
29 private Connection connection;
31 public SshClient(VirtualSocket socket,
32 AuthenticationHandler authenticationHandler) throws IOException {
34 this.authenticationHandler = authenticationHandler;
37 public SshSession openSession() throws IOException {
38 if(connection == null) connect();
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);
50 authenticationHandler.authenticate(connection);
53 public void closeSession(SshSession session) {
54 if( session.getState() == Channel.STATE_OPEN
55 || session.getState() == Channel.STATE_OPENING) {
56 session.session.close();
61 for(SshSession session : openSessions.values()) closeSession(session);
65 if(connection != null) connection.close();