The key to efficient Python programs for network management is the ability to manipulate data in a consumable format.
The pySROS libraries ensure that the YANG modeled data from SR OS is supplied in native Python data structures that are simple to understand and handle in code.
When data is obtained from an SR OS node, it is returned in a specific data structure format, a pySROS data structure.
The following table shows the pySROS conversion from model-driven YANG-based data structures to native Python data structures.
YANG structure | Python 3 structure |
---|---|
Container |
Dict |
Leaf |
Value (Type derived as shown in The pySROS data structure) |
Leaf-list |
List |
List |
Dict |
User-ordered List |
OrderedDict keyed on the YANG list key value |
The pySROS libraries provide Python class wrappers around each node type to assist with data manipulation. This information can be used with some of the features built into the pySROS API, such as pretty-printing.
Containers are wrapped in a Container() class.
Leafs are wrapped in a Leaf() class.
The following example shows how to obtain the value of the leaf that is wrapped in a Leaf() class by calling the .data function on the leaf.
from pysros.wrappers import Leaf
obj = Leaf('example')
print(obj.data)
Leaf-lists are wrapped in a LeafList() class.
All pySROS class wrappers for YANG nodes are provided by pysros.wrappers.
YANG schema metadata can be obtained on a specific data object by calling the schema function. The YANG metadata that is available within the pySROS data structure (data is the specific pySROS formatted data object) is the data.schema.module. This is the YANG module name the element comes from. If this element in the YANG schema is imported or included from another YANG modules, the root YANG module is displayed. If the YANG modules where the element is found is an augment to another module, the augment module is shown.
YANG types are also converted into native Python types. The following table describes the rules for this conversion.
Base YANG type | Python 3 type |
---|---|
binary |
String |
bits |
String |
boolean |
Boolean |
decimal64 |
String |
empty |
|
enumeration |
String |
identityref |
String |
int8 |
Integer |
int16 |
Integer |
int32 |
Integer |
int64 |
Integer |
leafref |
(not applicable2)
|
string |
String |
uint8 |
Integer |
uint16 |
Integer |
uint32 |
Integer |
uint64 |
Integer |
union |
String3
|
The specific type is provided by the pySROS libraries.
A leafref takes the YANG native type of the leaf it is referencing. This type is converted to Python according to the table.
A union YANG type may be a union of different YANG types, for example, a union of a string and a Boolean. As it is not possible to identify the intention at the time of obtaining the data, automatic type selection is not performed. Every union is treated as a string, allowing the developer to cast the element into a specified type.