def verify_cef_path_sets_summary(device): """ Verify cef path sets summary on active and standby device Args: device('obj'): device returns: True if cef path set uid is same on both active and standby, false in all other cases """ try: output = device.parse("show cef path sets summary") except SchemaEmptyParserError: raise SchemaEmptyParserError("Failed to parse commands") uids = output.q.get_values('path_set_id') # verify uid to be same on active and standby devices for uid in uids: # Path Set Id 0x00000001 - gets 00000001 u_id = uid.split("x")[1] try: active_uid = device.parse( f"show cef path set id {u_id} detail | in Replicate oce:") out = device.execute( f"show cef path set id {u_id} detail | in Replicate oce:", target="standby") standby_uid = device.parse( f"show cef path set id {u_id} detail | in Replicate oce:", output=out) except SchemaEmptyParserError: raise SchemaEmptyParserError("Failed to parse commands") active_u_id = active_uid.q.get_values('uid') standby_u_id = active_uid.q.get_values('uid') if not ((set(active_u_id) == set(standby_u_id))): return False return True
def get_routing_routes(device, vrf, address_family): """Execute 'show ip route vrf <vrf>' and retrieve the routes Args: device (`obj`): Device object vrf (`str`): VRF name address_family (`str`): Address family name Returns: Dictionary: received routes Raises: SchemaEmptyParserError KeyError """ try: out = device.parse("show ip route vrf {}".format(vrf)) except SchemaEmptyParserError: raise SchemaEmptyParserError("Parser did not return any routes for " "vrf {vrf}".format(vrf=vrf)) try: routes_received = out["vrf"][vrf]["address_family"][address_family][ "routes"] except KeyError as e: raise KeyError("Key issue with exception : {}".format(str(e))) return routes_received
def get_routing_route_count(device, vrf=None): """ Get route count for all vrfs Args: device(`str`): Device str vrf ('str'): VRF name Returns: int: route count Raises: SchemaEmptyParserError """ commands = ["show ip route vrf {} summary", "show ip route summary"] if vrf: cmd = commands[0].format(vrf) else: cmd = commands[1] try: output = device.parse(cmd) except SchemaEmptyParserError: raise SchemaEmptyParserError("Command '{}' has " "not returned any results".format(cmd)) if not vrf: vrf = "default" return output["vrf"][vrf]["total_route_source"]["networks"]
def get_routing_route_count_all_vrf(device): """ Get route count for every VRF Args: device ('obj'): Device object Returns: Integer: Route count Raises: SchemaEmptyParserError """ log.info("Getting route count for all vrf") try: out = device.parse("show vrf") except SchemaEmptyParserError as e: raise SchemaEmptyParserError("Could not find any VRF") route_count = 0 # Gets route count when VRF is 'default' try: route_count += get_routing_route_count(device=device) except SchemaEmptyParserError as e: pass for vrf in out["vrf"]: try: route_count += get_routing_route_count(device=device, vrf=vrf) except SchemaEmptyParserError as e: pass log.info("Route count for all vrf is {}".format(route_count)) return route_count
def verify_template_bind(device, interface, template_name, method='dynamic'): ''' check if template is bound to an interface Arg: device('obj'):Name of the dut interface('str'): switch interface template_name('str'): template name method(`str`, optional): {static|dynamic}. Default value is dynamic. returns: True if given template is bound to an interface with given method type False otherwise ''' intf = Common.convert_intf_name(interface) cmd = "show template binding target {interface}".format(interface=intf) try: output = device.parse(cmd) except SchemaEmptyParserError as e: log.error(str(e)) raise SchemaEmptyParserError("Failed to parse {}".format(cmd)) for bind_template in output[intf]['interface_templates']: if bind_template == template_name: if output[intf]['interface_templates'][bind_template]['method'] == method: return True return False
def get_ospf_interface_and_area(device): """ Retrieve interface for ospf on junos device Args: device ('obj'): Device object Returns: interface and area value dictionary """ try: out = device.parse("show ospf interface brief") except SchemaEmptyParserError as spe: raise SchemaEmptyParserError( "Could not parse output for" " command 'show ospf interface brief'") from spe key_val = {} try: interface_dict = out["instance"]["master"]["areas"] for k, v in interface_dict.items(): for interface in v["interfaces"].keys(): key_val.update({interface: k}) except KeyError as ke: raise KeyError("Key issue with exception: {}".format(str(ke))) from ke return key_val
def verify_interface_description_in_show_interfaces( device, interface, description ): """Verify interface description in show interfaces <interface> Args: device (`obj`): Device object interface (`str`): Interface name description (`str`): Interface description Returns: result(`bool`): verify result """ cmd = "show interfaces {interface}".format(interface=interface) try: output = device.parse(cmd) except SchemaEmptyParserError as e: log.error(str(e)) raise SchemaEmptyParserError("Failed to execute {}".format(cmd)) if output[interface].get("description", "") == description: return True return False
def verify_vrf_description_in_show_ip_vrf_detail(device, vrf, description): """Verify vrf description in show ip vrf detail <vrf> Args: device (`obj`): Device object vrf (`str`): Vrf name description (`str`): Description Returns: True False Raises: SchemaEmptyParserError Exception """ cmd = "show ip vrf detail {vrf}".format(vrf=vrf) try: output = device.parse(cmd) except Exception as e: log.error(str(e)) raise Exception("Failed to execute '{cmd}'".format(cmd=cmd)) except SchemaEmptyParserError: raise SchemaEmptyParserError("Command '{cmd}' has not returned any " "results".format(cmd=cmd)) if output[vrf].get("description", "") == description: return True return False
def verify_mpls_forwarding_table_gid_counter(device, prefix_type, bytes_labeled_switched, mdt_data_cnt=1, max_time=60, check_interval=10): """ Verifies counters for gid in mpls forwarding-table Args: prefix_type ('str') : prefix type bytes_labeled_switched ('str') : counter value mdt_data_cnt ('int', optional) : expected mdt count to be present in the output max_time (`int`, optional): Max time, default: 60 check_interval (`int`, optional): Check interval, default: 10 Raises: Exception Returns True or False """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: parsed_output1 = device.parse( "show mpls forwarding-table | sect gid") time.sleep(20) parsed_output2 = device.parse( "show mpls forwarding-table | sect gid") except SchemaEmptyParserError: raise SchemaEmptyParserError("Failed to parse commands") cnt = 0 # Verify counters are incrementing or not for mentioned prefix for labels in parsed_output1.q.get_values("local_label"): first_counter = parsed_output1.q.contains(labels).get_values( "bytes_label_switched")[0] second_counter = parsed_output2.q.contains(labels).get_values( "bytes_label_switched")[0] prefix = parsed_output1.q.contains(labels).get_values( 'prefix_or_tunnel_id')[0] if (int(second_counter) - int(first_counter)) > int(bytes_labeled_switched): cnt += 1 else: log.debug( "Packets are not flowing on prefix {prefix},1st counter-->{first_counter}," "second counter {second_counter}".format( prefix=prefix, first_counter=first_counter, second_counter=second_counter)) return False # verfiy mdt_data_cnt if set if cnt == mdt_data_cnt: return True timeout.sleep() return False
def is_interface_igp_sync_mpls_enabled( interface, device, vrf="", parsed_output="" ): """ Verifies if interface has LDP IGP sync enabled from command 'show mpls ldp igp sync' Args: parsed_output ('dict') : Output from parser interface ('str') : Interface being checked vrf ('str') : vrf name device ('str') : Device to be executed commands Raises: None Returns True False """ if not parsed_output: try: parsed_output = device.parse( "show mpls ldp igp sync interface {intf}".format( intf=interface ) ) except SchemaEmptyParserError: raise SchemaEmptyParserError( "Fail to parse 'show mpls ldp igp sync " "interface {intf}' command".format(intf=interface) ) vrf = vrf if vrf else "default" try: igp_synchronization_enabled = ( parsed_output["vrf"] .get(vrf, {}) .get("interface", {}) .get(interface, {}) .get("ldp", {}) .get("igp_synchronization_enabled", False) ) sync_achieved = ( parsed_output["vrf"] .get(vrf, {}) .get("interface", {}) .get(interface, {}) .get("sync", {}) .get("status", {}) .get("sync_achieved", False) ) except KeyError: return False return igp_synchronization_enabled and sync_achieved
def verify_etherchannel_counter(device, port_channel, field, transmitted_pkts, pps, percent): """Verifies packet flow on port-channel interface Args: device (`obj`): Device object port_channel (`str`): Port-channel interface (i.e. Port-channel5) field (`list`): fields be to checked in interface for multicast traffic Eg:["incoming","outgoing"] transmitted_pkts (`int`): packets sent through ixia pps ('int'): packet per second percent ('int'): expected percent of traffic to flow from transmitted_pkts Returns: result(`bool`): True if is packets recieved on port-channel are distributed among member interface or else return Flase """ try: output = device.parse( f"show interfaces {port_channel} counter etherchannel") except SchemaEmptyParserError: raise SchemaEmptyParserError("Failed to parse commands") counters = [] expected_packet_flow = percent / 100 * transmitted_pkts for direction in field: po_counter = output.q.contains(port_channel).contains( direction).get_values("mcast_pkts") for etherchannel in output.q.get_values("interface")[1:]: incmg_pkt = output.q.contains(etherchannel).contains( direction).get_values('mcast_pkts')[0] if incmg_pkt > expected_packet_flow: pkt = output.q.contains(etherchannel).contains( direction).get_values('mcast_pkts')[0] counters.append(pkt) else: return False if not ((sum(counters) < po_counter[0] + pps) and (sum(counters) > po_counter[0] - pps)): log.info(f"packets flowed on {port_channel} is {po_counter}, " "packets flowed on members are {sum(counters)}") return False counters.clear() return True
def verify_cef_uid_on_active_standby(device): """ Verify cef id on both active and standby device Args: device('obj'): device returns: True if cef uid is same on both active and standby, false in all other cases """ try: active_uid = device.parse("show cef uid") output = device.execute('show cef uid', target="standby") stanby_uid = device.parse("show cef uid", output=output) except SchemaEmptyParserError: raise SchemaEmptyParserError("Failed to parse commands") active_client_key_node = active_uid.q.get_values('client_key_nodes')[0] standby_client_key_node = stanby_uid.q.get_values('client_key_nodes')[0] active_uid_table_entry = active_uid.q.get_values('uid_table_entries')[0] standby_uid_table_entry = stanby_uid.q.get_values('uid_table_entries')[0] return ((active_client_key_node == standby_client_key_node) and \ (active_uid_table_entry == standby_uid_table_entry))
def verify_mpls_forwarding_table_vrf_mdt(device, vrf, prefix_type, expected_prefix, bytes_labeled_switched, mdt_cnt=1, max_time=60, check_interval=10): """ Verifies counters for mdt in mpls forwarding-table Args: vrf ('str') : vrf name prefix_type ('str') : prefix type expected_prefix ('str') : expected prefix bytes_labeled_switched ('str') : counter value mdt_cnt ('int', optional): mdt data configured, default: 1 max_time (`int`, optional): Max time, default: 60 check_interval (`int`, optional): Check interval, default: 10 Raises: Exception Returns True or False """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: parsed_output1 = device.parse( "show mpls forwarding-table vrf {vrf}".format(vrf=vrf)) time.sleep(20) parsed_output2 = device.parse( "show mpls forwarding-table vrf {vrf}".format(vrf=vrf)) except SchemaEmptyParserError: raise SchemaEmptyParserError("Failed to parse commands") count = 0 # Verify counters are incrementing or not for mentioned prefix for labels in parsed_output1.q.get_values("local_label"): if parsed_output1.q.contains(labels).contains(prefix_type): learnet_prefix = parsed_output1.q.contains(labels).get_values( 'prefix_or_tunnel_id')[0] if learnet_prefix == expected_prefix: first_counter = parsed_output1.q.contains( labels).get_values("bytes_label_switched")[0] second_counter = parsed_output2.q.contains( labels).get_values("bytes_label_switched")[0] expected_prefix = parsed_output1.q.contains( labels).get_values('prefix_or_tunnel_id')[0] if (int(second_counter) - int(first_counter)) > int(bytes_labeled_switched): count += 1 else: log.debug( "Packets are not flowing on prefix {expected_prefix}," "1st counter-->{first_counter}, second counter {second_counter}" .format(expected_prefix=expected_prefix, first_counter=first_counter, second_counter=second_counter)) return False else: log.debug( "Got unexpected {learnet_prefix}, expected {expected_prefix}" .format(learnet_prefix=learnet_prefix, expected_prefix=expected_prefix)) return False if count == mdt_cnt: return True timeout.sleep() return False
def verify_mpls_rlist_summary_before_and_after_sso(device, active_rlist_summary_bsso, standby_rlist_summary_bsso): ''' Verify whether rlist summary is same before and after sso on both active and standby device Args: device ('obj'): Device object active_rlist_summary_bsso ('int'): active device rlist summary result before sso standby_rlist_summary_bsso ('int'): standby device rlist summary result before sso Return: True if rlist summary are same on active and standby device before and after sso or else returns False ''' # current count Rlist active_rlist_bsso = active_rlist_summary_bsso.q.contains( "current_count").get_values("rlist")[0] standby_rlist_bsso = standby_rlist_summary_bsso.q.contains( "current_count").get_values("rlist")[0] # current count Rentry active_rentry_bsso = active_rlist_summary_bsso.q.contains( "current_count").get_values("rentry")[0] standby_rentry_bsso = standby_rlist_summary_bsso.q.contains( "current_count").get_values("rentry")[0] try: active_rlist_summary_asso = device.parse( "show platform software fed switch active mpls rlist summary") standby_rlist_summary_asso = device.parse( "show platform software fed switch standby mpls rlist summary") except SchemaEmptyParserError: raise SchemaEmptyParserError("Failed to parse commands") active_rlist_asso = active_rlist_summary_asso.q.contains( "current_count").get_values("rlist")[0] standby_rlist_asso = standby_rlist_summary_asso.q.contains( "current_count").get_values("rlist")[0] active_rentry_asso = active_rlist_summary_asso.q.contains( "current_count").get_values("rentry")[0] standby_rentry_asso = standby_rlist_summary_asso.q.contains( "current_count").get_values("rentry")[0] # verify rlist if not ((active_rlist_bsso == active_rlist_asso) and (active_rentry_bsso == active_rentry_asso)): log.debug( "Current count rlist and rentry verification failed after sso") return False # verify rentry if not ((standby_rlist_bsso == standby_rlist_asso) and (standby_rentry_bsso == standby_rentry_asso)): log.debug( "Current count rlist and rentry verification failed after sso") return False # Maximum reached Rlist active_rlist_bsso = active_rlist_summary_bsso.q.contains( "maximum_reached").get_values("rlist")[0] active_rlist_asso = active_rlist_summary_asso.q.contains( "maximum_reached").get_values("rlist")[0] standby_rlist_bsso = standby_rlist_summary_bsso.q.contains( "maximum_reached").get_values("rlist")[0] standby_rlist_asso = standby_rlist_summary_asso.q.contains( "maximum_reached").get_values("rlist")[0] # Maximum reached Rentry active_rentry_bsso = active_rlist_summary_bsso.q.contains( "maximum_reached").get_values("rentry")[0] active_rentry_asso = active_rlist_summary_asso.q.contains( "maximum_reached").get_values("rentry")[0] standby_rentry_bsso = standby_rlist_summary_bsso.q.contains( "maximum_reached").get_values("rentry")[0] standby_rentry_asso = standby_rlist_summary_asso.q.contains( "maximum_reached").get_values("rentry")[0] if not ((active_rlist_bsso <= active_rlist_asso * 2 and active_rlist_bsso >= active_rlist_asso) and (active_rentry_bsso <= active_rentry_asso * 2 and active_rentry_asso >= active_rentry_bsso)): log.debug("Maximum Reached entry verification failed on active switch") return False if not ((standby_rlist_bsso <= standby_rlist_asso * 2 and standby_rlist_bsso >= standby_rlist_asso) and (standby_rentry_bsso <= standby_rentry_asso * 2 and standby_rentry_asso >= standby_rentry_bsso)): log.debug( "Maximum Reached entry verification failed on standby switch") return False # Currrent lspvif adj label count active_adj_bsso = active_rlist_summary_bsso.q.get_values( "current_lspvif_adj_label_count")[0] active_adj_asso = active_rlist_summary_asso.q.get_values( "current_lspvif_adj_label_count")[0] if not active_adj_bsso == active_adj_asso: log.debug( "current_lspvif_adj_label_count verification failed on active switch" ) return False standby_adj_bsso = standby_rlist_summary_bsso.q.get_values( "current_lspvif_adj_label_count")[0] standby_adj_asso = standby_rlist_summary_asso.q.get_values( "current_lspvif_adj_label_count")[0] if not standby_adj_bsso == standby_adj_asso: log.debug( "current_lspvif_adj_label_count verification failed on standby switch" ) return False return True