Remove statement filter caches from AbstractEffectiveModule
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / module / ModuleStmtContext.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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 java.util.Objects.requireNonNull;
12
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;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.Set;
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;
41
42 /**
43  * A concentrator {@link StmtContext}, which makes it appear as though as all effective statements in submodules
44  * are included in it.
45  */
46 final class ModuleStmtContext extends ForwardingObject
47         implements StmtContext<String, ModuleStatement, ModuleEffectiveStatement> {
48
49     private final @NonNull StmtContext<String, ModuleStatement, ModuleEffectiveStatement> delegate;
50     private final @NonNull ImmutableList<StmtContext<?, ?, ?>> effectiveSubstatements;
51     private final @NonNull ImmutableSet<Module> submodules;
52
53     private ModuleStmtContext(final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> delegate,
54             final Collection<StmtContext<?, ?, ?>> submodules) {
55         this.delegate = requireNonNull(delegate);
56
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);
63
64             for (StmtContext<?, ?, ?> stmt : submoduleCtx.allSubstatements()) {
65                 if (stmt.isSupportedByFeatures()) {
66                     final EffectiveStatement<?, ?> effective = stmt.buildEffective();
67                     if (effective instanceof SchemaNode || effective instanceof DataNodeContainer) {
68                         statements.add(stmt);
69                     }
70                 }
71             }
72         }
73
74         this.effectiveSubstatements = ImmutableList.copyOf(statements);
75         this.submodules = ImmutableSet.copyOf(subs);
76     }
77
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());
84     }
85
86     @Override
87     protected @NonNull StmtContext<String, ModuleStatement, ModuleEffectiveStatement> delegate() {
88         return delegate;
89     }
90
91     ImmutableSet<Module> getSubmodules() {
92         return submodules;
93     }
94
95     @Override
96     public ImmutableList<StmtContext<?, ?, ?>> effectiveSubstatements() {
97         return effectiveSubstatements;
98     }
99
100     @Override
101     public ModuleEffectiveStatement buildEffective() {
102         throw new UnsupportedOperationException("Attempted to instantiate proxy context " + this);
103     }
104
105     @Override
106     public ModuleStatement buildDeclared() {
107         return delegate.buildDeclared();
108     }
109
110     @Override
111     public StatementSource getStatementSource() {
112         return delegate.getStatementSource();
113     }
114
115     @Override
116     public StatementSourceReference getStatementSourceReference() {
117         return delegate.getStatementSourceReference();
118     }
119
120     @Override
121     public StatementDefinition getPublicDefinition() {
122         return delegate.getPublicDefinition();
123     }
124
125     @Override
126     public StmtContext<?, ?, ?> getParentContext() {
127         return delegate.getParentContext();
128     }
129
130     @Override
131     public String rawStatementArgument() {
132         return delegate.rawStatementArgument();
133     }
134
135     @Override
136     public @Nullable String getStatementArgument() {
137         return delegate.getStatementArgument();
138     }
139
140     @Override
141     public @NonNull Optional<SchemaPath> getSchemaPath() {
142         return delegate.getSchemaPath();
143     }
144
145     @Override
146     public boolean isConfiguration() {
147         return delegate.isConfiguration();
148     }
149
150     @Override
151     public boolean isEnabledSemanticVersioning() {
152         return delegate.isEnabledSemanticVersioning();
153     }
154
155     @Override
156     public <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type,
157             final T key) {
158         return delegate.getFromNamespace(type, key);
159     }
160
161     @Override
162     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
163         return delegate.getAllFromNamespace(type);
164     }
165
166     @Override
167     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
168             final Class<N> type) {
169         return delegate.getAllFromCurrentStmtCtxNamespace(type);
170     }
171
172     @Override
173     public StmtContext<?, ?, ?> getRoot() {
174         return delegate.getRoot();
175     }
176
177     @Override
178     public Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
179         return delegate.declaredSubstatements();
180     }
181
182     @Override
183     public boolean isSupportedToBuildEffective() {
184         return delegate.isSupportedToBuildEffective();
185     }
186
187     @Override
188     public boolean isSupportedByFeatures() {
189         return delegate.isSupportedByFeatures();
190     }
191
192     @Override
193     public Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement() {
194         return delegate.getEffectOfStatement();
195     }
196
197     @Override
198     public CopyHistory getCopyHistory() {
199         return delegate.getCopyHistory();
200     }
201
202     @Override
203     public Optional<StmtContext<String, ModuleStatement, ModuleEffectiveStatement>> getOriginalCtx() {
204         return delegate.getOriginalCtx();
205     }
206
207     @Override
208     public Optional<StmtContext<String, ModuleStatement, ModuleEffectiveStatement>> getPreviousCopyCtx() {
209         return delegate.getPreviousCopyCtx();
210     }
211
212     @Override
213     public ModelProcessingPhase getCompletedPhase() {
214         return delegate.getCompletedPhase();
215     }
216
217     @Override
218     public @NonNull YangVersion getRootVersion() {
219         return delegate.getRootVersion();
220     }
221 }