Replace #getPath in processInstanceIdentifierType 99/99699/3
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Fri, 11 Feb 2022 19:57:19 +0000 (20:57 +0100)
committerTomas Cere <tomas.cere@pantheon.tech>
Thu, 17 Feb 2022 10:55:34 +0000 (10:55 +0000)
DefinitionGenerator#processInstanceIdentifierType method was used to
create an example of the instance-identifier leaf node's value.
For this purpose SchemaPath#getPath method was used to search for 'root'
container.

In fact, this approach leads to incorrect results because it can for example
find an RPC definition as 'root' container. RPC definition cannot be used as
instance-identifier value because its not a data node.

We have reworked this method to use as an example the first container child
of the node's model.

This way we can also get-rid of deprecated SchemaPath#getPath method.

Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
Change-Id: I2a4f1dc2e76ae9117bc826e45b0d9c0379cb5213

restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/DefinitionGenerator.java

index 939eaf6f535107f6e3e15becaaa4a3614ea8fbc3..4b7309105436c7c0d62e94a1800fd54494b692c7 100644 (file)
@@ -56,7 +56,6 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
@@ -895,16 +894,16 @@ public class DefinitionGenerator {
 
     private static String processInstanceIdentifierType(final DataSchemaNode node, final ObjectNode property,
                                                         final EffectiveModelContext schemaContext) {
-        SchemaPath path = node.getPath();
-
-        while (path.getParent() != null && path.getParent().getPathFromRoot().iterator().hasNext()) {
-            path = path.getParent();
+        // create example instance-identifier to the first container of node's module if exists or leave it empty
+        final var module = schemaContext.findModule(node.getQName().getModule());
+        if (module.isPresent()) {
+            final var container = module.get().getChildNodes().stream()
+                    .filter(n -> n instanceof ContainerSchemaNode)
+                    .findFirst();
+            container.ifPresent(c -> setDefaultValue(property, String.format("/%s:%s", module.get().getPrefix(),
+                    c.getQName().getLocalName())));
         }
 
-        final QName rootContainer = path.getLastComponent();
-        final String rootContainerName = rootContainer.getLocalName();
-        final String prefix = schemaContext.findModule(rootContainer.getModule()).get().getPrefix();
-        setDefaultValue(property, String.format("/%s:%s", prefix, rootContainerName));
         return STRING_TYPE;
     }