Merge "Fix duplicate artifact inclusion"
authorTony Tkacik <ttkacik@cisco.com>
Mon, 14 Sep 2015 07:51:03 +0000 (07:51 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 14 Sep 2015 07:51:03 +0000 (07:51 +0000)
binding/binding-parent/pom.xml
binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/Bug4145Test.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/resources/bug-4145/foo.yang [new file with mode: 0644]
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java
binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/BindingMapping.java

index 8757287c9e9bf988e74921914e2910fe1962f752..e34ca06c7622bc8d4133aa7a4373c3a86e0a6df0 100644 (file)
@@ -25,6 +25,7 @@
     <properties>
         <yangtools.version>0.8.0-SNAPSHOT</yangtools.version>
         <mdsal.version>2.0.0-SNAPSHOT</mdsal.version>
+        <mdsalmodel.version>0.8.0-SNAPSHOT</mdsalmodel.version>
         <salGeneratorPath>src/main/yang-gen-sal</salGeneratorPath>
     </properties>
 
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
+            <dependency>
+              <groupId>org.opendaylight.mdsal.model</groupId>
+              <artifactId>mdsal-model-artifacts</artifactId>
+              <version>${mdsalmodel.version}</version>
+              <type>pom</type>
+              <scope>import</scope>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
index 6cd4960c66203c16820024a5a2b707e3014d1bbb..d46d0f22157d939316b2fda05f3d7d641d948f5f 100644 (file)
@@ -740,7 +740,7 @@ class GeneratorImpl {
 
     def CharSequence schemaPathToId(SchemaPath path) {
         if(path !== null) {
-            return '''«FOR qName : path.path SEPARATOR "/"»«qName.localName»«ENDFOR»'''
+            return '''«FOR qName : path.pathFromRoot SEPARATOR "/"»«qName.localName»«ENDFOR»'''
         }
     }
 
index 3854be4c7c237f5c5f5364b10c0e32215f89fe43..8bb235a466cebb04ddd70a9c7fcd2b1d3261ced2 100644 (file)
@@ -25,6 +25,9 @@ import static org.opendaylight.yangtools.binding.generator.util.Types.typeForCla
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
+
+import com.google.common.base.Optional;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
@@ -917,21 +920,32 @@ public class BindingGeneratorImpl implements BindingGenerator {
             return null;
         }
 
-        boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
-        final Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
-        while (groupingUses.hasNext() && fromUses) {
-            result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
-            if (result != null) {
-                fromUses = ((DataSchemaNode) result).isAddedByUses();
+        if (result instanceof DerivableSchemaNode) {
+            DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
+            Optional<? extends SchemaNode> originalNode = castedResult
+                    .getOriginal();
+            if (castedResult.isAddedByUses() && originalNode.isPresent()) {
+                result = originalNode.get();
             }
         }
-        if (fromUses) {
-            // this indicates invalid yang and thus possible bug in code because
-            // invalid yang should be already spotted by parser
-            throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
-        }
 
-        return (DataSchemaNode) result;
+        if (result instanceof DataSchemaNode) {
+            DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
+            if (resultDataSchemaNode.isAddedByUses()) {
+                // The original node is required, but we have only the copy of
+                // the original node.
+                // Maybe this indicates a bug in Yang parser.
+                throw new IllegalStateException(
+                        "Failed to generate code for augment in "
+                                + parentUsesNode);
+            } else {
+                return resultDataSchemaNode;
+            }
+        } else {
+            throw new IllegalStateException(
+                    "Target node of uses-augment statement must be DataSchemaNode. Failed to generate code for augment in "
+                            + parentUsesNode);
+        }
     }
 
     /**
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/Bug4145Test.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/Bug4145Test.java
new file mode 100644 (file)
index 0000000..b1c264c
--- /dev/null
@@ -0,0 +1,30 @@
+package org.opendaylight.yangtools.sal.binding.generator.impl;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import java.io.IOException;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import java.net.URISyntaxException;
+import java.io.File;
+import java.util.List;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+import org.junit.Test;
+
+public class Bug4145Test {
+    @Test
+    public void bug4145Test() throws URISyntaxException, IOException, YangSyntaxErrorException {
+        File resourceFile = new File(getClass().getResource(
+                "/bug-4145/foo.yang").toURI());
+        File resourceDir = resourceFile.getParentFile();
+
+        YangParserImpl parser = YangParserImpl.getInstance();
+        SchemaContext context = parser.parseFile(resourceFile, resourceDir);
+
+        List<Type> generateTypes = new BindingGeneratorImpl(false)
+                .generateTypes(context);
+        assertNotNull(generateTypes);
+        assertTrue(generateTypes.size() > 0);
+    }
+}
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/bug-4145/foo.yang b/binding/mdsal-binding-generator-impl/src/test/resources/bug-4145/foo.yang
new file mode 100644 (file)
index 0000000..007c86b
--- /dev/null
@@ -0,0 +1,32 @@
+module foo {
+    namespace "foo";
+    prefix foo;
+
+    container root-container {
+        uses grouping-with-node-and-choice {
+            augment "node/choice-in-grouping" {
+                case two {
+                    leaf two {
+                        type string;
+                    }
+                }
+            }
+        }
+    }
+
+    grouping grouping-with-node-and-choice {
+        container node {
+            uses grouping-with-choice;
+        }
+    }
+
+    grouping grouping-with-choice {
+        choice choice-in-grouping {
+            case one {
+                leaf one {
+                    type string;
+                }
+            }
+        }
+    }
+}
index f5d72d762d038e561596fe4242ee18db3ebbc2bb..5609aeeda59c0b0e2b11b17b96eda4b6774a6ce7 100644 (file)
@@ -140,8 +140,68 @@ public final class BindingGeneratorUtil {
         return BindingMapping.getRootPackageName(module.getQNameModule());
     }
 
+    /**
+     * Creates package name from specified <code>basePackageName</code> (package
+     * name for module) and <code>schemaPath</code>.
+     *
+     * Resulting package name is concatenation of <code>basePackageName</code>
+     * and all local names of YANG nodes which are parents of some node for
+     * which <code>schemaPath</code> is specified.
+     *
+     * @param basePackageName
+     *            string with package name of the module, MUST be normalized,
+     *            otherwise this method may return an invalid string.
+     * @param schemaPath
+     *            list of names of YANG nodes which are parents of some node +
+     *            name of this node
+     * @return string with valid JAVA package name
+     * @throws NullPointerException if any of the arguments are null
+     */
     public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
-        return packageNameForGeneratedType(basePackageName, schemaPath, false);
+        final int size = Iterables.size(schemaPath.getPathTowardsRoot()) - 1;
+        if (size <= 0) {
+            return basePackageName;
+        }
+
+        return generateNormalizedPackageName(basePackageName, schemaPath.getPathFromRoot(), size);
+    }
+
+    /**
+     * Creates package name from specified <code>basePackageName</code> (package
+     * name for module) and <code>schemaPath</code> which crosses an augmentation.
+     *
+     * Resulting package name is concatenation of <code>basePackageName</code>
+     * and all local names of YANG nodes which are parents of some node for
+     * which <code>schemaPath</code> is specified.
+     *
+     * @param basePackageName
+     *            string with package name of the module, MUST be normalized,
+     *            otherwise this method may return an invalid string.
+     * @param schemaPath
+     *            list of names of YANG nodes which are parents of some node +
+     *            name of this node
+     * @return string with valid JAVA package name
+     * @throws NullPointerException if any of the arguments are null
+     */
+    public static String packageNameForAugmentedGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
+        final int size = Iterables.size(schemaPath.getPathTowardsRoot());
+        if (size == 0) {
+            return basePackageName;
+        }
+
+        return generateNormalizedPackageName(basePackageName, schemaPath.getPathFromRoot(), size);
+    }
+
+    private static String generateNormalizedPackageName(final String base, final Iterable<QName> path, final int size) {
+        final StringBuilder builder = new StringBuilder(base);
+        final Iterator<QName> iterator = path.iterator();
+        for (int i = 0; i < size; ++i) {
+            builder.append('.');
+            String nodeLocalName = iterator.next().getLocalName();
+            // FIXME: Collon ":" is invalid in node local name as per RFC6020, identifier statement.
+            builder.append(DASH_COLON_MATCHER.replaceFrom(nodeLocalName, '.'));
+        }
+        return BindingMapping.normalizePackageName(builder.toString());
     }
 
     /**
@@ -158,7 +218,11 @@ public final class BindingGeneratorUtil {
      *            list of names of YANG nodes which are parents of some node +
      *            name of this node
      * @return string with valid JAVA package name
+     *
+     * @deprecated Use {@link #packageNameForGeneratedType(String, SchemaPath)} or
+     *             {@link #packageNameForAugmentedGeneratedType(String, SchemaPath)} instead.
      */
+    @Deprecated
     public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath,
             final boolean isUsesAugment) {
         if (basePackageName == null) {
@@ -168,24 +232,20 @@ public final class BindingGeneratorUtil {
             throw new IllegalArgumentException("Schema Path cannot be NULL!");
         }
 
-        final StringBuilder builder = new StringBuilder();
-        builder.append(basePackageName);
         final Iterable<QName> iterable = schemaPath.getPathFromRoot();
-        final Iterator<QName> iterator = iterable.iterator();
-        int size = Iterables.size(iterable);
+        final int size = Iterables.size(iterable);
         final int traversalSteps;
         if (isUsesAugment) {
             traversalSteps = size;
         } else {
             traversalSteps = size - 1;
         }
-        for (int i = 0; i < traversalSteps; ++i) {
-            builder.append('.');
-            String nodeLocalName = iterator.next().getLocalName();
-            // FIXME: Collon ":" is invalid in node local name as per RFC6020, identifier statement.
-            builder.append(DASH_COLON_MATCHER.replaceFrom(nodeLocalName, '.'));
+
+        if (traversalSteps == 0) {
+            return BindingMapping.normalizePackageName(basePackageName);
         }
-        return BindingMapping.normalizePackageName(builder.toString());
+
+        return generateNormalizedPackageName(basePackageName, iterable, traversalSteps);
     }
 
     /**
@@ -202,7 +262,13 @@ public final class BindingGeneratorUtil {
      *             <li>if <code>basePackageName</code> equals <code>null</code></li>
      *             <li>if <code>typeDefinition</code> equals <code>null</code></li>
      *             </ul>
+     * @deprecated This method ignores typeDefinition argument and its result is only
+     *             <code>indingMapping.normalizePackageName(basePackageName)</code>.
+     *             Aside from tests, there is not a single user in OpenDaylight codebase,
+     *             hence it can be considered buggy and defunct. It is scheduled for removal
+     *             in Boron release.
      */
+    @Deprecated
     public static String packageNameForTypeDefinition(final String basePackageName,
             final TypeDefinition<?> typeDefinition) {
         if (basePackageName == null) {
@@ -212,9 +278,7 @@ public final class BindingGeneratorUtil {
             throw new IllegalArgumentException("Type Definition reference cannot be NULL!");
         }
 
-        final StringBuilder builder = new StringBuilder();
-        builder.append(basePackageName);
-        return BindingMapping.normalizePackageName(builder.toString());
+        return BindingMapping.normalizePackageName(basePackageName);
     }
 
     /**
@@ -266,7 +330,6 @@ public final class BindingGeneratorUtil {
      *             <li>if <code>token</code> equals null</li>
      *             </ul>
      */
-
     private static String parseToCamelCase(final String token, final boolean uppercase) {
         if (token == null) {
             throw new IllegalArgumentException("Name can not be null");
index 91e93ed1b8f7407de49d9647c57024e1bd9d1392..928958966c8af55c717abc42499bacb69a59db27 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-
 import com.google.common.base.Optional;
 import java.io.File;
 import java.io.IOException;
@@ -60,7 +59,7 @@ public class BindingGeneratorUtilTest {
     @Rule
     public ExpectedException expectedEx = ExpectedException.none();
 
-    private static List<File> loadTestResources(String testFile) {
+    private static List<File> loadTestResources(final String testFile) {
         final List<File> testModels = new ArrayList<File>();
         File listModelFile;
         try {
@@ -172,6 +171,7 @@ public class BindingGeneratorUtilTest {
      * </ul>
      */
     @Test
+    @Deprecated
     public void testPackageNameForTypeDefinitionNullBasePackageName() {
         expectedEx.expect(IllegalArgumentException.class);
         expectedEx.expectMessage("Base Package Name cannot be NULL!");
@@ -186,6 +186,7 @@ public class BindingGeneratorUtilTest {
      * </ul>
      */
     @Test
+    @Deprecated
     public void testPackageNameForTypeDefinitionNullTypeDefinition() {
         expectedEx.expect(IllegalArgumentException.class);
         expectedEx.expectMessage("Type Definition reference cannot be NULL!");
@@ -201,8 +202,7 @@ public class BindingGeneratorUtilTest {
      */
     @Test
     public void testPackageNameForGeneratedTypeNullBasePackageName() {
-        expectedEx.expect(IllegalArgumentException.class);
-        expectedEx.expectMessage("Base Package Name cannot be NULL!");
+        expectedEx.expect(NullPointerException.class);
         BindingGeneratorUtil.packageNameForGeneratedType(null, null);
     }
 
@@ -215,8 +215,7 @@ public class BindingGeneratorUtilTest {
      */
     @Test
     public void testPackageNameForGeneratedTypeNullSchemaPath() {
-        expectedEx.expect(IllegalArgumentException.class);
-        expectedEx.expectMessage("Schema Path cannot be NULL!");
+        expectedEx.expect(NullPointerException.class);
         BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
     }
 
index b13ab14043c44bdcce8863dacc8d858495c33121..06f750ff7866ef6978dac30a0f7759d118cf8c7c 100644 (file)
@@ -128,7 +128,9 @@ public final class BindingMapping {
             builder.append(p);
         }
 
-        return builder.toString();
+        // Prevent duplication of input string
+        final String result = builder.toString();
+        return packageName.equals(result) ? packageName : result;
     }
 
     public static String getMethodName(final QName name) {