Use UnqualifiedQName for {Module,Submodule}Statement
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / module / AbstractModuleStatementSupport.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.module;
9
10 import static com.google.common.base.Verify.verify;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
12
13 import com.google.common.collect.ImmutableList;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20 import org.opendaylight.yangtools.concepts.SemVer;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Submodule;
27 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
34 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
38 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
39 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
40 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
46 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
47 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
48 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
49 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
50 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
52 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
53 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
54 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
55 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
56 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
57
58 abstract class AbstractModuleStatementSupport
59         extends BaseStatementSupport<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> {
60     AbstractModuleStatementSupport() {
61         super(YangStmtMapping.MODULE);
62     }
63
64     @Override
65     public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
66         try {
67             return UnqualifiedQName.of(value);
68         } catch (IllegalArgumentException e) {
69             throw new SourceException(e.getMessage(), ctx.getStatementSourceReference(), e);
70         }
71     }
72
73     @Override
74     public final void onPreLinkageDeclared(
75             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
76         final String moduleName = stmt.coerceStatementArgument().getLocalName();
77
78         final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
79         SourceException.throwIfNull(moduleNs, stmt.getStatementSourceReference(),
80             "Namespace of the module [%s] is missing", moduleName);
81         stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
82
83         final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
84         SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
85             "Prefix of the module [%s] is missing", moduleName);
86         stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
87
88         stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
89
90         final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
91         final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
92
93         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
94         stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.coerceStatementArgument().getLocalName(),
95             revisionDate));
96     }
97
98     @Override
99     public final void onLinkageDeclared(
100             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
101
102         final Optional<URI> moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(),
103                 NamespaceStatement.class));
104         SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(),
105             "Namespace of the module [%s] is missing", stmt.getStatementArgument());
106
107         final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
108         final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern();
109         final StmtContext<?, ModuleStatement, ModuleEffectiveStatement> possibleDuplicateModule =
110                 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
111         if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
112             throw new SourceException(stmt.getStatementSourceReference(), "Module namespace collision: %s. At %s",
113                     qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference());
114         }
115
116         final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(
117             stmt.coerceStatementArgument().getLocalName(), revisionDate);
118
119         stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
120         stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
121         stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
122
123         final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
124         SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
125             "Prefix of the module [%s] is missing", stmt.getStatementArgument());
126
127         stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
128         stmt.addToNs(ModuleNameToModuleQName.class, stmt.getStatementArgument().getLocalName(), qNameModule);
129         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
130         stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
131         stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument().getLocalName());
132         stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
133
134         if (stmt.isEnabledSemanticVersioning()) {
135             addToSemVerModuleNamespace(stmt, moduleIdentifier);
136         }
137     }
138
139     @Override
140     protected final ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
141             final StmtContext<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> ctx,
142             final List<? extends StmtContext<?, ?, ?>> substatements) {
143         final ImmutableList<? extends EffectiveStatement<?, ?>> local =
144                 super.buildEffectiveSubstatements(ctx, substatements);
145         final Collection<StmtContext<?, ?, ?>> submodules = submoduleContexts(ctx);
146         if (submodules.isEmpty()) {
147             return local;
148         }
149
150         // Concatenate statements so they appear as if they were part of target module
151         final List<EffectiveStatement<?, ?>> others = new ArrayList<>();
152         for (StmtContext<?, ?, ?> submoduleCtx : submodules) {
153             for (EffectiveStatement<?, ?> effective : submoduleCtx.buildEffective().effectiveSubstatements()) {
154                 if (effective instanceof SchemaNode || effective instanceof DataNodeContainer) {
155                     others.add(effective);
156                 }
157             }
158         }
159
160         return ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(local.size() + others.size())
161                 .addAll(local)
162                 .addAll(others)
163                 .build();
164     }
165
166     @Override
167     protected final ModuleStatement createDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx,
168             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
169         return new ModuleStatementImpl(ctx, substatements);
170     }
171
172     @Override
173     protected final ModuleStatement createEmptyDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx) {
174         throw noNamespace(ctx);
175     }
176
177     @Override
178     protected final ModuleEffectiveStatement createEffective(
179             final StmtContext<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> ctx,
180             final ModuleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
181         final List<Submodule> submodules = new ArrayList<>();
182         for (StmtContext<?, ?, ?> submoduleCtx : submoduleContexts(ctx)) {
183             final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
184             verify(submodule instanceof Submodule, "Submodule statement %s is not a Submodule", submodule);
185             submodules.add((Submodule) submodule);
186         }
187
188         return new ModuleEffectiveStatementImpl(ctx, declared, substatements, submodules);
189     }
190
191     @Override
192     protected final ModuleEffectiveStatement createEmptyEffective(
193             final StmtContext<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> ctx,
194             final ModuleStatement declared) {
195         throw noNamespace(ctx);
196     }
197
198     private static Collection<StmtContext<?, ?, ?>> submoduleContexts(final StmtContext<?, ?, ?> ctx) {
199         final Map<String, StmtContext<?, ?, ?>> submodules = ctx.getAllFromCurrentStmtCtxNamespace(
200             IncludedSubmoduleNameToModuleCtx.class);
201         return submodules == null ? List.of() : submodules.values();
202     }
203
204     private static SourceException noNamespace(final StmtContext<?, ?, ?> ctx) {
205         return new SourceException("No namespace declared in module", ctx.getStatementSourceReference());
206     }
207
208     private static void addToSemVerModuleNamespace(
209             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt,
210             final SourceIdentifier moduleIdentifier) {
211         final String moduleName = stmt.coerceStatementArgument().getLocalName();
212         final SemVer moduleSemVer = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt);
213         final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(moduleName, moduleSemVer);
214         stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);
215     }
216 }