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.CommonStmtCtx;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
48 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
49 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
50 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
52 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier;
53 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
54 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
55 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
56 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
57 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
58 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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.sourceReference(), e);
76 public final void onPreLinkageDeclared(
77 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
78 final String moduleName = stmt.getRawArgument();
80 final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
81 SourceException.throwIfNull(moduleNs, stmt.sourceReference(),
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.sourceReference(),
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.getArgument().getLocalName(), revisionDate));
100 public final void onLinkageDeclared(
101 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
103 final Optional<URI> moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(),
104 NamespaceStatement.class));
105 SourceException.throwIf(!moduleNs.isPresent(), stmt.sourceReference(),
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.get(), 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.sourceReference(), "Module namespace collision: %s. At %s",
114 qNameModule.getNamespace(), 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 = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
125 SourceException.throwIfNull(modulePrefix, stmt.sourceReference(),
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);
192 return new ModuleEffectiveStatementImpl(stmt, substatements, submodules);
195 private static Collection<StmtContext<?, ?, ?>> submoduleContexts(final Current<?, ?> stmt) {
196 final Map<String, StmtContext<?, ?, ?>> submodules = stmt.getAllFromCurrentStmtCtxNamespace(
197 IncludedSubmoduleNameToModuleCtx.class);
198 return submodules == null ? List.of() : submodules.values();
201 private static SourceException noNamespace(final @NonNull CommonStmtCtx stmt) {
202 return new SourceException("No namespace declared in module", stmt.sourceReference());
205 private static void addToSemVerModuleNamespace(
206 final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt,
207 final SourceIdentifier moduleIdentifier) {
208 final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(stmt.getRawArgument(),
209 stmt.getFromNamespace(SemanticVersionNamespace.class, stmt));
210 stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);