Bug 6961: SchemaContext.getAllModuleIdentifiers() doesnt work for submodules 14/47314/1
authorFilip Gregor <fgregor@cisco.com>
Wed, 19 Oct 2016 11:41:28 +0000 (13:41 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 21 Oct 2016 08:40:50 +0000 (08:40 +0000)
added submodules to return value from method getAllModuleIdentifiers

Change-Id: I9f328d0050457b7094cbb420f0e0205b8034cf6f
Signed-off-by: Filip Gregor <fgregor@cisco.com>
(cherry picked from commit 097a1cc73aa3ffbf1898b38cba4f6c66956a4c3b)

yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6961Test.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug6961/bar-sub1.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug6961/bar.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug6961/baz.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo-sub1.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo-sub2.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo.yang [new file with mode: 0644]

index 197bae3024fa3445d9eba85789c796d1268cf99b..0223bdf1399c1b1d0d06197f29c31756627f5766 100644 (file)
@@ -64,6 +64,7 @@ public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext
             nameMap.put(m.getName(), m);
             nsMap.put(m.getNamespace(), m);
             modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision())));
+            resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
         }
 
         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
@@ -97,6 +98,7 @@ public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext
             nameMap.put(m.getName(), m);
             nsMap.put(m.getNamespace(), m);
             modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision())));
+            resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
         }
 
         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
@@ -111,6 +113,13 @@ public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext
        return new EffectiveSchemaContext(modules);
     }
 
+    private void resolveSubmoduleIdentifiers(final Set<Module> submodules, Set<ModuleIdentifier> modIdBuilder) {
+        for (Module submodule : submodules) {
+            modIdBuilder.add(new ModuleIdentifierImpl(submodule.getName(), Optional.of(
+                    submodule.getNamespace()), Optional.of(submodule.getRevision())));
+        }
+    }
+
     public List<DeclaredStatement<?>> getRootDeclaredStatements() {
         return rootDeclaredStatements;
     }
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6961Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6961Test.java
new file mode 100644 (file)
index 0000000..271a065
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. 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.yangtools.yang.stmt;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
+import java.net.URI;
+import java.util.Date;
+import java.util.Set;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
+
+public class Bug6961Test {
+
+    @Test
+    public void testBug6961SchemaContext() throws Exception {
+        final Optional<Date> date = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse("2016-01-01"));
+        final ModuleIdentifierImpl foo = new ModuleIdentifierImpl("foo", Optional.of(new URI("foo")), date);
+        final ModuleIdentifierImpl sub1Foo = new ModuleIdentifierImpl("sub1-foo", Optional.of(new URI("foo")), date);
+        final ModuleIdentifierImpl sub2Foo = new ModuleIdentifierImpl("sub2-foo", Optional.of(new URI("foo")), date);
+        final ModuleIdentifierImpl bar = new ModuleIdentifierImpl("bar", Optional.of(new URI("bar")), date);
+        final ModuleIdentifierImpl sub1Bar = new ModuleIdentifierImpl("sub1-bar", Optional.of(new URI("bar")), date);
+        final ModuleIdentifierImpl baz = new ModuleIdentifierImpl("baz", Optional.of(new URI("baz")), date);
+        final Set<ModuleIdentifier> testSet = Sets.newHashSet(foo, sub1Foo, sub2Foo, bar, sub1Bar, baz);
+        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6961/");
+        assertNotNull(context);
+        final Set<ModuleIdentifier> allModuleIdentifiers = context.getAllModuleIdentifiers();
+        assertNotNull(allModuleIdentifiers);
+        assertEquals(6, allModuleIdentifiers.size());
+        final SchemaContext schemaContext = EffectiveSchemaContext.resolveSchemaContext(context.getModules());
+        assertNotNull(schemaContext);
+        final Set<ModuleIdentifier> allModuleIdentifiersResolved = schemaContext.getAllModuleIdentifiers();
+        assertNotNull(allModuleIdentifiersResolved);
+        assertEquals(6, allModuleIdentifiersResolved.size());
+        assertEquals(allModuleIdentifiersResolved, allModuleIdentifiers);
+        assertEquals(allModuleIdentifiers, testSet);
+        assertTrue(allModuleIdentifiers.contains(foo));
+        final QNameModule fooQNameModule = foo.getQNameModule();
+        final QNameModule fooQNameModuleCreated = QNameModule.create(new URI("foo"), date.orNull());
+        assertEquals(fooQNameModule, fooQNameModuleCreated);
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6961/bar-sub1.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6961/bar-sub1.yang
new file mode 100644 (file)
index 0000000..ea1d323
--- /dev/null
@@ -0,0 +1,11 @@
+submodule sub1-bar {
+    belongs-to bar {
+        prefix bar;
+    }
+
+    revision 2016-01-01;
+
+    leaf sub1-bar-leaf {
+        type string;
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6961/bar.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6961/bar.yang
new file mode 100644 (file)
index 0000000..19d905c
--- /dev/null
@@ -0,0 +1,11 @@
+module bar {
+    namespace "bar";
+    prefix bar;
+    revision 2016-01-01;
+
+    include sub1-bar;
+
+    leaf bar-leaf {
+        type string;
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6961/baz.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6961/baz.yang
new file mode 100644 (file)
index 0000000..1b82bce
--- /dev/null
@@ -0,0 +1,9 @@
+module baz {
+    namespace "baz";
+    prefix baz;
+    revision 2016-01-01;
+
+    leaf baz-leaf {
+        type string;
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo-sub1.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo-sub1.yang
new file mode 100644 (file)
index 0000000..d942464
--- /dev/null
@@ -0,0 +1,11 @@
+submodule sub1-foo {
+    belongs-to foo {
+        prefix foo;
+    }
+
+    revision 2016-01-01;
+
+    leaf sub1-foo-leaf {
+        type string;
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo-sub2.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo-sub2.yang
new file mode 100644 (file)
index 0000000..dfef551
--- /dev/null
@@ -0,0 +1,11 @@
+submodule sub2-foo {
+    belongs-to foo {
+        prefix foo;
+    }
+
+    revision 2016-01-01;
+
+    leaf sub2-foo-leaf {
+        type string;
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6961/foo.yang
new file mode 100644 (file)
index 0000000..8b82d0d
--- /dev/null
@@ -0,0 +1,12 @@
+module foo {
+    namespace "foo";
+    prefix foo;
+    revision 2016-01-01;
+
+    include sub1-foo;
+    include sub2-foo;
+
+    leaf foo-leaf {
+        type string;
+    }
+}
\ No newline at end of file