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
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) {
}
}
-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
/// @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
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.
///
std::string const& name,
libyang::LeafBaseType const type);
-
/// @brief Delete basic value from YANG.
///
/// @param xpath The xpath of the basic value.
}
}
+ /// @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
}
}
+ /// @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_;