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, CopyPolicy.REJECT);
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, e);
77 public final void onPreLinkageDeclared(
78 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
79 final String moduleName = stmt.getRawArgument();
81 final URI moduleNs = SourceException.throwIfNull(
82 firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class), stmt,
83 "Namespace of the module [%s] is missing", moduleName);
84 stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
86 final String modulePrefix = SourceException.throwIfNull(
87 firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt,
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 URI moduleNs = SourceException.throwIfNull(
105 firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class), stmt,
106 "Namespace of the module [%s] is missing", stmt.argument());
108 final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
109 final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
110 final StmtContext<?, ModuleStatement, ModuleEffectiveStatement> possibleDuplicateModule =
111 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
112 if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
113 throw new SourceException(stmt, "Module namespace collision: %s. At %s", qNameModule.getNamespace(),
114 possibleDuplicateModule.sourceReference());
117 final String moduleName = stmt.getRawArgument();
118 final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(moduleName, revisionDate);
120 stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
121 stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
122 stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
124 final String modulePrefix = SourceException.throwIfNull(
125 firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt,
126 "Prefix of the module [%s] is missing", stmt.argument());
128 stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
129 stmt.addToNs(ModuleNameToModuleQName.class, moduleName, qNameModule);
130 stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
131 stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
132 stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, moduleName);
133 stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
135 if (stmt.isEnabledSemanticVersioning()) {
136 addToSemVerModuleNamespace(stmt, moduleIdentifier);
141 protected final ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
142 final Current<UnqualifiedQName, ModuleStatement> stmt,
143 final List<? extends StmtContext<?, ?, ?>> substatements) {
144 final ImmutableList<? extends EffectiveStatement<?, ?>> local =
145 super.buildEffectiveSubstatements(stmt, substatements);
146 final Collection<StmtContext<?, ?, ?>> submodules = submoduleContexts(stmt);
147 if (submodules.isEmpty()) {
151 // Concatenate statements so they appear as if they were part of target module
152 final List<EffectiveStatement<?, ?>> others = new ArrayList<>();
153 for (StmtContext<?, ?, ?> submoduleCtx : submodules) {
154 for (EffectiveStatement<?, ?> effective : submoduleCtx.buildEffective().effectiveSubstatements()) {
155 if (effective instanceof SchemaNode || effective instanceof DataNodeContainer) {
156 others.add(effective);
161 return ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(local.size() + others.size())
168 protected final ModuleStatement createDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx,
169 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
170 return new ModuleStatementImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
174 protected final ModuleStatement createEmptyDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx) {
175 throw noNamespace(ctx);
179 protected final ModuleEffectiveStatement createEffective(final Current<UnqualifiedQName, ModuleStatement> stmt,
180 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
181 if (substatements.isEmpty()) {
182 throw noNamespace(stmt);
185 final List<Submodule> submodules = new ArrayList<>();
186 for (StmtContext<?, ?, ?> submoduleCtx : submoduleContexts(stmt)) {
187 final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
188 verify(submodule instanceof Submodule, "Submodule statement %s is not a Submodule", submodule);
189 submodules.add((Submodule) submodule);
193 return new ModuleEffectiveStatementImpl(stmt, substatements, submodules);
194 } catch (SubstatementIndexingException e) {
195 throw new SourceException(e.getMessage(), stmt, e);
199 private static Collection<StmtContext<?, ?, ?>> submoduleContexts(final Current<?, ?> stmt) {
200 final Map<String, StmtContext<?, ?, ?>> submodules = stmt.getAllFromCurrentStmtCtxNamespace(
201 IncludedSubmoduleNameToModuleCtx.class);
202 return submodules == null ? List.of() : submodules.values();
205 private static SourceException noNamespace(final @NonNull CommonStmtCtx stmt) {
206 return new SourceException("No namespace declared in module", stmt);
209 private static void addToSemVerModuleNamespace(
210 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt,
211 final SourceIdentifier moduleIdentifier) {
212 final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(stmt.getRawArgument(),
213 stmt.getFromNamespace(SemanticVersionNamespace.class, stmt));
214 stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);