Fix availableNodes behavior when dealing with submodules

Before this, nodes that are part of a submodule would get returned with
the name of the submodule. However, libyang requires this type of nodes
to be prefixed with the main module.

Change-Id: I6654f0efba0f0ffffd805e7718809067bf88c5c5
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index b243fd2..96fc744 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -352,7 +352,7 @@
         } else {
             ModuleNodePair toInsert;
             if (topLevelModule.empty() || topLevelModule != node->module()->name()) {
-                toInsert.first = node->module()->name();
+                toInsert.first = node->module()->type() == 0 ? node->module()->name() : libyang::Submodule(node->module()).belongsto()->name();
             }
             toInsert.second = node->name();
             res.insert(toInsert);
diff --git a/tests/yang.cpp b/tests/yang.cpp
index 3c658b3..009465b 100644
--- a/tests/yang.cpp
+++ b/tests/yang.cpp
@@ -37,12 +37,28 @@
 }
 )";
 
+const char* included_submodule = R"(
+submodule sub-module {
+    yang-version 1.1;
+
+    belongs-to example-schema {
+        prefix sub;
+    }
+
+    leaf subLeaf {
+        type string;
+    }
+}
+)";
+
 const char* example_schema = R"(
 module example-schema {
     yang-version 1.1;
     namespace "http://example.com/example-sports";
     prefix coze;
 
+    include sub-module;
+
     identity drink {
     }
 
@@ -412,11 +428,21 @@
 
 TEST_CASE("yangschema")
 {
+    using namespace std::string_literals;
     using namespace std::string_view_literals;
     YangSchema ys;
-    ys.registerModuleCallback([]([[maybe_unused]] auto modName, auto, auto, auto) {
-        assert("example-schema"sv == modName);
-        return example_schema;
+    ys.registerModuleCallback([]([[maybe_unused]] auto modName, auto, auto subModule, auto) {
+        if (modName != "example-schema"sv) {
+            throw std::logic_error("unrecognized module "s + modName);
+        }
+        if (subModule == nullptr) {
+            return example_schema;
+        }
+        if (subModule == "sub-module"sv) {
+            return included_submodule;
+        }
+
+        throw std::logic_error("unrecognized submodule "s + subModule);
     });
     ys.addSchemaString(second_schema);
 
@@ -817,7 +843,8 @@
                         {"example-schema"s, "obsoleteLeafWithDeprecatedType"},
                         {"example-schema"s, "obsoleteLeafWithObsoleteType"},
                         {"example-schema"s, "myRpc"},
-                        {"example-schema"s, "systemStats"}};
+                        {"example-schema"s, "systemStats"},
+                        {"example-schema"s, "subLeaf"}};
                 }
 
                 SECTION("example-schema:a")
@@ -900,7 +927,8 @@
                         {"example-schema"s, "systemStats"},
                         {"example-schema"s, "twoKeyList"},
                         {"example-schema"s, "wavelength"},
-                        {"example-schema"s, "zero"}
+                        {"example-schema"s, "zero"},
+                        {"example-schema"s, "subLeaf"}
                     };
                     expectedRecursive = {
                         {boost::none, "/example-schema:_list"},
@@ -961,6 +989,7 @@
                         {boost::none, "/example-schema:portSettings/port"},
                         {boost::none, "/example-schema:systemStats"},
                         {boost::none, "/example-schema:systemStats/upTime"},
+                        {boost::none, "/example-schema:subLeaf"},
                         {boost::none, "/example-schema:twoKeyList"},
                         {boost::none, "/example-schema:twoKeyList/name"},
                         {boost::none, "/example-schema:twoKeyList/number"},
@@ -1013,6 +1042,12 @@
                 expected = yang::NodeTypes::List;
             }
 
+            SECTION("subLeaf")
+            {
+                path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("subLeaf")));
+                expected = yang::NodeTypes::Leaf;
+            }
+
             REQUIRE(ys.nodeType(pathToSchemaString(path, Prefixes::WhenNeeded)) == expected);
         }