-from resources.libraries.python.topology import NodeType
-from resources.libraries.python.VatExecutor import VatExecutor
-from resources.libraries.python.TrafficScriptExecutor\
- import TrafficScriptExecutor
-
-
-class IPv4Node(object):
- """Abstract class of a node in a topology."""
- __metaclass__ = ABCMeta
-
- def __init__(self, node_info):
- self.node_info = node_info
-
- @staticmethod
- def _get_netmask(prefix_length):
- bits = 0xffffffff ^ (1 << 32 - prefix_length) - 1
- return inet_ntoa(pack('>I', bits))
-
- @abstractmethod
- def set_ip(self, interface, address, prefix_length):
- """Configure IPv4 address on interface
-
- :param interface: interface name
- :param address:
- :param prefix_length:
- :type interface: str
- :type address: str
- :type prefix_length: int
- :return: nothing
- """
- pass
-
- @abstractmethod
- def set_interface_state(self, interface, state):
- """Set interface state
-
- :param interface: interface name string
- :param state: one of following values: "up" or "down"
- :return: nothing
- """
- pass
-
- @abstractmethod
- def set_route(self, network, prefix_length, gateway, interface):
- """Configure IPv4 route
-
- :param network: network IPv4 address
- :param prefix_length: mask length
- :param gateway: IPv4 address of the gateway
- :param interface: interface name
- :type network: str
- :type prefix_length: int
- :type gateway: str
- :type interface: str
- :return: nothing
- """
- pass
-
- @abstractmethod
- def unset_route(self, network, prefix_length, gateway, interface):
- """Remove specified IPv4 route
-
- :param network: network IPv4 address
- :param prefix_length: mask length
- :param gateway: IPv4 address of the gateway
- :param interface: interface name
- :type network: str
- :type prefix_length: int
- :type gateway: str
- :type interface: str
- :return: nothing
- """
- pass
-
- @abstractmethod
- def flush_ip_addresses(self, interface):
- """Flush all IPv4 addresses from specified interface
-
- :param interface: interface name
- :type interface: str
- :return: nothing
- """
- pass
-
- @abstractmethod
- def ping(self, destination_address, source_interface):
- """Send an ICMP request to destination node
-
- :param destination_address: address to send the ICMP request
- :param source_interface:
- :type destination_address: str
- :type source_interface: str
- :return: nothing
- """
- pass
-
-
-class Tg(IPv4Node):
- """Traffic generator node"""
- def __init__(self, node_info):
- super(Tg, self).__init__(node_info)
-
- def _execute(self, cmd):
- return ssh.exec_cmd_no_error(self.node_info, cmd)
-
- def _sudo_execute(self, cmd):
- return ssh.exec_cmd_no_error(self.node_info, cmd, sudo=True)
-
- def set_ip(self, interface, address, prefix_length):
- cmd = 'ip -4 addr flush dev {}'.format(interface)
- self._sudo_execute(cmd)
- cmd = 'ip addr add {}/{} dev {}'.format(address, prefix_length,
- interface)
- self._sudo_execute(cmd)
-
- # TODO: not ipv4-specific, move to another class
- def set_interface_state(self, interface, state):
- cmd = 'ip link set {} {}'.format(interface, state)
- self._sudo_execute(cmd)
-
- def set_route(self, network, prefix_length, gateway, interface):
- netmask = self._get_netmask(prefix_length)
- cmd = 'route add -net {} netmask {} gw {}'.\
- format(network, netmask, gateway)
- self._sudo_execute(cmd)
-
- def unset_route(self, network, prefix_length, gateway, interface):
- self._sudo_execute('ip route delete {}/{}'.
- format(network, prefix_length))
-
- def arp_ping(self, destination_address, source_interface):
- self._sudo_execute('arping -c 1 -I {} {}'.format(source_interface,
- destination_address))
-
- def ping(self, destination_address, source_interface):
- self._execute('ping -c 1 -w 5 -I {} {}'.format(source_interface,
- destination_address))
-
- def flush_ip_addresses(self, interface):
- self._sudo_execute('ip addr flush dev {}'.format(interface))
-
-
-class Dut(IPv4Node):
- """Device under test"""
- def __init__(self, node_info):
- super(Dut, self).__init__(node_info)
-
- def get_sw_if_index(self, interface):
- """Get sw_if_index of specified interface from current node
-
- :param interface: interface name
- :type interface: str
- :return: sw_if_index of 'int' type
- """
- return Topology().get_interface_sw_index(self.node_info, interface)
-
- def exec_vat(self, script, **args):
- """Wrapper for VAT executor.
-
- :param script: script to execute
- :param args: parameters to the script
- :type script: str
- :type args: dict
- :return: nothing
- """
- # TODO: check return value
- VatExecutor.cmd_from_template(self.node_info, script, **args)
-
- def set_arp(self, interface, ip_address, mac_address):
- """Set entry in ARP cache.
-
- :param interface: Interface name.
- :param ip_address: IP address.
- :param mac_address: MAC address.
- :type interface: str
- :type ip_address: str
- :type mac_address: str
- """
- self.exec_vat('add_ip_neighbor.vat',
- sw_if_index=self.get_sw_if_index(interface),
- ip_address=ip_address, mac_address=mac_address)
-
- def set_ip(self, interface, address, prefix_length):
- self.exec_vat('add_ip_address.vat',
- sw_if_index=self.get_sw_if_index(interface),
- address=address, prefix_length=prefix_length)
-
- def set_interface_state(self, interface, state):
- if state == 'up':
- state = 'admin-up link-up'
- elif state == 'down':
- state = 'admin-down link-down'
- else:
- raise Exception('Unexpected interface state: {}'.format(state))
-
- self.exec_vat('set_if_state.vat',
- sw_if_index=self.get_sw_if_index(interface), state=state)
-
- def set_route(self, network, prefix_length, gateway, interface):
- sw_if_index = self.get_sw_if_index(interface)
- self.exec_vat('add_route.vat',
- network=network, prefix_length=prefix_length,
- gateway=gateway, sw_if_index=sw_if_index)
-
- def unset_route(self, network, prefix_length, gateway, interface):
- self.exec_vat('del_route.vat', network=network,
- prefix_length=prefix_length, gateway=gateway,
- sw_if_index=self.get_sw_if_index(interface))
-
- def arp_ping(self, destination_address, source_interface):
- pass
-
- def flush_ip_addresses(self, interface):
- self.exec_vat('flush_ip_addresses.vat',
- sw_if_index=self.get_sw_if_index(interface))
-
- def ping(self, destination_address, source_interface):
- pass
-
-
-def get_node(node_info):
- """Creates a class instance derived from Node based on type.
-
- :param node_info: dictionary containing information on nodes in topology
- :return: Class instance that is derived from Node
- """
- if node_info['type'] == NodeType.TG:
- return Tg(node_info)
- elif node_info['type'] == NodeType.DUT:
- return Dut(node_info)
- else:
- raise NotImplementedError('Node type "{}" unsupported!'.
- format(node_info['type']))
-
-
-def get_node_hostname(node_info):
- """Get string identifying specifed node.
-
- :param node_info: Node in the topology.
- :type node_info: Dict
- :return: String identifying node.
- """
- return node_info['host']