/*
** Module   : SSH2Transport.java
** Abstract : establish an SSH2-based communications channel 
**
** Copyright (c) 2008-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090123 Created initial version. Provides an SSH2-based communications channel.
** 002 CA  20210827 Refactored to allow other test types, beside terminal (which can be executed from the same 
**                  test suite).  Added SOAP and REST web service tests.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.transport;

import java.io.*;
import com.jcraft.jsch.*;
import com.goldencode.util.*;
import com.goldencode.harness.*;

/**
 * Provides an SSH2-based communications channel using the JSCH project.
 */
class SSH2Transport
implements Connection
{
   /** JSCH session object used to obtaining a channel. */
   private Session session = null;
   
   /** JSCH channel that provides streams over SSH2. */
   private ChannelShell channel = null;
   
   /**
    * Connect to the defined system/account and initialize a working
    * communications session.
    *
    * @param    cfg
    *           Connection configuration.
    * @param    rc
    *           Object in which to store the details of failures.
    *
    * @return   <code>true</code> if the connection was successfully made
    *           and is now usable. <code>false</code> on any failure.
    */
   public synchronized boolean connect(TerminalSessionSettings cfg, FailureNotification rc)
   {
      if (channel == null)
      {
         try
         {
            JSch jsch = new JSch();
            
            session = jsch.getSession(cfg.userid, cfg.host, cfg.port);
            session.setPassword(cfg.password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(30000);
            
            channel = (ChannelShell) session.openChannel("shell");
            
            channel.setPtyType(cfg.termType,
                               cfg.termColumns,
                               cfg.termRows,
                               0,
                               0);
            channel.connect(3*1000);
         }
         
         catch (JSchException je)
         {
            rc.setResult(ExitCode.CONNECT_FAILURE);
            rc.setDetails(LogHelper.dumpThrowable(je));
            
            // cleanup
            session = null;
            channel = null;
            return false;
         }
      }
      
      return true;
   }
   
   /**
    * Disconnect the currently working communications session.
    */
   public synchronized void disconnect()
   {
      if (channel != null)
      {
         channel.disconnect();
         channel = null;
      }
      
      if (session != null)
      {
         session.disconnect();
         session = null;
      }
   }
   
   /**
    * Return the output destination for the currently working communications
    * session.
    *
    * @param    rc
    *           Object in which to store the details of failures.
    *
    * @return   The stream to be written to or <code>null</code> if the
    *           communications session is invalid or not active.
    */
   public synchronized OutputStream getOutputStream(FailureNotification rc)
   {
      OutputStream os = null;
      
      if (channel != null)
      {
         try
         {
            os = channel.getOutputStream();
         }
         
         catch (IOException ioe)
         {
            rc.setResult(ExitCode.UNKNOWN_FAILURE);
            rc.setDetails(LogHelper.dumpThrowable(ioe));
         }
      }
      
      return os; 
   }
   
   /**
    * Return the input source for the currently working communications
    * session.
    *
    * @param    rc
    *           Object in which to store the details of failures.
    *
    * @return   The stream to be read from or <code>null</code> if the
    *           communications session is invalid or not active.
    */
   public synchronized InputStream getInputStream(FailureNotification rc)
   {
      InputStream is = null;
      
      if (channel != null)
      {
         try
         {
            is = channel.getInputStream();
         }
         
         catch (IOException ioe)
         {
            rc.setResult(ExitCode.UNKNOWN_FAILURE);
            rc.setDetails(LogHelper.dumpThrowable(ioe));
         }
      }
      
      return is; 
   }
}
