Do not use StatementSourceReference in AbstractDeclaredEffectiveStatement
[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.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;
60
61 abstract class AbstractModuleStatementSupport
62         extends BaseStatementSupport<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> {
63     AbstractModuleStatementSupport() {
64         super(YangStmtMapping.MODULE);
65     }
66
67     @Override
68     public final UnqualifiedQName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
69         try {
70             return UnqualifiedQName.of(value);
71         } catch (IllegalArgumentException e) {
72             throw new SourceException(e.getMessage(), ctx.sourceReference(), e);
73         }
74     }
75
76     @Override
77     public final void onPreLinkageDeclared(
78             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
79         final String moduleName = stmt.getRawArgument();
80
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);
85
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);
90
91         stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
92
93         final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
94         final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
95
96         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
97         stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getArgument().getLocalName(), revisionDate));
98     }
99
100     @Override
101     public final void onLinkageDeclared(
102             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
103
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());
108
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());
116         }
117
118         final String moduleName = stmt.getRawArgument();
119         final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(moduleName, revisionDate);
120
121         stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
122         stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
123         stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
124
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());
128
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);
135
136         if (stmt.isEnabledSemanticVersioning()) {
137             addToSemVerModuleNamespace(stmt, moduleIdentifier);
138         }
139     }
140
141     @Override
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()) {
149             return local;
150         }
151
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);
158                 }
159             }
160         }
161
162         return ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(local.size() + others.size())
163                 .addAll(local)
164                 .addAll(others)
165                 .build();
166     }
167
168     @Override
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);
172     }
173
174     @Override
175     protected final ModuleStatement createEmptyDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx) {
176         throw noNamespace(ctx);
177     }
178
179     @Override
180     protected final ModuleEffectiveStatement createEffective(final Current<UnqualifiedQName, ModuleStatement> stmt,
181             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
182         if (substatements.isEmpty()) {
183             throw noNamespace(stmt);
184         }
185
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);
191         }
192
193         try {
194             return new ModuleEffectiveStatementImpl(stmt, substatements, submodules);
195         } catch (SubstatementIndexingException e) {
196             throw new SourceException(e.getMessage(), stmt.sourceReference(), e);
197         }
198     }
199
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();
204     }
205
206     private static SourceException noNamespace(final @NonNull CommonStmtCtx stmt) {
207         return new SourceException("No namespace declared in module", stmt.sourceReference());
208     }
209
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);
216     }
217 }