Codec Services¶
Codec Service¶
YDK CodecService class provides API for encoding and decoding of payload strings in XML or JSON format to/from instances of Entity,
which represent containers in the device supported YANG models.
-
class
ydk.services.CodecService¶ -
encode(provider, entity, pretty=True, subtree=False)¶ Encodes
Entityinto payload string in XML or JSON format.Parameters: - provider –
CodecServiceProvider- Codec Provider instance. - entity –
Entityinstance or collection ofEntityinstances of typelistordictorEntityCollection. - pretty –
boolflag, which specifies if resulting string must be in human readable way with indentation. - subtree –
boolflag, which directs to encode entity to XML subtree.
Returns: Type of returned object corresponds to the type of entity: single payload
str, orlistofstr, or adictionaryofstr.Raises: YServiceError, if error has occurred.- provider –
-
decode(provider, payload, subtree=False)¶ Decodes payload string in XML or JSON format to instances of
Entityclass.Parameters: - provider –
CodecServiceProvider- Codec Provider instance. - payload –
stror collection ofstrEither a single encoded payload or a collection of payloads encapsulated tolistordict. - subtree –
boolflag, which directs to encode entity to XML subtree.
Returns: Type of returned object corresponds to the type of payload. It is either an instance of
Entity, or a collection ofEntityinstances of typelistordict.Raises: YServiceError, if error has occurred.- provider –
-
XmlSubtreeCodec¶
-
class
ydk.entity_utils.XmlSubtreeCodec¶ XmlSubtreeCodec class designed to provide encoding and decoding Python model API objects of type
Entityto/from XML encoded string. Compared toCodecServicethe class does not validate encoded data for their types and values.-
XmlSubtreeCodec()¶ Constructs an instance of XmlSubtreeCodec class.
-
encode(entity, root_schema)¶ Performs encoding of Python model API objects of type
Entityto well formatted XML encoded string.Parameters: - entity – An instance of
Entityclass defined under a bundle. - root_schema – An instance of
RootSchemaNode, which includes the model bundle.
Returns: str, encoded well formatted multi-line XML payload string.Raises: YServiceError, if an error has occurred; usually appears when model is not present in the bundle.- entity – An instance of
-
decode(payload, entity)¶ Decodes the XML encoded string to produce corresponding instance of
Entity.Parameters: - payload –
str, XML encoded string to be decoded. - entity –
Entity, instance of shared pointer to expected top level Entity class.
Returns: Entity, shared pointer to the decoded Entity.Raises: YInvalidArgumentError, if an error has occurred; usually appears when payload does not correspond to Entity model.- payload –
-
JsonSubtreeCodec¶
-
class
ydk.entity_utils.JsonSubtreeCodec¶ JsonSubtreeCodec class designed to provide encoding and decoding Python model API objects of type
Entityto/from JSON encoded string. Compared toCodecServicethe class does not validate encoded data for their types and values.-
JsonSubtreeCodec()¶ Constructs an instance of JsonSubtreeCodec class.
-
encode(entity, root_schema, pretty)¶ Performs encoding of Python model API objects of type
Entityto JSON encoded string.Parameters: - entity – An instance of
Entityclass defined under a bundle. - root_schema – An instance of
RootSchemaNode, which includes the model bundle. - pretty –
bool. If set to True, the function produces well formatted multi-line JSON string. If set to False - one line string.
Returns: str, encoded JSON payload string.Raises: YServiceError, if an error has occurred; usually appears when model is not present in the bundle.- entity – An instance of
-
decode(payload, entity)¶ Decodes the JSON encoded string to produce corresponding instance of
Entity.Parameters: - payload –
str, JSON encoded string to be decoded. - entity –
Entity, instance of shared pointer to expected top level Entity class.
Returns: Entity, shared pointer to the decoded Entity.Raises: YInvalidArgumentError, if an error has occurred; usually appears when payload does not correspond to Entity model.- payload –
-
Example of JsonSubtreeCodec usage¶
In this example we use gNMIServiceProvider and
CRUDService to get interface configuration from IOS XR device and
then print it using JsonSubtreeCodec:
from ydk.services import CRUDService
from ydk.path import Repository
from ydk.gnmi.providers import gNMIServiceProvider
from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ifmgr_cfg as ifmgr
# Create gNMI service provider
repo = Repository("/home/yan/ydk-gen/scripts/repository/10.30.110.84")
provider = gNMIServiceProvider(repo=repo,
address=10.20.30.40,
port=57400,
username='admin',
password='admin')
# Create CRUD service
crud = CRUDService()
# Build filter for interface configuration
ifc_filter = ifmgr.InterfaceConfigurations()
ifc = ifmgr.InterfaceConfigurations.InterfaceConfiguration()
ifc.active = '"act"'
ifc.interface_name = '"Loopback0"'
ifc_filter.interface_configuration.append(ifc)
# Read interface configuration
ifc_read = crud.read(provider, ifc_filter)
# Print interface configuration
if ifc_read:
from ydk.entity_utils import JsonSubtreeCodec
jcodec = JsonSubtreeCodec()
payload = jcodec.encode(ifc_read, provider.get_session().get_root_schema(), True)
print('CREATED INTERFACE CONFIGURATION:')
print(payload)