From: Andrei Pavel Date: Wed, 19 Oct 2022 17:34:03 +0000 (+0300) Subject: [#2311] order methods alphabetically in the YANG translator X-Git-Tag: Kea-2.3.2~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2a0085fe4ee30ed38c77dbe8a6bfde8838b1ff24;p=thirdparty%2Fkea.git [#2311] order methods alphabetically in the YANG translator --- diff --git a/src/lib/yang/translator.cc b/src/lib/yang/translator.cc index 86aa9d6cd2..4b4c55239d 100644 --- a/src/lib/yang/translator.cc +++ b/src/lib/yang/translator.cc @@ -49,26 +49,48 @@ TranslatorBasic::TranslatorBasic(Session session, const string& model) TranslatorBasic::~TranslatorBasic() { } -ElementPtr -TranslatorBasic::value(optional data_node) { - NodeType const node_type(data_node->schema().nodeType()); - if (node_type == NodeType::Leaf || node_type == NodeType::Leaflist) { - DataNodeTerm const& leaf(data_node->asTerm()); - Value const& v(leaf.value()); - if (holds_alternative(v) || - holds_alternative(v) || - holds_alternative(v)) { - // Should be a string. Call create(). Should be slightly faster - // than wrapping value in double quotes and calling fromJSON(). - return Element::create(string(leaf.valueStr())); - } else if (holds_alternative(v)) { - return Element::create(decode64(string(leaf.valueStr()))); - } else { - // This can be various types so defer to fromJSON(). - return Element::fromJSON(string(leaf.valueStr())); +void +TranslatorBasic::checkAndGetLeaf(ElementPtr& storage, + const std::string& xpath, + const std::string& name) { + ConstElementPtr x = getItem(xpath + "/" + name); + if (x) { + storage->set(name, x); + } +} + +void TranslatorBasic::checkAndSetLeaf(ConstElementPtr const& from, + string const& xpath, + string const& name, + LeafBaseType const type) { + ConstElementPtr const& x(from->get(name)); + if (x) { + setItem(xpath + "/" + name, x, type); + } +} + +void +TranslatorBasic::delItem(const std::string& xpath) { + // TODO: Remove this if convenient. It is not strictly required and only done to detect + // missing schema nodes and throw an exception to keep old behavior. + try { + Context const& context(session_.getContext()); + context.findPath(xpath); + } catch (libyang::Error const& ex) { + isc_throw(SysrepoError, "sysrepo error getting item at '" << xpath + << "': " << ex.what()); + } + + try { + if (session_.getData(xpath)) { + session_.deleteItem(xpath); } + } catch (sysrepo::Error const& ex) { + isc_throw(SysrepoError, + "sysrepo error deleting item at '" + << xpath << "': " << ex.what()); } - return ElementPtr(); + session_.applyChanges(); } ElementPtr @@ -125,6 +147,43 @@ TranslatorBasic::getItems(const string& xpath) { return getItem(xpath); } +void +TranslatorBasic::setItem(const string& xpath, + ConstElementPtr elem, + LeafBaseType type) { + optional const s_val(value(elem, type)); + try { + session_.setItem(xpath, s_val); + } catch (sysrepo::Error const& ex) { + isc_throw(SysrepoError, + "sysrepo error setting item '" << elem->str() + << "' at '" << xpath << "': " << ex.what()); + } + session_.applyChanges(); +} + +ElementPtr +TranslatorBasic::value(optional data_node) { + NodeType const node_type(data_node->schema().nodeType()); + if (node_type == NodeType::Leaf || node_type == NodeType::Leaflist) { + DataNodeTerm const& leaf(data_node->asTerm()); + Value const& v(leaf.value()); + if (holds_alternative(v) || + holds_alternative(v) || + holds_alternative(v)) { + // Should be a string. Call create(). Should be slightly faster + // than wrapping value in double quotes and calling fromJSON(). + return Element::create(string(leaf.valueStr())); + } else if (holds_alternative(v)) { + return Element::create(decode64(string(leaf.valueStr()))); + } else { + // This can be various types so defer to fromJSON(). + return Element::fromJSON(string(leaf.valueStr())); + } + } + return ElementPtr(); +} + optional TranslatorBasic::value(ConstElementPtr const& element, LeafBaseType const type) { @@ -163,63 +222,5 @@ TranslatorBasic::value(ConstElementPtr const& element, } } -void -TranslatorBasic::setItem(const string& xpath, ConstElementPtr elem, - LeafBaseType type) { - optional const s_val(value(elem, type)); - try { - session_.setItem(xpath, s_val); - } catch (sysrepo::Error const& ex) { - isc_throw(SysrepoError, - "sysrepo error setting item '" << elem->str() - << "' at '" << xpath << "': " << ex.what()); - } - session_.applyChanges(); -} - -void -TranslatorBasic::checkAndGetLeaf(ElementPtr& storage, - const std::string& xpath, - const std::string& name) { - ConstElementPtr x = getItem(xpath + "/" + name); - if (x) { - storage->set(name, x); - } -} - -void TranslatorBasic::checkAndSetLeaf(ConstElementPtr const& from, - string const& xpath, - string const& name, - LeafBaseType const type) { - ConstElementPtr const& x(from->get(name)); - if (x) { - setItem(xpath + "/" + name, x, type); - } -} - -void -TranslatorBasic::delItem(const std::string& xpath) { - // TODO: Remove this if convenient. It is not strictly required and only done to detect - // missing schema nodes and throw an exception to keep old behavior. - try { - Context const& context(session_.getContext()); - context.findPath(xpath); - } catch (libyang::Error const& ex) { - isc_throw(SysrepoError, "sysrepo error getting item at '" << xpath - << "': " << ex.what()); - } - - try { - if (session_.getData(xpath)) { - session_.deleteItem(xpath); - } - } catch (sysrepo::Error const& ex) { - isc_throw(SysrepoError, - "sysrepo error deleting item at '" - << xpath << "': " << ex.what()); - } - session_.applyChanges(); -} - } // namespace yang } // namespace isc diff --git a/src/lib/yang/translator.h b/src/lib/yang/translator.h index 6cf521578d..d785e0f249 100644 --- a/src/lib/yang/translator.h +++ b/src/lib/yang/translator.h @@ -28,33 +28,6 @@ public: /// @brief Destructor. virtual ~TranslatorBasic(); - /// @brief Translate basic value from YANG to JSON. - /// - /// @note Please don't use this outside tests. - /// - /// @param s_val The value. - /// @return The Element representing the sysrepo value. - /// @throw NotImplemented when the value type is not supported. - static isc::data::ElementPtr value(std::optional s_val); - - /// @brief Get and translate basic value from YANG to JSON. - /// - /// @note Should be const as it is read only... - /// - /// @param xpath The xpath of the basic value. - /// @return The Element representing the item at xpath or null - /// when not found. - /// @throw SysrepoError when sysrepo raises an error. - /// @throw NotImplemented when the value type is not supported. - isc::data::ElementPtr getItem(const std::string& xpath); - - /// @brief Get and translate a list of basic values from YANG to JSON. - /// - /// @param xpath The xpath of the list of basic values. - /// @return The ListElement representing the leaf-list at xpath or - /// null when not found. - isc::data::ElementPtr getItems(const std::string& xpath); - /// @brief Retrieves an item and stores it in the specified storage. /// /// This will attempt to retrieve an item and, if exists, will @@ -67,24 +40,6 @@ public: const std::string& xpath, const std::string& name); - /// @brief Translate basic value from JSON to YANG. - /// - /// @note Please don't use this outside tests. - /// - /// @param elem The JSON element. - /// @param type The sysrepo type. - static std::optional value(isc::data::ConstElementPtr const& elem, - libyang::LeafBaseType const type); - - /// @brief Translate and set basic value from JSON to YANG. - /// - /// @param xpath The xpath of the basic value. - /// @param elem The JSON element. - /// @param type The sysrepo type. - void setItem(const std::string& xpath, isc::data::ConstElementPtr elem, - libyang::LeafBaseType type); - - /// @brief Get an element from given ElementPtr node and set it in sysrepo /// at given xpath. /// @@ -98,7 +53,6 @@ public: std::string const& name, libyang::LeafBaseType const type); - /// @brief Delete basic value from YANG. /// /// @param xpath The xpath of the basic value. @@ -125,6 +79,24 @@ public: } } + /// @brief Get and translate basic value from YANG to JSON. + /// + /// @note Should be const as it is read only... + /// + /// @param xpath The xpath of the basic value. + /// @return The Element representing the item at xpath or null + /// when not found. + /// @throw SysrepoError when sysrepo raises an error. + /// @throw NotImplemented when the value type is not supported. + isc::data::ElementPtr getItem(const std::string& xpath); + + /// @brief Get and translate a list of basic values from YANG to JSON. + /// + /// @param xpath The xpath of the list of basic values. + /// @return The ListElement representing the leaf-list at xpath or + /// null when not found. + isc::data::ElementPtr getItems(const std::string& xpath); + /// @brief Retrieve a list as ElementPtr from sysrepo from a certain xpath. /// /// @tparam T typename of the translator that holds the function that will @@ -160,6 +132,33 @@ public: } } + /// @brief Translate and set basic value from JSON to YANG. + /// + /// @param xpath The xpath of the basic value. + /// @param elem The JSON element. + /// @param type The sysrepo type. + void setItem(const std::string& xpath, + isc::data::ConstElementPtr elem, + libyang::LeafBaseType type); + + /// @brief Translate basic value from YANG to JSON. + /// + /// @note Please don't use this outside tests. + /// + /// @param s_val The value. + /// @return The Element representing the sysrepo value. + /// @throw NotImplemented when the value type is not supported. + static isc::data::ElementPtr value(std::optional s_val); + + /// @brief Translate basic value from JSON to YANG. + /// + /// @note Please don't use this outside tests. + /// + /// @param elem The JSON element. + /// @param type The sysrepo type. + static std::optional value(isc::data::ConstElementPtr const& elem, + libyang::LeafBaseType const type); + protected: /// @brief The sysrepo session. sysrepo::Session session_;