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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Traffic script executor library."""
16 from robot.api import logger
18 from resources.libraries.python.constants import Constants
19 from resources.libraries.python.ssh import SSH
21 __all__ = ['TrafficScriptExecutor']
24 class TrafficScriptExecutor(object):
25 """Traffic script executor utilities."""
29 """Escape quotation mark and dollar mark for shell command.
31 :param string: String to escape.
33 :return: Escaped string.
36 return string.replace('"', '\\"').replace("$", "\\$")
39 def run_traffic_script_on_node(script_file_name, node, script_args,
41 """Run traffic script on the TG node.
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
49 :type script_args: str
51 :raises RuntimeError: ICMP echo Rx timeout.
52 :raises RuntimeError: DHCP REQUEST Rx timeout.
53 :raises RuntimeError: DHCP DISCOVER timeout.
54 :raises RuntimeError: TCP/UDP Rx timeout.
55 :raises RuntimeError: ARP reply timeout.
56 :raises RuntimeError: Traffic script execution failed.
58 logger.trace("{}".format(timeout))
61 cmd = ("cd {}; virtualenv --system-site-packages env && " +
62 "export PYTHONPATH=${{PWD}}; " +
63 ". ${{PWD}}/env/bin/activate; " +
64 "resources/traffic_scripts/{} {}") \
65 .format(Constants.REMOTE_FW_DIR, script_file_name,
67 (ret_code, stdout, stderr) = ssh.exec_command_sudo(
68 'sh -c "{}"'.format(TrafficScriptExecutor._escape(cmd)),
70 logger.debug("stdout: {}".format(stdout))
71 logger.debug("stderr: {}".format(stderr))
72 logger.debug("ret_code: {}".format(ret_code))
74 if "RuntimeError: ICMP echo Rx timeout" in stderr:
75 raise RuntimeError("ICMP echo Rx timeout")
76 elif "RuntimeError: DHCP REQUEST Rx timeout" in stderr:
77 raise RuntimeError("DHCP REQUEST Rx timeout")
78 elif "RuntimeError('DHCP DISCOVER timeout')" in stderr:
79 raise RuntimeError("DHCP DISCOVER timeout")
80 elif "RuntimeError: TCP/UDP Rx timeout" in stderr:
81 raise RuntimeError("TCP/UDP Rx timeout")
82 elif "Error occurred: ARP reply timeout" in stdout:
83 raise RuntimeError("ARP reply timeout")
85 raise RuntimeError("Traffic script execution failed")
88 def traffic_script_gen_arg(rx_if, tx_if, src_mac, dst_mac, src_ip, dst_ip):
89 """Generate traffic script basic arguments string.
91 :param rx_if: Interface that receives traffic.
92 :param tx_if: Interface that sends traffic.
93 :param src_mac: Source MAC address.
94 :param dst_mac: Destination MAC address.
95 :param src_ip: Source IP address.
96 :param dst_ip: Destination IP address.
103 :return: Traffic script arguments string.
106 args = ('--rx_if {0} --tx_if {1} --src_mac {2} --dst_mac {3} --src_ip'
107 ' {4} --dst_ip {5}').format(rx_if, tx_if, src_mac, dst_mac,