Refactor {Module,Submodule}EffectiveStatementImpl
[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.opendaylight.yangtools.concepts.SemVer;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
33 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
34 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
37 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
38 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
39 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
45 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
46 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
47 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
48 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
49 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier;
50 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
52 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
53 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
54 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
55 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
56
57 abstract class AbstractModuleStatementSupport
58         extends BaseStatementSupport<String, ModuleStatement, ModuleEffectiveStatement> {
59     AbstractModuleStatementSupport() {
60         super(YangStmtMapping.MODULE);
61     }
62
63     @Override
64     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
65         return value;
66     }
67
68     @Override
69     public final void onPreLinkageDeclared(final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt) {
70         final String moduleName = stmt.getStatementArgument();
71
72         final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
73         SourceException.throwIfNull(moduleNs, stmt.getStatementSourceReference(),
74             "Namespace of the module [%s] is missing", stmt.getStatementArgument());
75         stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
76
77         final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
78         SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
79             "Prefix of the module [%s] is missing", stmt.getStatementArgument());
80         stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
81
82         stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
83
84         final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
85         final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
86
87         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
88         stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getStatementArgument(), revisionDate));
89     }
90
91     @Override
92     public final void onLinkageDeclared(final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt) {
93
94         final Optional<URI> moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(),
95                 NamespaceStatement.class));
96         SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(),
97             "Namespace of the module [%s] is missing", stmt.getStatementArgument());
98
99         final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
100         final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern();
101         final StmtContext<?, ModuleStatement, ModuleEffectiveStatement> possibleDuplicateModule =
102                 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
103         if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
104             throw new SourceException(stmt.getStatementSourceReference(), "Module namespace collision: %s. At %s",
105                     qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference());
106         }
107
108         final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(stmt.getStatementArgument(),
109                 revisionDate);
110
111         stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
112         stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
113         stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
114
115         final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
116         SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
117             "Prefix of the module [%s] is missing", stmt.getStatementArgument());
118
119         stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
120         stmt.addToNs(ModuleNameToModuleQName.class, stmt.getStatementArgument(), qNameModule);
121         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
122         stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
123         stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument());
124         stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
125
126         if (stmt.isEnabledSemanticVersioning()) {
127             addToSemVerModuleNamespace(stmt, moduleIdentifier);
128         }
129     }
130
131     @Override
132     protected final ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
133             final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx,
134             final List<? extends StmtContext<?, ?, ?>> substatements) {
135         final ImmutableList<? extends EffectiveStatement<?, ?>> local =
136                 super.buildEffectiveSubstatements(ctx, substatements);
137         final Collection<StmtContext<?, ?, ?>> submodules = submoduleContexts(ctx);
138         if (submodules.isEmpty()) {
139             return local;
140         }
141
142         // Concatenate statements so they appear as if they were part of target module
143         final List<EffectiveStatement<?, ?>> others = new ArrayList<>();
144         for (StmtContext<?, ?, ?> submoduleCtx : submodules) {
145             for (EffectiveStatement<?, ?> effective : submoduleCtx.buildEffective().effectiveSubstatements()) {
146                 if (effective instanceof SchemaNode || effective instanceof DataNodeContainer) {
147                     others.add(effective);
148                 }
149             }
150         }
151
152         return ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(local.size() + others.size())
153                 .addAll(local)
154                 .addAll(others)
155                 .build();
156     }
157
158     @Override
159     protected final ModuleStatement createDeclared(final StmtContext<String, ModuleStatement, ?> ctx,
160             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
161         return new ModuleStatementImpl(ctx.coerceRawStatementArgument(), substatements);
162     }
163
164     @Override
165     protected final ModuleStatement createEmptyDeclared(final StmtContext<String, ModuleStatement, ?> ctx) {
166         throw noNamespace(ctx);
167     }
168
169     @Override
170     protected final ModuleEffectiveStatement createEffective(
171             final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx,
172             final ModuleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
173         final List<Module> submodules = new ArrayList<>();
174         for (StmtContext<?, ?, ?> submoduleCtx : submoduleContexts(ctx)) {
175             final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
176             verify(submodule instanceof Module, "Submodule statement %s is not a Module", submodule);
177             submodules.add((Module) submodule);
178         }
179
180         return new ModuleEffectiveStatementImpl(ctx, declared, substatements, submodules);
181     }
182
183     @Override
184     protected final ModuleEffectiveStatement createEmptyEffective(
185             final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx, final ModuleStatement declared) {
186         throw noNamespace(ctx);
187     }
188
189     private static Collection<StmtContext<?, ?, ?>> submoduleContexts(final StmtContext<?, ?, ?> ctx) {
190         final Map<String, StmtContext<?, ?, ?>> submodules = ctx.getAllFromCurrentStmtCtxNamespace(
191             IncludedSubmoduleNameToModuleCtx.class);
192         return submodules == null ? List.of() : submodules.values();
193     }
194
195     private static SourceException noNamespace(final StmtContext<?, ?, ?> ctx) {
196         return new SourceException("No namespace declared in module", ctx.getStatementSourceReference());
197     }
198
199     private static void addToSemVerModuleNamespace(
200             final Mutable<String, ModuleStatement, ModuleEffectiveStatement> stmt,
201             final SourceIdentifier moduleIdentifier) {
202         final String moduleName = stmt.coerceStatementArgument();
203         final SemVer moduleSemVer = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt);
204         final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(moduleName, moduleSemVer);
205         stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);
206     }
207 }