Fix of Module.getPrefix() to return prefix also in case of submodule 64/59564/8
authorPeter Kajsa <pkajsa@cisco.com>
Tue, 27 Jun 2017 12:01:53 +0000 (14:01 +0200)
committerPeter Kajsa <pkajsa@cisco.com>
Mon, 31 Jul 2017 13:59:12 +0000 (15:59 +0200)
Module.getPrefix() returns null for all submodules.
However, in case of submodule a prefix for module's
namespace is defined in belongs-to statement.
This information is required by yang-export where
namespaces need to be binded to corresponding prefixes.

Change-Id: Ifa5406654021fd017b442b6c351a4e0b4f5fd55d
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BelongsToStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AbstractEffectiveModule.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/BelongsToEffectiveStatementImpl.java [moved from yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/BelongsEffectiveToStatementImpl.java with 83% similarity]

index b208d454ea73f180bcd8b57fafe5780153e4c1f9..d0ec8a6c786b27dc8e119aeb0ffe0183f54661af 100644 (file)
@@ -30,7 +30,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsEffectiveToStatementImpl;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsToEffectiveStatementImpl;
 
 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
         implements BelongsToStatement {
@@ -63,7 +63,7 @@ public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
         @Override
         public EffectiveStatement<String, BelongsToStatement> createEffective(
                 final StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
-            return new BelongsEffectiveToStatementImpl(ctx);
+            return new BelongsToEffectiveStatementImpl(ctx);
         }
 
         @Override
index 7b871c1c3232c8b53323f9e5611bd2a7ea7ef817..d80ea60cbf2676af109d0b900f8e047f031704ff 100644 (file)
@@ -51,6 +51,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
         AbstractEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
@@ -84,8 +85,18 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
 
         this.name = argument();
 
-        final PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
-        this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
+        EffectiveStatementBase<?, ?> parentOfPrefix = this;
+        if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
+            parentOfPrefix = firstEffective(BelongsToEffectiveStatementImpl.class);
+            SourceException.throwIfNull(parentOfPrefix, ctx.getStatementSourceReference(),
+                    "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
+        }
+
+        final PrefixEffectiveStatementImpl prefixStmt = parentOfPrefix
+                .firstEffective(PrefixEffectiveStatementImpl.class);
+        SourceException.throwIfNull(prefixStmt, ctx.getStatementSourceReference(),
+                "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
+        this.prefix = prefixStmt.argument();
 
         final YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
         this.yangVersion = (yangVersionStmt == null) ? YangVersion.VERSION_1 : yangVersionStmt.argument();
@@ -10,8 +10,8 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-public final class BelongsEffectiveToStatementImpl extends DeclaredEffectiveStatementBase<String, BelongsToStatement> {
-    public BelongsEffectiveToStatementImpl(final StmtContext<String, BelongsToStatement, ?> ctx) {
+public final class BelongsToEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, BelongsToStatement> {
+    public BelongsToEffectiveStatementImpl(final StmtContext<String, BelongsToStatement, ?> ctx) {
         super(ctx);
     }
 }