*** Comments ***
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


*** Settings ***
Documentation   Library providing keywords to interact with NF via SSH.
Library   SSHLibrary


*** Keywords ***
Test SSH Connection
    [Documentation]   Test the SSH connection to the host with the user and credentials passed as arguments.
    [Arguments]   ${host}   ${username}   ${password}   ${privatekey}
    Open Connection   ${host}
    IF   "${password}" != "${EMPTY}"
        Login   ${username}   ${password}
    ELSE
        Login With Public Key   ${username}   ${privatekey}
    END
    Execute Command   hostname
    Close All Connections

Check If Remote File Exists
    [Documentation]   Check via SSH if a file exists in in the remote host, using the user and credentials passed as arguments.
    [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${file}
    Open Connection   ${host}
    IF   "${password}" != "${EMPTY}"
        Login   ${username}   ${password}
    ELSE
        Login With Public Key   ${username}   ${privatekey}
    END
    ${rc}=   Execute Command   ls ${file} >& /dev/null   return_stdout=False   return_rc=True
    Close All Connections
    Should Be Equal As Integers   ${rc}   0

Check If Remote Folder Exists
    [Documentation]   Check via SSH if a folder exists in in the remote host, using the user and credentials passed as arguments.
    [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${folder}
    Open Connection   ${host}
    IF   "${password}" != "${EMPTY}"
        Login   ${username}   ${password}
    ELSE
        Login With Public Key   ${username}   ${privatekey}
    END
    ${output}=   Execute Command   ls -d ${folder}
    Close All Connections
    Should Be Equal As Strings   ${output}   ${folder}

Get Remote File Content
    [Documentation]   Get via SSH the content of a remote file and return it.
    [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${file}
    Open Connection   ${host}
    IF   "${password}" != "${EMPTY}"
        Login   ${username}   ${password}
    ELSE
        Login With Public Key   ${username}   ${privatekey}
    END
    ${output}=   Execute Command   cat ${file}
    Close All Connections
    RETURN   ${output}

Ping Many
    [Documentation]   Connect via SSH to a remote host and test ping to a list of IP addresses, passed as argument.
    [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   @{ip_list}
    Open Connection   ${host}
    IF   "${password}" != "${EMPTY}"
        Login   ${username}   ${password}
    ELSE
        Login With Public Key   ${username}   ${privatekey}
    END
    FOR   ${ip}   IN   @{ip_list}
        ${result}=   Execute Command   ping -c 5 -W 1 ${ip} > /dev/null && echo OK   shell=True
        Log   ${result}
        Should Contain   ${result}   OK
    END
    Close All Connections

Execute Remote Command Check Rc Return Output
    [Documentation]   Execute via SSH a command in a remote host and, if the command succeeds, return the output.
    [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${command}
    Open Connection   ${host}
    IF   "${password}" != "${EMPTY}"
        Login   ${username}   ${password}
    ELSE
        Login With Public Key   ${username}   ${privatekey}
    END
    ${stdout}   ${rc}=   Execute Command   ${command}   return_rc=True   return_stdout=True
    Log   ${rc}
    Log   ${stdout}
    Close All Connections
    Should Be Equal As Integers   ${rc}   0
    RETURN   ${stdout}
