193ba16695be564276cd5512d86f0e9a54774f60
[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.coerceStatementArgument().getLocalName(),
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(
58             stmt.coerceStatementArgument().getLocalName(),
59             StmtContextUtils.getLatestRevision(stmt.declaredSubstatements()));
60
61         final StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement>
62             possibleDuplicateSubmodule = stmt.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
63         if (possibleDuplicateSubmodule != null && possibleDuplicateSubmodule != stmt) {
64             throw new SourceException(stmt.getStatementSourceReference(), "Submodule name collision: %s. At %s",
65                     stmt.rawStatementArgument(), possibleDuplicateSubmodule.getStatementSourceReference());
66         }
67
68         stmt.addContext(SubmoduleNamespace.class, submoduleIdentifier, stmt);
69
70         final String belongsToModuleName = firstAttributeOf(stmt.declaredSubstatements(), BelongsToStatement.class);
71         final StmtContext<?, ?, ?> prefixSubStmtCtx = findFirstDeclaredSubstatement(stmt, 0,
72                 BelongsToStatement.class, PrefixStatement.class);
73         SourceException.throwIfNull(prefixSubStmtCtx, stmt.getStatementSourceReference(),
74                 "Prefix of belongsTo statement is missing in submodule [%s]", stmt.rawStatementArgument());
75
76         final String prefix = prefixSubStmtCtx.rawStatementArgument();
77         stmt.addToNs(BelongsToPrefixToModuleName.class, prefix, belongsToModuleName);
78     }
79
80     @Override
81     protected final SubmoduleStatement createDeclared(final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx,
82             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
83         return new SubmoduleStatementImpl(ctx, substatements);
84     }
85
86     @Override
87     protected final SubmoduleStatement createEmptyDeclared(
88             final StmtContext<UnqualifiedQName, SubmoduleStatement, ?> ctx) {
89         throw noBelongsTo(ctx);
90     }
91
92     @Override
93     protected final SubmoduleEffectiveStatement createEffective(
94             final StmtContext<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
95             final SubmoduleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
96         return new SubmoduleEffectiveStatementImpl(ctx, declared, substatements);
97     }
98
99     @Override
100     protected final SubmoduleEffectiveStatement createEmptyEffective(
101             final StmtContext<UnqualifiedQName, SubmoduleStatement, SubmoduleEffectiveStatement> ctx,
102             final SubmoduleStatement declared) {
103         throw noBelongsTo(ctx);
104     }
105
106     private static SourceException noBelongsTo(final StmtContext<?, ?, ?> ctx) {
107         return new SourceException("No belongs-to declared in submodule", ctx.getStatementSourceReference());
108     }
109 }