X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FRouting.py;h=767fb3aff035e5a8a7b7550bdf205adad7fd67b7;hp=199b6de8d593ad12ddeb4138a86ec304566eb159;hb=7c3e0cc41f55327d6eeb04fe757c6e80064ab28a;hpb=dec1188c7f89f5f8f97085b5f68c6f1d918586b8 diff --git a/resources/libraries/python/Routing.py b/resources/libraries/python/Routing.py index 199b6de8d5..767fb3aff0 100644 --- a/resources/libraries/python/Routing.py +++ b/resources/libraries/python/Routing.py @@ -15,9 +15,10 @@ from resources.libraries.python.VatExecutor import VatTerminal from resources.libraries.python.topology import Topology - +from resources.libraries.python.ssh import exec_cmd_no_error class Routing(object): + """Routing utilities.""" @staticmethod @@ -93,3 +94,25 @@ class Routing(object): prefix_length=prefix_len, fib_number=fib_id, where=place) + + @staticmethod + def add_route(node, ip, prefix, gw, namespace=None): + """Add route in namespace. + + :param node: Node where to execute command. + :param ip: Route destination IP. + :param prefix: IP prefix. + :param namespace: Execute command in namespace. Optional + :param gw: Gateway. + :type node: dict + :type ip: str + :type prefix: int + :type gw: str + :type namespace: str + """ + if namespace is not None: + cmd = 'ip netns exec {} ip route add {}/{} via {}'.format( + namespace, ip, prefix, gw) + else: + cmd = 'ip route add {}/{} via {}'.format(ip, prefix, gw) + exec_cmd_no_error(node, cmd, sudo=True)