ncdiff.ModelDevice class

class ncdiff.ModelDevice(session, device_handler, *args, **kwargs)[source]

Bases: ncclient.manager.Manager

Abstraction of a device that supports NetConf protocol and YANG models. This is a subclass of yang.connector.Netconf with some enhancements.

namespaces

A list of tuples. Each tuple has three elements: model name, model prefix, and model URL. This attribute is only available after scan_models() is called.

Type:list
models_loadable

A list of models this ModelDevice instance supports. The information is retrived from attribute server_capabilities.

Type:list
models_loaded

A list of models this ModelDevice instance has loaded. Loading a model means the ModelDevice instance has obtained schema infomation of the model.

Type:list
compiler

An instance of ModelCompiler.

Type:ModelCompiler
models

A dictionary of loaded models. Dictionary keys are model names, and values are Model instances.

Type:dict
roots

A dictionary of roots in loaded models. Dictionary keys are roots in {url}tagname notation, and values are model names.

Type:dict

__init__ instantiates a ModelDevice instance.

scan_models(folder='./yang', download='check')[source]

High-level api: Download models from the device by <get-schema> operation defined in RFC6022, and analyze dependencies among models using pyang package.

Parameters:
  • folder (str) – A path to a folder that stores YANG files downloaded.
  • download (str) – A string is check, force or ignore. If it is check, the content in the folder is compared with self.server_capabilities. Downloading will be skipped if the checking says good. If it is force, downloading starts without checking. Another option is ignore, which allows the compiler to work on existing YANG files in a folder without downloading.
Returns:

Nothing returns.

Return type:

None

Code Example:

>>> m = manager.connect(host='2.3.4.5', port=830,
                        username='admin', password='admin',
                        hostkey_verify=False, look_for_keys=False)
>>> m.scan_models()
...
>>>
>>> m = manager.connect(host='2.3.4.5', port=830,
                        username='admin', password='admin',
                        hostkey_verify=False, look_for_keys=False)
>>> m.scan_models(folder='/existing/yang', download='ignore')
>>> m.compiler.context.dependencies
<Element modules at 0x7fbc044b6600>
>>>
load_model(model)[source]

High-level api: Load schema information by compiling the model using pyang package.

Parameters:model (str) – Model name.
Returns:An instance of Model.
Return type:Model

Code Example:

>>> m = manager.connect(host='2.3.4.5', port=830,
                        username='admin', password='admin',
                        hostkey_verify=False, look_for_keys=False)
>>> m.scan_models()
>>>
>>> m1 = m.load_model('openconfig-system')
>>> print(m1)
...
>>>
execute(operation, *args, **kwargs)[source]

High-level api: Supported operations are get, get_config, get_schema, dispatch, edit_config, copy_config, validate, commit, discard_changes, delete_config, lock, unlock, close_session, kill_session, poweroff_machine and reboot_machine. Since ModelDevice is a subclass of manager in ncclient package, any method supported by ncclient is available here. Refer to ncclient document for more details.

take_notification(block=True, timeout=None)[source]

High-level api: Receive notification messages.

Parameters:
  • block (bool) – True if this is a blocking call.
  • timeout (int) – Timeout value in seconds.
Returns:

An instance of Notification in ncclient package.

Return type:

Notification

Code Example:

>>> reply = m.take_notification(block=True, timeout=60)
>>> assert(reply.ok)
>>> print(reply)
>>>
extract_config(reply, type='netconf')[source]

High-level api: Extract config from a rpc-reply of get-config or get message.

Parameters:reply (RPCReply) – An instance of RPCReply in ncclient package. It has to be a successful reply in order to extract config, since there is no config data in an errored reply.
Returns:An instance of Config, which represents a config state of the device.
Return type:Config

Code Example:

>>> reply = m.get_config(models='openconfig-interfaces')
>>> assert(reply.ok)
>>> config1 = m.extract_config(reply)
>>>
>>> reply = m.get(models='openconfig-interfaces')
>>> assert(reply.ok)
>>> config2 = m.extract_config(reply)
>>>
>>> config1 == config2
True
>>>
get_schema_node(config_node)[source]

High-level api: Given an Element node in config, get_schema_node returns a schema node (defined in RFC 6020), which is an Element node in the schema tree.

Parameters:

config_node (Element) – An Element node in config tree.

Returns:

A schema node.

Return type:

Element

Raises:
  • ModelError – If identifier is not unique in a namespace.
  • ConfigError – when nothing can be found.

Code Example:

>>> m.load_model('openconfig-interfaces')
>>> reply = m.get_config(models='openconfig-interfaces')
>>> config = m.extract_config(reply)
>>> print(config)
...
>>> config.ns
...
>>> config_nodes = config.xpath('/nc:config/oc-if:interfaces/oc-if:interface[oc-if:name="GigabitEthernet0/0"]')
>>> config_node = config_nodes[0]
>>>
>>> m.get_schema_node(config_node)
<Element {http://openconfig.net/yang/interfaces}interface at 0xf11acfcc>
>>>
get_model_name(node)[source]

High-level api: Given an Element node in config tree or schema tree, get_model_name returns the model name that the node belongs to.

Parameters:node (Element) – an Element node in config tree or schema tree.
Returns:Model name.
Return type:str

Code Example:

>>> m.get_model_name(config_node)
'openconfig-interfaces'
>>>
get_xpath(node, type=(1, 11, ['^(.+):(.+)$', '{}:{}']), instance=True)[source]

High-level api: Given a config or schema node, get_xpath returns an xpath of the node, which starts from the model root. Each identifier uses the prefix:tagname notation if argument ‘type’ is not specified.

Parameters:
  • node (Element) – A config or schema node.
  • type (tuple) – A tuple constant defined in yang.ncdiff.Tag. Most commonly it could be Tag.XPATH or Tag.LXML_XPATH.
  • instance (bool) – True if the xpath returned points to an instance. The xpath could point to a list or leaf-list when instance=False.
Returns:

An xpath of the config or schema node, which starts from the model root.

Return type:

str

Code Example:

>>> m.get_xpath(config_node)
'/oc-if:interfaces/interface[name="GigabitEthernet0/0"]'
>>> m.get_xpath(config_node, type=Tag.LXML_XPATH)
'/oc-if:interfaces/oc-if:interface[oc-if:name="GigabitEthernet0/0"]'
>>>
>>> m.get_xpath(schema_node)
'/oc-if:interfaces/interface'
>>>
get_statement(node)[source]

High-level api: Given a config or schema node, get_statement returns a pyang Statement object.

Parameters:node (Element) – A config or schema node.
Returns:A Statement object of pyang.
Return type:pyang.statements.Statement

Code Example:

>>> m.get_xpath(schema_node)
'/ios:native/bfd/ios-bfd:l2cos'
>>> s = m.get_statement(schema_node)
>>> s
<pyang.LeafLeaflistStatement 'leaf l2cos' at 0x7f46989a1d50>
>>> s.pprint()
leaf l2cos
 description Value of L2 COS for BFD Pkts over VLAN interfaces
 type uint8
  range 0..6
>>>
convert_tag(default_ns, tag, src=(2, 10, ['^{(.+)}(.+)$', '{{{}}}{}']), dst=(1, 12, ['^(.+):(.+)$', '{}:{}']))[source]

High-level api: Convert a tag or an identifier from one notation to another. Notations are defined by tuple constants in yang.ncdiff.Tag.

Parameters:
  • default_ns (str) – The default namespace. Usually it’s the namespace of parent node. It could be a model name, a model prefix, or a model URL, depending on your argument ‘src’. An empty string is considered as none default namespace.
  • tag (str) – A tag or an identifier of a config node or a schema node.
  • src (tuple) – The type of notation the input tag is, which is a tuple constant defined in yang.ncdiff.Tag. Most commonly it could be Tag.XPATH or Tag.LXML_XPATH.
  • dst (tuple) – The type of notation we want, which is a tuple constant defined in yang.ncdiff.Tag. Most commonly it could be Tag.XPATH or Tag.LXML_XPATH.
Returns:

A tuple that has two elements: The first element is the namespace of argument ‘tag’. It could be a model name, a model prefix, or a model URL, depending on your argument ‘src’. The second element is the converted tag or identifier, which is in notation specified by argument ‘dst’.

Return type:

tuple

Code Example:

>>> m.convert_tag('',
                  '{http://openconfig.net/yang/interfaces}interface',
                  dst=Tag.JSON_NAME)
('http://openconfig.net/yang/interfaces', 'openconfig-interfaces:interface')
>>>
convert_ns(ns, src=2, dst=0)[source]

High-level api: Convert from one namespace format, model name, model prefix or model URL, to another namespace format.

Parameters:
  • ns (str) – A namespace, which can be model name, model prefix or model URL.
  • src (int) – An int constant defined in class Tag, specifying the namespace format of ns.
  • dst (int) – An int constant defined in class Tag, specifying the namespace format of return value.
Returns:

Converted namespace in a format specified by dst.

Return type:

str