Fix augmentation pointing to a grouping action's input 28/88128/2
authormiroslav.kovac <miroslav.kovac@pantheon.tech>
Thu, 27 Feb 2020 16:51:37 +0000 (17:51 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 28 Feb 2020 15:08:36 +0000 (16:08 +0100)
When augmentation targets an action input which itself is defined
in a grouping, we need to actually lookup the original definition.

Add a type aliasing information, which should be consulted when
making a simple reference to a type -- and use it in
augmentationToGenTypes().

JIRA: MDSAL-517
Change-Id: I1ef383e8777a7c09a979cbdd84f71be3a0f9b021
Signed-off-by: miroslav.kovac <miroslav.kovac@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/ModuleContext.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal517Test.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/resources/mdsal-517/bar.yang [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/resources/mdsal-517/foo.yang [new file with mode: 0644]

index 0c80e94b604fc5d7b2b9860995b23e9a17bd1722..f536e84bcdec3393482d0b197be211dc508c1ed0 100644 (file)
@@ -440,8 +440,8 @@ abstract class AbstractTypeGenerator {
                 // Original definition may live in a different module, make sure we account for that
                 final ModuleContext origContext = moduleContext(
                     orig.getPath().getPathFromRoot().iterator().next().getModule());
-                input = origContext.getChildNode(orig.getInput().getPath()).build();
-                output = origContext.getChildNode(orig.getOutput().getPath()).build();
+                input = context.addAliasType(origContext, orig.getInput(), action.getInput());
+                output = context.addAliasType(origContext, orig.getOutput(), action.getOutput());
             } else {
                 input = actionContainer(context, RPC_INPUT, action.getInput(), inGrouping);
                 output = actionContainer(context, RPC_OUTPUT, action.getOutput(), inGrouping);
@@ -834,22 +834,30 @@ abstract class AbstractTypeGenerator {
             throw new IllegalArgumentException("augment target not found: " + targetPath);
         }
 
-        GeneratedTypeBuilder targetTypeBuilder = findChildNodeByPath(targetSchemaNode.getPath());
-        if (targetTypeBuilder == null) {
-            targetTypeBuilder = findCaseByPath(targetSchemaNode.getPath());
-        }
-        if (targetTypeBuilder == null) {
-            throw new IllegalStateException("Target type not yet generated: " + targetSchemaNode);
+        if (targetSchemaNode instanceof ChoiceSchemaNode) {
+            final GeneratedTypeBuilder builder = findChildNodeByPath(targetSchemaNode.getPath());
+            checkState(builder != null, "Choice target type not generated for %s", targetSchemaNode);
+            generateTypesFromAugmentedChoiceCases(context, builder.build(), (ChoiceSchemaNode) targetSchemaNode,
+                augSchema.getChildNodes(), null, false);
+            return;
         }
 
-        if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
-            final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getIdentifier());
-            addRawAugmentGenTypeDefinition(context, targetType, augSchema, false);
-
+        final JavaTypeName targetName;
+        if (targetSchemaNode instanceof CaseSchemaNode) {
+            final GeneratedTypeBuilder builder = findCaseByPath(targetSchemaNode.getPath());
+            checkState(builder != null, "Case target type not generated for %s", targetSchemaNode);
+            targetName = builder.getIdentifier();
         } else {
-            generateTypesFromAugmentedChoiceCases(context, targetTypeBuilder.build(),
-                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), null, false);
+            final GeneratedTypeBuilder builder = findChildNodeByPath(targetSchemaNode.getPath());
+            if (builder == null) {
+                targetName = findAliasByPath(targetSchemaNode.getPath());
+                checkState(targetName != null, "Target type not yet generated: %s", targetSchemaNode);
+            } else {
+                targetName = builder.getIdentifier();
+            }
         }
+
+        addRawAugmentGenTypeDefinition(context, new ReferencedTypeImpl(targetName), augSchema, false);
     }
 
     private void usesAugmentationToGenTypes(final ModuleContext context, final AugmentationSchemaNode augSchema,
@@ -1929,6 +1937,16 @@ abstract class AbstractTypeGenerator {
         return builder;
     }
 
+    private JavaTypeName findAliasByPath(final SchemaPath path) {
+        for (final ModuleContext ctx : genCtx.values()) {
+            final JavaTypeName result = ctx.getAlias(path);
+            if (result != null) {
+                return result;
+            }
+        }
+        return null;
+    }
+
     private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
         for (final ModuleContext ctx : genCtx.values()) {
             final GeneratedTypeBuilder result = ctx.getChildNode(path);
index baa5824c6d505a6ca87aeaff6ba21ff33bc2ce0f..617189b9ce9c8af3b694ad5add045c5a57050bd5 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.mdsal.binding.generator.impl;
 
+import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.BiMap;
@@ -22,6 +23,9 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
@@ -31,6 +35,7 @@ import org.opendaylight.yangtools.concepts.Mutable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
@@ -59,6 +64,7 @@ public final class ModuleContext implements Mutable {
     private final List<GeneratedTypeBuilder> augmentations = new ArrayList<>();
     private final Multimap<Type, Type> choiceToCases = HashMultimap.create();
     private final Set<GeneratedTypeBuilder> topLevelNodes = new HashSet<>();
+    private final Map<SchemaPath, JavaTypeName> aliases = new HashMap<>();
     private final Map<Type, WithStatus> typeToSchema = new HashMap<>();
     private final List<GeneratedTOBuilder> genTOs = new ArrayList<>();
     private final Map<SchemaPath, Type> innerTypes = new HashMap<>();
@@ -151,6 +157,23 @@ public final class ModuleContext implements Mutable {
         genTOs.add(builder);
     }
 
+    @NonNull GeneratedType addAliasType(final ModuleContext sourceContext, final ContainerSchemaNode source,
+            final ContainerSchemaNode alias) {
+        final GeneratedTypeBuilder builder = sourceContext.getChildNode(source.getPath());
+        checkState(builder != null, "Could not find builder for %s", source);
+
+        final JavaTypeName id = builder.getIdentifier();
+        final SchemaPath path = alias.getPath();
+        final JavaTypeName prev = aliases.putIfAbsent(path, id);
+        checkState(prev == null, "Type aliasing conflict on %s: %s vs %s", path, prev, id);
+
+        return builder.build();
+    }
+
+    @Nullable JavaTypeName getAlias(final SchemaPath path) {
+        return aliases.get(path);
+    }
+
     public void addChildNodeType(final SchemaNode def, final GeneratedTypeBuilder builder) {
         checkNamingConflict(def, builder.getIdentifier());
         childNodes.put(def.getPath(), builder);
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal517Test.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal517Test.java
new file mode 100644 (file)
index 0000000..8054c7d
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal517Test extends AbstractOpaqueTest {
+    @Test
+    public void generateAnydataTest() {
+        final List<Type> types = new BindingGeneratorImpl().generateTypes(
+                YangParserTestUtils.parseYangResourceDirectory("/mdsal-517"));
+        assertNotNull(types);
+        assertEquals(7, types.size());
+    }
+}
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-517/bar.yang b/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-517/bar.yang
new file mode 100644 (file)
index 0000000..94c9743
--- /dev/null
@@ -0,0 +1,17 @@
+module bar {
+  yang-version 1.1;
+  namespace "bar";
+  prefix bar;
+
+  import foo {
+    prefix foo;
+  }
+
+  augment "/foo:foo-cont/foo:foo-act/foo:input" {
+    description
+      "An augmentation of a action input of module a";
+    leaf b-aug-leaf {
+      type empty;
+    }
+  }
+}
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-517/foo.yang b/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-517/foo.yang
new file mode 100644 (file)
index 0000000..b550138
--- /dev/null
@@ -0,0 +1,19 @@
+module foo {
+  yang-version 1.1;
+  namespace "foo";
+  prefix foo;
+
+  grouping foo-grp {
+    action foo-act {
+      input {
+        leaf foo-action-input-leaf {
+          type empty;
+        }
+      }
+    }
+  }
+
+  container foo-cont {
+    uses foo:foo-grp;
+  }
+}