2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.submodule;
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
13 import com.google.common.collect.ImmutableList;
14 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
22 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
29 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 abstract class AbstractSubmoduleStatementSupport
33 extends BaseStatementSupport<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> {
34 AbstractSubmoduleStatementSupport() {
35 super(YangStmtMapping.SUBMODULE);
39 public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
41 return UnqualifiedQName.of(value);
42 } catch (IllegalArgumentException e) {
43 throw new SourceException(e.getMessage(), ctx.getStatementSourceReference(), e);
48 public final void onPreLinkageDeclared(
49 final Mutable<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
50 stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.coerceRawStatementArgument(),
51 StmtContextUtils.getLatestRevision(stmt.declaredSubstatements())));
55 public final void onLinkageDeclared(
56 final Mutable<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
57 final SourceIdentifier submoduleIdentifier = RevisionSourceIdentifier.create(stmt.coerceRawStatementArgument(),
58 StmtContextUtils.getLatestRevision(stmt.declaredSubstatements()));
60 final StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>
61 possibleDuplicateSubmodule = stmt.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
62 if (possibleDuplicateSubmodule != null && possibleDuplicateSubmodule != stmt) {
63 throw new SourceException(stmt.getStatementSourceReference(), "Submodule name collision: %s. At %s",
64 stmt.rawStatementArgument(), possibleDuplicateSubmodule.getStatementSourceReference());
67 stmt.addContext(SubmoduleNamespace.class, submoduleIdentifier, stmt);
69 final String belongsToModuleName = firstAttributeOf(stmt.declaredSubstatements(), BelongsToStatement.class);
70 final StmtContext<?, ?, ?> prefixSubStmtCtx = findFirstDeclaredSubstatement(stmt, 0,
71 BelongsToStatement.class, PrefixStatement.class);
72 SourceException.throwIfNull(prefixSubStmtCtx, stmt.getStatementSourceReference(),
73 "Prefix of belongsTo statement is missing in submodule [%s]", stmt.rawStatementArgument());
75 final String prefix = prefixSubStmtCtx.rawStatementArgument();
76 stmt.addToNs(BelongsToPrefixToModuleName.class, prefix, belongsToModuleName);
80 protected final SubmoduleStatement createDeclared(final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx,
81 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
82 return new SubmoduleStatementImpl(ctx, substatements);
86 protected final SubmoduleStatement createEmptyDeclared(
87 final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx) {
88 throw noBelongsTo(ctx);
92 protected final SubmoduleEffectiveStatement createEffective(
93 final StmtContext<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
94 final SubmoduleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
95 return new SubmoduleEffectiveStatementImpl(ctx, declared, substatements);
99 protected final SubmoduleEffectiveStatement createEmptyEffective(
100 final StmtContext<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
101 final SubmoduleStatement declared) {
102 throw noBelongsTo(ctx);
105 private static SourceException noBelongsTo(final StmtContext<?, ?, ?> ctx) {
106 return new SourceException("No belongs-to declared in submodule", ctx.getStatementSourceReference());