Simplify Module/Submodule statement argument usage
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / submodule / AbstractSubmoduleStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.submodule;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
12
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;
31
32 abstract class AbstractSubmoduleStatementSupport
33         extends BaseStatementSupport<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> {
34     AbstractSubmoduleStatementSupport() {
35         super(YangStmtMapping.SUBMODULE);
36     }
37
38     @Override
39     public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
40         try {
41             return UnqualifiedQName.of(value);
42         } catch (IllegalArgumentException e) {
43             throw new SourceException(e.getMessage(), ctx.getStatementSourceReference(), e);
44         }
45     }
46
47     @Override
48     public final void onPreLinkageDeclared(
49             final Mutable<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
50         stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.coerceRawStatementArgument(),
51             StmtContextUtils.getLatestRevision(stmt.declaredSubstatements())));
52     }
53
54     @Override
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()));
59
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());
65         }
66
67         stmt.addContext(SubmoduleNamespace.class, submoduleIdentifier, stmt);
68
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());
74
75         final String prefix = prefixSubStmtCtx.rawStatementArgument();
76         stmt.addToNs(BelongsToPrefixToModuleName.class, prefix, belongsToModuleName);
77     }
78
79     @Override
80     protected final SubmoduleStatement createDeclared(final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx,
81             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
82         return new SubmoduleStatementImpl(ctx, substatements);
83     }
84
85     @Override
86     protected final SubmoduleStatement createEmptyDeclared(
87             final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx) {
88         throw noBelongsTo(ctx);
89     }
90
91     @Override
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);
96     }
97
98     @Override
99     protected final SubmoduleEffectiveStatement createEmptyEffective(
100             final StmtContext<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
101             final SubmoduleStatement declared) {
102         throw noBelongsTo(ctx);
103     }
104
105     private static SourceException noBelongsTo(final StmtContext<?, ?, ?> ctx) {
106         return new SourceException("No belongs-to declared in submodule", ctx.getStatementSourceReference());
107     }
108 }