f730d3a0e3bb5345c8dd3fd5da3e52d7028969c7
[csit.git] / resources / libraries / python / TrafficScriptExecutor.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Traffic script executor library."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.constants import Constants
19 from resources.libraries.python.ssh import SSH
20
21 __all__ = ['TrafficScriptExecutor']
22
23
24 class TrafficScriptExecutor(object):
25     """Traffic script executor utilities."""
26
27     @staticmethod
28     def _escape(string):
29         """Escape quotation mark and dollar mark for shell command.
30
31         :param string: String to escape.
32         :type string: str
33         :return: Escaped string.
34         :rtype: str
35         """
36         return string.replace('"', '\\"').replace("$", "\\$")
37
38     @staticmethod
39     def run_traffic_script_on_node(script_file_name, node, script_args,
40                                    timeout=10):
41         """Run traffic script on the TG node.
42
43         :param script_file_name: Traffic script name.
44         :param node: Node to run traffic script on.
45         :param script_args: Traffic scripts arguments.
46         :param timeout: Timeout (optional).
47         :type script_file_name: str
48         :type node: dict
49         :type script_args: str
50         :type timeout: int
51         :raises RuntimeError: ICMP echo Rx timeout.
52         :raises RuntimeError: DHCP REQUEST Rx timeout.
53         :raises RuntimeError: TCP/UDP Rx timeout.
54         :raises RuntimeError: Traffic script execution failed.
55         """
56         logger.trace("{}".format(timeout))
57         ssh = SSH()
58         ssh.connect(node)
59         cmd = ("cd {}; virtualenv --system-site-packages env && " +
60                "export PYTHONPATH=${{PWD}}; " +
61                ". ${{PWD}}/env/bin/activate; " +
62                "resources/traffic_scripts/{} {}") \
63                   .format(Constants.REMOTE_FW_DIR, script_file_name,
64                           script_args)
65         (ret_code, stdout, stderr) = ssh.exec_command_sudo(
66             'sh -c "{}"'.format(TrafficScriptExecutor._escape(cmd)),
67             timeout=timeout)
68         logger.debug("stdout: {}".format(stdout))
69         logger.debug("stderr: {}".format(stderr))
70         logger.debug("ret_code: {}".format(ret_code))
71         if ret_code != 0:
72             if "RuntimeError: ICMP echo Rx timeout" in stderr:
73                 raise RuntimeError("ICMP echo Rx timeout")
74             elif "RuntimeError: DHCP REQUEST Rx timeout" in stderr:
75                 raise RuntimeError("DHCP REQUEST Rx timeout")
76             elif "RuntimeError: TCP/UDP Rx timeout" in stderr:
77                 raise RuntimeError("TCP/UDP Rx timeout")
78             else:
79                 raise RuntimeError("Traffic script execution failed")
80
81     @staticmethod
82     def traffic_script_gen_arg(rx_if, tx_if, src_mac, dst_mac, src_ip, dst_ip):
83         """Generate traffic script basic arguments string.
84
85         :param rx_if: Interface that receives traffic.
86         :param tx_if: Interface that sends traffic.
87         :param src_mac: Source MAC address.
88         :param dst_mac: Destination MAC address.
89         :param src_ip: Source IP address.
90         :param dst_ip: Destination IP address.
91         :type rx_if: str
92         :type tx_if: str
93         :type src_mac: str
94         :type dst_mac: str
95         :type src_ip: str
96         :type dst_ip: str
97         :return: Traffic script arguments string.
98         :rtype: str
99         """
100         args = ('--rx_if {0} --tx_if {1} --src_mac {2} --dst_mac {3} --src_ip'
101                 ' {4} --dst_ip {5}').format(rx_if, tx_if, src_mac, dst_mac,
102                                             src_ip, dst_ip)
103         return args