Fix YangDataRuntimeTypes not being available 50/106750/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 29 Jun 2023 11:18:03 +0000 (13:18 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 29 Jun 2023 11:54:33 +0000 (13:54 +0200)
CompositeRuntimeType children are always indexed only through
SchemaTree, which means we need to make another pass to get at
YangDataRuntimeTypes.

JIRA: MDSAL-805
Change-Id: I68cfc7fad2c1a7cb3333e48e9b435bc681992641
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/ModuleGenerator.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/rt/DefaultModuleRuntimeType.java

index 9769b74a993827653ac52ff7921555d57e78c966..69b526e6025ead96e81650ddee308f275f251d51 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.mdsal.binding.generator.impl.reactor;
 import static com.google.common.base.Verify.verify;
 import static com.google.common.base.Verify.verifyNotNull;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import org.eclipse.jdt.annotation.NonNull;
@@ -23,6 +24,7 @@ import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.YangDataRuntimeType;
 import org.opendaylight.yangtools.yang.binding.contract.Naming;
 import org.opendaylight.yangtools.yang.common.AbstractQName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -128,7 +130,15 @@ public final class ModuleGenerator extends AbstractCompositeGenerator<ModuleEffe
             ModuleRuntimeType build(final GeneratedType type, final ModuleEffectiveStatement statement,
                     final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
                 verify(augments.isEmpty(), "Unexpected augments %s", augments);
-                return new DefaultModuleRuntimeType(type, statement, children);
+
+                final var yangDataChildren = new ArrayList<YangDataRuntimeType>();
+                for (var child : ModuleGenerator.this) {
+                    if (child instanceof YangDataGenerator yangDataGen) {
+                        yangDataGen.runtimeType().ifPresent(yangDataChildren::add);
+                    }
+                }
+
+                return new DefaultModuleRuntimeType(type, statement, children, yangDataChildren);
             }
         };
     }
index 02bba8aa7d040cce29f873cf6a489d77efa8f587..92d861832269592c358697d6b0ca7fd5523c4bc7 100644 (file)
@@ -7,8 +7,11 @@
  */
 package org.opendaylight.mdsal.binding.generator.impl.rt;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 import java.util.List;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
@@ -20,27 +23,16 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 @Beta
 public final class DefaultModuleRuntimeType extends AbstractCompositeRuntimeType<ModuleEffectiveStatement>
         implements ModuleRuntimeType {
-    private final ImmutableList<YangDataRuntimeType> yangDataChildren;
+    private final ImmutableMap<YangDataName, YangDataRuntimeType> yangDataChildren;
 
     public DefaultModuleRuntimeType(final GeneratedType bindingType, final ModuleEffectiveStatement statement,
-            final List<RuntimeType> children) {
+            final List<RuntimeType> children, final List<YangDataRuntimeType> yangDataChildren) {
         super(bindingType, statement, children);
-        yangDataChildren = children.stream()
-            .filter(YangDataRuntimeType.class::isInstance)
-            .map(YangDataRuntimeType.class::cast)
-            .collect(ImmutableList.toImmutableList());
+        this.yangDataChildren = Maps.uniqueIndex(yangDataChildren, type -> type.statement().argument());
     }
 
     @Override
     public YangDataRuntimeType yangDataChild(final YangDataName templateName) {
-        if (statement().localQNameModule().equals(templateName.module())) {
-            final var name = templateName.name();
-            for (var child : yangDataChildren) {
-                if (name.equals(child.statement().argument())) {
-                    return child;
-                }
-            }
-        }
-        return null;
+        return yangDataChildren.get(requireNonNull(templateName));
     }
 }