Codec Service

class ydk::CodecService

Codec Service class for supporting encoding and decoding C++ model API objects of type Entity.

CodecService()

Constructs an instance of CodecService

std::string encode(CodecServiceProvider &provider, Entity &entity, bool pretty = false, bool subtree = false)

Perform encoding.

Parameters:
  • provider – An instance of CodecServiceProvider.
  • entity – An instance of Entity class defined under a bundle.
  • pretty – Optionally produce formatted output.
  • subtree – Subtree filter.
Returns:

Encoded payload.

Raises YError:

If an error has occurred

std::map<std::string, std::string> encode(CodecServiceProvider &provider, std::map<std::string, std::shared_ptr<Entity>> &entity, bool pretty = false)

Perform encoding

Parameters:
  • provider – An instance of CodecServiceProvider
  • entity – A map of Entity class defined under same bundle
  • pretty – Optionally produce formatted output
Returns:

A map of encodec payload.

Raises YError:

If an error has occurred

std::shared_ptr<ydk::Entity> decode(CodecServiceProvider &provider, const std::string &payload, bool subtree = false)

Decode the payload to produce an instance of Entity.

Parameters:
  • provider – An instance of CodecServiceProvider.
  • payload – Payload to be decoded.
  • subtree – Subtree filter.
Returns:

Pointer to the decoded Entity.

Raises YError:

If an error has occurred

std::map<std::string, std::shared_ptr<Entity>> decode(CodecServiceProvider &provider, std::map<std::string, std::string> &payload_map, std::map<std::string, std::shared_ptr<Entity>> entity_map)

Decode map of payload to map of Entity.

Parameters:
  • provider – An instance of CodecServiceProvider.
  • payload_map – Module name payload map.
  • entity_map – Module name entity map.
Returns:

A std::map of the decoded Entity.

Raises YError:

If an error has occurred.

XmlSubtreeCodec

class ydk::XmlSubtreeCodec

XmlSubtreeCodec class designed to provide encoding and decoding C++ model API objects of type Entity to/from XML encoded string. Compared to CodecService the class does not validate encoded data for their types and values.

XmlSubtreeCodec()

Constructs an instance of XmlSubtreeCodec class.

std::string encode(Entity &entity, path::RootSchemaNode &root_schema)

Performs encoding of C++ model API objects of type Entity to well formatted XML encoded string.

Parameters:
  • entity – An instance of Entity class defined under a bundle.
  • root_schema – An instance of RootSchemaNode, which includes the model bundle.
Returns:

A std::string. Encoded well formatted multi-line XML payload.

Raises YServiceProviderError:
 

If an error has occurred; usually appears when model is not present in the bundle.

std::shared_ptr<ydk::Entity> decode(const std::string &payload, std::shared_ptr<Entity> entity)

Decodes the XML encoded string to produce corresponding instance of Entity.

Parameters:
  • payloadstd::string, XML encoded string to be decoded.
  • entitystd::shared_ptr<Entity>, instance of shared pointer to expected top level Entity class.
Returns:

std::shared_ptr<Entity>, shared pointer to the decoded Entity.

Raises YServiceProviderError:
 

If an error has occurred; usually appears when payload does not correspond to Entity model.

JsonSubtreeCodec

class ydk::JsonSubtreeCodec

JsonSubtreeCodec class designed to provide encoding and decoding C++ model API objects of type Entity to/from JSON encoded string. Compared to CodecService the class does not validate encoded data for their types and values.

JsonSubtreeCodec()

Constructs an instance of JsonSubtreeCodec class.

std::string encode(Entity &entity, path::RootSchemaNode &root_schema, bool pretty = true)

Performs encoding of C++ model API objects of type Entity to JSON encoded string.

Parameters:
  • entity – An instance of Entity class defined under a bundle.
  • root_schema – An instance of RootSchemaNode, which includes the model bundle.
  • prettybool. If set to true, the function produces well formatted multi-line JSON string. If set to false - one line string.
Returns:

A std::string. Encoded JSON payload.

Raises YServiceProviderError:
 

If an error has occurred; usually appears when model is not present in the bundle.

std::shared_ptr<ydk::Entity> decode(const std::string &payload, std::shared_ptr<Entity> entity)

Decodes the JSON encoded string to produce corresponding instance of Entity.

Parameters:
  • payloadstd::string, JSON encoded string to be decoded.
  • entitystd::shared_ptr<Entity>, instance of shared pointer to expected top level Entity class.
Returns:

std::shared_ptr<Entity>, shared pointer to the decoded Entity.

Raises YServiceProviderError:
 

If an error has occurred; usually appears when payload does not correspond to Entity model.

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:

#include <iostream>

#include <ydk/crud_service.hpp>
#include <ydk/gnmi_provider.hpp>
#include <ydk/json_subtree_codec.hpp>

#include <ydk_cisco_ios_xr/Cisco_IOS_XR_ifmgr_cfg.hpp>

using namespace ydk;
using namespace std;
using namespace cisco_ios_xr;

int main()
{
    auto repo = path::Repository("/home/yan/ydk-workspace/ydk-gen/scripts/repository/10.30.110.84");
    gNMIServiceProvider provider{repo, "10.30.110.84", 57400, "admin", "admin"};
    CrudService crud{};

    // Build filter to retrieve interface configuration
    auto ifcs_config = Cisco_IOS_XR_ifmgr_cfg::InterfaceConfigurations();
    auto ifc_config = make_shared<Cisco_IOS_XR_ifmgr_cfg::InterfaceConfigurations::InterfaceConfiguration>();
    ifc_config->active = "\"act\"";
    ifc_config->interface_name = "\"Loopback0\"";
    ifcs_config.interface_configuration.append(ifc_config);

    // Read interface configuration
    auto ifc_read = crud.read(provider, ifcs_config);

    // Print interface configuration
    if (ifc_read) {
        JsonSubtreeCodec jcodec{};
        auto json_payload = jcodec.encode(*ifc_read, provider.get_session().get_root_schema(), true);
        cout << "Interface Configuration:" << endl << json_payload << endl;
    }
}