X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FIPv4Util.py;h=3043f230c2d1812acaf8040faea551e1c28d6652;hp=98c089fcdb5c65fb1bc1555da6d8c7e53f96b8d1;hb=7c3e0cc41f55327d6eeb04fe757c6e80064ab28a;hpb=bfde681c3d8ae0008ecec131a18d3a2560a046ae diff --git a/resources/libraries/python/IPv4Util.py b/resources/libraries/python/IPv4Util.py index 98c089fcdb..3043f230c2 100644 --- a/resources/libraries/python/IPv4Util.py +++ b/resources/libraries/python/IPv4Util.py @@ -17,10 +17,8 @@ from robot.api import logger as log from robot.api.deco import keyword from resources.libraries.python.topology import Topology -from resources.libraries.python.topology import NodeType -from resources.libraries.python.TrafficScriptExecutor\ - import TrafficScriptExecutor from resources.libraries.python.IPv4Setup import get_node +from resources.libraries.python.ssh import exec_cmd class IPv4Util(object): @@ -36,18 +34,37 @@ class IPv4Util(object): get_node(node).arp_ping(ip_address, interface) @staticmethod - @keyword('Node "${node}" routes to IPv4 network "${network}" with prefix ' - 'length "${prefix_length}" using interface "${interface}" via ' - '"${gateway}"') + def set_interface_address(node, interface, address, prefix_length): + """See IPv4Node.set_ip for more information. + + :param node: Node where IP address should be set to. + :param interface: Interface name. + :param address: IP address. + :param prefix_length: Prefix length. + :type node: dict + :type interface: str + :type address: str + :type prefix_length: int + """ + log.debug('Node {} interface {} has IPv4 address {} with prefix ' + 'length {}'.format(Topology.get_node_hostname(node), + interface, address, prefix_length)) + get_node(node).set_ip(interface, address, int(prefix_length)) + + @staticmethod def set_route(node, network, prefix_length, interface, gateway): """See IPv4Node.set_route for more information. - :param node: - :param network: - :param prefix_length: - :param interface: - :param gateway: - :return: + :param node: Node where IP address should be set to. + :param network: IP network. + :param prefix_length: Prefix length. + :param interface: Interface name. + :param gateway: Gateway. + :type node: dict + :type network: str + :type prefix_length: int + :type interface: str + :type gateway: str """ log.debug('Node {} routes to network {} with prefix length {} ' 'via {} interface {}'.format(Topology.get_node_hostname(node), @@ -56,46 +73,6 @@ class IPv4Util(object): get_node(node).set_route(network, int(prefix_length), gateway, interface) - @staticmethod - @keyword('After ping is sent in topology "${nodes_info}" from node ' - '"${src_node}" interface "${src_port}" "${src_ip}" with ' - 'destination IPv4 address "${dst_ip}" of node "${dst_node}" ' - 'interface "${dst_port}" a ping response arrives and TTL is ' - 'decreased by "${hops}"') - def send_ping(nodes_info, src_node, src_port, src_ip, dst_ip, dst_node, - dst_port, hops): - """Send IPv4 ping and wait for response. - - :param nodes_info: Dictionary containing information on all nodes - in topology. - :param src_node: Source node. - :param src_port: Source interface. - :param src_ip: Source ipv4 address. - :param dst_ip: Destination ipv4 address. - :param dst_node: Destination node. - :param dst_port: Destination interface. - :param hops: Number of hops between src_node and dst_node. - """ - log.debug('After ping is sent from node "{}" interface "{}" ' - 'with destination IPv4 address of node "{}" interface "{}" ' - 'a ping response arrives and TTL is decreased by "${}"'. - format(Topology.get_node_hostname(src_node), src_port, - Topology.get_node_hostname(dst_node), dst_port, hops)) - dst_mac = None - src_mac = Topology.get_interface_mac(src_node, src_port) - if dst_node['type'] == NodeType.TG: - dst_mac = Topology.get_interface_mac(src_node, src_port) - _, adj_int = Topology.\ - get_adjacent_node_and_interface(nodes_info, src_node, src_port) - first_hop_mac = adj_int['mac_address'] - args = '--src_if "{}" --src_mac "{}" --first_hop_mac "{}" ' \ - '--src_ip "{}" --dst_ip "{}" --hops "{}"'\ - .format(src_port, src_mac, first_hop_mac, src_ip, dst_ip, hops) - if dst_node['type'] == NodeType.TG: - args += ' --dst_if "{}" --dst_mac "{}"'.format(dst_port, dst_mac) - TrafficScriptExecutor.run_traffic_script_on_node( - "ipv4_ping_ttl_check.py", src_node, args) - @staticmethod @keyword('Get IPv4 address prefix of node "${node}" interface "${port}" ' 'from "${nodes_addr}"') @@ -104,7 +81,12 @@ class IPv4Util(object): :param node: Node dictionary. :param port: Interface name. - :return: IPv4 prefix length + :param nodes_addr: Available nodes IPv4 addresses. + :type node: dict + :type port: str + :type nodes_addr: dict + :return: IPv4 prefix length. + :rtype: int """ for net in nodes_addr.values(): for p in net['ports'].values(): @@ -122,7 +104,12 @@ class IPv4Util(object): :param node: Node dictionary. :param port: Interface name. - :return: IPv4 subnet of 'str' type + :param nodes_addr: Available nodes IPv4 addresses. + :type node: dict + :type port: int + :type nodes_addr: dict + :return: IPv4 subnet. + :rtype: str """ for net in nodes_addr.values(): for p in net['ports'].values(): @@ -148,7 +135,7 @@ class IPv4Util(object): """Get link IPv4 address. :param link: Link name. - :param nodes_addr: Available nodes IPv4 adresses. + :param nodes_addr: Available nodes IPv4 addresses. :type link: str :type nodes_addr: dict :return: Link IPv4 address. @@ -164,7 +151,7 @@ class IPv4Util(object): """Get link IPv4 address prefix. :param link: Link name. - :param nodes_addr: Available nodes IPv4 adresses. + :param nodes_addr: Available nodes IPv4 addresses. :type link: str :type nodes_addr: dict :return: Link IPv4 address prefix. @@ -174,3 +161,86 @@ class IPv4Util(object): if net is None: raise ValueError('Link "{0}" not found'.format(link)) return net.get('prefix') + + @staticmethod + def send_ping_from_node_to_dst(node, destination, namespace=None, + ping_count=3, interface=None): + """Send a ping from node to destination. Optionally, you can define a + namespace and interface from where to send a ping. + + :param node: Node to start ping on. + :param destination: IPv4 address where to send ping. + :param namespace: Namespace to send ping from. Optional + :param ping_count: Number of pings to send. Default 3 + :param interface: Interface from where to send ping. Optional + :type node: dict + :type destination: str + :type namespace: str + :type ping_count: int + :type interface: str + :raises RuntimeError: If no response for ping, raise error + """ + cmd = '' + if namespace is not None: + cmd = 'ip netns exec {0} ping -c{1} {2}'.format( + namespace, ping_count, destination) + elif interface is not None: + cmd = 'ping -I {0} -c{1} {2}'.format( + interface, ping_count, destination) + else: + cmd = 'ping -c{0} {1}'.format(ping_count, destination) + rc, stdout, stderr = exec_cmd(node, cmd, sudo=True) + if rc != 0: + raise RuntimeError("Ping Not Successful") + + @staticmethod + def set_linux_interface_arp(node, interface, ip, mac, namespace=None): + """Set arp on interface in linux. + + :param node: Node where to execute command. + :param interface: Interface in namespace. + :param ip: IP for arp. + :param mac: MAC address. + :param namespace: Execute command in namespace. Optional + :type node: dict + :type interface: str + :type ip: str + :type mac: str + :type namespace: str + :raises RuntimeError: Could not set ARP properly. + """ + if namespace is not None: + cmd = 'ip netns exec {} arp -i {} -s {} {}'.format( + namespace, interface, ip, mac) + else: + cmd = 'arp -i {} -s {} {}'.format(interface, ip, mac) + rc, _, stderr = exec_cmd(node, cmd, sudo=True) + if rc != 0: + raise RuntimeError("Arp set not successful, reason:{}". + format(stderr)) + + @staticmethod + def set_linux_interface_ip(node, interface, ip, prefix, namespace=None): + """Set IP address to interface in linux. + + :param node: Node where to execute command. + :param interface: Interface in namespace. + :param ip: IP to be set on interface. + :param prefix: IP prefix. + :param namespace: Execute command in namespace. Optional + :type node: dict + :type interface: str + :type ip: str + :type prefix: int + :type namespace: str + :raises RuntimeError: IP could not be set. + """ + if namespace is not None: + cmd = 'ip netns exec {} ip addr add {}/{} dev {}'.format( + namespace, ip, prefix, interface) + else: + cmd = 'ip addr add {}/{} dev {}'.format(ip, prefix, interface) + (rc, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True) + if rc != 0: + raise RuntimeError( + 'Could not set IP for interface, reason:{}'.format(stderr))