import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
+import com.google.common.collect.ImmutableList;
+import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+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.stmt.BelongsToStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException;
import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
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.meta.StmtContextUtils;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
abstract class AbstractSubmoduleStatementSupport
- extends AbstractStatementSupport<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> {
+ extends BaseStatementSupport<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> {
AbstractSubmoduleStatementSupport() {
- super(YangStmtMapping.SUBMODULE);
+ super(YangStmtMapping.SUBMODULE, CopyPolicy.REJECT);
}
@Override
- public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
- return value;
- }
-
- @Override
- public final SubmoduleStatement createDeclared(final StmtContext<String, SubmoduleStatement, ?> ctx) {
- return new SubmoduleStatementImpl(ctx);
- }
-
- @Override
- public final EffectiveStatement<String, SubmoduleStatement> createEffective(
- final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
- return new SubmoduleEffectiveStatementImpl(ctx);
+ public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+ try {
+ return UnqualifiedQName.of(value);
+ } catch (IllegalArgumentException e) {
+ throw new SourceException(e.getMessage(), ctx, e);
+ }
}
@Override
public final void onPreLinkageDeclared(
- final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> stmt) {
- stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getStatementArgument(),
+ final Mutable<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
+ stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getRawArgument(),
StmtContextUtils.getLatestRevision(stmt.declaredSubstatements())));
}
@Override
public final void onLinkageDeclared(
- final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> stmt) {
- final SourceIdentifier submoduleIdentifier = RevisionSourceIdentifier.create(stmt.coerceStatementArgument(),
+ final Mutable<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
+ final SourceIdentifier submoduleIdentifier = RevisionSourceIdentifier.create(stmt.getRawArgument(),
StmtContextUtils.getLatestRevision(stmt.declaredSubstatements()));
- final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>
+ final StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>
possibleDuplicateSubmodule = stmt.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
if (possibleDuplicateSubmodule != null && possibleDuplicateSubmodule != stmt) {
- throw new SourceException(stmt.getStatementSourceReference(), "Submodule name collision: %s. At %s",
- stmt.getStatementArgument(), possibleDuplicateSubmodule.getStatementSourceReference());
+ throw new SourceException(stmt, "Submodule name collision: %s. At %s", stmt.rawArgument(),
+ possibleDuplicateSubmodule.sourceReference());
}
stmt.addContext(SubmoduleNamespace.class, submoduleIdentifier, stmt);
final String belongsToModuleName = firstAttributeOf(stmt.declaredSubstatements(), BelongsToStatement.class);
- final StmtContext<?, ?, ?> prefixSubStmtCtx = findFirstDeclaredSubstatement(stmt, 0,
- BelongsToStatement.class, PrefixStatement.class);
- SourceException.throwIfNull(prefixSubStmtCtx, stmt.getStatementSourceReference(),
- "Prefix of belongsTo statement is missing in submodule [%s]", stmt.getStatementArgument());
-
- final String prefix = (String) prefixSubStmtCtx.getStatementArgument();
+ final StmtContext<?, ?, ?> prefixSubStmtCtx = SourceException.throwIfNull(
+ findFirstDeclaredSubstatement(stmt, 0, BelongsToStatement.class, PrefixStatement.class), stmt,
+ "Prefix of belongsTo statement is missing in submodule [%s]", stmt.rawArgument());
+ final String prefix = prefixSubStmtCtx.rawArgument();
stmt.addToNs(BelongsToPrefixToModuleName.class, prefix, belongsToModuleName);
}
-}
\ No newline at end of file
+
+ @Override
+ protected final SubmoduleStatement createDeclared(final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx,
+ final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+ return new SubmoduleStatementImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
+ }
+
+ @Override
+ protected final SubmoduleStatement createEmptyDeclared(
+ final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx) {
+ throw noBelongsTo(ctx);
+ }
+
+ @Override
+ protected SubmoduleEffectiveStatement createEffective(final Current<UnqualifiedQName, SubmoduleStatement> stmt,
+ final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ if (substatements.isEmpty()) {
+ throw noBelongsTo(stmt);
+ }
+ try {
+ return new SubmoduleEffectiveStatementImpl(stmt, substatements);
+ } catch (SubstatementIndexingException e) {
+ throw new SourceException(e.getMessage(), stmt, e);
+ }
+ }
+
+ private static SourceException noBelongsTo(final CommonStmtCtx stmt) {
+ return new SourceException("No belongs-to declared in submodule", stmt);
+ }
+}