|
| 1 | +From b27a65d5d5f6ffffbd2c26c1b5bea57a6de584d0 Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= < [email protected]> |
| 3 | +Date: Mon, 20 Oct 2025 18:46:38 +0200 |
| 4 | +Subject: [PATCH 3/7] wrap lyd_validate_op |
| 5 | +MIME-Version: 1.0 |
| 6 | +Content-Type: text/plain; charset=UTF-8 |
| 7 | +Content-Transfer-Encoding: 8bit |
| 8 | +Organization: Wires |
| 9 | + |
| 10 | +This patch wraps lyd_validate_op from libyang. Downstream users can now |
| 11 | +validate operations (RPC input, replies, and notifications). |
| 12 | + |
| 13 | +Change-Id: Ib03070bbc3e1d0dccd6bf38eda82e944db1093b1 |
| 14 | +Signed-off-by: Mattias Walström < [email protected]> |
| 15 | +--- |
| 16 | + include/libyang-cpp/DataNode.hpp | 2 + |
| 17 | + src/DataNode.cpp | 24 +++++++++++ |
| 18 | + tests/context.cpp | 2 +- |
| 19 | + tests/data_node.cpp | 72 ++++++++++++++++++++++++++++++++ |
| 20 | + tests/example_schema.hpp | 22 ++++++++++ |
| 21 | + 5 files changed, 121 insertions(+), 1 deletion(-) |
| 22 | + |
| 23 | +diff --git a/include/libyang-cpp/DataNode.hpp b/include/libyang-cpp/DataNode.hpp |
| 24 | +index 50d6c0e..e202a96 100644 |
| 25 | +--- a/include/libyang-cpp/DataNode.hpp |
| 26 | ++++ b/include/libyang-cpp/DataNode.hpp |
| 27 | +@@ -59,6 +59,7 @@ template <typename Operation, typename Siblings> |
| 28 | + void handleLyTreeOperation(DataNode* affectedNode, Operation operation, Siblings siblings, std::shared_ptr<internal_refcount> newRefs); |
| 29 | + |
| 30 | + LIBYANG_CPP_EXPORT void validateAll(std::optional<libyang::DataNode>& node, const std::optional<ValidationOptions>& opts = std::nullopt); |
| 31 | ++LIBYANG_CPP_EXPORT void validateOp(libyang::DataNode& input, const std::optional<libyang::DataNode>& opsTree, OperationType opType); |
| 32 | + |
| 33 | + LIBYANG_CPP_EXPORT Set<DataNode> findXPathAt( |
| 34 | + const std::optional<libyang::DataNode>& contextNode, |
| 35 | +@@ -147,6 +148,7 @@ public: |
| 36 | + friend LIBYANG_CPP_EXPORT lyd_node* getRawNode(DataNode node); |
| 37 | + |
| 38 | + friend LIBYANG_CPP_EXPORT void validateAll(std::optional<libyang::DataNode>& node, const std::optional<ValidationOptions>& opts); |
| 39 | ++ friend LIBYANG_CPP_EXPORT void validateOp(libyang::DataNode& input, const std::optional<libyang::DataNode>& opsTree, OperationType opType); |
| 40 | + friend LIBYANG_CPP_EXPORT Set<DataNode> findXPathAt(const std::optional<libyang::DataNode>& contextNode, const libyang::DataNode& forest, const std::string& xpath); |
| 41 | + |
| 42 | + bool operator==(const DataNode& node) const; |
| 43 | +diff --git a/src/DataNode.cpp b/src/DataNode.cpp |
| 44 | +index 7e87917..e28ef30 100644 |
| 45 | +--- a/src/DataNode.cpp |
| 46 | ++++ b/src/DataNode.cpp |
| 47 | +@@ -1226,6 +1226,30 @@ void validateAll(std::optional<libyang::DataNode>& node, const std::optional<Val |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | ++/** @brief Validate op after parsing with lyd_parse_op. |
| 52 | ++ * |
| 53 | ++ * Wraps `lyd_validate_op`. |
| 54 | ++ * |
| 55 | ++ * @param input The tree with the op to validate. |
| 56 | ++ * @param opsTree The optional data tree to validate the input against. |
| 57 | ++ * @param opType The operation type. Contrary to `lyd_validate_op`, we accept not only YANG but also NETCONF and RESTCONF operation types (and internally convert them to YANG). |
| 58 | ++ */ |
| 59 | ++void validateOp(libyang::DataNode& input, const std::optional<libyang::DataNode>& opsTree, OperationType opType) |
| 60 | ++{ |
| 61 | ++ if (opType == OperationType::RpcYang || opType == OperationType::RpcRestconf || opType == OperationType::RpcNetconf) { |
| 62 | ++ opType = OperationType::RpcYang; |
| 63 | ++ } else if (opType == OperationType::ReplyYang || opType == OperationType::ReplyRestconf || opType == OperationType::ReplyNetconf) { |
| 64 | ++ opType = OperationType::ReplyYang; |
| 65 | ++ } else if (opType == OperationType::NotificationYang || opType == OperationType::NotificationRestconf || opType == OperationType::NotificationNetconf) { |
| 66 | ++ opType = OperationType::NotificationYang; |
| 67 | ++ } else { |
| 68 | ++ throw Error("validateOp: DataYang datatype is not supported"); |
| 69 | ++ } |
| 70 | ++ |
| 71 | ++ auto ret = lyd_validate_op(input.m_node, opsTree ? opsTree->m_node : nullptr, utils::toOpType(opType), nullptr); |
| 72 | ++ throwIfError(ret, "libyang:validateOp: lyd_validate_op failed"); |
| 73 | ++} |
| 74 | ++ |
| 75 | + /** @short Find instances matching the provided XPath |
| 76 | + * |
| 77 | + * @param contextNode The node which serves as the "context node" for XPath evaluation. Use nullopt to start at root. |
| 78 | +diff --git a/tests/context.cpp b/tests/context.cpp |
| 79 | +index 6c5dde8..c0b7e09 100644 |
| 80 | +--- a/tests/context.cpp |
| 81 | ++++ b/tests/context.cpp |
| 82 | +@@ -154,7 +154,7 @@ TEST_CASE("context") |
| 83 | + { |
| 84 | + auto mod = ctx->parseModule(example_schema, libyang::SchemaFormat::YANG); |
| 85 | + auto rpcs = mod.actionRpcs(); |
| 86 | +- REQUIRE(rpcs.size() == 1); |
| 87 | ++ REQUIRE(rpcs.size() == 2); |
| 88 | + REQUIRE(rpcs[0].module().name() == "example-schema"); |
| 89 | + REQUIRE(rpcs[0].name() == "myRpc"); |
| 90 | + |
| 91 | +diff --git a/tests/data_node.cpp b/tests/data_node.cpp |
| 92 | +index db5a28e..9215b12 100644 |
| 93 | +--- a/tests/data_node.cpp |
| 94 | ++++ b/tests/data_node.cpp |
| 95 | +@@ -2430,6 +2430,78 @@ TEST_CASE("Data Node manipulation") |
| 96 | + "Can't parse into operation data tree: LY_EVALID", libyang::Error); |
| 97 | + } |
| 98 | + } |
| 99 | ++ |
| 100 | ++ DOCTEST_SUBCASE("Validation") |
| 101 | ++ { |
| 102 | ++ DOCTEST_SUBCASE("Valid input") |
| 103 | ++ { |
| 104 | ++ std::string rpcInput; |
| 105 | ++ std::string rpcPath; |
| 106 | ++ std::string expected; |
| 107 | ++ std::optional<libyang::DataNode> depTree; |
| 108 | ++ |
| 109 | ++ DOCTEST_SUBCASE("RPC") |
| 110 | ++ { |
| 111 | ++ rpcInput = R"({"example-schema:input": { "number": 42 } })"; |
| 112 | ++ rpcPath = "/example-schema:rpc-with-choice"; |
| 113 | ++ expected = R"({ |
| 114 | ++ "example-schema:rpc-with-choice": { |
| 115 | ++ "number": 42 |
| 116 | ++ } |
| 117 | ++} |
| 118 | ++)"; |
| 119 | ++ } |
| 120 | ++ |
| 121 | ++ DOCTEST_SUBCASE("Action") |
| 122 | ++ { |
| 123 | ++ rpcInput = R"({ "example-schema:input": { "friend": "Kuba" } })"; |
| 124 | ++ rpcPath = "/example-schema:person[name='Franta']/poke-a-friend"; |
| 125 | ++ expected = R"({ |
| 126 | ++ "example-schema:poke-a-friend": { |
| 127 | ++ "friend": "Kuba" |
| 128 | ++ } |
| 129 | ++} |
| 130 | ++)"; |
| 131 | ++ depTree = ctx.newPath("/example-schema:person[name='Kuba']"); |
| 132 | ++ } |
| 133 | ++ |
| 134 | ++ auto [parent, rpcTree] = ctx.newPath2(rpcPath); |
| 135 | ++ auto rpcOp = rpcTree->parseOp(rpcInput, dataTypeFor(rpcInput), libyang::OperationType::RpcRestconf); |
| 136 | ++ |
| 137 | ++ REQUIRE(!rpcOp.op); |
| 138 | ++ REQUIRE(rpcOp.tree); |
| 139 | ++ |
| 140 | ++ libyang::validateOp(*rpcTree, depTree, libyang::OperationType::RpcRestconf); |
| 141 | ++ REQUIRE(*rpcTree->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::KeepEmptyCont) == expected); |
| 142 | ++ } |
| 143 | ++ |
| 144 | ++ DOCTEST_SUBCASE("Nodes in disjunctive cases defined together") |
| 145 | ++ { |
| 146 | ++ auto rpcInput = R"({ "example-schema:input": { "number": 42, "text": "The ultimate answer" } })"; |
| 147 | ++ |
| 148 | ++ auto rpcTree = ctx.newPath("/example-schema:rpc-with-choice"); |
| 149 | ++ auto rpcOp = rpcTree.parseOp(rpcInput, dataTypeFor(rpcInput), libyang::OperationType::RpcRestconf); |
| 150 | ++ |
| 151 | ++ REQUIRE(!rpcOp.op); |
| 152 | ++ REQUIRE(rpcOp.tree); |
| 153 | ++ |
| 154 | ++ REQUIRE_THROWS_WITH_AS(libyang::validateOp(rpcTree, std::nullopt, libyang::OperationType::RpcRestconf), "libyang:validateOp: lyd_validate_op failed: LY_EVALID", libyang::Error); |
| 155 | ++ } |
| 156 | ++ |
| 157 | ++ DOCTEST_SUBCASE("Action without the leafref node") |
| 158 | ++ { |
| 159 | ++ auto rpcInput = R"({ "example-schema:input": { "friend": "Kuba" } })"; |
| 160 | ++ auto rpcPath = "/example-schema:person[name='Franta']/poke-a-friend"; |
| 161 | ++ |
| 162 | ++ auto [parent, rpcTree] = ctx.newPath2(rpcPath); |
| 163 | ++ auto rpcOp = rpcTree->parseOp(rpcInput, dataTypeFor(rpcInput), libyang::OperationType::RpcRestconf); |
| 164 | ++ |
| 165 | ++ REQUIRE(!rpcOp.op); |
| 166 | ++ REQUIRE(rpcOp.tree); |
| 167 | ++ |
| 168 | ++ REQUIRE_THROWS_WITH_AS(libyang::validateOp(*rpcTree, std::nullopt, libyang::OperationType::RpcRestconf), "libyang:validateOp: lyd_validate_op failed: LY_EVALID", libyang::Error); |
| 169 | ++ } |
| 170 | ++ } |
| 171 | + } |
| 172 | + |
| 173 | + DOCTEST_SUBCASE("comparing") { |
| 174 | +diff --git a/tests/example_schema.hpp b/tests/example_schema.hpp |
| 175 | +index 02d3d74..acc6ecc 100644 |
| 176 | +--- a/tests/example_schema.hpp |
| 177 | ++++ b/tests/example_schema.hpp |
| 178 | +@@ -108,6 +108,16 @@ module example-schema { |
| 179 | + } |
| 180 | + |
| 181 | + action poke { } |
| 182 | ++ |
| 183 | ++ action poke-a-friend { |
| 184 | ++ input { |
| 185 | ++ leaf friend { |
| 186 | ++ type leafref { |
| 187 | ++ path '../../../person/name'; |
| 188 | ++ } |
| 189 | ++ } |
| 190 | ++ } |
| 191 | ++ } |
| 192 | + } |
| 193 | + |
| 194 | + leaf bossPerson { |
| 195 | +@@ -284,6 +294,18 @@ module example-schema { |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | ++ rpc rpc-with-choice { |
| 200 | ++ input { |
| 201 | ++ choice the-impossible-choice { |
| 202 | ++ leaf text { |
| 203 | ++ type string; |
| 204 | ++ } |
| 205 | ++ leaf number { |
| 206 | ++ type int32; |
| 207 | ++ } |
| 208 | ++ } |
| 209 | ++ } |
| 210 | ++ } |
| 211 | + |
| 212 | + anydata myData { |
| 213 | + } |
| 214 | +-- |
| 215 | +2.43.0 |
| 216 | + |
0 commit comments