From: Peter Kajsa Date: Thu, 21 Jul 2016 10:46:22 +0000 (+0200) Subject: Bug 6240: Entities of imported module's submodule are not visible X-Git-Tag: release/boron~47 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=73d292d8da3aeb61ddb3318d64c6601b3b4af666;p=yangtools.git Bug 6240: Entities of imported module's submodule are not visible Yang statement parser didn't lookup yang elements in submodules of imported module. Therefore each submodule element was always invisible to other modules even if they imported parent module of the submodule. This patch adds IncludeContext to the each RootStatementContext in order to allow lookup of yang elements also in included submodules. Change-Id: I9993b9fe110a5e80e9a61258a40841af13c18320 Signed-off-by: Peter Kajsa --- diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java index 045be49378..54f93ebe8a 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java @@ -8,16 +8,21 @@ package org.opendaylight.yangtools.yang.parser.stmt.reactor; import com.google.common.base.Optional; +import java.util.ArrayList; import java.util.Collection; +import java.util.Map; +import javax.annotation.Nullable; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode; import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry; import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IncludedModuleContext; /** * root statement class for a Yang source @@ -26,6 +31,7 @@ public class RootStatementContext, E extends E StatementContextBase { private final SourceSpecificContext sourceContext; + private final Collection includedContexts = new ArrayList<>(); private final A argument; RootStatementContext(final ContextBuilder builder, final SourceSpecificContext sourceContext) { @@ -180,4 +186,44 @@ public class RootStatementContext, E extends E public boolean isEnabledSemanticVersioning() { return sourceContext.isEnabledSemanticVersioning(); } + + @Override + public > void addToLocalStorage(final Class type, final K key, + final V value) { + if (IncludedModuleContext.class.isAssignableFrom(type)) { + includedContexts.add((NamespaceStorageNode) value); + } + super.addToLocalStorage(type, key, value); + } + + @Override + public > V getFromLocalStorage(final Class type, final K key) { + final V potentialLocal = super.getFromLocalStorage(type, key); + if (potentialLocal != null) { + return potentialLocal; + } + for (final NamespaceStorageNode includedSource : includedContexts) { + final V potential = includedSource.getFromLocalStorage(type, key); + if (potential != null) { + return potential; + } + } + return null; + } + + @Nullable + @Override + public > Map getAllFromLocalStorage(final Class type) { + final Map potentialLocal = super.getAllFromLocalStorage(type); + if (potentialLocal != null) { + return potentialLocal; + } + for (final NamespaceStorageNode includedSource : includedContexts) { + final Map potential = includedSource.getAllFromLocalStorage(type); + if (potential != null) { + return potential; + } + } + return null; + } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6240Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6240Test.java new file mode 100644 index 0000000000..dda220cd79 --- /dev/null +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6240Test.java @@ -0,0 +1,70 @@ +/* + * 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 java.io.FileNotFoundException; +import java.net.URISyntaxException; +import java.util.Set; +import org.junit.Test; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; + +public class Bug6240Test { + private static final String NS = "bar"; + private static final String REV = "2016-07-19"; + + @Test + public void testModels() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException { + final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6240/correct"); + assertNotNull(context); + + final Set modules = context.getModules(); + assertEquals(2, modules.size()); + + Module bar = null; + for (final Module module : modules) { + if ("bar".equals(module.getName())) { + bar = module; + break; + } + } + + assertNotNull(bar); + assertTrue(bar.getDataChildByName(QName.create(NS, REV, "foo-grp-con")) instanceof ContainerSchemaNode); + assertTrue(bar.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")) instanceof ContainerSchemaNode); + + assertEquals(1, bar.getSubmodules().size()); + + final DataSchemaNode dataChildByName = bar.getDataChildByName(QName.create(NS, REV, "sub-bar-con")); + assertTrue(dataChildByName instanceof ContainerSchemaNode); + final ContainerSchemaNode subBarCon = (ContainerSchemaNode) dataChildByName; + + assertTrue(subBarCon.getDataChildByName(QName.create(NS, REV, "foo-grp-con")) instanceof ContainerSchemaNode); + assertTrue(subBarCon.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")) instanceof ContainerSchemaNode); + } + + @Test + public void testInvalidModels() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException { + try { + StmtTestUtils.parseYangSources("/bugs/bug6240/incorrect"); + } catch (final SomeModifiersUnresolvedException e) { + assertTrue(e.getCause().getCause().getMessage() + .startsWith("Grouping '(bar?revision=2016-07-19)foo-imp-grp' was not resolved.")); + } + } +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/bar.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/bar.yang new file mode 100644 index 0000000000..0136d582d2 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/bar.yang @@ -0,0 +1,14 @@ +module bar { + namespace "bar"; + prefix "bar"; + + include sub-bar { + revision-date 2016-07-19; + } + import foo { prefix foo; revision-date 2016-07-19; } + + revision "2016-07-19"; + + uses foo:sub-foo-grp; + uses foo:foo-grp; +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/foo.yang new file mode 100644 index 0000000000..7fe73c4cff --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/foo.yang @@ -0,0 +1,15 @@ +module foo { + namespace "foo"; + prefix foo; + + include sub-foo { + revision-date 2016-07-19; + } + + revision 2016-07-19; + + grouping foo-grp { + container foo-grp-con { + } + } +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/sub-bar.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/sub-bar.yang new file mode 100644 index 0000000000..1b21c4565a --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/sub-bar.yang @@ -0,0 +1,14 @@ +submodule sub-bar { + belongs-to bar { + prefix bar; + } + + import foo { prefix foo; revision-date 2016-07-19; } + + revision 2016-07-19; + + container sub-bar-con { + uses foo:sub-foo-grp; + uses foo:foo-grp; + } +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/sub-foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/sub-foo.yang new file mode 100644 index 0000000000..d3e858b75d --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/correct/sub-foo.yang @@ -0,0 +1,12 @@ +submodule sub-foo { + belongs-to foo { + prefix foo; + } + + revision 2016-07-19; + + grouping sub-foo-grp { + container sub-foo-grp-con { + } + } +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/bar.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/bar.yang new file mode 100644 index 0000000000..9ece862fad --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/bar.yang @@ -0,0 +1,12 @@ +module bar { + namespace "bar"; + prefix "bar"; + + import foo { prefix foo; revision-date 2016-07-19; } + + revision "2016-07-19"; + + uses foo:sub-foo-grp; + uses foo:foo-grp; + uses foo-imp-grp; +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/foo-imp.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/foo-imp.yang new file mode 100644 index 0000000000..e52496117c --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/foo-imp.yang @@ -0,0 +1,11 @@ +module foo-imp { + namespace "foo-imp"; + prefix foo-imp; + + revision 2016-07-19; + + grouping foo-imp-grp { + container foo-imp-grp-con { + } + } +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/foo.yang new file mode 100644 index 0000000000..7fe73c4cff --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/foo.yang @@ -0,0 +1,15 @@ +module foo { + namespace "foo"; + prefix foo; + + include sub-foo { + revision-date 2016-07-19; + } + + revision 2016-07-19; + + grouping foo-grp { + container foo-grp-con { + } + } +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/sub-foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/sub-foo.yang new file mode 100644 index 0000000000..d3e858b75d --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug6240/incorrect/sub-foo.yang @@ -0,0 +1,12 @@ +submodule sub-foo { + belongs-to foo { + prefix foo; + } + + revision 2016-07-19; + + grouping sub-foo-grp { + container sub-foo-grp-con { + } + } +}