2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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 java.util.Objects.requireNonNull;
13 import com.google.common.collect.ForwardingObject;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.LinkedHashSet;
19 import java.util.List;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.yangtools.yang.common.YangVersion;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
32 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
33 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
34 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
39 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
40 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
43 * A concentrator {@link StmtContext}, which makes it appear as though as all effective statements in submodules
46 final class ModuleStmtContext extends ForwardingObject
47 implements StmtContext<String, ModuleStatement, ModuleEffectiveStatement> {
49 private final @NonNull StmtContext<String, ModuleStatement, ModuleEffectiveStatement> delegate;
50 private final @NonNull ImmutableList<StmtContext<?, ?, ?>> effectiveSubstatements;
51 private final @NonNull ImmutableSet<Module> submodules;
53 private ModuleStmtContext(final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> delegate,
54 final Collection<StmtContext<?, ?, ?>> submodules) {
55 this.delegate = requireNonNull(delegate);
57 final List<StmtContext<?, ?, ?>> statements = new ArrayList<>(delegate.effectiveSubstatements());
58 final Set<Module> subs = new LinkedHashSet<>(submodules.size());
59 for (StmtContext<?, ?, ?> submoduleCtx : submodules) {
60 final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
61 verify(submodule instanceof Module, "Submodule statement %s is not a Module", submodule);
62 subs.add((Module) submodule);
64 for (StmtContext<?, ?, ?> stmt : submoduleCtx.allSubstatements()) {
65 if (stmt.isSupportedByFeatures()) {
66 final EffectiveStatement<?, ?> effective = stmt.buildEffective();
67 if (effective instanceof SchemaNode || effective instanceof DataNodeContainer) {
74 this.effectiveSubstatements = ImmutableList.copyOf(statements);
75 this.submodules = ImmutableSet.copyOf(subs);
78 static @NonNull ModuleStmtContext create(
79 final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> delegate) {
80 final Map<String, StmtContext<?, ?, ?>> includedSubmodules = delegate.getAllFromCurrentStmtCtxNamespace(
81 IncludedSubmoduleNameToModuleCtx.class);
82 return new ModuleStmtContext(delegate, includedSubmodules == null || includedSubmodules.isEmpty()
83 ? ImmutableList.of() : includedSubmodules.values());
87 protected @NonNull StmtContext<String, ModuleStatement, ModuleEffectiveStatement> delegate() {
91 ImmutableSet<Module> getSubmodules() {
96 public ImmutableList<StmtContext<?, ?, ?>> effectiveSubstatements() {
97 return effectiveSubstatements;
101 public ModuleEffectiveStatement buildEffective() {
102 throw new UnsupportedOperationException("Attempted to instantiate proxy context " + this);
106 public ModuleStatement buildDeclared() {
107 return delegate.buildDeclared();
111 public StatementSource getStatementSource() {
112 return delegate.getStatementSource();
116 public StatementSourceReference getStatementSourceReference() {
117 return delegate.getStatementSourceReference();
121 public StatementDefinition getPublicDefinition() {
122 return delegate.getPublicDefinition();
126 public StmtContext<?, ?, ?> getParentContext() {
127 return delegate.getParentContext();
131 public String rawStatementArgument() {
132 return delegate.rawStatementArgument();
136 public @Nullable String getStatementArgument() {
137 return delegate.getStatementArgument();
141 public @NonNull Optional<SchemaPath> getSchemaPath() {
142 return delegate.getSchemaPath();
146 public boolean isConfiguration() {
147 return delegate.isConfiguration();
151 public boolean isEnabledSemanticVersioning() {
152 return delegate.isEnabledSemanticVersioning();
156 public <K, V, T extends K, N extends IdentifierNamespace<K, V>> @NonNull V getFromNamespace(final Class<N> type,
158 return delegate.getFromNamespace(type, key);
162 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
163 return delegate.getAllFromNamespace(type);
167 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
168 final Class<N> type) {
169 return delegate.getAllFromCurrentStmtCtxNamespace(type);
173 public StmtContext<?, ?, ?> getRoot() {
174 return delegate.getRoot();
178 public Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
179 return delegate.declaredSubstatements();
183 public boolean isSupportedToBuildEffective() {
184 return delegate.isSupportedToBuildEffective();
188 public boolean isSupportedByFeatures() {
189 return delegate.isSupportedByFeatures();
193 public Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement() {
194 return delegate.getEffectOfStatement();
198 public CopyHistory getCopyHistory() {
199 return delegate.getCopyHistory();
203 public Optional<StmtContext<?, ?, ?>> getOriginalCtx() {
204 return delegate.getOriginalCtx();
208 public Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx() {
209 return delegate.getPreviousCopyCtx();
213 public ModelProcessingPhase getCompletedPhase() {
214 return delegate.getCompletedPhase();
218 public @NonNull YangVersion getRootVersion() {
219 return delegate.getRootVersion();