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 global_vars_and_constants_init(): global tf_rest_data global vars vars = st.ensure_min_topology('D1') tf_rest_data = SpyTestDict() hw_constants = st.get_datastore(vars.D1, "constants") tf_rest_data.all_ports = intapi.get_all_interfaces(vars.D1, int_type='physical') # Global Vars tf_rest_data.platform = bcapi.get_hwsku(vars.D1) tf_rest_data.show_version = bcapi.show_version(vars.D1) tf_rest_data.feature = "bst" tf_rest_data.configure_bst_thresholds = "configure-bst-thresholds" tf_rest_data.configure_bst_multi_thresholds = "configure-bst-multi-thresholds" tf_rest_data.get_bst_thresholds = "get-bst-thresholds" tf_rest_data.clear_bst_thresholds = "clear-bst-thresholds" tf_rest_data.queues_to_check = [ 'COUNTERS_PG_NAME_MAP', 'COUNTERS_QUEUE_NAME_MAP' ] tf_rest_data.max_time_to_check_sys_maps = [150, 2] # Seconds tf_rest_data.default_threshold_value = 0 # Percentage # Common Constants tf_rest_data.pg_headroom_un_supported_platforms = \ hw_constants['THRESHOLD_FEATURE_PG_HEADROOM_UN_SUPPORTED_PLATFORMS'] tf_rest_data.build_product_info = hw_constants[ 'BUILD_BROADCOM_CLOUD_ADVANCED'] + hw_constants[ 'BUILD_BROADCOM_ENTERPRISE_ADVANCED']
def get_static_portchannel(dut, portchannel): """ Author: Jagadish Chatrasi ([email protected]) :param dut: :type dut: :param portchannel: :type str: :return: :rtype: """ rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['aggregate_state_get'].format(portchannel) ret_val = [] try: lag_info = {} get_info = get_rest(dut, rest_url=url) get_info = get_info["output"]["openconfig-if-aggregate:state"] lag_info['name'] = portchannel lag_info['protocol'] = get_info['lag-type'].capitalize() lag_info['state'] = get_oper_status(dut, portchannel) lag_info['group'] = re.search(r"(\d+)", portchannel).group(0) lag_info['members'] = [] for member in get_info['member']: if member: member_info = {} member_info['port'] = member member_info['port_state'] = get_oper_status(dut, member) lag_info['members'].append(member_info) ret_val.append(lag_info) return ret_val except Exception as e: st.log("{} exception occurred".format(e)) return ret_val
def _get_rest_brief_dhcp_relay_data(dut, family='ipv4'): """ To get the dhcp-relay brief data Author Jagadish Chatrasi ([email protected]) :param dut: :param family: :return: """ retval = list() rest_urls = st.get_datastore(dut, 'rest_urls') ip_string = '' if family == 'ipv4' else 'v6' output = get_interface_ip_address(dut, family=family) interfaces = {entry['interface'] for entry in output} interfaces.discard('eth0') for intf in interfaces: url = rest_urls['get_dhcp{}_relay_helper_address'.format(ip_string)].format(id=intf) out = get_rest(dut, rest_url=url) if isinstance(out, dict) and out.get('output') and out['output'].get('openconfig-relay-agent:helper-address') and isinstance(out['output']['openconfig-relay-agent:helper-address'], list): addresses = out['output']['openconfig-relay-agent:helper-address'] for address in addresses: temp = dict() temp['intf'] = intf temp['dhcprelay_addr'] = address retval.append(temp) st.debug(retval) return retval
def delete_ntp_servers(dut, cli_type=''): """ :param dut: :param iplist: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) output = show_ntp_server(dut) commands = [] if output is None: st.log("No servers to delete") return True else: for ent in iterable(output): server_ip = ent["remote"].strip("+*#o-x").strip() if cli_type == "click": commands.append("config ntp del {}".format(server_ip)) elif cli_type == "klish": commands.append("no ntp server {}".format(server_ip)) elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, "rest_urls") url1 = rest_urls['config_ntp_server'].format(server_ip) if not delete_rest(dut, rest_url=url1): st.error("Failed to delete ntp {} server".format(server_ip)) else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False st.config(dut, commands, type=cli_type) return True
def show_threshold_breaches(dut, cli_type=""): """ Show Threshold Breaches Author: Prudvi Mangadu ([email protected]) :param dut: :cli_type :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type in ["click", "klish"]: command = "show threshold breaches" output = st.show(dut, command, type=cli_type) elif cli_type in ["rest-patch", "rest-put"]: output = list() url = st.get_datastore(dut, "rest_urls")["threshold_breaches"] try: get_resp = get_rest(dut, rest_url=url)["output"]["openconfig-qos-ext:threshold-breaches"]["breach"] for each in get_resp: breach = dict() if "state" in each: breach["eventid"] = str(each["id"]) breach["index"] = str(each["state"]["index"]) breach["buffer"] = each["state"]["buffer"] breach["threshold_type"] = each["state"]["type"] breach["counter"] = each["state"]["counter"] breach["value"] = str(each["state"]["breach-value"]) breach["port"] = each["state"]["port"] output.append(breach) except Exception as e: st.debug("Error in getting threshold breaches") st.debug(e) return [] return output
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 get_platform_psu_summary(dut, psu=None, cli_type=''): """ To Get Platform PSU Status Author: Prudvi Mangadu ([email protected]) :param dut: :param psu: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) result = list() if cli_type in ["click", "klish"]: command = "show platform psusummary" output = st.show(dut, command, type=cli_type) elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, "rest_urls") url1 = rest_urls['get_fan_psu'] data = get_rest(dut, rest_url=url1) output = _get_psu_server_info(data['output']) else: st.error("UNSUPPORTED CLI-TYPE: {}").format(cli_type) return [] if output: for each in output: if each["psu_status"] == "ACTIVE": each["psu_status"] = "OK" elif each["psu_status"] == "INACTIVE": each["psu_status"] = "NOT OK" result.append(each) if psu and result: return filter_and_select(result, None, {"psu": psu}) else: return result
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 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 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_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 vlan_variables(): global tg global tg_handler,hw_constants_DUT sc_data.cli_type_click = "click" sc_data.cli_type= st.get_ui_type(vars.D1, cli_type="") sc_data.vlan_list = random_vlan_list(count=2) sc_data.vlan_id = str(sc_data.vlan_list[0]) sc_data.vlan = str(sc_data.vlan_list[1]) sc_data.kbps = 1000 sc_data.frame_size = 68 sc_data.rate_pps =5000 sc_data.packets = (sc_data.kbps*1024)/(sc_data.frame_size*8) sc_data.bum_deviation = int(0.10 * sc_data.packets) sc_data.lower_pkt_count = int(sc_data.packets - sc_data.bum_deviation) sc_data.higher_pkt_count = int(sc_data.packets + sc_data.bum_deviation) sc_data.max_vlan = 4093 sc_data.source_mac = "00:0A:01:00:00:01" sc_data.source_mac1 = "00:0A:02:00:00:01" sc_data.line_rate = 100 sc_data.wait_stream_run = 10 sc_data.wait_for_stats = 10 sc_data.free_port = st.get_free_ports(vars.D1)[0] tg_handler = tgapi.get_handles_byname("T1D1P1", "T1D1P2", "T1D2P1", "T1D2P2") tg = tg_handler["tg"] tg_info['tg_info'] = tg_handler tg_info['vlan_id'] = sc_data.vlan sc_data.vlan_id_start = 1 sc_data.mac_count = 100 sc_data.dut_platform = basic_obj.get_hwsku(vars.D1) sc_data.vlan_data = [{"dut": [vars.D1], "vlan_id": sc_data.vlan, "tagged": [vars.D1T1P1, vars.D1T1P2]}] hw_constants_DUT = st.get_datastore(vars.D1, "constants") sc_data.warm_reboot_supported_platforms = hw_constants_DUT['WARM_REBOOT_SUPPORTED_PLATFORMS']
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 global_vars_and_constants_init(): global vars global tf_data vars = st.ensure_min_topology('D1T1:4') tf_data = SpyTestDict() hw_constants = st.get_datastore(vars.D1, "constants") scapi.get_running_config(vars.D1) # Global Vars tf_data.tg_port_list = [vars.T1D1P1, vars.T1D1P2, vars.T1D1P3, vars.T1D1P4] tf_data.port_list = [vars.D1T1P1, vars.D1T1P2, vars.D1T1P3, vars.D1T1P4] tf_data.platform = bcapi.get_hwsku(vars.D1).lower() tf_data.unicast = 'unicast' tf_data.multicast = 'multicast' tf_data.tg_current_mode = tf_data.unicast tf_data.queues_to_check = [ 'COUNTERS_PG_NAME_MAP', 'COUNTERS_QUEUE_NAME_MAP' ] tf_data.max_time_to_check_sys_maps = [150, 2] # Seconds tf_data.traffic_duration = 3 # Seconds tf_data.test_max_retries_count = 3 tf_data.need_debug_prints = True # Common Constants tf_data.pg_headroom_un_supported_platforms = hw_constants[ 'THRESHOLD_FEATURE_PG_HEADROOM_UN_SUPPORTED_PLATFORMS'] tf_data.th3_platforms = hw_constants['TH3_PLATFORMS'] return tf_data
def test_dhcp_relay_warm_reboot(): ################################################# # # Objective - Configure DHCP relay and verify if the configuration is retained after warm reboot. # ################################################# data.platform = basic_obj.get_hwsku(vars.D2) data.constants = st.get_datastore(vars.D2, "constants", 'default') st.log("OUTPUT:{}".format(data.constants)) if not data.platform.lower( ) in data.constants['WARM_REBOOT_SUPPORTED_PLATFORMS']: st.report_fail('test_case_unsupported') st.log("Performing Config save") rb_obj.config_save(vars.D2) st.log("Performing warm Reboot") st.reboot(vars.D2, "warm") if not basic_obj.poll_for_system_status(vars.D2, 'dhcp_relay.service', 120, 1): st.report_fail("service_not_running", "dhcp-relay") if not st.poll_wait(basic_obj.verify_service_status, 60, vars.D2, "dhcp_relay"): st.log("DHCP relay service not running") st.log("Verifying DHCP Helper configuration post reboot") check_dhcp_relay_config() dhcp_relay_obj.dhcp_client_start(vars.D3, vars.D3D2P1) if ip_obj.verify_interface_ip_address(vars.D3, vars.D3D2P1, data.pool_ip_address, family="ipv4", vrfname=''): st.report_fail("IP_address_assignment_failed", vars.D3) st.log( "Successfully verified DHCP Helper configuration is retained after warm reboot" ) st.report_pass("test_case_passed")
def copp_module_hooks(request): global vars, tg, tg_ph_1, d1_p1, hw_constants, deviation_percentage, d1_p1_mac, copp_data, vlan_igmp, copp_data_pir vars = st.ensure_min_topology("D1T1:1") hw_constants = st.get_datastore(vars.D1, "constants", "default") st.debug("hw_constants: {}".format(hw_constants)) tg, tg_ph_1 = tgapi.get_handle_byname("T1D1P1") d1_p1 = vars.D1T1P1 vlan_igmp = 3188 vlan_obj.create_vlan(vars.D1, vlan_igmp) deviation_percentage = 0.05 ret_val = copp_obj.get_copp_config(dut=vars.D1, table_name='all') if ret_val: copp_data = ret_val else: st.report_fail('module_config_failed', 'show copp config command failed') copp_data_pir = copp_obj.set_copp_pir_config(vars.D1, config='get') # Get the DUT mac address d1_p1_mac = basic_obj.get_ifconfig(vars.D1, d1_p1)[0]['mac'] # Config the routing interface ip_obj.config_ip_addr_interface(dut=vars.D1, interface_name=d1_p1, ip_address='1.1.1.2', subnet='24') yield # Un-configure the routing interface ip_obj.delete_ip_interface(dut=vars.D1, interface_name=d1_p1, ip_address='1.1.1.2', subnet='24') vlan_obj.delete_vlan(vars.D1, vlan_igmp)
def set_crm_nothresholds(dut, cli_type=""): """ Set no thresholds (default). :param dut: :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 clear" elif cli_type == "klish": command = "no crm thresholds all" elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['crm_thresholds'] if not delete_rest(dut, rest_url=url): return False else: st.error("Unsupported cli type: {}".format(cli_type)) return False if command: rv = st.config(dut, command, type=cli_type) if 'Error' in rv: st.error("{}".format(rv)) return False return True
def get_crm_summary(dut, cli_type=""): """ Get CRM polling interval in seconds. Author : Prudvi Mangadu ([email protected]) :param dut: :param cli_type: click or klish designation: :return: List of dictionary polling interface """ cli_type = st.get_ui_type(dut, cli_type=cli_type) if cli_type == "click": command = "crm show summary" output = st.show(dut, command, type=cli_type) elif cli_type == "klish": command = "show crm summary" output = st.show(dut, command, type=cli_type) elif cli_type in ["rest-patch", "rest-put"]: output = list() polling_interval = dict() rest_urls = st.get_datastore(dut, "rest_urls") url = rest_urls['crm_summary'] result = get_rest(dut, rest_url=url)["output"] if "openconfig-system-crm:polling-interval" in result: polling_interval["pollinginterval"] = str( result["openconfig-system-crm:polling-interval"]) output.append(polling_interval) else: st.error("Unsupported cli type: {}".format(cli_type)) return output
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 verify_aaa(dut, login=None, failthrough=None, fallback=None, **kwargs): ''' To verify the 'show aaa' parameters ''' cli_type = kwargs.pop('cli_type', st.get_ui_type(dut, **kwargs)) output = '' if cli_type == "click" or cli_type == "klish": output = show_aaa(dut, cli_type=cli_type) st.log("output===================started") st.log(output) st.log("output===================End") elif cli_type in ["rest-patch", "rest-put"]: rest_urls = st.get_datastore(dut, 'rest_urls') rest_url1 = rest_urls['show_aaa'] output1 = get_rest(dut, rest_url=rest_url1, timeout=time_out) st.log("Before output1===================started") st.log(output1) st.log("Before output1===================End") out1 = output1.get('output', {}) st.log("output1===================started") st.log(output1) st.log("output1===================End") output = convert_aaa_rest_output(out1) st.log("output===================started") st.log(output) st.log("output===================End") if 'cli_type' in kwargs: del kwargs['cli_type'] if len(output) == 0: st.error("Output is Empty") return False if cli_type == "click": if login and not filter_and_select(output, ['login'], {"login": login}): st.error("Provided and Configured login values are not match.") return False if failthrough and not filter_and_select(output, ['failthrough'], {"failthrough": failthrough}): st.error( "Provided and Configured failthrough values are not match.") return False if fallback and not filter_and_select(output, ['fallback'], {"fallback": fallback}): st.error("Provided and Configured fallback values are not match.") return False elif cli_type == "klish" or cli_type == "rest-patch" or cli_type == "rest-put": for key in kwargs: if str(kwargs[key]) != str(output[0][key]): st.error( "Match NOT FOUND for {} : Expected -<{}> Actual-<{}> ". format(key, kwargs[key], output[0][key])) return False else: st.log( "Match FOUND for {} : Expected -<{}> Actual-<{}> ".format( key, kwargs[key], output[0][key])) return True
def rest_get_queue_counters(dut, port): """ Author: Jagadish Chatrasi ([email protected]) :param dut: :type dut: :param port: :type port: :return: :rtype: """ ret_val = list() rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['queue_counters_get'].format(port) try: get_info = get_rest(dut, rest_url=url, timeout=60) for entry in get_info['output']['openconfig-qos:queues']['queue']: temp = dict() counters_info = entry['state'] port, queue = counters_info['name'].split(':') temp['port'] = port temp['txq'] = "{}{}".format( counters_info["openconfig-qos-ext:traffic-type"], queue) temp['pkts_drop'] = counters_info['dropped-pkts'] temp['byte_drop'] = counters_info[ 'openconfig-qos-ext:dropped-octets'] temp['pkts_count'] = counters_info['transmit-pkts'] temp['byte_count'] = counters_info['transmit-octets'] ret_val.append(temp) st.debug(ret_val) return ret_val except Exception as e: st.error("{} exception occurred".format(e)) st.log("The output is:{}".format(get_info)) st.debug(ret_val) return ret_val
def show_ntp_server(dut, cli_type=''): """ :param dut: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) st.log("show ntp servers") if cli_type == "click": command = "show ntp" output = st.show(dut, command, type=cli_type) elif cli_type == "klish": command = "show ntp associations" output = st.show(dut, command, type=cli_type) elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, "rest_urls") url1 = rest_urls['show_ntp'] server_output = get_rest(dut, rest_url=url1) output = get_rest_server_info(server_output['output']) else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False data = output output = _get_show_ntp_with_hostname_to_ip_conversion(data) return output
def rest_get_fallback_status(dut, portchannel): """ Author: Jagadish Chatrasi ([email protected]) :param dut: :type dut: :param portchannel: :type str: :return: :rtype: """ rest_urls = st.get_datastore(dut, 'rest_urls') ret_val = [] try: fallback_info = dict() url = rest_urls['fallback_oper_status_get'].format(portchannel) oper_info = get_rest(dut, rest_url=url) url = rest_urls['fallback_enable_config'].format(portchannel) enable_info = get_rest(dut, rest_url=url) fallback_info['port_channel_name'] = portchannel fallback_info['fallback_config'] = 'Enabled' if enable_info['output'][ 'openconfig-interfaces-ext:fallback'] else 'Disabled' fallback_info['fallback_oper_status'] = 'Enabled' if oper_info[ 'output']['openconfig-interfaces-ext:fallback'] else 'Disabled' ret_val.append(fallback_info) st.debug(ret_val) return ret_val except Exception as e: st.error("{} exception occurred".format(e)) st.debug(ret_val) return ret_val
def nat_pre_config(): global vars vars = st.ensure_min_topology("D1D2:1", "D2D3:1") platform = basic_obj.get_hwsku(vars.D2) common_constants = st.get_datastore(vars.D2, "constants", "default") if platform.lower() in common_constants['TH3_PLATFORMS']: st.error("NAT is not supported for this platform {}".format(platform)) st.report_unsupported('NAT_unsupported_platform',platform) ip_obj.config_ip_addr_interface(vars.D1, vars.D1D2P1, data.d1d2_ip_addr, data.ip_addr_mask, family=data.af_ipv4) ip_obj.config_ip_addr_interface(vars.D2, vars.D2D1P1, data.d2d1_ip_addr, data.ip_addr_mask, family=data.af_ipv4) ip_obj.config_ip_addr_interface(vars.D2, vars.D2D3P1, data.d2d3_ip_addr, data.ip_addr_mask, family=data.af_ipv4) ip_obj.config_ip_addr_interface(vars.D3, vars.D3D2P1, data.d3d2_ip_addr, data.ip_addr_mask, family=data.af_ipv4) ip_obj.create_static_route(vars.D1, data.d2d1_ip_addr,"{}/{}".format(data.d1_static_nw, data.ip_addr_mask), shell=data.shell_vtysh, family=data.af_ipv4) ip_obj.create_static_route(vars.D3, data.d2d3_ip_addr, "{}/{}".format(data.d3_static_nw, data.ip_addr_mask), shell=data.shell_vtysh, family=data.af_ipv4) st.log("NAT Configuration") nat_obj.config_nat_feature(vars.D2, 'enable') util_nat_zone_config(vars.D2, [vars.D2D1P1, vars.D2D3P1], [data.zone_1, data.zone_2], config=data.config_add) st.log("Creating NAT Pool") nat_obj.config_nat_pool(vars.D2, pool_name=data.pool_name[0], global_ip_range=data.d2d1_ip_addr, global_port_range=data.global_port_range, config=data.config_add) st.log("Creating NAT Pool binding") nat_obj.config_nat_pool_binding(vars.D2, binding_name=data.bind_name[0], pool_name=data.pool_name[0], config=data.config_add) utils.exec_all(True, [[ip_obj.show_ip_route, vars.D1], [ip_obj.show_ip_route, vars.D2]]) ip_obj.show_ip_route(vars.D3)
def _get_rest_neighbor_entries(dut, interface, is_arp=True): """ This API returns ARP/NDP entries Author: Jagadish Chatrasi (jagadish.chatrasi@broadcom) :param dut: :param interfaces: :return: """ retval = list() non_physical_ports = ['vlan'] rest_urls = st.get_datastore(dut, 'rest_urls') intf_index = get_subinterface_index(dut, interface) if is_arp: if any(port in interface.lower() for port in non_physical_ports): url = rest_urls['get_arp_per_vlan_port'].format(name=interface) else: url = rest_urls['get_arp_per_port'].format(name=interface, index=intf_index) else: if any(port in interface.lower() for port in non_physical_ports): url = rest_urls['get_ndp_per_vlan_port'].format(name=interface) else: url = rest_urls['get_ndp_per_port'].format(name=interface, index=intf_index) intf = 'iface' if is_arp else 'interface' out = get_rest(dut, rest_url=url) arp_entries = out['output']['openconfig-if-ip:neighbors'][ 'neighbor'] if isinstance(out, dict) and out.get( 'output' ) and 'openconfig-if-ip:neighbors' in out['output'] and out['output'][ 'openconfig-if-ip:neighbors'].get('neighbor') and isinstance( out['output']['openconfig-if-ip:neighbors']['neighbor'], list) else '' if arp_entries: for arp_entry in arp_entries: temp = dict() if isinstance(arp_entry, dict) and arp_entry.get('state'): arp = arp_entry['state'] temp['address'] = arp['ip'] if arp.get('ip') else '' temp['macaddress'] = arp['link-layer-address'].lower( ) if arp.get('link-layer-address') else '' temp['count'] = '' if interface == 'eth0': temp[intf] = 'Management0' temp['vlan'] = '-' elif any(port in interface.lower() for port in non_physical_ports): temp[intf] = interface egr_port = get_vlan_member(dut, vlan_list=interface.replace( 'Vlan', '')) temp['vlan'] = egr_port[interface.replace( 'Vlan', '')][0] if isinstance(egr_port, dict) and egr_port.get( interface.replace('Vlan', '')) else '-' else: temp[intf] = interface temp['vlan'] = '-' retval.append(temp) return retval
def get_ndp_ageout_time(dut, **kwargs): """ To get ndp aging time. Author: Prudvi Mangadu ([email protected]) :param dut: :return: """ cli_type = st.get_ui_type(dut, **kwargs) if cli_type in ['click', 'klish']: command = "cat /proc/sys/net/ipv6/neigh/default/gc_stale_time" out = st.config(dut, command) try: return out.split("\n")[0] except IndexError as e: st.log(e) st.error("Failed to get the NDP age-out time") return None elif cli_type in ['rest-patch', 'rest-put']: rest_urls = st.get_datastore(dut, 'rest_urls') url = rest_urls['ndp_ageout_get'].format(name='Values') out = get_rest(dut, rest_url=url) if 'output' in out and out.get( 'output' ) and 'openconfig-neighbor:ipv6-nd-cache-expiry' in out['output']: return out['output']['openconfig-neighbor:ipv6-nd-cache-expiry'] return 60 else: st.error("Unsupported CLI_TYPE: {}".format(cli_type)) return False
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 delete_vlan(dut, vlan_list, cli_type=''): cli_type = st.get_ui_type(dut, cli_type=cli_type) """ To delete list of VLANs. Author : Prudvi Mangadu ([email protected]) :param dut: :param vlan_list: :param cli_type: :return: """ st.log("Delete vlan") vlan_li = map(str, vlan_list) if isinstance(vlan_list, list) else [vlan_list] commands = list() rest_fail_status = False try: for each_vlan in vlan_li: if cli_type == "click": commands.append("config vlan del {}".format(each_vlan)) elif cli_type == "klish": commands.append("no interface Vlan {}".format(each_vlan)) elif cli_type in ["rest-put", "rest-patch"]: rest_url = st.get_datastore( dut, "rest_urls")["per_interface_details"].format( "Vlan{}".format(each_vlan)) output = delete_rest(dut, rest_url=rest_url, get_response=True) if not output: st.error("OUTPUT IS EMPTY FROM DELETE VLAN REST CALL") return False st.log("STATUS: {}".format(output["status"])) if not rest_status(output["status"]): rest_fail_status = True else: st.log("Unsupported CLI type") return False if rest_fail_status: st.log("One of VLAN DELETE REST call failed") return False if commands: response = st.config(dut, commands, skip_error_check=True, type=cli_type) if "Error" in response: st.log(response) return False else: vlan_list = get_vlan_list(dut, cli_type=cli_type) for each_vlan in vlan_li: if each_vlan in vlan_list: st.error(" Vlan{} is not deleted".format(each_vlan)) return False return True except Exception as e1: st.log(e1) st.error(" Vlan is not deleted due to other reasons") return False