Reformulate StatementContextFactory.createEffective()
[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.EffectiveStmtCtx.Current;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
33
34 abstract class AbstractSubmoduleStatementSupport
35         extends BaseStatementSupport<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> {
36     AbstractSubmoduleStatementSupport() {
37         super(YangStmtMapping.SUBMODULE);
38     }
39
40     @Override
41     public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
42         try {
43             return UnqualifiedQName.of(value);
44         } catch (IllegalArgumentException e) {
45             throw new SourceException(e.getMessage(), ctx.getStatementSourceReference(), e);
46         }
47     }
48
49     @Override
50     public final void onPreLinkageDeclared(
51             final Mutable<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
52         stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.coerceRawStatementArgument(),
53             StmtContextUtils.getLatestRevision(stmt.declaredSubstatements())));
54     }
55
56     @Override
57     public final void onLinkageDeclared(
58             final Mutable<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
59         final SourceIdentifier submoduleIdentifier = RevisionSourceIdentifier.create(stmt.coerceRawStatementArgument(),
60             StmtContextUtils.getLatestRevision(stmt.declaredSubstatements()));
61
62         final StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>
63             possibleDuplicateSubmodule = stmt.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
64         if (possibleDuplicateSubmodule != null && possibleDuplicateSubmodule != stmt) {
65             throw new SourceException(stmt.getStatementSourceReference(), "Submodule name collision: %s. At %s",
66                     stmt.rawStatementArgument(), possibleDuplicateSubmodule.getStatementSourceReference());
67         }
68
69         stmt.addContext(SubmoduleNamespace.class, submoduleIdentifier, stmt);
70
71         final String belongsToModuleName = firstAttributeOf(stmt.declaredSubstatements(), BelongsToStatement.class);
72         final StmtContext<?, ?, ?> prefixSubStmtCtx = findFirstDeclaredSubstatement(stmt, 0,
73                 BelongsToStatement.class, PrefixStatement.class);
74         SourceException.throwIfNull(prefixSubStmtCtx, stmt.getStatementSourceReference(),
75                 "Prefix of belongsTo statement is missing in submodule [%s]", stmt.rawStatementArgument());
76
77         final String prefix = prefixSubStmtCtx.rawStatementArgument();
78         stmt.addToNs(BelongsToPrefixToModuleName.class, prefix, belongsToModuleName);
79     }
80
81     @Override
82     protected final SubmoduleStatement createDeclared(final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx,
83             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
84         return new SubmoduleStatementImpl(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
85             substatements);
86     }
87
88     @Override
89     protected final SubmoduleStatement createEmptyDeclared(
90             final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx) {
91         throw noBelongsTo(ctx.getStatementSourceReference());
92     }
93
94     @Override
95     protected SubmoduleEffectiveStatement createEffective(final Current<UnqualifiedQName, SubmoduleStatement> stmt,
96             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
97         if (substatements.isEmpty()) {
98             throw noBelongsTo(stmt.sourceReference());
99         }
100         return new SubmoduleEffectiveStatementImpl(stmt, substatements);
101     }
102
103     private static SourceException noBelongsTo(final StatementSourceReference ref) {
104         return new SourceException("No belongs-to declared in submodule", ref);
105     }
106 }