def verify_path_type(device, expected_interface, expected_path_type, max_time=60, check_interval=10): """Verify 'show ospf route network extensive' Args: device ('obj'): device to use expected_interface ('str'): address to verify expected_path_type ('str'): path to verify max_time ('int'): Maximum time to keep checking. Defaults to 60 check_interval ('int'): How often to check. Defaults to 10 Raise: None Returns: Boolean """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse('show ospf route network extensive') except SchemaEmptyParserError: timeout.sleep() continue #{'ospf-route-entry': # {'address-prefix': '1.1.1.1/32', # 'route-path-type': 'Intra', 'route-type': 'Network', # 'next-hop-type': 'IP', 'interface-cost': '0', # 'ospf-next-hop': {'next-hop-name': {'interface-name': 'lo0.0'}}, # 'ospf-area': '0.0.0.0', # 'route-origin': '1.1.1.1', # 'route-priority': 'low' # } # } ospf_route = Dq(out).get_values('ospf-route') for routes in ospf_route: #{'address-prefix': '1.1.1.1/32'} address_prefix = Dq(routes).get_values('address-prefix', 0) if address_prefix and address_prefix != expected_interface: continue #{'route-path-type': 'Intra'} path_type = Dq(routes).get_values('route-path-type', 0) if path_type and path_type.lower() != expected_path_type.lower(): continue return True timeout.sleep() return False
def verify_ospf_metric(device, interface, metric, max_time=60, check_interval=10): """Verify the OSPF metric Args: device (obj): Device object interface (str): Interface name metric (str): OSPF metric Returns: True/False Raises: N/A """ timeout = Timeout(max_time, check_interval) while timeout.iterate(): out = None try: out = device.parse('show ospf interface extensive') except SchemaEmptyParserError: timeout.sleep() continue # Example dictionary # "ospf-interface": [ # { # "interface-name": "ge-0/0/0.0", # "ospf-interface-topology": { # "ospf-topology-metric": "5", # }, # }, ospf_interface_list = Dq(out).get_values('ospf-interface') for ospf_interface_dict in ospf_interface_list: #{'interface-name': 'ge-0/0/1.0'} interface_ = Dq(ospf_interface_dict).get_values( 'interface-name', 0) #{'ospf-topology-metric': '5'} metric_ = Dq(ospf_interface_dict).get_values( 'ospf-topology-metric', 0) if interface_.lower() == interface.lower() and str(metric_) == str( metric): return True timeout.sleep() return False
def verify_specific_route(device, address, learn_protocol, max_time=60, check_interval=10): """Verifies address list agianst 'show route protocol {protocol}' Args: device ('obj'): device to use address('str'): address to search for learn_protocol('str'): Learned protocol 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 route {address}".format(address=address)) except SchemaEmptyParserError: timeout.sleep() continue # [{'rt-entry': {'active-tag': '*', 'protocol-name': 'OSPF', # 'preference': '10', 'metric': '1', 'age': {'#text': '00:00:01'}, # 'nh': [{'to': '106.187.14.158', 'via': 'ge-0/0/0.0'}]}, # 'rt-destination': '59.128.2.250/32'}] rt_destination_ = Dq(output).get_values("rt-destination", 0) # [{'rt-entry': {'active-tag': '*', 'protocol-name': 'OSPF', # 'preference': '10', 'metric': '1', 'age': {'#text': '00:00:01'} protocol_name = Dq(output).get_values("protocol-name", 0) if protocol_name: if rt_destination_.startswith(str(address)) and \ protocol_name.lower() == learn_protocol.lower(): return True else: timeout.sleep() continue timeout.sleep() return False
def get_route_destination_address(device, extensive=None, prefix='inet.0', protocol='Direct', interface='ge-0/0/0.0'): """Get destination address that matches criteria Args: device (obj): device object extensive (bool): Show extensive output. Defaults to None. prefix (str, optional): Route prefix. Defaults to None. protocol (str, optional): Route protocol. Defaults to None. interface (str, optional): Route interface. Defaults to None. Returns: str: The destination address """ try: if extensive: out = device.parse('show route extensive') else: out = device.parse('show route') except SchemaEmptyParserError: return None # Example dictionary structure: # { # 'rt': [{'rt-destination': '0.0.0.0/0', # 'rt-entry': {'active-tag': '*', # 'age': {'#text': '02:53:14'}, # 'nh': [{'to': '172.16.1.254', # 'via': 'ge-0/0/0.0'}], # 'preference': '12', # 'protocol-name': 'Access-internal'}}, # {'rt-destination': '12.1.1.0/24', # 'rt-entry': {'active-tag': '*', # 'age': {'#text': '5w1d ' # '19:01:21'}, # 'nh': [{'via': 'ge-0/0/3.0'}], # 'preference': '0', # 'protocol-name': 'Direct'}}, # {'rt-destination': '12.1.1.2/32', # 'rt-entry': {'active-tag': '*', # 'age': {'#text': '5w1d ' # '19:01:21'}, # 'nh': [{'nh-local-interface': 'ge-0/0/3.0'}], # 'preference': '0', # 'protocol-name': 'Local'}}, # }, route_table_list = Dq(out).get_values("route-table") for route in route_table_list: if prefix: prefix_ = Dq(route).get_values('table-name', 0) if not prefix_.lower().startswith(prefix.lower()): continue rt_list = Dq(route).get_values('rt') for rt_dict in rt_list: if protocol: protocol_ = Dq(rt_dict).get_values('protocol-name', 0) if not protocol_.lower().startswith(protocol.lower()): continue if interface: interface_ = Dq(rt_dict).get_values( 'via', 0) or Dq(rt_dict).get_values( 'nh-local-interface', 0) if not interface_.lower().startswith(interface.lower()): continue return Dq(rt_dict).get_values('rt-destination', 0) return None