Navigating Data¶
In the previous tutorial we saw how to parse configuration. In this tutorial we are going to see how we can navigate the data we extracted.
Let’s start by parsing the configuration as in the previous example:
[1]:
from ntc_rosetta import get_driver
ios = get_driver("ios", "openconfig")
ios_driver = ios()
with open("data/ios/config.txt", "r") as f:
config = f.read()
parsed = ios_driver.parse(native={"dev_conf": config})
Raw value¶
The most basic form of navigating the data is by using the method raw_value
, which returns the object using only builtin datastructures:
[2]:
raw = parsed.raw_value()
raw
[2]:
{'openconfig-interfaces:interfaces': {'interface': [{'name': 'FastEthernet1',
'config': {'name': 'FastEthernet1',
'type': 'iana-if-type:ethernetCsmacd',
'description': 'This is Fa1',
'enabled': False},
'subinterfaces': {'subinterface': [{'index': 1,
'config': {'index': 1, 'description': 'This is Fa1.1'}},
{'index': 2, 'config': {'index': 2, 'description': 'This is Fa1.2'}}]}},
{'name': 'FastEthernet3',
'config': {'name': 'FastEthernet3',
'type': 'iana-if-type:ethernetCsmacd',
'description': 'This is Fa3',
'enabled': True},
'openconfig-if-ethernet:ethernet': {'openconfig-vlan:switched-vlan': {'config': {'interface-mode': 'ACCESS',
'access-vlan': 10}}}},
{'name': 'FastEthernet4',
'config': {'name': 'FastEthernet4',
'type': 'iana-if-type:ethernetCsmacd',
'enabled': False},
'openconfig-if-ethernet:ethernet': {'openconfig-vlan:switched-vlan': {'config': {'interface-mode': 'TRUNK',
'trunk-vlans': [10, 20]}}}}]},
'openconfig-network-instance:network-instances': {'network-instance': [{'name': 'default',
'config': {'name': 'default'},
'vlans': {'vlan': [{'vlan-id': 10,
'config': {'vlan-id': 10, 'name': 'prod', 'status': 'ACTIVE'}},
{'vlan-id': 20,
'config': {'vlan-id': 20, 'name': 'dev', 'status': 'SUSPENDED'}}]}}]}}
[3]:
print(raw["openconfig-interfaces:interfaces"]["interface"][0]["config"]["description"])
This is Fa1
Instance identifiers¶
You can also use instance identifiers to get data from the object, to do so use the method peek
:
[4]:
parsed.peek("/openconfig-interfaces:interfaces/interface=FastEthernet1/config/description")
[4]:
'This is Fa1'
[5]:
parsed.peek("/openconfig-interfaces:interfaces/interface=FastEthernet1/subinterfaces/subinterface=1/config/description")
[5]:
'This is Fa1.1'
[6]:
parsed.peek("/openconfig-interfaces:interfaces/interface=FastEthernet3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan")
[6]:
{'config': {'interface-mode': 'ACCESS', 'access-vlan': 10}}