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;
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)
39 Session session = connection.openSession();
40 SshSession sshSession = new SshSession(session);
41 openSessions.put(openSessions.size(), sshSession);
46 private void connect() throws IOException {
47 connection = new Connection(socket);
50 authenticationHandler.authenticate(connection);
53 public void closeSession(SshSession session) {
54 if (session.getState() == Channel.STATE_OPEN || session.getState() == Channel.STATE_OPENING) {
55 session.session.close();
60 for (SshSession session : openSessions.values())
61 closeSession(session);
65 if (connection != null)