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.rfc7950.stmt.SubstatementIndexingException;
39 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
41 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
49 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
50 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
51 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
52 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
53 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier;
54 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
55 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
56 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
57 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
58 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
59 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
61 abstract class AbstractModuleStatementSupport
62 extends BaseStatementSupport<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> {
63 AbstractModuleStatementSupport() {
64 super(YangStmtMapping.MODULE);
68 public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
70 return UnqualifiedQName.of(value);
71 } catch (IllegalArgumentException e) {
72 throw new SourceException(e.getMessage(), ctx.sourceReference(), e);
77 public final void onPreLinkageDeclared(
78 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
79 final String moduleName = stmt.getRawArgument();
81 final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
82 SourceException.throwIfNull(moduleNs, stmt.sourceReference(),
83 "Namespace of the module [%s] is missing", moduleName);
84 stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
86 final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
87 SourceException.throwIfNull(modulePrefix, stmt.sourceReference(),
88 "Prefix of the module [%s] is missing", moduleName);
89 stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
91 stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
93 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
94 final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
96 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
97 stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getArgument().getLocalName(), revisionDate));
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.sourceReference(),
107 "Namespace of the module [%s] is missing", stmt.argument());
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.sourceReference(), "Module namespace collision: %s. At %s",
115 qNameModule.getNamespace(), possibleDuplicateModule.sourceReference());
118 final String moduleName = stmt.getRawArgument();
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.sourceReference(),
127 "Prefix of the module [%s] is missing", stmt.argument());
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.getRawArgument(), ctx.getArgument(), substatements);
175 protected final ModuleStatement createEmptyDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx) {
176 throw noNamespace(ctx);
180 protected final ModuleEffectiveStatement createEffective(final Current<UnqualifiedQName, ModuleStatement> stmt,
181 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
182 if (substatements.isEmpty()) {
183 throw noNamespace(stmt);
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);
194 return new ModuleEffectiveStatementImpl(stmt, substatements, submodules);
195 } catch (SubstatementIndexingException e) {
196 throw new SourceException(e.getMessage(), stmt.sourceReference(), e);
200 private static Collection<StmtContext<?, ?, ?>> submoduleContexts(final Current<?, ?> stmt) {
201 final Map<String, StmtContext<?, ?, ?>> submodules = stmt.getAllFromCurrentStmtCtxNamespace(
202 IncludedSubmoduleNameToModuleCtx.class);
203 return submodules == null ? List.of() : submodules.values();
206 private static SourceException noNamespace(final @NonNull CommonStmtCtx stmt) {
207 return new SourceException("No namespace declared in module", stmt.sourceReference());
210 private static void addToSemVerModuleNamespace(
211 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt,
212 final SourceIdentifier moduleIdentifier) {
213 final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(stmt.getRawArgument(),
214 stmt.getFromNamespace(SemanticVersionNamespace.class, stmt));
215 stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);