def get_psuutil_data(dut, mode="status", cli_type=""): """ API to get psuutil data based on type of the command Author: Chaitanya Vella ([email protected]) :param dut: :param mode: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) if mode not in ["status", "numpsus", "version"]: st.log("Unsupported command type") return False if cli_type == "click": command = "sudo psuutil {}".format(mode) skip_tmpl = False if mode == "numpsus": skip_tmpl = True output = st.show(dut, command, skip_tmpl=skip_tmpl) if mode == "numpsus": return {"numpsus": utils.remove_last_line_from_string(output)} else: return output elif cli_type in ["klish", "rest-patch", "rest-put"]: if mode == "numpsus": return {"numpsus": str(len(get_platform_psu_summary(dut, cli_type=cli_type)))} output = get_platform_psu_summary(dut, cli_type=cli_type) for i in range(0, len(output)): output[i]["psu"] = output[i]["psu"].replace(" ", "") output[i]["status"] = output[i]["psu_status"] return output else: st.error("Unsupported CLI Type provided: {}".format(cli_type)) return []
def dhcp_client_start(dut, interface, family="ipv4", run_bckgrnd=False): """ API to start DHCLIENT in foreground for v4 and background for v6 Author Chaitanya Vella ([email protected]) :param dut: :type dut: :param portlist: :type portlist: """ if interface != None: if '/' in interface: interface = st.get_other_names(dut,[interface])[0] v6_opt = "" if family == "ipv4" else "-6" run_bckgrnd = True if (family == "ipv6" or run_bckgrnd) else False bckgrd = "&" if run_bckgrnd else "" command = "dhclient {} {} {}".format(v6_opt, interface, bckgrd) output = st.config(dut, command, skip_error_check=True) if bckgrd: output = remove_last_line_from_string(output) if output: return output.split(" ")[1] else: return None else: return True
def show_pddf_psuutils(dut, mode, cli_type=""): """ API to get PDDF PSUUTIL DATA based on type of mode Author: Chaitanya Vella ([email protected]) :param dut: :param mode: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) if mode not in ["numpsus", "status", "mfrinfo", "seninfo", "version"]: st.log("Unsupported modes provided ") return False if cli_type == "click": skip_tmpl = False if mode == "numpsus": skip_tmpl = True command = "sudo pddf_psuutil {}".format(mode) output = st.show(dut, command, skip_tmpl=skip_tmpl) if mode == "numpsus": return {"numpsus": utils.remove_last_line_from_string(output)} else: return output elif cli_type in ["click", "klish", "rest-patch"]: if mode in ["numpsus", "status", "version"]: return get_psuutil_data(dut, mode, cli_type) else: return get_platform_psu_summary(dut, cli_type=cli_type) else: st.error("Unsupported CLI Type provided: {}".format(cli_type)) return []
def sync_with_server_time(dut_list, ip, username, password): """ To Sync DUT with the CHEF server time. Author : Prudvi Mangadu ([email protected]) :param dut: :param ip: :param username: :param password: :return: """ ssh_obj = con_obj.connect_to_device(ip, username, password, 'ssh') if ssh_obj: prompt = ssh_obj.find_prompt() command = "date" output = ssh_obj.send_command(command, expect_string="{}|#|$".format(prompt)) if not output: return False st.log(output) date = utils_obj.remove_last_line_from_string(output) con_obj.ssh_disconnect(ssh_obj) st.log("Server Time : {}".format(date)) for dut in dut_list: ntp_obj.config_date(dut, date) return True else: con_obj.ssh_disconnect(ssh_obj) st.log( "SSH to Server is failed - failed to sync time between DUT and CHEF Server" ) return False
def generate_tech_support(dut): """ To Generate tech support and return the error if occurs. Author: Prudvi Mangadu ([email protected]) """ command = "show techsupport > /dev/null" return utils.remove_last_line_from_string(st.config(dut, command))
def show_pddf_fanutil(dut, mode, cli_type=""): """ API to get PDDF FANUTIL DATA based on type of mode Author: Chaitanya Vella ([email protected]) :param dut: :param mode: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) cli_type = "click" if mode == "version" else cli_type if cli_type == "click": if mode not in ["direction", "getspeed", "numfans", "status", "version"]: st.log("Unsupported modes provided ") return False skip_tmpl = False if mode == "numfans": skip_tmpl = True command = "sudo pddf_fanutil {}".format(mode) output = st.show(dut, command, skip_tmpl=skip_tmpl) if mode == "numfans": return {"numfans": utils.remove_last_line_from_string(output)} else: return output elif cli_type in ["klish", "rest-patch", "rest-put"]: if mode == "numfans": return {"numfans": str(len(get_platform_fan_status(dut, cli_type=cli_type)))} return get_platform_fan_status(dut, cli_type=cli_type) else: st.error("Unsupported CLI Type provided: {}".format(cli_type)) return []
def get_config_db(dut, table=None, object=None, attribute=None): """ Get Config DB json value based on table, object, attribute Author: Rakesh Kumar Vooturi ([email protected]) :param dut: :param table: :param object: :param attribute: :return: """ command = "cat /etc/sonic/config_db.json" output = st.show(dut, command, skip_tmpl=True) reg_output = utils_obj.remove_last_line_from_string(output) try: data = eval(json.dumps(json.loads(reg_output))) if table is None and object is None and attribute is None: return data elif table is not None and object is None and attribute is None: return data[table] elif table is not None and object is not None and attribute is None: return data[table][object] elif table is not None and object is not None and attribute is not None: return data[table][object][attribute] except Exception as e: st.log(e) return None
def get_copp_config(dut, **kwargs): """ Gets value of an attribute from a table Author : Chaitanya Lohith Bollapragada ([email protected]) :param dut: :param table_name: :param attribute: :return value: """ if 'table_name' not in kwargs: st.error("Mandatory parameter table_name not found") return False #command = "docker exec swss cat /etc/swss/config.d/00-copp.config.json" output = st.show(dut, "show copp config", skip_tmpl=True) reg_output = utils_obj.remove_last_line_from_string(output) if not reg_output: return False data = do_eval(reg_output) if not isinstance(data,dict): return False key = kwargs["table_name"] if key == "all": return data else: for each in data: if key in each: return data[each]['value'] return False
def verify_show_environment(dut, verify_str_list): """ To get show environment. Author: Prudvi Mangadu ([email protected]) """ command = "show environment" output = utils.remove_last_line_from_string(st.show(dut, command, skip_tmpl=True)) result = True for item in verify_str_list: if not re.findall(item, output, re.IGNORECASE): st.error("Item '{}' is NOT found".format(item)) result = False return result
def show_pddf_thermalutil(dut, mode): """ API to get PDDF thermal util DATA based on type of mode Author: Prudvi Mangadu ([email protected]) :param dut: :param mode: :return: """ if mode not in ["gettemp", "numthermals", "version"]: st.log("Unsupported modes provided ") return False skip_tmpl = False if mode in ["numthermals", 'version']: skip_tmpl = True command = "sudo pddf_thermalutil {}".format(mode) output = st.show(dut, command, skip_tmpl=skip_tmpl) if mode == "numthermals": return {"numthermals": utils.remove_last_line_from_string(output)} elif mode == "version": return {"version": utils.remove_last_line_from_string(output)} else: return output
def config_pddf_fanutil(dut, speed): """ API to set PDDF FANUTIL speed Author: Chaitanya Vella ([email protected]) :param dut: :param speed: :return: """ command = "sudo pddf_fanutil setspeed {}".format(speed) output = st.config(dut, command) if "Successful" not in utils.remove_last_line_from_string(output): st.log("Configuration of fan speed failed") return False return True
def config_pddf_ledutil(dut, mode, led_type, state=None): """ API to set PDDF LEDUTIL Author: Prudvi Mangadu ([email protected]) :param dut: :param mode: :param led_type: :param state: :return: """ command = "sudo pddf_ledutil getstatusled {}".format(led_type) if mode.lower() == 'set': command = "sudo pddf_ledutil setstatusled {} {}".format(led_type, state) output = st.config(dut, command) return utils.remove_last_line_from_string(output)
def upload_role_chef_server(chef_conn_obj, file_name="sonic.json"): """ :param chef_conn_obj: :param file_name: :return: """ if not chef_conn_obj: return False command = "sudo knife role from file {}".format(file_name) st.log("Remote Command: {}".format(command)) console_log = utils_obj.remove_last_line_from_string( con_obj.execute_command(chef_conn_obj, command)) st.log(console_log) if console_log.find("ERROR") > 1: return False return True
def set_copp_config(dut, *argv): """ To set the config into copp Author : Chaitanya Lohith Bollapragada ([email protected]) Expected input from user should be [[table_name,attribute,value],[table_name,attribute,value],...] :param dut: :param table_name: :param attribute: :param value: :return bool: Example : set_copp_config(dut, ["COPP_TABLE:trap.group.bgp.lacp","queue","4"]) set_copp_config(dut, ["COPP_TABLE:trap.group.bgp.lacp","queue","4"],["COPP_TABLE:trap.group.lldp.dhcp.udld","trap_priority","6"]) """ command = "docker exec swss cat /etc/swss/config.d/00-copp.config.json" output = st.show(dut, command, skip_tmpl=True) reg_output = utils_obj.remove_last_line_from_string(output) try: data = do_eval(reg_output) except Exception as e: st.log(e) reg_output = "{} ]".format(reg_output) data = do_eval(reg_output) st.log("ARGV {}".format(argv)) for eachli in argv: if len(eachli) != 3: st.error("Invalid input is provided {}".format(eachli)) return False table = eachli[0] attribute = eachli[1] value = eachli[2] for each in data: if table in each: each[table][attribute] = value break else: st.error("Table not found {}".format(table)) return False file_path = utils_obj.write_to_json_file(data, "/tmp/00-copp.config.json") st.log("FILE PATH -- {}".format(file_path)) st.upload_file_to_dut(dut, file_path, "/tmp/00-copp.config.json") command = "docker cp /tmp/00-copp.config.json swss:/etc/swss/config.d/00-copp.config.json" st.config(dut, command) command = "rm /tmp/00-copp.config.json" st.config(dut, command) return True
def set_copp_pir_config(dut, config, *args): """ To set the config into copp_config.json Author : [email protected] :param dut: :param config: :param args: :return: """ command = "sudo cat /etc/sonic/copp_config.json" output = st.show(dut, command, skip_tmpl=True) reg_output = utils_obj.remove_last_line_from_string(output) try: data = eval(reg_output) except Exception as e: st.log(e) reg_output = str(reg_output) + "\n" + "}" data = eval(reg_output) st.log("ARGS {}".format(args)) if config == "get": return data for eachli in args: if len(eachli) != 3: st.error("Invalid input is provided {}".format(eachli)) return False table = eachli[0] attribute = eachli[1] value = eachli[2] found_table = False if table in data['SCHEDULER'].keys(): data['SCHEDULER'][table][attribute] = value found_table = True if not found_table: st.error("Table not found {}".format(table)) return False file_path = utils_obj.write_to_json_file(data, "/tmp/00-copp.config.json") st.log("FILE PATH -- {}".format(file_path)) st.upload_file_to_dut(dut, file_path, "/tmp/00-copp.config.json") command = "sudo cp /tmp/00-copp.config.json /etc/sonic/copp_config.json" st.config(dut, command) command = "rm /tmp/00-copp.config.json" st.config(dut, command) return True
def gnmi_delete(dut, xpath, **kwargs): """ API to do GNMI get operations Author: Chaitanya Vella ([email protected]) :param dut: :param xpath: :param kwargs: :return: """ gnmi_debug(dut) st.log("Performing GNMI DELETE OPERATION ...") ip_address = kwargs.get('ip_address', '127.0.0.1') port = kwargs.get('port', '8080') insecure = kwargs.get('insecure', '') credentails = st.get_credentials(dut) username = kwargs.get('username', credentails[0]) password = kwargs.get('password', credentails[3]) cert = kwargs.get('cert') docker_command = get_docker_command() try: gnmi_command = 'gnmi_set --delete {} --target_addr {}:{}'.format( xpath, ip_address, port) if username: gnmi_command += " --username {}".format(username) if password: gnmi_command += " --password {}".format(password) if cert: gnmi_command += " --cert {}".format(cert) gnmi_command += " --insecure {}".format(insecure) command = '{} -c "{}"'.format(docker_command, gnmi_command) output = st.config(dut, command) st.log("OUTPUT : {}".format(output)) if not output: st.log("Observed empty OUTPUT") return False error_strings = [ "Error response", "rpc error", "gnmi_set.go", "Set failed", "Unknown desc", "failed" ] for err_code in error_strings: if err_code in util_obj.remove_last_line_from_string(output): st.log(output) return False return output except Exception as e: st.error(e) return False
def gnmi_cli(dut, query_type="once", ip_address="127.0.0.1", port=8080, **kwargs): """ API to configure gnmi using cli Author: Chaitanya Vella ([email protected]) :param dut: :param query_type: once, stream, poll :param ip_address: :param port: :param kwargs: :return: """ docker_command = get_docker_command() if not docker_command: st.log("Docker command not found ..") return False if query_type not in ["stream", "poll", "once"]: st.log("Provided unsupported query type") return False mandatory_kwargs = ["query_type", "xpath"] for arg in mandatory_kwargs: if arg not in kwargs: st.log("Please provide {} attribute".format(arg)) return False insecure = "" if "insecure" in kwargs and not kwargs["insecure"] else "-insecure" logstostderr = "" if "logstostderr" in kwargs and not kwargs["logstostderr"] else "-logstostderr" xpath_list = list(kwargs["xpath"]) if isinstance(kwargs["xpath"], list) else [kwargs["xpath"]] version = kwargs["version"] if "version" in kwargs and kwargs["version"] else 0 target = kwargs["target"] if "target" in kwargs and kwargs["target"] else "OC-YANG" gnmi_cmd = "gnmi_cli {} {} -address {}:{} ".format(insecure, logstostderr, ip_address, port) if query_type == "stream": stream_type = kwargs["streaming_type"] if "streaming_type" in kwargs and kwargs["streaming_type"] else 1 gnmi_cmd += " -query_type {} -streaming_type {} -q {} -v {} -target {}".\ format("s", stream_type, ",".join(xpath_list), version, target) elif query_type == "poll": poll_interval = kwargs["poll_interval"] if "poll_interval" in kwargs and kwargs["poll_interval"] else 1 gnmi_cmd += " -query_type {} -pi {} -q {} -v {} -target {}".\ format("p", poll_interval, ",".join(xpath_list), version, target) else: gnmi_cmd += " -query_type {} -q {} -v {} -target {}".format("o", ",".join(xpath_list), version, target) if gnmi_cmd: command = '{} -c "{}"'.format(docker_command, gnmi_cmd) output = st.config(dut, command) if "Error response" in util_obj.remove_last_line_from_string(output): st.log(output) return False return True
def create_chef_node(chef_conn_obj, node_name): """ :param chef_conn_obj: :param node_name: :return: """ if not chef_conn_obj: return False command = "knife node create {}".format(node_name) st.log("Remote Command: {}".format(command)) console_log = utils_obj.remove_last_line_from_string( con_obj.execute_command(chef_conn_obj, command)) st.log(console_log) if console_log.find("ERROR") > 1: st.log(console_log) return False return True
def get_debug_stp_log(dut, filter_list=[]): """" :param dut: :param filter_list: :return: """ if isinstance(filter_list, list): filter_list = list(filter_list) else: filter_list = [filter_list] command = "cat {}".format(debug_log_path) for each_filter in filter_list: command += " | grep '{}'".format(each_filter) output = st.show(dut, command, skip_tmpl=True, skip_error_check=True) reg_output = utils.remove_last_line_from_string(output) out_list = reg_output.split('\n') return out_list
def update_node_run_list(chef_conn_obj, client_ip, recipe_role): """ :param chef_conn_obj: :param client_ip: :param recipe_role: :return: """ if not chef_conn_obj: return False client_ip = client_ip.replace(".", "-") command = "sudo knife node run_list add {} '{}'".format( client_ip, recipe_role) st.log("Remote Command: {}".format(command)) console_log = utils_obj.remove_last_line_from_string( con_obj.execute_command(chef_conn_obj, command)) st.log(console_log) if console_log.find("ERROR") > 1: return False return True
def operations_on_runlist(chef_conn_obj, run_list, action="remove"): """ :param chef_conn_obj: :param run_list: :param action: :return: """ if not chef_conn_obj: return False command = "sudo knife node run_list {} {}".format(action, run_list) st.log("Remote Command: {}".format(command)) out = con_obj.execute_command(chef_conn_obj, command) if not out: return False console_log = utils_obj.remove_last_line_from_string(out) st.log(console_log) if console_log.find("ERROR") > 1: return False return True
def show_node_run_list(chef_conn_obj, node_name="sonic"): """ :param chef_conn_obj: :param node_name: :return: """ if not chef_conn_obj: return False command = "knife node show {} run_list".format(node_name) st.log("Remote Command: {}".format(command)) out = con_obj.execute_command(chef_conn_obj, command) if not out: return False console_log = utils_obj.remove_last_line_from_string(out) st.log(console_log) if console_log.find("ERROR") > 1: st.log(console_log) return False return True
def run_debug_commands(dut, mode=None, module="pddf"): """ API to execute debug commands Author: Chaitanya Vella ([email protected]) :param dut: :param mode: :param module: :return: """ if mode: modes = cutils.make_list(mode) else: modes = ["lsmode", "systemctl", "pddf_fanutil", "pddf_psuutil"] for each_mode in modes: if each_mode in ["lsmode", "systemctl"]: command = "{} | grep -i {}".format(each_mode, module) if each_mode in ["pddf_fanutil", "pddf_psuutil"]: command = "sudo {} debug dump_sysfs".format(each_mode) output = st.config(dut, command) return utils.remove_last_line_from_string(output)
def show_pddf_fanutil(dut, mode): """ API to get PDDF FANUTIL DATA based on type of mode Author: Chaitanya Vella ([email protected]) :param dut: :param mode: :return: """ if mode not in ["direction", "getspeed", "numfans", "status", "version"]: st.log("Unsupported modes provided ") return False skip_tmpl = False if mode == "numfans": skip_tmpl = True command = "sudo pddf_fanutil {}".format(mode) output = st.show(dut, command, skip_tmpl=skip_tmpl) if mode == "numfans": return {"numfans": utils.remove_last_line_from_string(output)} else: return output
def get_psuutil_data(dut, mode="status"): """ API to get psuutil data based on type of the command Author: Chaitanya Vella ([email protected]) :param dut: :param mode: :return: """ if mode not in ["status", "numpsus", "version"]: st.log("Unsupported command type") return False command = "sudo psuutil {}".format(mode) skip_tmpl = False if mode == "numpsus": skip_tmpl = True output = st.show(dut, command, skip_tmpl=skip_tmpl) if mode == "numpsus": return {"numpsus": utils.remove_last_line_from_string(output)} else: return output
def get_running_config(dut, table=None, object=None, attribute=None, max_retry=3): """ Get running config value based on table, object, attribute Author: Rakesh Kumar Vooturi ([email protected]) :param dut: :param table: :param object: :param attribute: :return: """ command = "sudo show runningconfiguration all" i = 1 while True: try: output = st.show(dut, command, skip_tmpl=True) reg_output = utils_obj.remove_last_line_from_string(output) data = eval(json.dumps(json.loads(reg_output))) break except Exception as e: st.error("Exception occured in try-{} - {}".format(i, e)) if i == max_retry: st.error("MAX retry {} reached..".format(i)) return None i += 1 try: if table is None and object is None and attribute is None: return data elif table is not None and object is None and attribute is None: return data[table] elif table is not None and object is not None and attribute is None: return data[table][object] elif table is not None and object is not None and attribute is not None: return data[table][object][attribute] except Exception as e: st.log(e) return None
def update_node_run_list(chef_conn_obj, node, recipe_role, action='add'): """ :param chef_conn_obj: :param client_ip: :param recipe_role: :return: """ if not chef_conn_obj: return False # client_ip = client_ip.replace(".", "-") command = "knife node run_list {} {} '{}'".format(action, node, recipe_role) st.log("Remote Command: {}".format(command)) out = con_obj.execute_command(chef_conn_obj, command) if not out: return False console_log = utils_obj.remove_last_line_from_string(out) st.log(console_log) if console_log.find("ERROR") > 1: return False return True
def delete_session(dut, mirror_session='', skip_err_check=False, cli_type=""): cli_type = st.get_ui_type(dut, cli_type=cli_type) ''' :param dut: :param mirror_session: :return: ''' if not mirror_session: st.error("Mirror session name not provided ...") return False if cli_type == "click": command = "config mirror_session remove {}".format(mirror_session) st.config(dut, command, skip_error_check=skip_err_check) return True if show_session(dut, mirror_session) else False elif cli_type == "klish": commands = list() command = "no mirror-session {}".format(mirror_session) commands.append(command) output = st.config(dut, commands, type=cli_type, skip_error_check=skip_err_check) st.config(dut, "exit", type=cli_type) output = remove_last_line_from_string(output) if output: if "Failed" not in output or "Error" not in output: return False return True elif cli_type in ['rest-put', "rest-patch"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['get_session_session_name'].format(mirror_session) if not delete_rest(dut, rest_url=url): return False return True else: st.log("Unsupported cli") return False
def gnmi_set(dut, xpath, json_content, **kwargs): """ API to set GNMI configuration Author: Chaitanya Vella ([email protected]) :param dut: :param xpath: :param json_content: :param kwargs: :return: """ gnmi_debug(dut) credentails = st.get_credentials(dut) ip_address = kwargs.get('ip_address', '127.0.0.1') port = kwargs.get('port', '8080') insecure = kwargs.get('insecure', '') username = kwargs.get('username', credentails[0]) password = kwargs.get('password', credentails[3]) cert = kwargs.get('cert') target_name = kwargs.get('target_name') pretty = kwargs.get('pretty') logstostderr = kwargs.get('logstostderr') mode = kwargs.get('mode', '-update') docker_command = get_docker_command() if not docker_command: st.log("Docker command not found ..") return False if json_content: temp_dir = tempfile.gettempdir() current_datetime = util_obj.get_current_datetime() file_name = "sonic_gnmi_{}.json".format(current_datetime) tmp_path = "{}/{}".format(temp_dir, file_name) docker_path = '/{}'.format(file_name) cp_cmd = 'docker cp {} telemetry:{}'.format(tmp_path, docker_path) rm_cmds = ['rm {}'.format(tmp_path), '{} -c "rm {}"'.format(docker_command, docker_path)] file_operation = util_obj.write_to_json_file(json_content, tmp_path) if not file_operation: st.log("File operation failed.") return False st.upload_file_to_dut(dut, tmp_path, tmp_path) st.config(dut, cp_cmd) gnmi_command = 'gnmi_set {} {}:@{} -target_addr {}:{}'.format(mode, xpath, docker_path, ip_address, port) if username: gnmi_command += " -username {}".format(username) if password: gnmi_command += " -password {}".format(password) if cert: gnmi_command += " -cert {}".format(cert) if target_name: gnmi_command += " -target_name {}".format(target_name) if pretty: gnmi_command += " -pretty" if logstostderr: gnmi_command += " -alsologstostderr" if insecure != 'none': gnmi_command += " -insecure {}".format(insecure) command = '{} -c "{}"'.format(docker_command, gnmi_command) output = st.config(dut, command) for rm_cmd in rm_cmds: st.config(dut, rm_cmd) error_strings = ["Error response", "rpc error", "Set failed", "Unknown desc", "failed"] for err_code in error_strings: if err_code in util_obj.remove_last_line_from_string(output): st.log(output) return False return output else: return False
def create_session(dut, **kwargs): cli_type = st.get_ui_type(dut, **kwargs) """ API to configure mirror session for both erspan, span and legacy configuration support Author: Chaitanya Vella ([email protected]) :param dut: :param kwargs: session_name : Name of the session (Mandatory) mirror_type: erspan or span. (Not mamdatory, as to support Mirror configuration in ARLO) destination_ifname: destination interface name in case of span source_ifname: source interface name in case of span rx_tx: rx/tx in case of span src_ip: source ip address in case of erspan dst_ip: destination ip address in case of erspan dscp: DSCP in case of erspan ttl: TTL in case of erspan queue: QUEUE in case of erspan gre_type: GRE_TYPE in case of erspan :return: """ kwargs["mirror_type"] = kwargs.get("mirror_type", "erspan") skip_err_check = kwargs.get("skip_err_check", False) if 'session_name' not in kwargs: st.error("Session name not provided ...") return False if kwargs.get("mirror_type") not in ["erspan", "span"]: st.log("Unsupported mirror type ..") return False if cli_type == "click": command = "config mirror_session add " if st.is_feature_supported("span-mirror-session", dut): command += " {}".format(kwargs["mirror_type"]) command += " {}".format(kwargs["session_name"]) if "mirror_type" in kwargs and kwargs["mirror_type"] == "span": if "destination_ifname" in kwargs: command += " {}".format(kwargs["destination_ifname"]) if "source_ifname" in kwargs: command += " {}".format(kwargs["source_ifname"]) if "rx_tx" in kwargs: command += " {}".format(kwargs["rx_tx"]) else: if 'src_ip' not in kwargs: st.error("Source IP not provided ...") return False command += " {}".format(kwargs["src_ip"]) if 'dst_ip' not in kwargs: st.error("Destination IP not provided ...") return False command += " {}".format(kwargs["dst_ip"]) if 'dscp' not in kwargs: st.error("dscp not provided ...") return False command += " {}".format(kwargs["dscp"]) if 'ttl' in kwargs: command += " {}".format(kwargs["ttl"]) if 'gre_type' not in kwargs: st.error("gre_type not provided ...") return False gre_type = kwargs["gre_type"] command += " {}".format(gre_type) if 'queue' in kwargs: command += " {}".format(kwargs["queue"]) if kwargs.get("src_port"): command += " {}".format(kwargs.get("src_port")) if kwargs.get("direction"): command += " {}".format(kwargs.get("direction")) output = st.config(dut, command, type=cli_type, skip_error_check=skip_err_check) output = remove_last_line_from_string(output) if skip_err_check: if "Failed" or "Error" in output: st.debug("Failed to create/delete mirror session") return False if ("mirror_type" in kwargs and kwargs["mirror_type"] != "span") or ("mirror_type" not in kwargs): session_data = verify_session(dut, session_name=kwargs["session_name"], src_ip=kwargs["src_ip"], dst_ip=kwargs["dst_ip"], dscp=kwargs["dscp"], ttl=kwargs["ttl"], gre_type=gre_type, queue=kwargs["queue"]) return False if not session_data else True return True elif cli_type == "klish": commands = list() cmd_mirror_session = "mirror-session {}".format(kwargs["session_name"]) commands.append(cmd_mirror_session) if kwargs.get("mirror_type") == "span": if not kwargs.get("destination_ifname"): st.log("Please provide destination interface") return False command = "destination {}".format(kwargs.get("destination_ifname")) if kwargs.get("source_ifname"): command = "{} source {}".format(command, kwargs.get("source_ifname")) if kwargs.get("rx_tx"): command = "{} direction {}".format(command, kwargs.get("rx_tx")) else: command = "destination erspan" gre_type = kwargs["gre_type"] if kwargs.get("src_ip"): command += " src-ip {}".format(kwargs.get("src_ip")) if kwargs.get("dst_ip"): command += " dst-ip {}".format(kwargs.get("dst_ip")) if kwargs.get("gre_type"): command += " gre {}".format(gre_type) if kwargs.get("dscp"): command += " dscp {}".format(kwargs.get("dscp")) if kwargs.get("ttl"): command += " ttl {}".format(kwargs.get("ttl")) if kwargs.get("queue"): command += " queue {}".format(kwargs.get("queue")) if kwargs.get("src_port"): command += " source {}".format(kwargs.get("src_port")) if kwargs.get("direction"): command += " direction {}".format(kwargs.get("direction")) commands.append(command) commands.append("exit") st.log("COMMAND : {}".format(commands)) output = st.config(dut, commands, type=cli_type, skip_error_check=skip_err_check) if skip_err_check: if "Error" in output: st.debug("Failed to create mirror session") return False if ("mirror_type" in kwargs and kwargs["mirror_type"] != "span") or ("mirror_type" not in kwargs): session_data = verify_session(dut, session_name=kwargs["session_name"], src_ip=kwargs["src_ip"], dst_ip=kwargs["dst_ip"], dscp=kwargs["dscp"], ttl=kwargs["ttl"], gre_type=gre_type, queue=kwargs["queue"]) if not session_data: return False if kwargs.get("no_form_session_name"): command = "no mirror-session {}".format(kwargs["session_name"]) output = st.config(dut, command, type=cli_type, skip_error_check=skip_err_check) output = remove_last_line_from_string(output) if skip_err_check: if "Error" in output: st.debug("Failed to delete mirror session") return False return True elif cli_type in ["rest-put", "rest-patch"]: rest_urls = st.get_datastore(dut, "rest_urls") mirror_type = kwargs.get("mirror_type") url = rest_urls['config_mirror_session'].format(kwargs["session_name"]) data = {} if mirror_type == "span": if kwargs.get("source_ifname"): data["src-port"] = str(kwargs.get("source_ifname")) if kwargs.get("destination_ifname"): data["dst-port"] = str(kwargs.get("destination_ifname")) if kwargs.get("rx_tx"): data["direction"] = str(kwargs.get("rx_tx").upper()) else: if kwargs.get("src_ip"): data["src-ip"] = str(kwargs.get("src_ip")) if kwargs.get("dst_ip"): data["dst-ip"] = str(kwargs.get("dst_ip")) if kwargs.get("dscp"): data["dscp"] = int(kwargs.get("dscp")) if kwargs.get("gre_type"): data["gre-type"] = str(kwargs.get("gre_type")) if kwargs.get("ttl"): data["ttl"] = int(kwargs.get("ttl")) if 'queue' in kwargs: data["queue"] = int(kwargs.get("queue")) if kwargs.get("src_port"): data["src-port"] = str(kwargs.get("src_port")) if kwargs.get("direction"): data["direction"] = str(kwargs.get("direction").upper()) config_data = {"openconfig-mirror-ext:config": data} if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=config_data): return False else: st.log("Unsupported cli") return False return True