2 * Copyright (c) 2015 Cisco Systems, Inc. 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.module;
10 import static com.google.common.base.Verify.verify;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
13 import com.google.common.collect.ImmutableList;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
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.EffectiveStmtCtx.Current;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
47 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
48 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
49 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
50 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier;
52 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
53 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
54 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
55 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
56 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
57 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
58 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
60 abstract class AbstractModuleStatementSupport
61 extends BaseStatementSupport<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> {
62 AbstractModuleStatementSupport() {
63 super(YangStmtMapping.MODULE);
67 public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
69 return UnqualifiedQName.of(value);
70 } catch (IllegalArgumentException e) {
71 throw new SourceException(e.getMessage(), ctx.getStatementSourceReference(), e);
76 public final void onPreLinkageDeclared(
77 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
78 final String moduleName = stmt.coerceRawStatementArgument();
80 final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
81 SourceException.throwIfNull(moduleNs, stmt.getStatementSourceReference(),
82 "Namespace of the module [%s] is missing", moduleName);
83 stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
85 final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
86 SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
87 "Prefix of the module [%s] is missing", moduleName);
88 stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
90 stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
92 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
93 final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
95 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
96 stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.coerceStatementArgument().getLocalName(),
101 public final void onLinkageDeclared(
102 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
104 final Optional<URI> moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(),
105 NamespaceStatement.class));
106 SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(),
107 "Namespace of the module [%s] is missing", stmt.getStatementArgument());
109 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
110 final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern();
111 final StmtContext<?, ModuleStatement, ModuleEffectiveStatement> possibleDuplicateModule =
112 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
113 if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
114 throw new SourceException(stmt.getStatementSourceReference(), "Module namespace collision: %s. At %s",
115 qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference());
118 final String moduleName = stmt.coerceRawStatementArgument();
119 final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(moduleName, revisionDate);
121 stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
122 stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
123 stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
125 final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
126 SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
127 "Prefix of the module [%s] is missing", stmt.getStatementArgument());
129 stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
130 stmt.addToNs(ModuleNameToModuleQName.class, moduleName, qNameModule);
131 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
132 stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
133 stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, moduleName);
134 stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
136 if (stmt.isEnabledSemanticVersioning()) {
137 addToSemVerModuleNamespace(stmt, moduleIdentifier);
142 protected final ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
143 final Current<UnqualifiedQName, ModuleStatement> stmt,
144 final List<? extends StmtContext<?, ?, ?>> substatements) {
145 final ImmutableList<? extends EffectiveStatement<?, ?>> local =
146 super.buildEffectiveSubstatements(stmt, substatements);
147 final Collection<StmtContext<?, ?, ?>> submodules = submoduleContexts(stmt);
148 if (submodules.isEmpty()) {
152 // Concatenate statements so they appear as if they were part of target module
153 final List<EffectiveStatement<?, ?>> others = new ArrayList<>();
154 for (StmtContext<?, ?, ?> submoduleCtx : submodules) {
155 for (EffectiveStatement<?, ?> effective : submoduleCtx.buildEffective().effectiveSubstatements()) {
156 if (effective instanceof SchemaNode || effective instanceof DataNodeContainer) {
157 others.add(effective);
162 return ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(local.size() + others.size())
169 protected final ModuleStatement createDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx,
170 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
171 return new ModuleStatementImpl(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(), substatements);
175 protected final ModuleStatement createEmptyDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx) {
176 throw noNamespace(ctx.getStatementSourceReference());
180 protected final ModuleEffectiveStatement createEffective(final Current<UnqualifiedQName, ModuleStatement> stmt,
181 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
182 if (substatements.isEmpty()) {
183 throw noNamespace(stmt.sourceReference());
186 final List<Submodule> submodules = new ArrayList<>();
187 for (StmtContext<?, ?, ?> submoduleCtx : submoduleContexts(stmt)) {
188 final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
189 verify(submodule instanceof Submodule, "Submodule statement %s is not a Submodule", submodule);
190 submodules.add((Submodule) submodule);
193 return new ModuleEffectiveStatementImpl(stmt, substatements, submodules);
196 private static Collection<StmtContext<?, ?, ?>> submoduleContexts(final Current<?, ?> stmt) {
197 final Map<String, StmtContext<?, ?, ?>> submodules = stmt.getAllFromCurrentStmtCtxNamespace(
198 IncludedSubmoduleNameToModuleCtx.class);
199 return submodules == null ? List.of() : submodules.values();
202 private static SourceException noNamespace(final @NonNull StatementSourceReference ref) {
203 return new SourceException("No namespace declared in module", ref);
206 private static void addToSemVerModuleNamespace(
207 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt,
208 final SourceIdentifier moduleIdentifier) {
209 final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(stmt.coerceRawStatementArgument(),
210 stmt.getFromNamespace(SemanticVersionNamespace.class, stmt));
211 stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);