Types¶
Table of Contents
This module contains YDK Python types. It provides built-in types specified in YANG RFC 6020 and types used in YDK Python APIs.
YANG built-in types¶
For YANG Built-In types,
ydkgen generates Python classes for bits
, decimal64
, empty
,
enumeration
, identityref
and instance-identifier
. Other built-in
types, such as boolean
and int8
could be specified by YLeaf
and YType
.
-
class
ydk.types.
Bits
¶ Represents a YANG built-in bits type.
Instantiate a bit object:
>>> from ydk.types import Bits >>> bits = Bits()
-
__setitem__(self, name, value):
Called to implement assignment to
self[name]
. Assign boolean value forname
:>>> bits['disable-nagle'] = False
-
__getitem__(self, name):
Called to implement evaluation of
self[name]
. Return boolean value forname
:>>> bits['disable-nagle'] False
-
get_bitmap(self):
Return a dictionary wrapper for an internal C++
std::map<std::string, bool>
bitmap:>>> bits.get_bitmap() {'disable-nagle': False}
-
-
class
ydk.types.
Decimal64
(value)¶ Represents a YANG built-in decimal64 type.
Parameters: value – ( str
) String representation of value:Instantiate a decimal64 object:
>>> from ydk.types import Decimal64 >>> decimal = Decimal64('922337203685.4775807')
-
value
¶ A string representation for decimal value.
>>> decimal.value '922337203685.4775807'
-
-
class
ydk.types.
Empty
¶ Represents a YANG built-in empty type.
Instantiate an empty object:
>>> from ydk.types import Empty >>> empty = Empty()
-
class
ydk.types.
Enum
¶ Represents a YANG built-in enum type, a base type for all YDK enums. The usage is the same as a Python enum:
>>> from ydk.models.openconfig import openconfig_bgp_types >>> e = openconfig_bgp_types.BgpSessionDirection.INBOUND
-
class
ydk.types.
Identity
¶ Represents a YANG built-in identity type, a base type for all YDK identities:
>>> from ydk.models.openconfig import openconfig_bgp_types >>> identity = openconfig_bgp_types.L3VpnIpv6Multicast()
YDK types¶
-
class
ydk.types.
Entity
¶ Super class of all classes that represent containers and list elements in YANG.
-
parent
¶ Pointer to parent entity; the parent is set automatically during entity initialization except for presence container, which must be set manually after presence container is initialized
-
yang_name
¶ YANG name of container or list that this entity represents
-
yang_parent_name
¶ YANG name of container or list of parent entity
-
is_presence_container
¶ Boolean flag set to True if this entity represents presence container
-
is_top_level_class
¶ Boolean flag set to True if this entity represents top-level container (does not have parent entity)
-
has_list_ancestor
¶ Boolean flag set to True if this entity is member of a list
-
ignore_validation
¶ Boolean flag for user to control validation of entity data (leaf and leaf-list data values); default setting is False, meaning the validation is on
-
ylist_key_names
¶ If this entity is member of a list, the attribute specifies
list
of leaf names, which represent list keys
-
ylist_key
¶ If this entity is member of a YList, the ylist_key is set to composite key of this entity
-
clone
()¶ Returns complete copy of this entity
-
get_segment_path
()¶ Returns relative path of this entity in terms of XPath
-
get_absolute_path
()¶ Returns absolute path of this entity in terms of XPath
-
has_data
()¶ Returns True if any leaf in this entity or its child entity is assigned value; False otherwise
-
has_operation
()¶ Returns True if any leaf or container in this entity or its child entity has setting of yfilter; False otherwise
-
void set_filter(path, filter)
Sets yfilter value in leaf
Parameters: - path – YANG name of the leaf
- filter –
YFilter
, filter value
-
children
()¶ Gets dictionary of child entities, where child key is a segment path of child entity
-
-
class
ydk.types.
EntityCollection
¶ Base class to represent collection of unique
Entity
instances. EntityCollection designed as a wrapper class around ordered dictionary collection of typeOrderedDict
. It is created specifically to collect Entity class instances. Each Entity instance has unique segment path value, which is used as a key in the dictionary. If ‘entity’ is and instance ofEntity
class, the key could be retrieved ad ‘entity.get_segment_path()’ or simply ‘entity.path()’.-
__init__(*entities):
Create EntityCollection instance:
Parameters: entities – If not present or None
, creates empty collection. Otherwise the instances ofEntity
class should be listed as parameters.Raises: Exception YInvalidArgumentError
, if type of entities is different.
-
append(entities):
Add entity or multiple entities to collection.
Parameters: entities – Instance of an Entity
or Pythonlist
ofEntity
instances.Raises: Exception YInvalidArgumentError
, if type of entities is different.Example usage for creating EntityCollection:
>>> from ydk.models.ydktest import ydktest_sanity as ysanity >>> from ydk.types import EntityCollection >>> >>> runner = ysanity.Runner() >>> native = ysanity.Native() >>> >>> config = EntityCollection() >>> config.append(runner) >>> config.append(native) >>> # or simply >>> config = EntityCollection(runner, native)
-
__getitem__
(item)¶ Get single entity instance from collection.
Parameters: item – If item type is
int
, the operator returnsEntity
instance by its sequence number in the collection.If item type is
str
, the operator returnsEntity
instance, which has matching key (entity.path()==item).If item type is
Entity
, the operator returnsEntity
instance, which has matching key (entity.path()==item.path()).Returns: Instance of Entity
orNone
, if matching instance is not in the collection.Raises: Exception YInvalidArgumentError
, if type of item is other thanint
orstr
orEntity
.Examples for accessing EntityCollection members:
>>> from ydk.models.ydktest import ydktest_sanity as ysanity >>> from ydk.types import EntityCollection >>> >>> config = EntityCollection(ysanity.Runner(), ysanity.Native()) >>> >>> runner = config[0] >>> native = config['ydktest-sanity:native'] >>> native = config[ysanity.Native()]
-
entities
()¶ Get collection of all entities as Python
list
container. If collection is empty the method returns empty list.
-
keys
()¶ Get list of keys for the collection entities. If collection is empty, the method returns empty list.
Examples of accessing the entire EntityCollection content:
>>> from ydk.models.ydktest import ydktest_sanity as ysanity >>> from ydk.types import EntityCollection >>> >>> config = EntityCollection(ysanity.Runner(), ysanity.Native()) >>> >>> print(config.entities()) ['ydk.models.ydktest.ydktest_sanity.Runner', 'ydk.models.ydktest.ydktest_sanity.Native'] >>> print(config.keys()) ['ydktest-sanity:runner', 'ydktest-sanity:native']
-
clear
()¶ Delete all collection members.
-
pop
(item)¶ Delete single entity instance from collection.
Parameters: item – If item type is
int
, finds and deletes collection member by its sequence number.If item type is
str
, finds and deletes collection member, which has Entity.get_segment_path()==item.If item type is
Entity
, finds and deletes collection member, which has matching key.Returns: Entity
instance of deleted member of collection, orNone
, if matching instance is not found in collection.Raises: Exception YInvalidArgumentError
, if type of item is other thanint
orstr
orEntity
.Examples of deleting items in the collection:
>>> from ydk.models.ydktest import ydktest_sanity as ysanity >>> from ydk.types import EntityCollection >>> >>> config = EntityCollection(ysanity.Runner(), ysanity.Native()) >>> >>> native = config.pop('ydktest-sanity:native') >>> # or >>> del config[ysanity.Runner()]
The class also overwrites the following methods of
OrderedDict
class:-
has_key
(key)¶
-
get
(item)¶
-
__eq__
(other)¶
-
__ne__
(other)¶
-
__len__
()¶
-
__delitem__
(item)¶
-
__iter__():
-
__str__():
-
-
class
ydk.types.
Config
¶ Alias of class
EntityCollection
-
class
ydk.types.
Filter
¶ Alias of class
EntityCollection
-
class
ydk.types.
YLeaf
(leaf_type, name)¶ Concrete class that represents a YANG
leaf
to which data can be assigned.Create a
YLeaf
instance.Parameters: - leaf_type – (
YType
) YANG type for thisleaf
. - name – (
str
) YANG argument for this leaf.
-
operation
¶ Optional attribute of the
Entity
class which can be set to perform variousoperations
, see How do I create, update, read and delete?.
-
set(self, value):
Set value for current leaf.
Parameters: value – Value to be set.
-
get(self):
Get leaf value.
Example usage for creating a
YLeaf
of YANG typeint8
:>>> from ydk.types import YLeaf, YType >>> yleaf = YLeaf(YType.int8, 'afi-safi-name')
- leaf_type – (
-
class
ydk.types.
YLeafList
(self, leaflist_type, name)¶ Concrete class that represents a YANG
leaf-list
to which multiple instances of data can be appended to.Parameters: - leaflist_type – (
YType
) YANG type for thisleaf-list
. - name – (
str
) YANG argument for thisleaf-list
.
-
append(self, value):
Append value to current
leaf-list
.
- leaflist_type – (
-
class
ydk.types.
YList
(parent)¶ Concrete class that represents a YANG
list
, with pointer to its parent. The class extendsEntityCollection
and implements OrderedDict functionality.Parameters: parent – Entity
object, which hosts the YList.-
append(self, item):
Append single
Entity
object to current list.Parameters: item – Entity
object to be appended.
-
extend(self, items):
Append multiple instances of
Entity
class to current list.Parameters: items – List of Entity
objects to be appended.
-
get(self, key) and __getitem__(self, key), which implements operator `[]`
Access list elements by their key(s), if it is defined in the Yang model.
Parameters: key – Key value for single key or key list for multiple keys as defined in the Yang model of the list. Returns: List element having matching key value - Entity
object, or None if element is not found. If multiple elements have matching key, the function returns list of Entity objects.
-
keys(self):
Returns: List of keys for all elements in the list.
-
-
class
ydk.types.
YType
¶ Enum class representing YANG types.
-
bits
¶ bits type.
-
boolean
¶ boolean type.
-
decimal64
¶ decimal64 type.
-
empty
¶ empty type.
-
enumeration
¶ enumeration type.
-
identityref
¶ identityref type.
-
int16
¶ int16 type.
-
int32
¶ int32 type.
-
int64
¶ int64 type.
-
int8
¶ int8 type.
-
str
¶ string type.
-
uint16
¶ uint16 type.
-
uint32
¶ uint32 type.
-
uint64
¶ uint64 type.
-
uint8
¶ uint8 type.
-
Utility functions¶
-
ydk.types.
entity_to_dict
(entity)¶ Utility function to get dictionary of all leaves and presence containers recursively in this entity and its children.
Parameters: entity – An instance of Entity
.Returns: A dictionary, where key represents leaf absolute path and value represents string value of the leaf; In case of presence container the key represents the container’s absolute path and value is empty string.
-
ydk.types.
entity_diff
(entity1, entity2)¶ Utility function to compare two entities of the same underlying type. Compared are presence containers and all leaves recursively.
Parameters: Returns: A dictionary of differences between two entities, where key of type str represents leaf or presence container absolute path and value of type tuple represents difference in str values of the leaves.
Raises: Exception
YInvalidArgumentError
, if supplied entities are None or have different types.
Examples¶
In examples below we assume that you have openconfig bundle installed, see Core Installation
How instantiate and use Entity objects¶
1 2 3 4 5 6 | from ydk.models.openconfig import openconfig_bgp as oc_bgp
from ydk.models.openconfig import openconfig_bgp_types as oc_bgp_types
from ydk.models.openconfig import openconfig_routing_policy as oc_routing_policy
bgp = oc_bgp.Bgp()
afi_safi = bgp.Global_.AfiSafis.AfiSafi()
bgp.global_.afi_safis.afi_safi.append(afi_safi)
|
How to assign values to leaves¶
7 8 9 10 11 12 13 14 15 16 17 18 19 20 | bgp.global_.config.as_ = 65172 # uint32
bgp.global_.config.router_id = '1.2.3.4' # string
afi_safi.afi_safi_name = oc_bgp_types.L3VpnIpv4Unicast() # identityref
afi_safi.config.enabled = True # bool
neighbor.config.peer_type = oc_bgp_types.PeerType.INTERNAL # enum
neighbor.timers.config.hold_time = Decimal64('90.00') # decimal64
routing_policy = oc_routing_policy.RoutingPolicy()
policy_definition = routing_policy.policy_definitions.PolicyDefinition()
statement = policy_definition.statements.Statement()
statement.actions.accept_route = Empty() # empty
node.bits_type['first-option'] = True # bits, node is a dummy container
node.bits_type['second-option'] = False
|
How to append values to leaf-list¶
21 22 23 24 25 26 27 28 29 30 31 32 33 | config.as_list.append(65172) # uint32, config is a dummy container
config.router_id.append("1.2.3.4") # ip-address, config is a dummy container
id = oc_bgp_types.L3VpnIpv4Unicast # identityref
config.types_list.append(id) # identityref, config is a dummy container
config.enabled_list.append(false) # bool, config is a dummy container
config.peer_types.append(PeerTypeEnum::INTERNAL) # enum, config is a dummy container
deci = Decimal64("1.2")
node.decimal_values.append(deci) # decimal64, node is a dummy container
bits_value = Bits() # bits
bits_value["first-position"] = True # bits
bits_value["first-position"] = False # bits
node.bits_values.append(bits_value) # bits, node is a dummy container
|
How to setting the read leaf values¶
Here we use filter for a leaf
(specifically, the as number leaf) under openconfig BGP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from ydk.filters import YFilter
# Instantiate a bgp object representing the bgp container from the openconfig-bgp YANG model
bgp = ydk.models.openconfig_bgp.Bgp()
# Indicate that the `as number` is desried to be read
bgp.config.as_.operation = YFilter.read
# Instantiate the CRUD service and Netconf provider to connect to a device with address 10.0.0.1
CrudService crud_service{};
NetconfServiceProvider provider{"10.0.0.1", "test", "test", 830};
# Invoke the CRUD Read method
crud_service.read(provider, bgp);
|
Validation¶
YDK performs local validation of leafs based on the model type definition. A few examples of validation are given below (assuming you have openconfig
bundle installed, see Core Installation). Assigning an invalid type results in a local validation error.
The openconfig BGP
model defines the field as_ as a int (specifically, unsigned 32 bit integer).
1 2 3 | # Instantiate a bgp object representing the bgp container from the openconfig-bgp YANG model
bgp = ydk.models.openconfig_bgp.Bgp()
bgp.global_.config.as_ = "Hello" #invalid type
|
Assigning invalid type results in a YModelError
being thrown.
YModelError: Invalid value Hello for 'as_'. Got type: 'str'. Expected types: 'int'
The openconfig BGP
model defines the field router_id as a str (specifically, with IP address pattern).
1 2 3 | # Instantiate a bgp object representing the bgp container from the openconfig-bgp YANG model
bgp = ydk.models.openconfig_bgp.Bgp()
bgp.global_.config.router_id = "Hello" #invalid value
|
Assigning invalid value results in a YModelError
being thrown.
YModelError: Value "Hello" does not satisfy the constraint "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" (range, length, or pattern). Path: /openconfig-bgp:bgp/global/config/router-id.