#!/usr/bin/env python3
# @file testNIC.py
#
# @brief
#
# @author Manuel Castilla
# @ingroup
# @date 16/04/2019
#
###########################################################################

from ssh import SshCmd,SshCmdException
from vuart import Vuart
import configparser

def configurationIpInterface(hostName,userName,password,busId,interfaceName,ip):
    """
    Set ip of white rabbit interface

    Args:
        hostName: ip remote host
        username: user name
        password: password
        busId: number PCIe bus
        interfaceName : interface name
        ip : ip set interface
    Raises:
        error function
    """
    try:
        sshCmd = SshCmd()
        command = "sudo ifconfig " + interfaceName + " " + ip
        sshCmd.exec(hostName,command,userName,password)
        command = "ifconfig | grep " + ip + " | wc -l"
        value = sshCmd.exec(hostName,command,userName,password)
        if (int(value) == 1):
            vuart = Vuart(hostName,userName,password,busId)
            value = vuart.setIp(ip)

            if (("IP-address: " + str(ip)) in value):
                print("Configuration ip interface: ok in host:",hostName)
            else:
                raise
    except SshCmdException as e:
        sshCmd.printSshErrorCommand(e.strError,command)
        print("Configuration ip interface: error in host:",hostName)
        raise
    except:
        print("Configuration ip interface2: error in host:",hostName)
        raise


def testIperf(hostName,userName,password,ip,hostName2,userName2,password2):
    """
    Test iperf by white rabbit interfaces

    Args:
        hostName: ip remote host client
        username: user name client
        password: password
        ip : ip server
        hostName2: ip remote host server
        username2: user name server
        password2: password
    Raises:
        error function
    """
    try:
        sshCmdServer = SshCmd()
        sshCmdClient = SshCmd()
        command = "iperf -s"
        sshCmdServer.exec(hostName2,command,userName2,password2,withResponse=False)
        command = "iperf -t 4 -c " + str(ip)
        value = sshCmdClient.exec(hostName,command,userName,password)

        if ("failed" in value):
            print("Test iperf: error")
            raise
        else:
            print(value)

        print("Test iperf: ok")
    except SshCmdException as e:
        print("Test iperf: error")
        print(e.strError)
        raise


#--------------------------------------------------#
#--------------------------------------------------#
# MAIN
#--------------------------------------------------#
#--------------------------------------------------#

def main(configuration):

    hostName1 = configuration["hostName1"]
    userName1 = configuration["userName1"]
    password1 = configuration["password1"]
    busIdSpec1 = configuration["busIdSpec1"]
    ipWR1 = configuration["ipWR1"]
    interfaceName1 = configuration["interfaceName1"]
    hostName2 = configuration["hostName2"]
    userName2 = configuration["userName2"]
    password2 = configuration["password2"]
    busIdSpec2 = configuration["busIdSpec2"]
    ipWR2 = configuration["ipWR2"]
    interfaceName2 = configuration["interfaceName2"]

    try:
        print("Executing NIC test")
        configurationIpInterface(hostName1,userName1,password1,busIdSpec1,interfaceName1,ipWR1)
        configurationIpInterface(hostName2,userName2,password2,busIdSpec2,interfaceName2,ipWR2)
        testIperf(hostName1,userName1,password1,ipWR2,hostName2,userName2,password2)
        print("Test NIC: successful")
    except:
        print("Test NIC: error")


if __name__ == '__main__':
    try:
        configuration = {}
        nameConfig = "configuration.cfg"

        config = configparser.ConfigParser()
        config.readfp(open(nameConfig))

        configuration["hostName1"] = config.get('config',"hostName1")
        configuration["userName1"] = config.get('config',"userName1")
        configuration["password1"] = config.get('config',"password1")
        configuration["busIdSpec1"] = config.get('config',"busIdSpec1")
        configuration["ipWR1"] = config.get('config',"ipWR1")
        configuration["interfaceName1"] = config.get('config',"interfaceName1")
        configuration["hostName2"] = config.get('config',"hostName2")
        configuration["userName2"] = config.get('config',"userName2")
        configuration["password2"] = config.get('config',"password2")
        configuration["busIdSpec2"] = config.get('config',"busIdSpec2")
        configuration["ipWR2"] = config.get('config',"ipWR2")
        configuration["interfaceName2"] = config.get('config',"interfaceName2")

        main(configuration)
    except configparser.Error as e:
        print("Error load configuration:",e.message)
