#!/usr/bin/env python3
# @file testNIC.py
#
# @brief Check the correct operation of the Network Interface Core
#
# @author Manuel Castilla
# @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()

        #check network manager
        command = "sudo service network-manager status | grep running"
        value = sshCmd.exec(hostName,command,userName,password)
        if ("running" in value):
            print("\nWarning: The network manager is running on host %s, the test may not work correctly\n" % ip)

        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):
            print("Configuration ip interface: ok on host:",hostName)
        else:
            raise
    except SshCmdException as e:
        sshCmd.printSshErrorCommand(e.strError,command)
        print("Configuration ip interface: error on host:",hostName)
        raise
    except:
        print("Configuration ip interface: error on 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("\nExecuting NIC test")
        print("This test needs the network manager stopped")
        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('pc1',"hostName")
        configuration["userName1"] = config.get('pc1',"userName")
        configuration["password1"] = config.get('pc1',"password")
        configuration["busIdSpec1"] = config.get('pc1',"busIdSpec")
        configuration["ipWR1"] = config.get('pc1',"ipWR")
        configuration["interfaceName1"] = config.get('pc1',"interfaceName")
        configuration["hostName2"] = config.get('pc2',"hostName")
        configuration["userName2"] = config.get('pc2',"userName")
        configuration["password2"] = config.get('pc2',"password")
        configuration["busIdSpec2"] = config.get('pc2',"busIdSpec")
        configuration["ipWR2"] = config.get('pc2',"ipWR")
        configuration["interfaceName2"] = config.get('pc2',"interfaceName")

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