def delete_mac(dut, mac, vlan, cli_type=""): """ :param dut: :type dut: :return: :rtype: """ #st.log("config mac del <mac> <vlan>") cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == 'click': command = "config mac del {} {}".format(mac, vlan) if not st.is_feature_supported("config-mac-add-command", dut): st.community_unsupported(command, dut) _json_mac_del(dut, mac, vlan) else: st.config(dut, command, type=cli_type) elif cli_type == 'klish': command = "no mac address-table {} vlan {}".format(mac, vlan) st.config(dut, command, type=cli_type) elif cli_type in ["rest-patch", "rest-put"]: network_instance_name = 'default' rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['mac_entry_based_vlan_interface'].format( network_instance_name, mac, vlan) delete_rest(dut, rest_url=url) else: st.log("Unsupported cli") return False return True
def set_crm_clear_config(dut, cli_type=""): """ API to clear CRM configuration. Author : Prudvi Mangadu ([email protected]) :param dut: :param cli_type: click or klish designation: :return: Command output """ cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == "click": command = "crm config clear" if not st.is_feature_supported("crm-config-clear-command", dut): st.community_unsupported(command, dut) command = "crm config polling interval 9999999" elif cli_type == "klish": command = "no crm all" elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['crm_all'] if not delete_rest(dut, rest_url=url): return False return True else: st.error("Unsupported cli type: {}".format(cli_type)) return False return st.config(dut, command, type=cli_type)
def get_mac_agetime(dut, cli_type=""): """ :param dut: :type dut: :return: :rtype: """ command = '' cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == 'click': if st.is_feature_supported("show-mac-aging-time-command", dut): command = "show mac aging-time" elif st.is_feature_supported("show-mac-aging_time-command", dut): command = "show mac aging_time" else: st.community_unsupported("show mac aging-time", dut) return 300 elif cli_type == 'klish': command = "show 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') out = get_rest(dut, rest_url= url) if isinstance(out, dict) and out.get('output') and 'openconfig-network-instance:mac-aging-time' in out['output']: return int(out['output']['openconfig-network-instance:mac-aging-time']) else: st.error("Unsupported CLI_TYPE: {}".format(cli_type)) return False if command: output = st.show(dut, command, type=cli_type) return int(output[0]["aging_time"]) return True
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 get_mac_count(dut, cli_type=""): """ To get the MAC count using - 'show mac count' command. :param dut: :type dut: :return: :rtype: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == 'click': field = "mac_count" command = "show mac count" if not st.is_feature_supported("show-mac-count-command", dut): st.community_unsupported(command, dut) return 0 output = st.show(dut, command, type=cli_type) elif cli_type == 'klish': field = "count" command = "show mac address-table count" output = st.show(dut, command, type=cli_type) elif cli_type in ["rest-patch", "rest-put"]: result = get_mac(dut, cli_type=cli_type) mac_count = len(result) return mac_count else: st.log("Unsupported cli") return False if not output: ### When MAC table is empty, klish doesn't display output so return 0 return 0 return int(output[0][field])
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 verify_vrf_verbose(dut, **kwargs): """ verify_vrf_verbose(dut1,vrfname="Vrf-103",interface='Ethernet2') """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut)) #cli_type = "klish" if cli_type in ["rest-put", "rest-patch"] else cli_type vrfname = kwargs['vrfname'] interface = kwargs['interface'] if not isinstance(vrfname, list): vrfname = [vrfname] if cli_type == 'click': cmd = "show vrf --verbose" if not st.is_feature_supported("show-vrf-verbose-command", dut): st.community_unsupported(cmd, dut) cmd = "show vrf" st.log("verify {} output".format(cmd)) output = st.show(dut, cmd) for vname, intf in zip(vrfname, interface): match = {"vrfname": vname, "interfaces": intf} entries = filter_and_select(output, ["vrfname"], match) if not bool(entries): return bool(entries) return True elif cli_type == 'klish': cmd = "show ip vrf" output = st.show(dut, cmd, type=cli_type) for vname, intf in zip(vrfname, interface): match = {"vrfname": vname, "interfaces": intf} entries = filter_and_select(output, ["vrfname"], match) if not bool(entries): return bool(entries) return True elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') for vname, intf in zip(vrfname, interface): rest_url = rest_urls['vrf_config'].format(vname) payload = get_rest( dut, rest_url=rest_url )['output']['openconfig-network-instance:network-instance'] for item in payload: if item['state'][ 'type'] != 'openconfig-network-instance-types:L3VRF': return False if item['state']['name'] != str(vname): return False for intface in item['interfaces']['interface']: if intface['state']['id'] == intf: return False return True else: st.log("Unsupported cli")
def get_vrf_verbose(dut, **kwargs): """ get_vrf_verbose(dut1,vrfname="Vrf-1") """ match_dict = {} if 'vrfname' in kwargs: match_dict['vrfname'] = kwargs['vrfname'] else: st.error("Mandatory parameter peeraddress is not found") return False cli_type = kwargs.pop('cli_type', st.get_ui_type(dut)) #cli_type = "klish" if cli_type in ["rest-put", "rest-patch"] else cli_type if cli_type == 'click': cmd = "show vrf --verbose" if not st.is_feature_supported("show-vrf-verbose-command", dut): st.community_unsupported(cmd, dut) cmd = "show vrf" st.log("get {} output".format(cmd)) output = st.show(dut, cmd) if len(output) == 0: st.error("OUTPUT is Empty") return [] entries = filter_and_select(output, None, match_dict) return entries[0] elif cli_type == 'klish': cmd = "show ip vrf" output = st.show(dut, cmd, type=cli_type) entries = filter_and_select(output, None, match_dict) if len(output) == 0: st.error("OUTPUT is Empty") return [] return entries[0] elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') vname = kwargs['vrfname'] vrf_info = {} interfaces = [] rest_url = rest_urls['vrf_config'].format(vname) payload = get_rest( dut, rest_url=rest_url )['output']['openconfig-network-instance:network-instance'] #klish output = {u'interfaces': ['PortChannel10', 'Vlan3'], u'vrfname': 'Vrf-103'} for item in payload: vrf_info['vrfname'] = item['state']['name'] for interface in item['interfaces']['interface']: interfaces.append(interface['state']['id']) vrf_info['interfaces'] = interfaces return vrf_info
def get_interface_counters_all(dut, port=None, cli_type=''): cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == 'click': # To avoid traffic rate inaccuracy, run and ignore first show command in click & use second one st.show(dut, "show interfaces counters -a", type=cli_type) if port: command = "show interfaces counters -a -i {}".format(port) if not st.is_feature_supported("show-interfaces-counters-interface-command", dut): st.community_unsupported(command, dut) command = "show interfaces counters -a | grep -w {}".format(port) return st.show(dut, command) else: return st.show(dut, "show interfaces counters -a", type=cli_type) elif cli_type == 'klish': if port: cmd = "show interface counters rate | grep \"{} \"".format(port) else: cmd = "show interface counters rate" cmd_op = st.show(dut, cmd, type=cli_type) for i in range(0,len(cmd_op)): # Creating rx_bps and tx_bps for legacy script. # Click doesnt have mpbs data cmd_op[i]['rx_bps'] = cmd_op[i]['rx_mbps'] cmd_op[i]['tx_bps'] = cmd_op[i]['tx_mbps'] return cmd_op elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") result = [] url = rest_urls['all_interfaces_details'] output = get_rest(dut, rest_url = url, timeout=60) processed_output = process_intf_counters_rest_output(output) if processed_output: result.extend(processed_output) return result else: st.log("Unsupported CLI TYPE {}".format(cli_type)) return False