ncs.maagic Module
Confd/NCS data access module.
This module implements classes and function for easy access to the data store. There is no need to manually instantiate any of the classes herein. The only functions that should be used are cd(), get_node() and get_root().
Node Comparison in NSO 6.1.17+ (May 2025-):
In NSO 6.1.17, 6.2.12, 6.3.9, 6.4.5, 6.5.1 and 6.6 node object caching changed, due to excessive memory usage. This change broke services that use node == comparisons. Use get_node_path() for reliable node identification:
from ncs.maagic import get_node_path
# Instead of: device1 == device2
# Use: get_node_path(device1) == get_node_path(device2)
# Dictionary keys:
device_cache = {get_node_path(device): data}Functions
as_pyval
as_pyval(mobj, name_type=3, include_oper=False, enum_as_string=True)Convert maagic object to python value.
The types are converted as follows:
List is converted to list.
Container is converted to dict.
Leaf is converted to python value.
EmptyLeaf is converted to bool.
ActionParams is converted to dict.
If include_oper is False and and a oper Node is passed then None is returned.
Arguments:
mobj -- maagic object (maagic.Enum, maagic.Bits, maagic.Node)
name_type -- one of NODE_NAME_SHORT, NODE_NAME_FULL, NODE_NAME_PY_SHORT and NODE_NAME_PY_FULL and controls dictionary key names
include_oper -- include operational data (boolean)
enum_as_string -- return enumerator in str form (boolean)
cd
Return the node at path 'path', starting from node 'node'.
Arguments:
path -- relative or absolute keypath as a string (HKeypathRef or maagic.Node)
Returns:
node (maagic.Node)
get_maapi
Get Maapi object from obj.
Return Maapi object from obj. raise BackendError if provided object does not contain a Maapi object.
Arguements:
object (obj)
Returns:
maapi object (maapi.Maapi)
get_memory_node
Return a Node at 'path' using 'backend' only for schema information.
All operations towards the returned Node is cached in memory and not communicated to the server. This can be useful for effectively building a large data set which can later be converted to a TagValue array by calling get_tagvalues() or written directly to the server by calling set_memory_tree() and shared_set_memory_tree().
Arguments:
backend_or_node -- backend or node object for reading schema information under mount points (maagic.Node, maapi.Transaction or maapi.Maapi)
path -- absolute keypath as a string (HKeypathRef or maagic.Node)
Example use:
get_memory_root
Return Root object with a memory-only backend.
The passed in 'backend' is only used to read schema information when traversing past a mount point. All operations towards the returned Node is cached in memory and not communicated to the server.
Arguments:
backend_or_node -- backend or node object for reading schema information under mount points (maagic.Node, maapi.Transaction or maapi.Maapi)
get_node
Return the node at path 'path' using 'backend'.
Arguments:
backend_or_node -- backend object (maapi.Transaction, maapi.Maapi or None) or maapi.Node.
path -- relative or absolute keypath as a string (HKeypathRef or maagic.Node). Relative paths are only supported if backend_or_node is a maagic.Node.
shared -- if set to 'True', fastmap-friendly maapi calls, such as shared_set_elem, will be used within the returned tree (boolean)
Example use:
get_node_path
Get the keypath of a maagic node.
Provides reliable node identification across NSO versions where object caching behavior has changed.
Arguments:
node -- the maagic node (maagic.Node)
Returns:
keypath of the node as a string (str or None)
Example: if get_node_path(device1) == get_node_path(device2): print("Same device")
get_root
Return a Root object for 'backend'.
If 'backend' is a Transaction object, the returned Maagic object can be used to read and write transactional data. When 'backend' is a Maapi object you cannot read and write data, however, you may use the Maagic object to call an action (that doesn't require a transaction). If 'backend' is a Node object the underlying Transaction or Maapi object will be used (if any), otherwise backend will be assumed to be None. 'backend' may also be None (default) in which case the returned Maagic object is not connected to NCS in any way. You can still use the maagic object to build an in-memory tree which may be converted to an array of TagValue objects.
Arguments:
backend -- backend object (maagic.Node, maapi.Transaction, maapi.Maapi or None)
shared -- if set to 'True', fastmap-friendly maapi calls, such as shared_set_elem, will be used within the returned tree (boolean)
Returns:
root node (maagic.Root)
Example use:
get_tagvalues
Return a list of TagValue's representing 'node'.
Arguments:
node -- A Node object.
get_trans
Get Transaction object from node_or_trans.
Return Transaction object from node_or_trans. Raise BackendError if provided object does not contain a Transaction object.
set_memory_tree
Calls Maapi.set_values() using using TagValue's from 'node'.
The backend specified when obtaining the initial node, most likely by using 'get_memory_node()' or 'get_memory_root()', will be used if that is a maapi.Transaction backend, otherwise 'trans_obj' will be used.
Arguments:
node -- a Node object (Node)
trans_obj -- another transaction object to use in case node's backend is not a transaction backend (Node or maapi.Transaction)
set_values_xml
Parses the XML document in 'xml' and sets values in the transaction.
The XML document must be explicit with regards to namespaces and tags and the top node must represent the corresponding 'node' object.
shared_set_memory_tree
Calls Maapi.shared_set_values() using using TagValue's from 'node'.
For use in FASTMAP code (services). See set_memory_tree().
shared_set_values_xml
Parses the XML document in 'xml' and sets values in the transaction.
The XML document must be explicit with regards to namespaces and tags and the top node must represent the corresponding 'node' object. This variant is to be used in services where FASTMAP attributes must be preserved.
Classes
class Action
Represents a tailf:action node.
Initialize an Action node. Should not be called explicitly.
Members:
get_input(...)
Method:
Return a node tree representing the input node of this action.
Returns:
action inputs (maagic.ActionParams)
get_output(...)
Method:
Return a node tree representing the output node of this action.
Note that this does not actually request the action. Should not normally be called explicitly.
Returns:
action outputs (maagic.ActionParams)
request(...)
Method:
Request the action and return the result as an ActionParams node.
Arguments:
params -- input parameters of the action (maagic.ActionParams, optional)
Returns:
outparams -- output parameters of the action (maagic.ActionParams)
class ActionParams
Represents the input or output parameters of a tailf:action.
The ActionParams node is the root of a tree representing either the input or the output parameters of an action. Action parameters can be read and set just like any other nodes in the tree.
Initialize an ActionParams node.
Should not be called explicitly. Use 'get_input()' on an Action node to retrieve its input parameters or 'request()' to request the action and obtain the output parameters.
Members:
None
class BackendError
Exception type used within maagic backends.
Members:
class Bits
Representation of a YANG bits leaf with position > 63.
Initialize a Bits object.
Note that a Bits object has no connection to the YANG model and will not check that the given value matches the string representation according to the schema. Normally it is not necessary to create Bits objects using this constructor as bits leaves can be set using bytearrays alone.
Attributes:
value -- a Value object of type C_BITBIG
cs_node -- a CsNode representing the YANG bits leaf. Without this you cannot get a string representation of the bits value; in that case repr(self) will be returned for the str() call. (default: None)
Members:
class Case
Represents a case node.
If this case node has any nested choice nodes, those will appear as children of this object.
Initialize a Case node. Should not be called explicitly.
Members:
None
class Choice
Represents a choice node.
Initialize a Choice node. Should not be called explicitly.
Members:
get_value(...)
Method:
Return the currently selected case of this choice.
The case is returned as a Case node. If no case is selected for this choice, None is returned.
Returns:
current selection of choice (maagic.Case)
class Container
Represents a YANG container.
A (non-presence) container node or a list element, contains other nodes.
Initialize Container node. Should not be called explicitly.
Members:
delete(...)
Method:
Delete the container.
Deletes all nodes inside the container. The container itself is not affected as it carries no state of its own.
Example use:
class Empty
Simple represention of a yang empty value.
This is used to represent an empty value in unions and list keys.
Initialize an Empty object.
Members:
None
class EmptyLeaf
Represents a leaf with the type "empty".
Initialize an EmptyLeaf node. Should not be called explicitly.
Members:
class Enum
Simple represention of a YANG enumeration instance.
Contains the string and integer representation of the enumeration. An Enum object supports comparisons with other 'Enum' objects as well as with other objects. For equality checks, strings, numbers, 'Enum' objects and 'Value' objects are allowed. For relational operators, all of the above except strings are acceptable.
Attributes:
string -- string representation of the enumeration
value -- integer representation of the enumeration
Initialize an Enum object from a given string and integer.
Note that an Enum object has no connection to the YANG model and will not check that the given value matches the string representation according to the schema. Normally it is not necessary to create Enum objects using this constructor as enum leaves can be set using strings alone.
Arguments:
string -- string representation of the enumeration (str)
value -- integer representation of the enumeration (int)
Members:
None
class Leaf
Base class for leaf nodes.
Subclassed by NonEmptyLeaf, EmptyLeaf and LeafList.
Initialize Leaf node. Should not be called explicitly.
Members:
class LeafList
Represents a leaf-list node.
Initialize a LeafList node. Should not be called explicitly.
Members:
create(...)
Method:
Create a new leaf-list item.
Arguments:
key -- item key (str or maapi.Key)
Example use:
remove(...)
Method:
Remove a specific leaf-list item'.
Arguments:
key -- item key (str or maapi.Key)
Example use:
class LeafListIterator
LeafList iterator.
An instance of this class will be returned when iterating a leaf-list.
Initialize this object.
An instance of this class will be created when iteration of a leaf-list starts. Should not be called explicitly.
Members:
class List
Represents a list node.
A list can be treated mostly like a python dictionary. It supports indexing, iteration, the len function, and the in and del operators. New items must, however, be created explicitly using the 'create' method.
Initialize a List node. Should not be called explicitly.
Members:
create(...)
Method:
Create and return a new list item with the key '*keys'.
Arguments can be a single 'maapi.Key' object or one value for each key in the list. For a keyless oper or in-memory list (eg in action parameters), no argument should be given.
Arguments:
keys -- item keys (list[str] or maapi.Key )
Returns:
list item (maagic.ListElement)
exists(...)
Method:
Check if list has an item matching 'keys'.
Arguments:
keys -- item keys (list[str] or maapi.Key )
Returns:
boolean
filter(...)
Method:
Return a filtered iterator for the list.
With this method it is possible to filter the selection using an XPath expression and/or a secondary index. If supported by the data provider, filtering will be done there.
Not available for in-memory lists.
Keyword arguments:
xpath_expr -- a valid XPath expression for filtering or None (string, default: None) (optional)
secondary_index -- secondary index to use or None (string, default: None) (optional)
Returns:
iterator (maagic.ListIterator)
keys(...)
Method:
Return all keys in the list.
Note that this will immediately retrieve every key value from the CDB. For a long list this could be a time-consuming operation. The keys selection may be filtered using 'xpath_expr' and 'secondary_index'.
Not available for in-memory lists.
Keyword arguments:
xpath_expr -- a valid XPath expression for filtering or None (string, default: None) (optional)
secondary_index -- secondary index to use or None (string, default: None) (optional)
move(...)
Method:
Move the item with key 'key' in an ordered-by user list.
The destination is given by the arguments 'where' and 'to'.
Arguments:
key -- key of the element that is to be moved (str or maapi.Key)
where -- one of 'maapi.MOVE_BEFORE', 'maapi.MOVE_AFTER', 'maapi.MOVE_FIRST', or 'maapi.MOVE_LAST'
Keyword arguments:
to -- key of the destination item for relative moves, only applicable if 'where' is either 'maapi.MOVE_BEFORE' or 'maapi.MOVE_AFTER'.
class ListElement
Represents a list element.
This is a Container object with a specialized repr() method.
Initialize Container node. Should not be called explicitly.
Members:
delete(...)
Method:
Delete the container.
Deletes all nodes inside the container. The container itself is not affected as it carries no state of its own.
Example use:
class ListIterator
List iterator.
An instance of this class will be returned when iterating a list.
Initialize this object.
An instance of this class will be created when iteration of a list starts. Should not be called explicitly.
Members:
class MaagicError
Exception type used within maagic.
Members:
class Node
Base class of all nodes in the configuration tree.
Contains magic overrides that make children in the YANG tree appear as attributes of the Node object and as elements in the list 'self'.
Attributes:
_name -- the YANG name of this node (str)
_path -- the keypath of this node in string form (HKeypathRef)
_parent -- the parent of this node, or None if this node has no parent (maagic.Node)
_cs_node -- the schema node of this node, or None if this node is not in the schema (maagic.Node)
Initialize a Node object. Should not be called explicitly.
Members:
None
class NonEmptyLeaf
Represents a leaf with a type other than "empty".
Initialize a NonEmptyLeaf node. Should not be called explicitly.
Members:
exists(...)
Method:
Check if leaf exists.
Return True if this leaf exists (has a value) in the data tree.
get_value(...)
Method:
Return the value of this leaf.
The value is returned as the most appropriate python data type.
set_cache(...)
Method:
Set the cached value of this leaf without updating the data tree.
Use of this method is strongly discouraged.
set_value(...)
Method:
Set the value of this leaf.
Arguments:
value -- the value to be set. If 'value' is not a Value object, it will be converted to one using Value.str2val.
update_cache(...)
Method:
Read this leaf's value from the data tree and store it in the cache.
There is no need to call this method explicitly.
class PresenceContainer
Represents a presence container.
Initialize a PresenceContainer. Should not be called explicitly.
Members:
class Root
Represents the root node in the configuration tree.
The root node is not represented in the schema, it is added for convenience and can contain the top level nodes from any number of namespaces as children.
Initialize a Root node.
Should not be called explicitly. Instead, use the function 'get_root()'.
Arguments:
backend -- backend to use, or 'None' for an in-memory tree (maapi.Maapi or maapi.Transaction)
namespaces -- which namespaces to include in the tree (list)
Members:
None
Predefined Values
Last updated
Was this helpful?

