]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2311] order methods alphabetically in the YANG translator
authorAndrei Pavel <andrei@isc.org>
Wed, 19 Oct 2022 17:34:03 +0000 (20:34 +0300)
committerAndrei Pavel <andrei@isc.org>
Fri, 21 Oct 2022 14:58:48 +0000 (17:58 +0300)
src/lib/yang/translator.cc
src/lib/yang/translator.h

index 86aa9d6cd20bc779952f97a39f9f6b59dc27c661..4b4c55239d044a4a42d0fb5896ece28c98e11fc5 100644 (file)
@@ -49,26 +49,48 @@ TranslatorBasic::TranslatorBasic(Session session, const string& model)
 TranslatorBasic::~TranslatorBasic() {
 }
 
-ElementPtr
-TranslatorBasic::value(optional<DataNode> 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<string>(v) ||
-            holds_alternative<Enum>(v) ||
-            holds_alternative<IdentityRef>(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<Binary>(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<string> 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<DataNode> 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<string>(v) ||
+            holds_alternative<Enum>(v) ||
+            holds_alternative<IdentityRef>(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<Binary>(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<string>
 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<string> 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
index 6cf521578da1da02be4deaaf95ecab4decbbc32e..d785e0f249423cb72943baffbbe80ef0b7bdd9b9 100644 (file)
@@ -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<libyang::DataNode> 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<std::string> 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<libyang::DataNode> 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<std::string> value(isc::data::ConstElementPtr const& elem,
+                                            libyang::LeafBaseType const type);
+
 protected:
     /// @brief The sysrepo session.
     sysrepo::Session session_;