I'm trying to define a container in a sub module as below
submodule sub-module {
belongs-to main-module{
prefix "sub";
}
grouping my-container-group {
container my-container {
leaf id {
type string;
}
leaf value {
type string;
}
}
}
}
and use this container in the main-module to create a list as below
module main-module {
namespace "http://example-ns";
prefix "sub";
include sub-module;
list my-list {
key id;
uses my-container-group;
}
}
But, the pyang throws an error as below
test# pyang main-module.yang -f tree
main-module.yang:10: error: the key "id" does not reference an existing leaf
module: main-module
+--rw my-list* [id]
+--rw my-container
+--rw id? string
+--rw value? string
What am I doing wrong here?
List's keys must be children of that list. It is not enough for them to be just descendants. You have a container in between; a relative schema node identifier for leaf "id" in the context of list "my-list" would be "my-container/id".
RFC 6020, Section 7.8.2. The list's key Statement
The "key" statement, which MUST be present if the list represents configuration, and MAY be present otherwise, takes as an argument a string that specifies a space-separated list of leaf identifiers of this list. A leaf identifier MUST NOT appear more than once in the key. Each such leaf identifier MUST refer to a child leaf of the list. The leafs can be defined directly in substatements to the list, or in groupings used in the list.
Additionally, the text above requires a space separated list of identifiers as an argument to this statement, not a list of schema node identifiers (like in case of the "unique" statement).
The same applies with YANG 1.1, RFC 7950.
You can refactor that grouping into two groupings and use the one that defines proper contents:
submodule sub-module {
belongs-to main-module{
prefix "sub";
}
grouping my-container-group {
container my-container {
uses my-id-and-value-group;
}
}
grouping my-id-and-value-group {
leaf id {
type string;
}
leaf value {
type string;
}
}
}
module main-module {
namespace "http://example-ns";
prefix "sub";
include sub-module;
list my-list {
key id;
uses my-id-and-value-group;
}
}