def config_nat_feature(dut, oper='enable', cli_type="", skip_error_check=True): """ Config NAT Feature Author:[email protected] :param dut: :param oper: enable/disable :param cli_type: :param skip_error_check: :return: """ instance = 0 cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == "click": command = "config nat feature {}".format(oper) elif cli_type == "klish": command = list() operation = "enable" if oper == "enable" else "no enable" command.append("nat") command.append("{}".format(operation)) command.append("exit") elif cli_type in ["rest-patch", "rest-put"]: operation = True if oper == "enable" else False url = st.get_datastore(dut, "rest_urls")['config_nat_feature'].format(instance) data = {"openconfig-nat:config": {"enable": operation}} config_rest(dut, http_method=cli_type, rest_url=url, json_data=data) return True else: st.log("UNSUPPORTED CLI TYPE") return False st.config(dut, command, type=cli_type, skip_error_check=skip_error_check) return True
def config_nat_interface(dut, **kwargs): """ Config NAT interface Author:[email protected] :param :dut: :param :config: add-del: :param :interface_name: :param :zone_value: usage: config_nat_interface(dut1, interface_name, zone_value, config="add") config_nat_interface(dut1, interface_name, config="del") """ result = False command = '' cli_type = st.get_ui_type(dut, **kwargs) skip_error_check = kwargs.get("skip_error_check", True) if cli_type not in ["klish", "click", "rest-patch", "rest-put"]: st.log("UNSUPPORTE CLI TYPE") return False if "interface_name" not in kwargs and "config" not in kwargs: st.error("Mandatory params interface_name or config not provided") return result if kwargs["config"] == "add": if "zone_value" not in kwargs: st.error("Mandatory params zone_vlaue not provided") return result if cli_type == "click": command = "config nat add interface {} -nat_zone {}".format(kwargs["interface_name"], kwargs["zone_value"]) elif cli_type == "klish": command = list() intf_data = get_interface_number_from_name(kwargs["interface_name"]) command.append("interface {} {}".format(intf_data["type"], intf_data["number"])) command.append("nat-zone {}".format(kwargs["zone_value"])) command.append("exit") elif cli_type in ["rest-patch", "rest-put"]: url = st.get_datastore(dut, "rest_urls")['config_nat_interface'].format(kwargs["interface_name"]) data = {"openconfig-interfaces-ext:nat-zone": int(kwargs["zone_value"])} config_rest(dut, http_method=cli_type, rest_url=url, json_data=data) return True if kwargs["config"] == "del": if cli_type == "click": command = "config nat remove interface {}".format(kwargs["interface_name"]) elif cli_type == "klish": command = list() intf_data = get_interface_number_from_name(kwargs["interface_name"]) command.append("interface {} {}".format(intf_data["type"], intf_data["number"])) command.append("no nat-zone") command.append("exit") elif cli_type in ["rest-patch", "rest-put"]: url = st.get_datastore(dut, "rest_urls")['del_nat_interface'].format(kwargs["interface_name"]) delete_rest(dut, rest_url=url) return True st.config(dut, command, type=cli_type, skip_error_check=skip_error_check) return True
def set_aaa_authorization_properties(dut, property, value, cli_type=""): ''' Configuring aaa authorization properties. ''' cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == "click": command = "config aaa authorization {} {}".format(property, value) st.config(dut, command, type=cli_type) elif cli_type == "klish": value = "local" if value == "default" else value if value == "local": command = "aaa authorization {} default {}".format(property, value) else: command = "aaa authorization {} default group {}".format( property, value) st.config(dut, command, type=cli_type) elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['aaa_authorization_method'] if property == "login" and value == "ldap": data = json.loads(""" { "openconfig-system:authorization-method": [ "ldap" ] } """) if not config_rest(dut, http_method=cli_type, rest_url=url, json_data=data, timeout=time_out): return False if property == "login" and value == "local": data = json.loads(""" { "openconfig-system:authorization-method": [ "local" ] } """) if not config_rest(dut, http_method=cli_type, rest_url=url, json_data=data, timeout=time_out): return False else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False return True
def test_ft_radius_login_rest(): """ Verify that aaa authentication can be configured to radius and login authentication is successful. """ data1 = json.loads(""" { "openconfig-system-ext:auth-type": "pap" } """) data2 = json.loads(""" { "openconfig-system:config": { "auth-port": 1812, "secret-key": "Lvl7india" } } """) data3 = json.loads(""" { "openconfig-system:authentication-method": [ "radius", "local" ] } """) rest_url1 = "/restconf/data/openconfig-system:system/aaa/server-groups/server-group={}/servers/server={}/config/openconfig-system-ext:auth-type".format( rbac.feature, rbac.host_ip) rest_url2 = "/restconf/data/openconfig-system:system/aaa/server-groups/server-group={}/servers/server={}/radius/config".format( rbac.feature, rbac.host_ip) rest_url3 = "/restconf/data/openconfig-system:system/aaa/authentication/config/authentication-method" st.log("Configuring Radius server configuration with REST") if not config_rest( vars.D1, http_method='put', rest_url=rest_url1, json_data=data1): st.report_fail("rest_call_failed", "PUT") if not config_rest( vars.D1, http_method='patch', rest_url=rest_url2, json_data=data2): st.report_fail("rest_call_failed", "PATCH") st.log("Setting login authentication to radius and local") if not config_rest( vars.D1, http_method='put', rest_url=rest_url3, json_data=data3): st.report_fail("rest_call_failed", "PUT") st.log("SSH to device using radius credentials with auth_type pap") if not ssh.connect_to_device(rbac.ip_address, rbac.host_username, rbac.host_password): st.report_fail("ssh_login_failed", rbac.global_auth_type) st.report_pass("ssh_login_with_radius_successful", rbac.global_auth_type)
def config_mac(dut, mac, vlan, intf, cli_type=""): """ :param dut: :type dut: :return: :rtype: """ #st.log("config mac add <mac> <vlan> <intf>") cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == 'click': command = "config mac add {} {} {}".format(mac, vlan, intf) if not st.is_feature_supported("config-mac-add-command", dut): st.community_unsupported(command, dut) _json_mac_add(dut, mac, vlan, intf) else: st.config(dut, command, type='click') elif cli_type == 'klish': interface = get_interface_number_from_name(intf) command = "mac address-table {} vlan {} {} {}".format( mac, vlan, interface["type"], interface["number"]) st.config(dut, command, type=cli_type) elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['static_mac_config'] json = { "openconfig-network-instance:network-instances": { "network-instance": [{ "name": "default", "fdb": { "mac-table": { "entries": { "entry": [{ "mac-address": mac, "vlan": int(vlan), "config": { "mac-address": mac, "vlan": int(vlan) }, "interface": { "interface-ref": { "config": { "interface": intf, "subinterface": 0 } } } }] } } } }] } } if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=json): return False else: st.log("Unsupported cli") return False return True
def config_mac_agetime(dut, agetime, cli_type="", config= "add", **kwargs): """ This proc is to config mac aging and setting it back to default. :param dut: DUT Number :param agetime: fdb age time in seconds :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) skip_error_check = kwargs.get('skip_error', False) command = '' if cli_type == 'click': command = "config mac aging_time {}".format(int(agetime)) if not st.is_feature_supported("config-mac-aging_time-command", dut): st.community_unsupported(command, dut) skip_error_check=True elif cli_type == 'klish': if config == 'add': command = "mac address-table aging-time {}".format(int(agetime)) else: command = "no mac address-table aging-time" elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['mac_aging'].format(name='default') config_data = {"openconfig-network-instance:mac-aging-time": int(agetime)} if not config_rest(dut, rest_url=url, http_method=cli_type, json_data=config_data): st.error("Failed to configure aging as {}".format(agetime)) return False if command: st.config(dut, command, skip_error_check=skip_error_check, type=cli_type) return True
def config_invalid_shaper(dut, policy_name, **kwargs): """ API to configure invalid shaper :param dut: :type dut: :param policy_name: :type policy_name: """ cli_type = st.get_ui_type(dut, **kwargs) cli_type = 'klish' if cli_type == 'click' else cli_type skip_error = kwargs.get('skip_error', False) if cli_type == 'klish': command = "qos scheduler-policy {}".format(policy_name) response = st.config(dut, command, type=cli_type, skip_error_check=skip_error) if any(error in response.lower() for error in errors_list): st.error("The response is: {}".format(response)) return False elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['policy_config'] config_data = {"openconfig-qos:scheduler-policy": [{"name": policy_name, "config": {"name": policy_name}}]} if not config_rest(dut, rest_url=url, http_method=cli_type, json_data=config_data): st.error("Failed to configure Policy: {}".format(policy_name)) return False else: st.error("Unsupported CLI Type: {}".format(cli_type)) return False return True
def config_intf_udld_mode(dut, **kwargs): """ config_intf_udld_mode(dut=data.dut1,intf ='Ethernet10',udld_mode='yes',config='yes') config_intf_udld_mode(dut=data.dut1,intf ='Ethernet10',udld_mode='yes') config_intf_udld_mode(dut=data.dut1,intf ='Ethernet10',udld_mode='',config='no') udld.config_intf_udld_mode(dut=dut2,intf ='Ethernet37',udld_mode='yes',config='yes',cli_type='rest-put') Author: [email protected] Enable UDLD mode Aggressive at interface level :param dut: :param intf: :param udld_mode: :return: """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut)) if 'config' in kwargs: config = kwargs['config'] else: config = 'yes' if config.lower() == 'yes': config_cmd = '' else: config_cmd = 'no' if 'intf' in kwargs: if type(kwargs['intf']) is list: kwargs['intf'] = list(kwargs['intf']) else: kwargs['intf'] = [kwargs['intf']] my_cmd = '' if cli_type == 'klish' or cli_type == 'click': for intf in kwargs['intf']: intf_details = get_interface_number_from_name(intf) my_cmd += 'interface {} {}\n'.format(intf_details['type'], intf_details['number']) if 'udld_mode' in kwargs: my_cmd += '{} udld aggressive\n'.format(config_cmd) my_cmd += 'exit\n' st.config(dut, my_cmd, type='klish') elif cli_type in ['rest-patch', 'rest-put']: http_method = kwargs.pop('http_method', cli_type) rest_urls = st.get_datastore(dut, 'rest_urls') for intf in kwargs['intf']: rest_url = rest_urls['udld_interface_aggressive_config'].format( intf) if config_cmd == '': ocdata = {"openconfig-udld-ext:aggressive": bool(1)} else: ocdata = {"openconfig-udld-ext:aggressive": bool(0)} response = config_rest(dut, http_method=http_method, rest_url=rest_url, json_data=ocdata) if not response: st.log('UDLD mode for interface config/unconfig failed') st.log(response) return False return True else: st.log("Unsupported CLI TYPE - {}".format(cli_type)) return False
def add_static_arp(dut, ipaddress, macaddress, interface="", cli_type=""): """ To add static arp Author: Prudvi Mangadu ([email protected]) :param dut: :param ipaddress: :param macaddress: :param interface: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) command = '' if cli_type == "click": command = "arp -s {} {}".format(ipaddress, macaddress) if interface: command += " -i {}".format(interface) elif cli_type == "klish": if interface: intf = get_interface_number_from_name(interface) command = "interface {} {}".format(intf['type'], intf['number']) command = command + "\n" + "ip arp {} {}".format( ipaddress, macaddress) + "\n" + "exit" else: st.error( "'interface' option is mandatory for adding static arp entry in KLISH" ) return False elif cli_type in ['rest-patch', 'rest-put']: if not interface: st.error( "'interface' option is mandatory for adding static arp entry in REST" ) return False port_index = get_subinterface_index(dut, interface) rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['config_static_arp'].format(name=interface, index=port_index) config_data = { "openconfig-if-ip:neighbor": [{ "ip": ipaddress, "config": { "ip": ipaddress, "link-layer-address": macaddress } }] } if not config_rest( dut, rest_url=url, http_method=cli_type, json_data=config_data): st.error( "Failed to configure static ARP with IP: {}, MAC: {}, INTF: {}" .format(ipaddress, macaddress, interface)) return False else: st.error("Unsupported CLI_TYPE: {}".format(cli_type)) return False if command: st.config(dut, command, type=cli_type) return True
def config_mclag_system_mac(dut, skip_error=False, **kwargs): ''' Configures< mclag system-mac <add|del> <domain-id> mac > for Mclag Author: [email protected] :param dut: :param kwargs: optional parameters can be <config|domain_id|mac|cli_type> :return: Usage: config_mclag_system_mac(dut1, domain_id= 1, config='add', mac='xx:xx:xx:xx:xx:xx') config_mclag_system_mac(dut1, domain_id= 1, config='del') ''' ### Optional parameters processing config = kwargs.get('config', 'add') if config == 'add' and 'mac' not in kwargs: st.error("Mandatory parameter mac address not found for config = add") return False if 'domain_id' not in kwargs: st.error("Mandatory parameter domain_id not found") return False mac_val = kwargs.get('mac') domain_id = kwargs.get('domain_id') cli_type = kwargs.get('cli_type', st.get_ui_type(dut, **kwargs)) mac_val = '' if config == 'del' else mac_val if cli_type == 'click': cmd = "config mclag system-mac {} {} {}\n".format( config, domain_id, mac_val) output = st.config(dut, cmd) if "configure mclag domain first" in output: st.error("Domain_id doesn't exist") return False elif cli_type == 'klish': config = 'no' if config == 'del' else '' cmd = "mclag domain {} \n{} mclag-system-mac {}".format( domain_id, config, mac_val) cmd = cmd + "\n" + "exit" output = st.config(dut, cmd, type="klish", conf=True) if "Could not connect to Management REST Server" in output: st.error("klish mode not working.") return False elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['mclag_config_mclag_system_mac'].format(int(domain_id)) if config == "add": payload = {"openconfig-mclag:mclag-system-mac": mac_val} if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=payload): return False else: if not delete_rest(dut, rest_url=url): return False else: st.log("Invalid cli_type provided: {}".format(cli_type)) return False return True
def set_tacacs_properties(dut, property, value, cli_type="", **kwargs): ''' Configuring tacacs properties. ''' cli_type = st.get_ui_type(dut, cli_type=cli_type) property_mapping = { "authtype": "auth-type", "passkey": "key", "timeout": "timeout", "sourceip": "host", "source-interface": "source-interface" } if cli_type == "click": command = "config tacacs {} {}".format(property, value) st.config(dut, command, type=cli_type) elif cli_type == "klish": if property != "default": property = property_mapping[property] command = "tacacs-server {} {}".format(property, value) else: property = property_mapping[value] command = "no tacacs-server {}".format(property) st.config(dut, command, type=cli_type) elif cli_type in ['rest-put', 'rest-patch']: rest_urls = st.get_datastore(dut, "rest_urls") property_mapping = { "authtype": "openconfig-system-ext:auth-type", "passkey": "openconfig-system-ext:secret-key", "timeout": "openconfig-system-ext:timeout" } url_mapping = { "authtype": "tacacs_global_authtype_config", "passkey": "tacacs_global_passkey_config", "timeout": "tacacs_global_timeout_config" } if property != 'default': url = rest_urls[url_mapping[property]].format("TACACS") data = {property_mapping[property]: value} if not config_rest(dut, http_method=cli_type, rest_url=url, json_data=data, timeout=time_out, **kwargs): st.error("Failed to configure tacacs global params") return False else: url = rest_urls[url_mapping[value]].format("TACACS") if not delete_rest(dut, rest_url=url, timeout=time_out, **kwargs): st.error("Failed to delete tacacs global params") return False else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False return True
def config(dut, **kwargs): cli_type = st.get_ui_type(dut, **kwargs) """ API to create Mgmt VRF. Author : Kiran Vedula ([email protected]) :param :dut: :param :mgmtvrf: :param :cli_type: default - klish :param :no_form: default - False :return: Usage: config(vars.D1, cli_type='klish') config(vars.D1, no_form=True,cli_type='klish') """ no_form = "no" if kwargs.get("no_form") else "" command_list = [] if cli_type == 'klish': if not no_form: command_list.append("ip vrf mgmt") st.config(dut, command_list, type=cli_type, expect_ipchange=True) else: command_list.append("no ip vrf mgmt") st.config(dut, command_list, type=cli_type, expect_ipchange=True) elif cli_type == "click": st.log('Config Mgmt VRF API') if no_form != 'no': my_cmd = 'sudo config vrf add mgmt' else: my_cmd = 'sudo config vrf del mgmt' st.config(dut, my_cmd, type=cli_type) elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") if no_form != 'no': url = rest_urls['config_mgmt_vrf'].format("mgmt") config_data = { "openconfig-network-instance:config": { "enabled": True, "name": "mgmt" } } if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=config_data): return False else: url = rest_urls['unconfig_mgmt_vrf'].format("mgmt") if not delete_rest(dut, rest_url=url): return False else: st.error("Unsupported cli_type: {}".format(cli_type)) return False return True
def clear_stats(dut, **kwargs): """ Click: 1. sonic-clear ip helper_address statistics 2. sonic-clear ip helper_address statistics <interface_name> Klish: 1. clear ip helper-address statistics 2. clear ip helper-address statistics <interface_name> """ cli_type = kwargs.get("cli_type", st.get_ui_type(dut, **kwargs)) skip_error_check = kwargs.get("skip_error_check", True) command = '' if cli_type == 'click': command = "sonic-clear ip helper_address statistics" if 'intf_name' in kwargs: command += " {intf_name}".format(**kwargs) if not st.config( dut, command, type=cli_type, skip_error_check=skip_error_check): return False elif cli_type == "klish": command = list() if 'intf_name' in kwargs: command.append( "clear ip helper-address statistics {intf_name}".format( **kwargs)) else: command.append("clear ip helper-address statistics") if not st.config( dut, command, type=cli_type, skip_error_check=skip_error_check): return False elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['clear_stats_ip_helper'] if 'intf_name' in kwargs: data = { "openconfig-ip-helper:input": { "interface": str(kwargs['intf_name']) } } else: data = {"openconfig-ip-helper:input": {}} if not config_rest( dut, http_method="post", rest_url=url, json_data=data): return False else: st.error("Unsupported CLI Type {}".format(cli_type)) return False return True
def config_port_qos_map(dut, obj_name, interface, **kwargs): cli_type = st.get_ui_type(dut, **kwargs) if cli_type == 'click': final_data = dict() temp_data = dict() if not obj_name or not interface: st.log( "Please provide obj_name like 'AZURE' and interface like 'Ethernet0,Ethernet1'" ) return False else: cos_specific_dict = { "tc_to_queue_map": "[TC_TO_QUEUE_MAP|" + obj_name + "]", "dscp_to_tc_map": "[DSCP_TO_TC_MAP|" + obj_name + "]" } temp_data[interface] = cos_specific_dict final_data['PORT_QOS_MAP'] = temp_data final_data = json.dumps(final_data) st.apply_json(dut, final_data) elif cli_type == 'klish': commands = list() intf_data = get_interface_number_from_name(interface) commands.append('interface {} {}'.format(intf_data['type'], intf_data['number'])) commands.append('qos-map tc-queue {}'.format(obj_name)) commands.append('qos-map dscp-tc {}'.format(obj_name)) commands.append('exit') response = st.config(dut, commands, type=cli_type) if any(error in response.lower() for error in errors_list): st.error("The response is: {}".format(response)) return False elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['port_qos_map_config'].format(interface) port_qos_map_data = { "openconfig-qos-maps-ext:interface-maps": { "config": { "dscp-to-forwarding-group": obj_name, "forwarding-group-to-queue": obj_name } } } if not config_rest(dut, rest_url=url, http_method=cli_type, json_data=port_qos_map_data): st.error("Failed configure PORT_QOS_MAP") return False else: st.error("Unsupported CLI Type: {}".format(cli_type)) return False return True
def config_udld_multiplier(dut, **kwargs): """ Author: [email protected] config_udld_multiplier(dut=data.dut1,udld_multiplier='4',config='yes') config_udld_multipier(dut=data.dut1,udld_multiplier='4') config_udld_multipier(dut=data.dut1,udld_multiplier='4',config='no') udld.config_udld_multiplier(dut=dut1,udld_multiplier='3',config='yes',cli_type='rest-put') Configure udld multipllier globally :param dut: :param udld_multipier: :return: """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut)) if 'config' in kwargs: config = kwargs['config'] else: config = 'yes' if config.lower() == 'yes': config_cmd = '' else: config_cmd = 'no' udld_multiplier = kwargs.get('udld_multiplier', None) if cli_type == 'klish' or cli_type == 'click': if 'udld_multiplier' in kwargs: my_cmd = '{} udld multiplier {} \n'.format( config_cmd, kwargs['udld_multiplier']) st.config(dut, my_cmd, type='klish') elif cli_type in ['rest-patch', 'rest-put']: http_method = kwargs.pop('http_method', cli_type) rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['udld_multiplier_config'] ocdata = { "openconfig-udld-ext:multiplier": int(kwargs['udld_multiplier']) } if config_cmd == '' and udld_multiplier != None: response = config_rest(dut, http_method=http_method, rest_url=rest_url, json_data=ocdata) else: response = delete_rest(dut, http_method='delete', rest_url=rest_url, json_data=ocdata) if not response: st.log('UDLD global message time config/unconfig failed') st.log(response) return False return True else: st.log("Unsupported CLI TYPE - {}".format(cli_type)) return False
def clear_nat(dut, **kwargs): """ Clear NAT Author:[email protected] :param dut: :param :translations: Ture/False :param :statistics: True/False usage: clear_nat(dut1, translations=True) clear_nat(dut1, statistics=True) """ command = '' cli_type = st.get_ui_type(dut, **kwargs) if cli_type == "click": if "translations" in kwargs: command = "sonic-clear nat translations" if "statistics" in kwargs: command = "sonic-clear nat statistics" elif cli_type == "klish": if "translations" in kwargs: command = "clear nat translations" if "statistics" in kwargs: command = "clear nat statistics" elif cli_type in ["rest-patch", "rest-put"]: url = st.get_datastore(dut, "rest_urls")['clear_nat'] data = {} if "translations" in kwargs: data = {"sonic-nat:input": {"nat-param": "ENTRIES"}} if "statistics" in kwargs: data = {"sonic-nat:input": {"nat-param": "STATISTICS"}} config_rest(dut, http_method='post', rest_url=url, json_data=data) return True else: st.log("UNSUPPORTE CLI TYPE") return False st.config(dut, command, type=cli_type) return True
def clear_interface_counters(dut, **kwargs): cli_type = st.get_ui_type(dut, **kwargs) interface_name = kwargs.get("interface_name", "") interface_type = kwargs.get("interface_type", "all") if cli_type == "klish": confirm = kwargs.get("confirm") if kwargs.get("confirm") else "y" if interface_type != "all": interface_type = get_interface_number_from_name( str(interface_name)) if interface_type["type"] and interface_type["number"]: interface_val = "{} {}".format(interface_type.get("type"), interface_type.get("number")) else: interface_val = "" else: interface_val = "all" if not interface_val: st.log("Invalid interface type") return False command = "clear counters interface {}".format(interface_val) st.config(dut, command, type=cli_type, confirm=confirm, conf=False, skip_error_check=True) elif cli_type == "click": command = "show interfaces counters -c" if not st.is_feature_supported( "show-interfaces-counters-clear-command", dut): st.community_unsupported(command, dut) return st.config(dut, "sonic-clear counters") return st.show(dut, command) elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['clear_interface_counters'] clear_type = 'all' if interface_type == 'all' else interface_name clear_counters = { "sonic-interface:input": { "interface-param": clear_type } } if not config_rest( dut, http_method='post', rest_url=url, json_data=clear_counters): st.error("Failed to clear interface counters") return False else: st.log("Unsupported CLI TYPE {}".format(cli_type)) return False return True
def add_ntp_servers(dut, iplist=[], cli_type=''): """ :param dut: :param iplist: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) st.log("add ntp servers") final_data = {} temp_data = {} if iplist: for ip in iplist: temp_data[ip] = {} else: st.log("please provide atleast 1 server to configure") return False if cli_type == "click": final_data['NTP_SERVER'] = temp_data final_data = json.dumps(final_data) st.apply_json(dut, final_data) elif cli_type == "klish": for ip in iplist: commands = "ntp server {}".format(ip) st.config(dut, commands, type=cli_type) elif cli_type in ['rest-patch', 'rest-put']: for ip in iplist: data={ "openconfig-system:servers": { "server": [ { "address": str(ip), "config": { "address": str(ip) } } ] } } rest_urls = st.get_datastore(dut, "rest_urls") url1 = rest_urls['config_ntp_server'].format(ip) if not config_rest(dut, http_method=cli_type, rest_url=url1, json_data=data): st.error("Failed to configure ntp {} server".format(ip)) return False else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False st.log("Regenerate the ntp-config") command = "systemctl restart ntp-config" st.config(dut, command) return True
def config_udld_global(dut, **kwargs): """ Author: [email protected] config_udld_global(dut=data.dut1,udld_enable='yes',config='yes') config_udld_global(dut=data.dut1,udld_enable='yes') config_udld_global(dut=data.dut1,udld_enable='',config='no') udld.config_udld_global(dut=dut1,udld_enable='yes',config='yes',cli_type='rest-put') Configure udld global :param dut: :param udld_enable: :return: """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut)) st.log("Starting UDLD Module Configurations1...") if 'config' in kwargs: config = kwargs['config'] else: config = 'yes' if config.lower() == 'yes': config_cmd = '' else: config_cmd = 'no' udld_enable = kwargs.get('udld_enable', None) if cli_type == 'klish' or cli_type == 'click': if 'udld_enable' in kwargs: my_cmd = '{} udld enable \n'.format(config_cmd) st.config(dut, my_cmd, type='klish') elif cli_type in ['rest-patch', 'rest-put']: http_method = kwargs.pop('http_method', cli_type) rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['udld_admin_config'] if config_cmd == '' and udld_enable != None: ocdata = {"openconfig-udld-ext:admin-enable": bool(1)} else: ocdata = {"openconfig-udld-ext:admin-enable": bool(0)} response = config_rest(dut, http_method=http_method, rest_url=rest_url, json_data=ocdata) if not response: st.log('UDLD global config/unconfig failed') st.log(response) return False return True else: st.log("Unsupported CLI TYPE - {}".format(cli_type)) return False
def create_vlan(dut, vlan_list, cli_type=''): cli_type = st.get_ui_type(dut, cli_type=cli_type) """ To create list of VLANs. Author : Prudvi Mangadu ([email protected]) :param dut: :param vlan_list: :param cli_type: :return: """ st.log("Creating vlan {}".format(vlan_list)) vlan_li = map(str, vlan_list) if isinstance(vlan_list, list) else [vlan_list] commands = list() for each_vlan in vlan_li: if cli_type == "click": commands.append("config vlan add {}".format(each_vlan)) elif cli_type == "klish": commands.append("interface Vlan {}".format(each_vlan)) commands.append('exit') elif cli_type in ["rest-put", "rest-patch"]: vlan_data = dict() vlan_data["openconfig-interfaces:interface"] = list() vlan_data["openconfig-interfaces:interface"].append({ "name": "Vlan{}".format(each_vlan), "config": { "name": "Vlan{}".format(each_vlan) } }) url = st.get_datastore(dut, "rest_urls")["config_interface"] if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=vlan_data): return False else: st.log("Unsupported CLI TYPE {}".format(cli_type)) return False if commands: st.config(dut, commands, type=cli_type) return True
def enable_ssh_in_user_vrf(dut, **kwargs): """ To enable SSH-in over user defined VRF :param dut: :param kwargs: :return: Usage:enable_ssh_in_user_vrf(vars.D1, config='add',vrf_name='mgmt') """ cli_type = st.get_ui_type(dut, **kwargs) st.log(" Configure SSH-in for user VRF") if not kwargs.get('vrf_name'): st.error("Mandatory arguments are missing.") return False if kwargs.get('config') not in ['add', 'del']: st.error("Incorrect config type") return False if cli_type == "click": command = "config ssh-server vrf {} {}".format(kwargs.get('config'), kwargs.get('vrf_name')) st.config(dut, command, type=cli_type, skip_error_check=True) elif cli_type == "klish": config = "no " if kwargs.get('config') == "del" else "" command = "{}ssh-server vrf {}".format(config, kwargs.get('vrf_name')) st.config(dut, command, type=cli_type, skip_error_check=True) elif cli_type in ["rest-patch","rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['config_ssh_server'].format(kwargs.get('vrf_name')) ssh_config={"openconfig-system-ext:ssh-server-vrf":[{"vrf-name": kwargs.get('vrf_name'),"config":{ "vrf-name": kwargs.get('vrf_name'),"port": 22}}]} if kwargs.get('config') == 'add': if not config_rest(dut, http_method=cli_type, rest_url=url, json_data=ssh_config): st.error("ssh server configuration failed") return False else: if not delete_rest(dut, rest_url=url): st.error("ssh server deletion failed") return False else: st.error("Unsupported cli_type: {}".format(cli_type)) return False return True
def set_ndp_ageout_time(dut, timeout, cli_type=""): """ To set ndp aging time Author: Prudvi Mangadu ([email protected]) :param dut: :param timeout: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) command = '' if cli_type == "click": command = "sudo bash -c 'echo {} >/proc/sys/net/ipv6/neigh/default/gc_stale_time'".format( timeout) elif cli_type == "klish": command = "ipv6 nd cache expire {}".format(timeout) elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['ageout_set'] config_data = { "openconfig-neighbor:neighbor-globals": { "neighbor-global": [{ "name": "Values", "config": { "ipv6-nd-cache-expiry": int(timeout) } }] } } if not config_rest( dut, rest_url=url, http_method=cli_type, json_data=config_data): st.error("Failed to configure NDP timeout as: {}".format(timeout)) return False else: st.error("Unsupported CLI_TYPE: {}".format(cli_type)) return False if command: st.config(dut, command, type=cli_type) return True
def ensure_ntp_config(dut,iplist=[], cli_type=''): cli_type = st.get_ui_type(dut, cli_type=cli_type) if not iplist: iplist = ensure_service_params(dut, "ntp", "default") if not iplist: st.log("NTP server IPs missing") return False commands = [] for ip in iplist: if not verify_ntp_server_exists(dut, ip, remote=ip): if cli_type == "click": commands.append("config ntp add {}".format(ip)) elif cli_type == "klish": commands.append("ntp server {}".format(ip)) elif cli_type in ['rest-patch', 'rest-put']: data = { "openconfig-system:servers": { "server": [ { "address": str(ip), "config": { "address": str(ip) } } ] } } rest_urls = st.get_datastore(dut, "rest_urls") url1 = rest_urls['config_ntp_server'].format(ip) if not config_rest(dut, http_method=cli_type, rest_url=url1, json_data=data): st.error("Failed to configure ntp {} server".format(ip)) return False else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False st.config(dut, commands, type=cli_type) return True
def set_crm_polling_interval(dut, polling_interval, cli_type=""): """ Set CRM polling interval in seconds. Author : Prudvi Mangadu ([email protected]) :param dut: :param polling_interval: interface in seconds :param cli_type: click or klish designation: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) command = "" if cli_type == "click": command = "crm config polling interval {}".format(polling_interval) elif cli_type == "klish": command = "crm polling interval {}".format(polling_interval) elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['crm_polling_interval'] config_json = { "openconfig-system-crm:config": { "polling-interval": int(polling_interval) } } if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=config_json): return False else: st.error("Unsupported cli type: {}".format(cli_type)) return False if command: rv = st.config(dut, command, type=cli_type, skip_error_check=True) if 'Error' in rv: st.error("{}".format(rv)) return False return True
def config_udld_recover_timer(dut, **kwargs): """ Author: [email protected] config_udld_recover_timer(dut=data.dut1,udld_recover_timer='30') config_udld_recover_timer(dut=data.dut1,udld_recover_timer='300') udld.config_udld_recover_timer(dut=dut1,udld_recover_timer='30',cli_type = 'klish') Configure udld recover timer :param dut: :param udld_recover_timer: 300 default in sec :return: """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut)) udld_recover_timer = kwargs.get('udld_recover_timer', None) config = kwargs.get('config', '') st.log("Starting UDLD recover timer Configurations1...") my_cmd = '' if cli_type == 'click': if 'udld_recover_timer' in kwargs: my_cmd = 'config errdisable recovery interval {}'.format( kwargs['udld_recover_timer']) else: st.error("Mandatory argument udld recover timer should be given") return False st.config(dut, my_cmd, type=cli_type) elif cli_type == 'klish': if udld_recover_timer != None: if config == '': my_cmd = 'errdisable recovery interval {}'.format( kwargs['udld_recover_timer']) else: my_cmd = 'no errdisable recovery interval' else: st.error("Mandatory argument udld recover timer should be given") return False st.config(dut, my_cmd, type=cli_type) elif cli_type in ['rest-patch', 'rest-put']: http_method = kwargs.pop('http_method', cli_type) rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['errdisable_recover_interval_config'] if udld_recover_timer != None: ocdata = { "openconfig-errdisable-ext:interval": int(udld_recover_timer) } if config == '': response = config_rest(dut, http_method=http_method, rest_url=rest_url, json_data=ocdata) else: response = delete_rest(dut, http_method='delete', rest_url=rest_url, json_data=ocdata) else: st.error( "Mandatory arguments udld recover interval should be given") return False if not response: st.log('Errdisable recovery interval config/unconfig failed') st.log(response) return False return True else: st.log("Unsupported CLI TYPE - {}".format(cli_type)) return False
def config_udld_recover(dut, **kwargs): """ Author: [email protected] config_udld_recover(dut=data.dut1,udld_recover='enable',module="udld") config_udld_recover(dut=data.dut1,udld_recover='disable',module="udld") udld.config_udld_recover(dut=dut1,udld_recover='enable',module="udld",cli_type = 'klish') udld.config_udld_recover(dut=dut1,udld_recover='enable',module="udld",cli_type = 'rest-put') Configure udld recover global :param dut: :param udld_recover: :module: :return: """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut)) module = kwargs.get('module', None) udld_recover = kwargs.get('udld_recover', None) st.log("Starting UDLD recover Configurations1...") my_cmd = '' if cli_type == 'click': if 'udld_recover' in kwargs and 'module' in kwargs: my_cmd = 'config errdisable recovery cause {} {}'.format( kwargs['udld_recover'], kwargs['module']) else: st.error( "Mandatory arguments udld enable or disable and module name should be given" ) return False st.config(dut, my_cmd, type=cli_type) elif cli_type == 'klish': if udld_recover != None and module != None: if udld_recover == 'enable': my_cmd = 'errdisable recovery cause {}'.format( kwargs['module']) elif udld_recover == 'disable': my_cmd = 'no errdisable recovery cause {}'.format( kwargs['module']) else: st.error( "Mandatory arguments udld recover and module name should be given" ) return False st.config(dut, my_cmd, type=cli_type) elif cli_type in ['rest-patch', 'rest-put']: http_method = kwargs.pop('http_method', cli_type) rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['errdisable_recover_cause_config'] if module != None and udld_recover != None: ocdata = {"openconfig-errdisable-ext:cause": [module.upper()]} if udld_recover.lower() == 'enable': response = config_rest(dut, http_method=http_method, rest_url=rest_url, json_data=ocdata) elif udld_recover.lower() == 'disable': response = delete_rest(dut, http_method='delete', rest_url=rest_url, json_data=ocdata) else: st.error( "Mandatory arguments udld recover and module name should be given" ) return False if not response: st.log('Errdisable recovery cause config/unconfig failed') st.log(response) return False return True else: st.log("Unsupported CLI TYPE - {}".format(cli_type)) return False
def config_snapshot_interval(dut, **kwargs): """ configuring the water mark intervals Author: prudviraj k ([email protected]) :param dut: :param kwargs: Needed arguments to update the watermark configuration :return: """ snapshot_arg = kwargs cli_type = st.get_ui_type(dut, **kwargs) if cli_type in ["rest-patch", "rest-put"] and snapshot_arg['snap'] in [ "clear_buffer-pool watermark", "sonic-clear buffer-pool watermark" ]: cli_type = 'klish' if not snapshot_arg: st.error("Mandatory arguments are not given") if cli_type == "click": if snapshot_arg['snap'] == "interval": command = "config watermark interval {}".format( snapshot_arg['interval_val']) elif snapshot_arg['snap'] == "telemetry": command = "config watermark telemetry interval {}".format( snapshot_arg['interval_val']) elif snapshot_arg['snap'] == "clear_snaphot_interval": command = "sonic-clear watermark interval" elif snapshot_arg['snap'] == "clear_snapshot_counters": command = "sonic-clear {} {} {}".format( snapshot_arg['group'], snapshot_arg['table'], snapshot_arg['counter_type']) elif snapshot_arg['snap'] == "clear_buffer-pool watermark": command = "sonic-clear buffer-pool watermark" elif snapshot_arg['snap'] == "clear_buffer-pool persistent-watermark": command = "sonic-clear buffer-pool persistent-watermark" st.config(dut, command, type=cli_type) return True elif cli_type == "klish": if snapshot_arg['snap'] == "interval": command = "watermark interval {}".format( snapshot_arg['interval_val']) elif snapshot_arg['snap'] == "telemetry": command = "watermark telemetry interval {}".format( snapshot_arg['interval_val']) elif snapshot_arg['snap'] == "clear_snaphot_interval": command = "no watermark interval" elif snapshot_arg['snap'] == "clear_snapshot_counters": command = "clear {} {} {}".format(snapshot_arg['group'], snapshot_arg['table'], snapshot_arg['counter_type']) elif snapshot_arg['snap'] == "clear_buffer-pool watermark": command = "clear buffer_pool watermark" elif snapshot_arg['snap'] == "clear_buffer-pool persistent-watermark": command = "clear buffer_pool persistent-watermark" st.config(dut, command, type=cli_type) return True elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") if snapshot_arg['snap'] == "interval": url = rest_urls['config_watermark_interval'] data = { "openconfig-qos-ext:refresh-interval": snapshot_arg['interval_val'] } if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=data): return False elif snapshot_arg['snap'] == "telemetry": url = rest_urls['config_telemetry_interval'] data = { "openconfig-qos-ext:refresh-interval": snapshot_arg['interval_val'] } if not config_rest( dut, http_method=cli_type, rest_url=url, json_data=data): return False elif snapshot_arg['snap'] == "clear_snaphot_interval": url = rest_urls['config_watermark_interval'] if not delete_rest(dut, http_method='delete', rest_url=url): return False elif snapshot_arg['snap'] == "clear_snapshot_counters": url = rest_urls['clear_watermark_counters'] data = { "openconfig-qos-ext:input": { "buffer": snapshot_arg['group'], "buffer-type": snapshot_arg['counter_type'], "watermark-type": snapshot_arg['table'] } } if not config_rest( dut, http_method='post', rest_url=url, json_data=data): return False return True else: st.error("Unsupported UI Type: {} provided".format(cli_type)) return False
def config_static_ndp(dut, ip6_address, mac_address, interface, operation="add", **kwargs): """ Config static ndp Author: Chaitanya Vella ([email protected]) :param dut: :param ip6_address: :param mac_address: :param interface: :param operation: :return: """ cli_type = st.get_ui_type(dut, **kwargs) command = '' if cli_type == 'click': interface = st.get_other_names( dut, [interface])[0] if '/' in interface else interface oper = "replace" if operation == "add" else "del" command = "ip -6 neighbor {} {} lladdr {} dev {}".format( oper, ip6_address, mac_address, interface) elif cli_type == 'klish': command = list() intf = get_interface_number_from_name(interface) command.append('interface {} {}'.format(intf["type"], intf["number"])) cmd = 'ipv6 neighbor {} {}'.format( ip6_address, mac_address ) if operation == 'add' else 'no ipv6 neighbor {} {}'.format( ip6_address, mac_address) command.extend([cmd, 'exit']) elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') port_index = get_subinterface_index(dut, interface) if operation == 'add': url = rest_urls['config_static_ndp'].format(name=interface, index=port_index) config_data = { "openconfig-if-ip:neighbor": [{ "ip": ip6_address, "config": { "ip": ip6_address, "link-layer-address": mac_address } }] } if not config_rest( dut, rest_url=url, http_method=cli_type, json_data=config_data): st.error( "Failed to configure static neighbor with IP: {} MAC: {} on INTF: {}" .format(ip6_address, mac_address, interface)) return False else: url = rest_urls['delete_static_ndp'].format(name=interface, index=port_index, ip=ip6_address) if not delete_rest(dut, rest_url=url): st.error( "Failed to delete static neighbor with IP: {} MAC: {} on INTF: {}" .format(ip6_address, mac_address, interface)) return False else: st.error("Unsupported CLI Type: {}".format(cli_type)) return False if command: st.config(dut, command, type=cli_type) return True
def config(dut, **kwargs): """ Add/Delete username with password and role to the device. Author : Prudvi Mangadu ([email protected]) :param :dut: :param :username: :param :password: :param :role: admin | operator :param :group: :param :cli_type: click | klish :param :no_form: 0[False] | 1[True] :Usage: config(vars.D1, username='******', password='******', role='operator', cli_type='kilsh') config(vars.D1, username='******', cli_type='kilsh', no_form=True) config(vars.D1, username='******', password='******', role='admin', cli_type='click', no_form=0) config(vars.D1, username='******', password_update='test1234', cli_type='click', no_form=0) config(vars.D1, group='admin_test', cli_type='click', no_form=0) config(vars.D1, group='admin_test', cli_type='click', no_form=1) config(vars.D1, username='******', password='******', role='admin', cli_type='click', no_form=1) """ cli_type = kwargs.get("cli_type", "") cli_type = st.get_ui_type(dut, cli_type=cli_type) no_form = kwargs.get("no_form", False) if cli_type == "click": if not no_form: if kwargs.get('group'): st.config(dut, "groupadd {}".format(kwargs['group'])) if kwargs.get('username'): command = "useradd {} -m".format(kwargs['username']) if kwargs.get('role'): command += " -g {}".format(kwargs['role']) st.config(dut, command) if kwargs.get('username') and kwargs.get('password'): st.change_passwd(dut, kwargs['username'], kwargs['password']) if kwargs.get('username') and kwargs.get('append_role'): st.config( dut, "usermod -aG {} {}".format(kwargs['append_role'], kwargs['username'])) else: if kwargs.get('username') and kwargs.get('role'): st.config( dut, "gpasswd -d {} {}".format(kwargs['username'], kwargs['role'])) if kwargs.get('group'): st.config(dut, "groupdel {}".format(kwargs['group'])) if kwargs.get('username'): st.config(dut, "userdel {} -r".format(kwargs['username'])) elif cli_type == "klish": if not kwargs.get('username'): st.error("Mandatory parameter 'username' is missing") return False if not no_form: if not kwargs.get("password") and not kwargs.get('role'): st.error( "Mandatory parameter 'password' and 'role' is missing") return False if kwargs['role'] == 'sudo': kwargs['role'] = 'operator' command = "username {} password {} role {}".format( kwargs['username'], kwargs['password'], kwargs['role']) if no_form: command = "no username {} ".format(kwargs['username']) st.config(dut, command, type=cli_type, skip_error_check=True) add_user(dut, kwargs['username']) elif cli_type in ['rest-patch', 'rest-put']: if not no_form: if kwargs['role'] == 'sudo': kwargs['role'] = 'operator' data = { "openconfig-system:user": [{ "username": str(kwargs['username']), "config": { "username": str(kwargs['username']), "password": "", "password-hashed": hashed_pwd(kwargs['password']), "ssh-key": "", "role": str(kwargs['role']) } }] } rest_urls = st.get_datastore(dut, "rest_urls") url1 = rest_urls['user_creation_del'].format(kwargs['username']) if not config_rest( dut, http_method=cli_type, rest_url=url1, json_data=data): st.error("Failed to configure user {} ".format( kwargs['username'])) return False if no_form: rest_urls = st.get_datastore(dut, "rest_urls") url1 = rest_urls['user_creation_del'].format(kwargs['username']) if not delete_rest(dut, http_method=cli_type, rest_url=url1): st.error("Failed to delete user {} ".format( kwargs['username'])) else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False return True
def add_vlan_member(dut, vlan, port_list, tagging_mode=False, skip_error=False, no_form=False, cli_type=''): """ Add Members to VLAN Author : Prudvi Mangadu ([email protected]) :param dut: :param vlan: :param port_list: :param tagging_mode: :param skip_error: :param no_form: :param cli_type: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) st.log("Add member {} to the VLAN {}".format(port_list, vlan)) port_li = make_list(port_list) for each_port in port_li: if cli_type == "click": if tagging_mode: command = "config vlan member add {} {}".format( vlan, each_port) else: command = "config vlan member add {} {} -u ".format( vlan, each_port) # Here handling the error while adding interface to vlan out = st.config(dut, command, skip_error_check=True) if "is already a member of Vlan{}".format(vlan) in out: st.error("{} is already a member of Vlan{}".format( each_port, vlan)) return False if "Vlan{} doesn't exist".format(vlan) in out: st.error(" Vlan{} doesn't exist".format(vlan)) return False if "has ip address configured" in out: st.error( "Error: {} has ip address configured".format(each_port)) return False if "Vlan{} does not exist".format(vlan) in out: st.error(" Vlan{} does not exist".format(vlan)) return False elif cli_type == "klish": commands = list() interface_details = get_interface_number_from_name(each_port) if not interface_details: st.log( "Interface details not found {}".format(interface_details)) return False commands.append("interface {} {}".format( interface_details.get("type"), interface_details.get("number"))) participation_mode = "trunk" if tagging_mode else "access" if participation_mode == "trunk": command = "switchport trunk allowed Vlan {} {}" commands.append( command.format('remove', vlan) if no_form else command. format('add', vlan)) elif participation_mode == "access": command = "switchport access Vlan" commands.append("no {}".format(command) if no_form else "{} {}".format(command, vlan)) commands.append("exit") if commands: out = st.config(dut, commands, type=cli_type, skip_error_check=True) if "Invalid VLAN:" in out: st.log("Vlan{} doesn't exist".format(vlan)) return False elif cli_type in ["rest-put", "rest-patch"]: cli_type = "rest-patch" interface_details = get_interface_number_from_name(each_port) if not interface_details: st.log( "Interface details not found {}".format(interface_details)) return False if "Eth" in interface_details.get("type"): url = st.get_datastore( dut, "rest_urls")["interface_member_config"].format(each_port) else: intf_name = get_portchannel_name_for_rest(each_port) url = st.get_datastore( dut, "rest_urls")["aggregate_member_config"].format(intf_name) if not no_form: add_member = json.loads(""" {"openconfig-vlan:switched-vlan": {"config": {"interface-mode": "ACCESS"}}}""" ) if tagging_mode: vlan_id = str(vlan).split('-') vlan = '{}..{}'.format( vlan_id[0], vlan_id[1]) if len(vlan_id) > 1 else int(vlan) add_member["openconfig-vlan:switched-vlan"]["config"][ "trunk-vlans"] = [vlan] add_member["openconfig-vlan:switched-vlan"]["config"][ "interface-mode"] = "TRUNK" else: add_member["openconfig-vlan:switched-vlan"]["config"][ "access-vlan"] = int(vlan) if not config_rest(dut, http_method=cli_type, rest_url=url, json_data=add_member): return False else: if not delete_vlan_member(dut, vlan, each_port, tagging_mode=tagging_mode, cli_type=cli_type, skip_error_check=skip_error): return False else: st.log("Unsupported CLI type") return False return True