Kea 2.7.6
isc::yang::TranslatorHost Class Reference

Translation between YANG and JSON for a single host reservation. More...

#include <translator_host.h>

+ Inheritance diagram for isc::yang::TranslatorHost:

Public Member Functions

 TranslatorHost (sysrepo::Session session, const std::string &model)
 Constructor.
 
virtual ~TranslatorHost ()=default
 Destructor.
 
isc::data::ElementPtr getHost (libyang::DataNode const &data_node)
 Translate a host reservation from YANG to JSON.
 
isc::data::ElementPtr getHostFromAbsoluteXpath (std::string const &xpath)
 Translate a host reservation from YANG to JSON.
 
void setHost (const std::string &xpath, isc::data::ConstElementPtr elem)
 Translate and set host reservation from JSON to YANG.
 
- Public Member Functions inherited from isc::yang::TranslatorOptionDataList
 TranslatorOptionDataList (sysrepo::Session session, const std::string &model)
 Constructor.
 
virtual ~TranslatorOptionDataList ()=default
 Destructor.
 
isc::data::ConstElementPtr getOptionDataList (libyang::DataNode const &data_node)
 Translate option data list from YANG to JSON.
 
isc::data::ConstElementPtr getOptionDataListFromAbsoluteXpath (std::string const &xpath)
 Translate option data list from YANG to JSON.
 
void setOptionDataList (const std::string &xpath, isc::data::ConstElementPtr elem)
 Translate and set option data list from JSON to YANG.
 
- Public Member Functions inherited from isc::yang::TranslatorOptionData
 TranslatorOptionData (sysrepo::Session session, const std::string &model)
 Constructor.
 
virtual ~TranslatorOptionData ()=default
 Destructor.
 
isc::data::ElementPtr getOptionData (libyang::DataNode const &data_node)
 Translate an option data from YANG to JSON.
 
isc::data::ElementPtr getOptionDataFromAbsoluteXpath (std::string const &xpath)
 Translate an option data from YANG to JSON.
 
void setOptionData (const std::string &xpath, isc::data::ConstElementPtr elem)
 Translate and set option data from JSON to YANG.
 
- Public Member Functions inherited from isc::yang::Translator
 Translator (sysrepo::Session session, const std::string &model)
 Constructor.
 
virtual ~Translator ()=default
 Destructor.
 
template<typename T >
void checkAndGet (isc::data::ElementPtr const &storage, libyang::DataNode const &data_node, std::string const &xpath, T translate) const
 Calls {translate} for the element found at {xpath} relative to {data_node} and sets the result in {storage} with the {xpath} key.
 
void checkAndGetAndJsonifyLeaf (isc::data::ElementPtr &storage, libyang::DataNode const &data_node, const std::string &name) const
 Retrieves a child YANG data node identified by name from the given parent YANG container node, converts it from string to JSON and stores it in the specified storage.
 
template<typename T >
void checkAndGetDiverging (isc::data::ElementPtr const &storage, libyang::DataNode const &data_node, std::string const &key, std::string const &xpath, T translate) const
 Calls {translate} for the element found at {xpath} relative to {data_node} and sets the result in {storage} with the {key} key.
 
void checkAndGetDivergingLeaf (isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name, std::string const &yang_name) const
 Retrieves a child YANG data node identified by name from the given parent YANG container node and stores it in the specified storage.
 
void checkAndGetLeaf (isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
 Retrieves a child YANG data node identified by name from the given parent YANG container node and stores it in the specified storage.
 
void checkAndSetDivergingLeaf (isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, std::string const &yang_name, libyang::LeafBaseType const type)
 Get an element from given ElementPtr node and set it in sysrepo at given xpath.
 
void checkAndSetLeaf (isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
 Get an element from given ElementPtr node and set it in sysrepo at given xpath.
 
void checkAndSetLeafList (isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
 Get an element from given ElementPtr node and set it in sysrepo at given xpath as a leaf-list.
 
void checkAndSetUserContext (isc::data::ConstElementPtr const &from, std::string const &xpath)
 Get an element from given ElementPtr node and set it in sysrepo at given xpath.
 
void checkAndStringifyAndSetLeaf (isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name)
 Get an element from given ElementPtr node and set it in sysrepo at given xpath.
 
void deleteItem (const std::string &xpath)
 Delete basic value from YANG.
 
libyang::DataNode findXPath (std::string const &xpath) const
 Retrieves a YANG data node by xpath.
 
template<typename functor_t >
void forAll (std::string const &xpath, functor_t f) const
 Run a function for a node and all its children.
 
std::optional< libyang::DataNode > getData (std::string const &xpath) const
 Get a YANG data node found at the given absolute xpath.
 
isc::data::ElementPtr getItem (libyang::DataNode const &data_node, std::string const &xpath) const
 Translate a basic value from YANG to JSON for a given xpath that is relative to the given source node.
 
isc::data::ElementPtr getItemFromAbsoluteXpath (std::string const &xpath) const
 Translate a basic value from YANG to JSON for a given absolute xpath.
 
template<typename T >
isc::data::ElementPtr getList (libyang::DataNode const &data_node, std::string const &xpath, T &t, isc::data::ElementPtr(T::*f)(libyang::DataNode const &)) const
 Retrieve a list as ElementPtr from sysrepo from a certain xpath.
 
void getMandatoryDivergingLeaf (isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name, std::string const &yang_name) const
 Retrieves a child YANG data node identified by one name from the given parent YANG container node and stores it in the specified storage under a different name.
 
void getMandatoryLeaf (isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
 Retrieves a child YANG data node identified by name from the given parent YANG container node and stores it in the specified storage.
 
bool schemaNodeExists (std::string const &xpath) const
 Checks whether a YANG node exists in the schema.
 
void setItem (const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
 Translate and set basic value from JSON to YANG.
 
void setMandatoryDivergingLeaf (isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, std::string const &yang_name, libyang::LeafBaseType const type)
 Get an element from given ElementPtr node and set it in sysrepo at given xpath.
 
void setMandatoryLeaf (isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
 Get an element from given ElementPtr node and set it in sysrepo at given xpath.
 

Protected Member Functions

isc::data::ElementPtr getHostKea (libyang::DataNode const &data_node)
 getHost for kea-dhcp[46]-server models.
 
void setHostKea (const std::string &xpath, isc::data::ConstElementPtr elem)
 setHost for kea-dhcp[46]-server models.
 
- Protected Member Functions inherited from isc::yang::TranslatorOptionDataList
isc::data::ConstElementPtr getOptionDataListKea (libyang::DataNode const &data_node)
 getOptionDataList for kea-dhcp[46].
 
void setOptionDataListKea (const std::string &xpath, isc::data::ConstElementPtr elem)
 setOptionDataList for kea-dhcp[46].
 
- Protected Member Functions inherited from isc::yang::TranslatorOptionData
isc::data::ElementPtr getOptionDataKea (libyang::DataNode const &data_node)
 getOptionData JSON for kea-dhcp[46].
 
void setOptionDataKea (const std::string &xpath, isc::data::ConstElementPtr elem)
 setOptionData for kea-dhcp[46].
 

Additional Inherited Members

- Static Public Member Functions inherited from isc::yang::Translator
static isc::data::ElementPtr translateFromYang (std::optional< libyang::DataNode > data_node)
 Translate basic value from the given YANG data node to JSON element.
 
static std::optional< std::string > translateToYang (isc::data::ConstElementPtr const &elem, libyang::LeafBaseType const type)
 Translate basic value from JSON to YANG.
 
- Protected Types inherited from isc::yang::Translator
using Deserializer
 Maps YANG types to functions that transform a YANG type into an ElementPtr.
 
using Serializer
 Maps YANG types to functions that transform the string representation of an Element into a string that can be passed into Session::setItem().
 
- Static Protected Member Functions inherited from isc::yang::Translator
static std::string decode64 (std::string const &input)
 Decode a YANG element of binary type to a string that can be stored in an Element::string JSON.
 
static std::string encode64 (std::string const &input)
 Encode a string such that it can be stored in a YANG element of binary type.
 
static Deserializer initializeDeserializer ()
 Initializes the deserializer which is used to translate a YANG node to an ElementPtr.
 
static Serializer initializeSerializer ()
 Initializes the serializer which is used to translate the string value of an Element to a string that can be passed into Session::setItem().
 
- Protected Attributes inherited from isc::yang::Translator
std::string model_
 The model.
 
sysrepo::Session session_
 The sysrepo session.
 

Detailed Description

Translation between YANG and JSON for a single host reservation.

JSON syntax for kea-dhcp4 is:

{
"hw-address": <hardware address>,
"duid": <duid>,
"circuit-id": <circuit id>,
"client-id": <client id>,
"flex-id": <flex id>,
"ip-address": <ipv4 reserved address>,
"hostname": <hostname>,
"next-server": "<next server>",
"server-hostname": "<server hostname>",
"boot-file-name": "<boot file name>",
"client-classes": "<client class names>",
"option-data": [ <list of option data> ],
"user-context": { <json map> },
"comment": "<comment>"
}

JSON syntax for kea-dhcp6 is:

{
"hw-address": <hardware address>,
"duid": <duid>,
"flex-id": <flex id>,
"ip-addresses": <ipv6 reserved addresses>,
"prefixes": <ipv6 reserved prefixes>,
"hostname": <hostname>,
"client-classes": "<client class names>",
"option-data": [ <list of option data> ],
"user-context": { <json map> },
"comment": "<comment>"
}

YANG syntax for kea-dhcp[46] is with identifier-type and identifier as the list keys:

+--rw identifier-type host-identifier-type
+--rw identifier string
+--rw hostname? string
+--rw client-classes* string
+--rw option-data* [code space]
+--rw user-context? user-context
DHCPv4 only:
+--rw ip-address? inet:ipv4-address
+--rw next-server? inet:ipv4-address
+--rw server-hostname? string
+--rw boot-file-name? string
DHCPv6 only:
+--rw ip-addresses* inet:ipv6-address
+--rw prefixes* inet:ipv6-prefix

An example in JSON and YANG formats:

[
{
"flex-id": "00:ff",
"ip-address": "10.0.0.1",
"hostname": "foo"
}
]
/kea-dhcp4-server:config (container)
/kea-dhcp4-server:config/subnet4[id='111'] (list instance)
/kea-dhcp4-server:config/subnet4[id='111']/id = 111
/kea-dhcp4-server:config/subnet4[id='111']/subnet = 10.0.0.0/24
/kea-dhcp4-server:config/subnet4[id='111']/
host[identifier-type='flex-id'][identifier='00:ff'] (list instance)
/kea-dhcp4-server:config/subnet4[id='111']/
host[identifier-type='flex-id'][identifier='00:ff']/
identifier-type = flex-id
/kea-dhcp4-server:config/subnet4[id='111']/
host[identifier-type='flex-id'][identifier='00:ff']/
identifier = 00:ff
/kea-dhcp4-server:config/subnet4[id='111']/
host[identifier-type='flex-id'][identifier='00:ff']/
hostname = foo
/kea-dhcp4-server:config/subnet4[id='111']/
host[identifier-type='flex-id'][identifier='00:ff']/
ip-address = 10.0.0.1

A translator class for converting a host reservation between YANG and JSON.

Currently supported models are:

  • kea-dhcp4-server
  • kea-dhcp6-server

ietf-dhcpv6-server is not supported yet.

Definition at line 113 of file translator_host.h.

Constructor & Destructor Documentation

◆ TranslatorHost()

isc::yang::TranslatorHost::TranslatorHost ( sysrepo::Session session,
const std::string & model )

Constructor.

Parameters
sessionSysrepo session.
modelModel name.

Definition at line 22 of file translator_host.cc.

◆ ~TranslatorHost()

virtual isc::yang::TranslatorHost::~TranslatorHost ( )
virtualdefault

Destructor.

Member Function Documentation

◆ getHost()

ElementPtr isc::yang::TranslatorHost::getHost ( libyang::DataNode const & data_node)

Translate a host reservation from YANG to JSON.

Parameters
data_nodethe YANG node representing the host reservation
Returns
the JSON representation of the host reservation
Exceptions
NetconfErrorwhen sysrepo raises an error.

Definition at line 29 of file translator_host.cc.

References getHostKea(), isc_throw, isc::yang::Translator::model_, and isc::Exception::what().

Referenced by getHostFromAbsoluteXpath(), and isc::yang::TranslatorHosts::getHosts().

+ Here is the call graph for this function:

◆ getHostFromAbsoluteXpath()

ElementPtr isc::yang::TranslatorHost::getHostFromAbsoluteXpath ( std::string const & xpath)

Translate a host reservation from YANG to JSON.

Note
This is a computationally expensive operation that makes a lookup in the sysrepo datastore by calling Session::getData(). It should be used sparingly in production code, mainly to get an initial data node to work with. It may be used at will in unit tests. Use getHost(libyang::DataNode) as a scalable alternative.
Parameters
xpathThe xpath of the host reservation.
Returns
JSON representation of the host reservation.
Exceptions
NetconfErrorwhen sysrepo raises an error.

Definition at line 45 of file translator_host.cc.

References isc::yang::Translator::findXPath(), and getHost().

+ Here is the call graph for this function:

◆ getHostKea()

ElementPtr isc::yang::TranslatorHost::getHostKea ( libyang::DataNode const & data_node)
protected

getHost for kea-dhcp[46]-server models.

Parameters
data_nodethe YANG node representing the host reservation
Returns
JSON representation of the host reservation.

Definition at line 54 of file translator_host.cc.

References isc::yang::Translator::checkAndGetAndJsonifyLeaf(), isc::yang::Translator::checkAndGetLeaf(), isc::data::Element::createMap(), isc::yang::Translator::getItem(), isc::yang::TranslatorOptionDataList::getOptionDataList(), isc_throw, and isc::yang::Translator::model_.

Referenced by getHost().

+ Here is the call graph for this function:

◆ setHost()

void isc::yang::TranslatorHost::setHost ( const std::string & xpath,
isc::data::ConstElementPtr elem )

Translate and set host reservation from JSON to YANG.

Parameters
xpathThe xpath of the host reservation.
elemThe JSON element.

Definition at line 87 of file translator_host.cc.

References isc_throw, isc::yang::Translator::model_, setHostKea(), and isc::Exception::what().

Referenced by isc::yang::TranslatorHosts::setHostsKea().

+ Here is the call graph for this function:

◆ setHostKea()

void isc::yang::TranslatorHost::setHostKea ( const std::string & xpath,
isc::data::ConstElementPtr elem )
protected

setHost for kea-dhcp[46]-server models.

Parameters
xpathThe xpath of the host reservation.
elemThe JSON element.

Definition at line 104 of file translator_host.cc.

References isc::yang::Translator::checkAndSetLeaf(), isc::yang::Translator::checkAndSetLeafList(), isc::yang::Translator::checkAndSetUserContext(), isc::yang::Translator::model_, isc::yang::Translator::setItem(), and isc::yang::TranslatorOptionDataList::setOptionDataList().

Referenced by setHost().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: