def verify_static_arp(device, ip_address, mac_address, max_time=15, check_interval=5): """ Verify static arp entry is present in ARP table Args: device (`obj`): Device object ip_address (`str`): IP address mac_address (`str`): MAC address max_time (int): Maximum wait time for the trigger, in second. Default: 15 check_interval (int): Wait time between iterations when looping is needed, in second. Default: 5 Returns: True False """ log.info('Verifying ARP table contains static entry with "IP ' 'Address:{}" and "MAC Address:{}"'.format(ip_address, mac_address)) timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse("show arp") except SchemaEmptyParserError: pass if out: static_table = out.get("global_static_table") if static_table and static_table.get(ip_address, None): if (static_table[ip_address].get("mac_address", None) == mac_address): return True timeout.sleep() return False
def verify_ospf3_database_prefix(device, expected_prefix, max_time=60, check_interval=10): """API for verifying ospf3 prefix exists in database Args: device (obj): device object expected_prefix (string): prefix being searched for max_time (int, optional): maximum timeoute time. Defaults to 60. check_interval (int, optional): check interval. Defaults to 10. Returns: True/False Raises: N/A """ # 'ospf3-database':{ # 'ospf3-inter-area-prefix-lsa': # 'FFFF::FF/128' # } timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: out = device.parse('show ospf3 database extensive') except SchemaEmptyParserError: timeout.sleep() continue for ospf3_database in Dq(out).get_values('ospf3-database'): prefix_ = Dq(ospf3_database).get_values('ospf3-prefix', 0) if not isinstance(prefix_, list): if prefix_.startswith(expected_prefix): return True timeout.sleep() return False
def verify_bfd_neighbors_details_session_state( device, address_family, address, expected_session_state, max_time=60, check_interval=10, ): """ Verifies bfd neighbors details session state Args: device ('obj'): device to use address_family ('str'): Address family value address ('str'): IP address for command expected_session_state ('str'): Session state to verify max_time ('int'): Max time to retry. Default to 60 check_interval ('int'): Number of check in interval. Default to 10 Returns: True False Raises: None """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: out = device.parse("show bfd neighbors {} {} details".format( address_family.lower(), address)) except SchemaEmptyParserError: timeout.sleep() continue state = out.q.contains('session').get_values('state', 0) if state == expected_session_state: return True timeout.sleep() return False
def verify_ospf_neighbor_state(device, state, max_time=15, check_interval=5): """Verify OSPF neighbor is state Args: device (`obj`): Device object state (`str`): State to check for neighbor max_time (int): Maximum wait time for the trigger, in second. Default: 15 check_interval (int): Wait time between iterations when looping is needed, in second. Default: 5 Returns: True False """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse("show ip ospf neighbor") except SchemaEmptyParserError: pass if out: try: for intf in out["interfaces"]: for neighbor in out["interfaces"][intf]["neighbors"]: if ( state in out["interfaces"][intf]["neighbors"][neighbor][ "state" ] ): return True except KeyError: pass timeout.sleep() return False
def verify_show_ospf_database_lsa_types(device, expected_types, max_time=60, check_interval=10): """Verify 'show ospf database' lsa-types contains expected_types Args: device ('obj'): device to use expected_types ('str'): types to verify max_time ('int'): Maximum time to keep checking check_interval ('int'): How often to check Raise: None Returns: Boolean """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse('show ospf database') except SchemaEmptyParserError: timeout.sleep() continue found_types = out.q.get_values("lsa-type") verified = set(found_types).issuperset(set(expected_types)) if verified: return True else: timeout.sleep() continue return False
def is_routing_repair_path_in_cef( device, prefix, max_time=60, check_interval=10, vrf='default', address_family='ipv4', ): """ Verify 'repair path' is presente in express forwarding Args: device ('obj'): Device object route ('str'): Route address max_time ('int'): Max time in seconds retrieving and checking output check_interval ('int') vrf ('str'): VRF name address_family ('str'): Address family Raises: None Returns: True False """ timeout = Timeout(max_time=max_time, interval=check_interval) while timeout.iterate(): is_present = get_cef_repair_path_of_route( device=device, prefix=prefix, vrf=vrf, address_family=address_family, ) if is_present: return True timeout.sleep() return False
def verify_ospf_no_router_id(device, ipaddress, expected_id, max_time=60, check_interval=10): """Verify 'show ospf database network lsa-id {ipaddress} detail' attached-router doesn't contain expected_id Args: device ('obj'): device to use expected_id ('str'): expected router id ipaddress ('str'): address to use in show command max_time ('int'): Maximum time to keep checking check_interval ('int'): How often to check Raise: None Returns: Boolean """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse( f'show ospf database network lsa-id {ipaddress} detail') except SchemaEmptyParserError: timeout.sleep() continue attached_router_list = out.q.get_values("attached-router") if expected_id not in attached_router_list: return True else: timeout.sleep() continue return False
def verify_ospf_spf_delay(device, expected_spf_delay=None, max_time=60, check_interval=10): """ Verify SPF delay Args: device('obj'): device to use expected_spf_delay('float'): SPF delay time max_time ('int'): Maximum time to keep checking check_interval ('int'): How often to check Returns: True/False Raises: N/A """ timeout = Timeout(max_time, check_interval) # show commands: "show ospf overview" while timeout.iterate(): try: output = device.parse('show ospf overview') except SchemaEmptyParserError: timeout.sleep() continue spf_delay = output.q.get_values('ospf-spf-delay', None) if spf_delay: spf_delay = float(spf_delay[0]) if spf_delay == expected_spf_delay: return True timeout.sleep() return False
def verify_node_state( device, simulation_name, node_name=None, max_time=300, check_interval=15, state="ACTIVE", ): """Verify node reachable Args: device (`obj`): Device object simulation_name ('str'): simulation name node_name ('str'): node name max_time (`int`): maximum wait time in seconds. Default: 300 check_interval (`int`): Wait time between iterations when looping\ is needed in secnods. Default: 15 state (`str`): state to verify Returns: state ('str'): node state Raise: None """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): nodes = [] if node_name: nodes.append(node_name) else: nodes = get_node_summary(device, simulation_name) for node in nodes: if nodes[node]["state"] != state: break else: return True timeout.sleep() return False
def verify_ptp_8275_local_clock_port_priority(device, interfaces, local_port_priority, max_time=15, check_interval=5): """ Verify ptp 8275 local clock port priority value in show ptp clock command Args: device ('obj'): Device object interface ('list'): PTP interface local_port_priority ('int'): PTP local clock port priority max_time ('int'): Maximum wait time for the trigger, in second. Default: 15 check_interval ('int'): Wait time between iterations when looping is needed, in second. Default: 5 Returns: True False """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None for intf in interfaces: try: out = device.parse("show ptp port {intf}".format(intf=intf)) except SchemaEmptyParserError: pass if out: loc_port_prio = out['local_port_priority'] if (int(loc_port_prio) == int(local_port_priority)): result = True else: result = False if result: return True timeout.sleep() return False
def verify_bfd_neighbors_details_registered_protocols( device, address_family, address, expected_registered_protocols, max_time=60, check_interval=10, ): """ Verifies bfd configuration is applied with correct interval and protocol Args: device ('obj'): device to use address_family ('str'): Address family value address ('str'): IP address for command expected_registered_protocols ('list'): protocols to verify max_time ('int'): Max time to retry. Default to 60 check_interval ('int'): Number of check in interval. Default to 10 Returns: True False Raises: None """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: out = device.parse("show bfd neighbors {} {} details".format( address_family.lower(), address)) except SchemaEmptyParserError: timeout.sleep() continue registered_protocols = out.q.get_values('registered_protocols') if registered_protocols in expected_registered_protocols: return True timeout.sleep() return False
def verify_synced_ntp_server(device, ip_address, max_time=1200, check_interval=30): """ Verify synched NTP server Args: device (`obj`): Device object ip_address (`list`): list of Server peer IP address max_time (int): Maximum wait time for the trigger, in seconds. Default: 1200 check_interval (int): Wait time between iterations when looping is needed, in seconds. Default: 30 Returns: peer_dict (`dict`): Peer dictionary """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: out = device.parse("show ntp associations") except SchemaEmptyParserError: log.error('No NTP server found') return False for ip in ip_address: try: if ( out.get("peer").get(ip).get("local_mode").get("client").get("mode") == "synchronized" ): log.info("IP {} is synchronized".format(ip)) # will sync with only one NTP server. so, check only one return True else: log.warning("IP {} is not yet synchronized".format(ip)) except Exception as e: log.error("{}".format(e)) timeout.sleep() return False
def verify_ipv6_neighbor_state(device, expected_interface, expected_state, max_time=60, check_interval=10): """Verify interface exists with expected state Args: device (obj): Device object expected_interface (str): Interface to check for expected_state (str): Expected interface state max_time (int, optional): Maximum timeout time. Defaults to 60. check_interval (int, optional): Check interval. Defaults to 10. """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: out = device.parse('show ipv6 neighbor') except SchemaEmptyParserError as e: timeout.sleep() continue # "ipv6-nd-information": { # "ipv6-nd-entry": [ # { # "ipv6-nd-state": str for entry in out.q.get_values("ipv6-nd-entry"): if expected_interface.lower() == entry.get( "ipv6-nd-neighbor-address").lower(): if expected_state.lower() == entry.get( "ipv6-nd-state").lower(): return True timeout.sleep() return False
def verify_installed_pies(device, installed_packages, max_time=300, check_interval=60): ''' Verify module serial number is matched with expected number Args: device (`obj`): Device object installed_packages (`list`): List of packages to verify that exist max_time (`int`): Maximum time to wait while checking for pies installed Default 300 seconds (Optional) check_interval (`int`): Time interval while checking for pies installed Default 30 seconds (Optional) Returns: result (`bool`): Verified result ''' timeout = Timeout(max_time, check_interval) while timeout.iterate(): active_packages = device.api.get_current_active_pies() # Trim out active_packages if active_packages: active_packages = [item.split(":")[1] for item in active_packages] if set(installed_packages).intersection(active_packages): log.info("Installed packages {} present under 'Active Packages'".\ format(installed_packages)) return True log.warning("Installed packages {} *not* present in 'Active Packages'" "\nRe-checking after {} seconds...".\ format(installed_packages, check_interval)) timeout.sleep() return False
def verify_module_status(device, timeout=180, interval=30): ''' Check status of slot using 'show platform' Args: device ('obj'): Device object timeout ('int'): Max timeout to re-check slot status interval ('int'): Max interval to re-check slot status ''' timeout = Timeout(max_time=timeout, interval=interval) while timeout.iterate(): # Reset failed_slots = [] try: output = device.parse("show platform") except SchemaEmptyParserError: timeout.sleep() continue # Check state for all slots failed_slots = Dq(output).contains('state').\ not_contains_key_value('state', '.*ok.*|standby|ha-standby|Ready', value_regex=True).\ get_values('slot') if not failed_slots: log.info("All modules on '{}' are in stable state".\ format(device.name)) break else: log.warning("The following modules are not in stable state {}".\ format(failed_slots)) log.warning( "Sleeping {} seconds before rechecking".format(interval)) timeout.sleep() continue else: raise Exception("Modules on '{}' are not in stable state".\ format(device.name))
def is_ntp_clock_synchronized(device, ip_address_peer=None, max_time=60, check_interval=5, check_leap=False): """ Verify that clock is synchronized to given peer Args: device (`obj`): Device object ip_address_peer (`str`): peer ip address. If None, peer/reference clock is ignored. max_time (int): Maximum wait time for the trigger, in second. Default: 60 check_interval (int): Wait time between iterations when looping is needed, in second. Default: 5 Returns: result (`bool`): Verified result """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: out = device.parse("show ntp status") if out["clock_state"]["system_status"]["status"] == "synchronized": if check_leap: if (out["clock_state"]["system_status"]["leapsecond"] == True): return True elif ip_address_peer: if (out["clock_state"]["system_status"]["refid"] == ip_address_peer): return True else: return True except Exception: pass timeout.sleep() return False
def verify_hardware_spa_exist(device, spa, max_time=300, check_interval=30): """ Verify spa exists Args: device (`obj`): Device object spa (`str`): spa slot max_time (`int`): max time check_interval (`int`): check interval Returns: result(`bool`): verify result Raises: None """ slots = spa.split("/") reqs = R([ "slot", slots[0], "(?P<type>.*)", "(?P<name>.*)", "subslot", slots[1], "(?P<sub_dict>.*)", ]) timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: out = device.parse("show platform") except SchemaEmptyParserError: timeout.sleep() continue found = find([out], reqs, filter_=False, all_keys=True) if found: return True timeout.sleep() return False
def verify_interface_port_channel_status_bundled(device, port_channel, max_time, check_interval, bundled_count, down_count): """ Verify bundled interfaces mode Args: device (`obj`): Device object port_channel (`str`): Port channel interface max_time ('int'): maximum time to check check_interval ('int'): how often to check Returns: result(`bool`): verify result """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = device.parse("show etherchannel summary") if (out and "interfaces" in out and port_channel.capitalize() in out["interfaces"] and "members" in out["interfaces"][port_channel.capitalize()]): bundled_count = 0 down_count = 0 for intf in out["interfaces"][ port_channel.capitalize()]["members"]: if out["interfaces"][ port_channel.capitalize()]["members"][intf]["bundled"]: bundled_count += 1 elif ("D" in out["interfaces"][port_channel.capitalize()] ["members"][intf]["flags"]): down_count += 1 if bundled_count == 3 and down_count == 1: return True timeout.sleep() return False
def verify_interface_config_carrier_delay( device, interface, max_time=60, check_interval=10, flag=True ): """Verify interface carrier_delay config in - show run interface Args: device (`obj`): Device object interface (`str`): Interface name max_time (`int`): max time check_interval (`int`): check interval flag (`bool`): True if verify has carrier delay False if verify no carrier delay Returns: result(`bool`): verify result """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = device.execute("show run interface {}".format(interface)) cfg_dict = get_config_dict(out) key = "interface {}".format(interface) if key in cfg_dict: for line in cfg_dict[key].keys(): if "carrier-delay" in line: result = True break else: result = False else: result = False if flag == result: return True timeout.sleep() return False
def verify_snmp_statistics( device, expected_output_counter, max_time=30, check_interval=10, ): """ Verify snmp statistics Args: device (`obj`): Device object expected_output_counter (`str`): Expected output counter max_time (`int`): Max time, default: 60 seconds check_interval (`int`): Check interval, default: 10 seconds Returns: result (`bool`): Verified result Raises: N/A """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: output = device.parse('show snmp statistics') except SchemaEmptyParserError: return None # Example output # "snmp-statistics": { # "snmp-output-statistics": { # "traps": "0" traps = Dq(output).contains('snmp-output-statistics').get_values( 'traps') if traps and expected_output_counter in traps: return True timeout.sleep() return False
def reload(self, steps, device, reload_timeout=RELOAD_TIMEOUT, sleep_after_reload=SLEEP_AFTER_RELOAD): with steps.start("Reloading '{dev}'".format(dev=device.name)) as step: reload_dialog = Dialog([ Statement( pattern= r".*This command will restart this device\, Proceed\? \[y\/N\].*", action='sendline(y)') ]) device.sendline('acidiag reboot') reload_dialog.process(device.spawn) timeout = Timeout(reload_timeout, 60) while timeout.iterate(): timeout.sleep() device.destroy() try: device.connect(learn_hostname=True) except Exception: log.info( "{dev} is not reloaded".format(dev=device.hostname)) else: log.info( "Sleeping for '{}' seconds for '{}' to stabilize.". format(sleep_after_reload, device.name)) time.sleep(sleep_after_reload) step.passed("{dev} has successfully reloaded".format( dev=device.hostname)) step.failed("{dev} failed to reboot".format(dev=device.hostname))
def verify_ip_mac_binding_count(device, origin, expected, max_time=60, check_interval=10): """ Verify ip mac binding count in device tracking database Args: device('obj'): device object origin('str'): binding table entry origin expected('int'): expected number for specific type of entry max_time('int',optional): max check time. Defaults to 60 check_interval('int',optional): check intervals. Defaults to 10 Returns: Bool Raises: None """ timeout = Timeout(max_time, check_interval) count = 0 while timeout.iterate(): output = device.parse('show device-tracking database') if output['device']: entries = output['device'] for i in range(1, len(entries) + 1): if entries[i]['dev_code'] == origin: count += 1 if count == expected: log.debug( 'Sepcific {} entry number met the expected'.format(origin)) return True timeout.sleep() log.debug('Sepcific {} entry number not met the expected'.format(origin)) return False
def get_platform_standby_rp(device, max_time=1200, interval=120): """ Get standby router slot on device Args: device ('obj'): Device object max_time ('int'): Max time in seconds retrieving router information interval ('int'): Interval in seconds retrieving router information Raise: None Return: Integer: Number of RP """ log.info("Getting standby slot") rs = R( ["slot", "(?P<val1>.*)", "rp", "(?P<val2>.*)", "state", "ok, standby"]) timeout = Timeout(max_time=max_time, interval=interval) while timeout.iterate(): try: output = device.parse("show platform") except SchemaEmptyParserError: timeout.sleep() continue ret = find([output], rs, filter_=False, all_keys=True) if ret: standby_rp = ret[0][1][1] srp = re.search("(?P<srp>(\d))", standby_rp).groupdict()["srp"] if srp: log.info("Standby RP on '{dev}' is: '{standby_rp}'".format( dev=device.name, standby_rp=standby_rp)) return srp timeout.sleep() return None
def verify_all_ospf_neighbor_states(device, expected_state, max_time=60, check_interval=10): """ Verifies state of ospf neighbor Args: device ('obj'): device to use expected_state ('str'): OSPF adjacency state that is expected max_time ('int'): Maximum time to keep checking check_interval ('int'): How often to check Returns: True/False Raises: N/A """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: output = device.parse('show ospf neighbor') except SchemaEmptyParserError: timeout.sleep() continue #{'ospf-neighbor-information': {'ospf-neighbor': [{"neighbor-address": "10.189.5.94", # "ospf-neighbor-state": "Full",}]}} neighbor_states = set(output.q.get_values('ospf-neighbor-state')) if len(neighbor_states) == 1 and expected_state in neighbor_states: return True else: timeout.sleep() return False
def verify_single_ospf_neighbor_address(device, neighbor_address, max_time=60, check_interval=10): """ Verifies single ospf neighbor exists Args: device ('obj'): device to use neighbor_address ('str'): ospf neighbor address max_time ('int'): Maximum time to keep checking check_interval ('int'): How often to check Returns: True/False Raises: N/A """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: output = device.parse('show ospf neighbor') except SchemaEmptyParserError: timeout.sleep() continue #{'ospf-neighbor-information': {'ospf-neighbor': [{"neighbor-address": "10.189.5.94", # "interface-name": "ge-0/0/0.0",}]}} neighbors = set(output.q.get_values('neighbor-address')) if neighbor_address in neighbors and len(neighbors) == 1: return True else: timeout.sleep() return False
def reconnect_device(device, max_time=300, interval=30, sleep_disconnect=30): """ Reconnect device Args: device ('obj'): Device object max_time ('int'): Max time in seconds trying to connect to device interval ('int'): Interval in seconds of checking connection sleep_disconnect ('int'): Waiting time after device disconnection Raise: ConnectionError Returns: N/A """ destroy_connection(device=device) time.sleep(sleep_disconnect) timeout = Timeout(max_time=max_time, interval=interval) while timeout.iterate(): try: device.connect() _error_patterns(device=device) except Exception as e: log.info("Device {dev} is not connected".format(dev=device.name)) destroy_connection(device=device) timeout.sleep() continue if device.is_connected(): break timeout.sleep() if not device.is_connected(): raise ConnectionError( "Could not reconnect to device {dev}".format(dev=device.name)) log.info("Reconnected to device {dev}".format(dev=device.name))
def verify_acl_usage( device, expected_acl_type, acl_id, max_time=15, check_interval=5 ): """ Verify acl usage Args: device (`obj`): Device object expected_acl_type (`str`): type of ACL acl_id (`str`): Name of ACL max_time ('int',optional): Maximum wait time for the trigger, in second. Default: 15 check_interval (`int`, optional): Wait time between iterations when looping is needed, in second. Default: 5 Returns: True False """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse("show platform software fed active acl usage") except SchemaEmptyParserError: pass if out: get_acl_type = out['acl_usage']['acl_name'][acl_id]['direction']['Ingress']['feature_type'] if expected_acl_type == get_acl_type: return True timeout.sleep() return False
def verify_flow_with_source_and_destination_exists( device, flow_monitor, source_address, destination_address, max_time=60, check_interval=10, ): """ Verifies a flow under flow_monitor with specified source and destination address' exist Args: device ('obj'): Device to use flow_monitor ('str'): Flow monitor to search under source_address ('str'): Source address to match destination_address ('str'): Destination address to match max_time ('int'): Max time to keep checking check_interval ('int'): How often to check Raises: N/A Returns: True/False """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): flow_address_pairs = device.api.get_flows_src_dst_address_pairs( device=device, flow_monitor=flow_monitor) if (source_address, destination_address) in flow_address_pairs: return True timeout.sleep() return False
def verify_ospf_two_router_id(device, ipaddress, expected_id_1, expected_id_2, max_time=60,check_interval=10): """Verify 'show ospf database lsa-id ipaddress detail' contains expected_id_1 and expected_id_2 Args: device ('obj'): device to use expected_id_1 ('str'): expected router id expected_id_2 ('str'): expected router id ipaddress ('str'): address to use in show command max_time ('int'): Maximum time to keep checking check_interval ('int'): How often to check Raise: None Returns: Boolean """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse('show ospf database lsa-id {ipaddress} detail'.format(ipaddress=ipaddress)) except SchemaEmptyParserError: timeout.sleep() continue attached_router_list = out.q.get_values("attached-router") if (expected_id_1 in attached_router_list) and (expected_id_2 in attached_router_list): return True else: timeout.sleep() continue return False
def verify_interface_state_admin_up(device, interface, max_time=60, check_interval=10): """Verify interface state is administratively up and line protocol is up Args: device (`obj`): Device object interface (`str`): Interface name max_time (`int`): max time check_interval (`int`): check interval Returns: result(`bool`): True if is up else False """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): try: cmd = 'show interfaces {interface}'.format(interface=interface) out = device.parse(cmd) except SchemaEmptyParserError: timeout.sleep() continue # Any(): { # 'oper_status': str, # Optional('line_protocol'): str, oper_status = out[interface]["oper_status"] line_protocol = out[interface]["line_protocol"] enabled = out[interface]["enabled"] if oper_status == line_protocol == "up" and enabled == True: return True timeout.sleep() return False