API¶
Models¶
Models are generated by pyangbind
so it’s better to check it’s documentation for up to date
information: http://pynms.io/pyangbind/generic_methods/
Utils¶
-
napalm_yang.utils.
model_to_dict
(model, mode='', show_defaults=False)¶ Given a model, return a representation of the model in a dict.
This is mostly useful to have a quick visual represenation of the model.
Parameters: - model (PybindBase) – Model to transform.
- mode (string) – Whether to print config, state or all elements (“” for all)
Returns: A dictionary representing the model.
Return type: dict
Examples
>>> config = napalm_yang.base.Root() >>> >>> # Adding models to the object >>> config.add_model(napalm_yang.models.openconfig_interfaces()) >>> config.add_model(napalm_yang.models.openconfig_vlan()) >>> # Printing the model in a human readable format >>> pretty_print(napalm_yang.utils.model_to_dict(config)) >>> { >>> "openconfig-interfaces:interfaces [rw]": { >>> "interface [rw]": { >>> "config [rw]": { >>> "description [rw]": "string", >>> "enabled [rw]": "boolean", >>> "mtu [rw]": "uint16", >>> "name [rw]": "string", >>> "type [rw]": "identityref" >>> }, >>> "hold_time [rw]": { >>> "config [rw]": { >>> "down [rw]": "uint32", >>> "up [rw]": "uint32" (trimmed for clarity)
-
napalm_yang.utils.
diff
(f, s)¶ Given two models, return the difference between them.
Parameters: - f (Pybindbase) – First element.
- s (Pybindbase) – Second element.
Returns: A dictionary highlighting the differences.
Return type: dict
Examples
>>> diff = napalm_yang.utils.diff(candidate, running) >>> pretty_print(diff) >>> { >>> "interfaces": { >>> "interface": { >>> "both": { >>> "Port-Channel1": { >>> "config": { >>> "mtu": { >>> "first": "0", >>> "second": "9000" >>> } >>> } >>> } >>> }, >>> "first_only": [ >>> "Loopback0" >>> ], >>> "second_only": [ >>> "Loopback1" >>> ] >>> } >>> } >>> }
Root¶
-
class
napalm_yang.base.
Root
¶ Bases:
object
This is a container you can use as root for your other models.
Examples
>>> config = napalm_yang.base.Root() >>> >>> # Adding models to the object >>> config.add_model(napalm_yang.models.openconfig_interfaces()) >>> config.add_model(napalm_yang.models.openconfig_vlan())
-
add_model
(model, force=False)¶ Add a model.
The model will be asssigned to a class attribute with the YANG name of the model.
Parameters: - model (PybindBase) – Model to add.
- force (bool) – If not set, verify the model is in SUPPORTED_MODELS
Examples
>>> import napalm_yang >>> config = napalm_yang.base.Root() >>> config.add_model(napalm_yang.models.openconfig_interfaces) >>> config.interfaces <pyangbind.lib.yangtypes.YANGBaseClass object at 0x10bef6680>
-
compliance_report
(validation_file='validate.yml')¶ Return a compliance report. Verify that the device complies with the given validation file and writes a compliance report file. See https://napalm.readthedocs.io/en/latest/validate.html.
-
elements
()¶
-
get
(filter=False)¶ Returns a dictionary with the values of the model. Note that the values of the leafs are YANG classes.
Parameters: filter (bool) – If set to True
, show only values that have been set.Returns: A dictionary with the values of the model. Return type: dict Example
>>> pretty_print(config.get(filter=True)) >>> { >>> "interfaces": { >>> "interface": { >>> "et1": { >>> "config": { >>> "description": "My description", >>> "mtu": 1500 >>> }, >>> "name": "et1" >>> }, >>> "et2": { >>> "config": { >>> "description": "Another description", >>> "mtu": 9000 >>> }, >>> "name": "et2" >>> } >>> } >>> } >>> }
-
load_dict
(data, overwrite=False, auto_load_model=True)¶ Load a dictionary into the model.
Parameters: - data (dict) – Dictionary to loead
- overwrite (bool) – Whether the data present in the model should be overwritten by the data in the dict or not.
- auto_load_model (bool) – If set to true models will be loaded as they are needed
Examples
>>> vlans_dict = { >>> "vlans": { "vlan": { 100: { >>> "config": { >>> "vlan_id": 100, "name": "production"}}, >>> 200: { >>> "config": { >>> "vlan_id": 200, "name": "dev"}}}}} >>> config.load_dict(vlans_dict) >>> print(config.vlans.vlan.keys()) ... [200, 100] >>> print(100, config.vlans.vlan[100].config.name) ... (100, u'production') >>> print(200, config.vlans.vlan[200].config.name) ... (200, u'dev')
-
parse_config
(device=None, profile=None, native=None, attrs=None)¶ Parse native configuration and load it into the corresponding models. Only models that have been added to the root object will be parsed.
If
native
is passed to the method that’s what we will parse, otherwise, we will use thedevice
to retrieve it.Parameters: - device (NetworkDriver) – Device to load the configuration from.
- profile (list) – Profiles that the device supports. If no
profile
is passed it will be read fromdevice
. - native (list of strings) – Native configuration to parse.
Examples
>>> # Load from device >>> running_config = napalm_yang.base.Root() >>> running_config.add_model(napalm_yang.models.openconfig_interfaces) >>> running_config.parse_config(device=d)
>>> # Load from file >>> with open("junos.config", "r") as f: >>> config = f.read() >>> >>> running_config = napalm_yang.base.Root() >>> running_config.add_model(napalm_yang.models.openconfig_interfaces) >>> running_config.parse_config(native=[config], profile="junos")
-
parse_state
(device=None, profile=None, native=None, attrs=None)¶ Parse native state and load it into the corresponding models. Only models that have been added to the root object will be parsed.
If
native
is passed to the method that’s what we will parse, otherwise, we will use thedevice
to retrieve it.Parameters: - device (NetworkDriver) – Device to load the configuration from.
- profile (list) – Profiles that the device supports. If no
profile
is passed it will be read fromdevice
. - native (list string) – Native output to parse.
Examples
>>> # Load from device >>> state = napalm_yang.base.Root() >>> state.add_model(napalm_yang.models.openconfig_interfaces) >>> state.parse_config(device=d)
>>> # Load from file >>> with open("junos.state", "r") as f: >>> state_data = f.read() >>> >>> state = napalm_yang.base.Root() >>> state.add_model(napalm_yang.models.openconfig_interfaces) >>> state.parse_config(native=[state_data], profile="junos")
-
to_dict
(filter=True)¶ Returns a dictionary with the values of the model. Note that the values of the leafs are evaluated to python types.
Parameters: filter (bool) – If set to True
, show only values that have been set.Returns: A dictionary with the values of the model. Return type: dict Example
>>> pretty_print(config.to_dict(filter=True)) >>> { >>> "interfaces": { >>> "interface": { >>> "et1": { >>> "config": { >>> "description": "My description", >>> "mtu": 1500 >>> }, >>> "name": "et1" >>> }, >>> "et2": { >>> "config": { >>> "description": "Another description", >>> "mtu": 9000 >>> }, >>> "name": "et2" >>> } >>> } >>> } >>> }
-
translate_config
(profile, merge=None, replace=None)¶ Translate the object to native configuration.
In this context, merge and replace means the following:
- Merge - Elements that exist in both
self
andmerge
will use by default the values inmerge
unlessself
specifies a new one. Elements that exist only inself
will be translated as they are and elements present only inmerge
will be removed. - Replace - All the elements in
replace
will either be removed or replaced by elements inself
.
You can specify one of
merge
,replace
or none of them. If none of them are set we will just translate configuration.Parameters: - Merge - Elements that exist in both
-