Tighten {Inferred,Substatement}Context.getParentNamespaceStorage()
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / InferredStatementContext.java
1 /*
2  * Copyright (c) 2020 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.stmt.reactor;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Stream;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * A statement which has been inferred to exist. Functionally it is equivalent to a SubstatementContext, but it is not
36  * backed by a declaration (and declared statements). It is backed by a prototype StatementContextBase and has only
37  * effective substatements, which are either transformed from that prototype or added by inference.
38  */
39 final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
40         extends StatementContextBase<A, D, E> {
41     private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
42
43     private final @NonNull StatementContextBase<A, D, E> prototype;
44     private final @NonNull StatementContextBase<?, ?, ?> parent;
45     private final @NonNull StmtContext<A, D, E> originalCtx;
46     private final @NonNull CopyType childCopyType;
47     private final QNameModule targetModule;
48     private final A argument;
49
50     private InferredStatementContext(final InferredStatementContext<A, D, E> original,
51             final StatementContextBase<?, ?, ?> parent) {
52         super(original);
53         this.parent = requireNonNull(parent);
54         this.childCopyType = original.childCopyType;
55         this.targetModule = original.targetModule;
56         this.prototype = original.prototype;
57         this.originalCtx = original.originalCtx;
58         this.argument = original.argument;
59     }
60
61     InferredStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> prototype,
62             final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
63         super(prototype.definition(), CopyHistory.of(myCopyType, prototype.getCopyHistory()));
64         this.parent = requireNonNull(parent);
65         this.prototype = requireNonNull(prototype);
66         this.argument = targetModule == null ? prototype.getStatementArgument()
67                 : prototype.definition().adaptArgumentValue(prototype, targetModule);
68         this.childCopyType = requireNonNull(childCopyType);
69         this.targetModule = targetModule;
70         this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
71
72         // FIXME: YANGTOOLS-784: instantiate these lazily
73         addEffectiveSubstatements(createEffective());
74     }
75
76     @Override
77     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
78         return ImmutableList.of();
79     }
80
81     @Override
82     public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
83         // No need to concat with declared
84         return effectiveSubstatements();
85     }
86
87     @Override
88     public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
89         // No need to concat with declared
90         return effectiveSubstatements().stream();
91     }
92
93     @Override
94     public StatementSourceReference getStatementSourceReference() {
95         return originalCtx.getStatementSourceReference();
96     }
97
98     @Override
99     public String rawStatementArgument() {
100         return originalCtx.rawStatementArgument();
101     }
102
103     @Override
104     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
105         return Optional.of(originalCtx);
106     }
107
108     @Override
109     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
110         return Optional.of(prototype);
111     }
112
113     @Override
114     public D buildDeclared() {
115         /*
116          * Share original instance of declared statement between all effective statements which have been copied or
117          * derived from this original declared statement.
118          */
119         return originalCtx.buildDeclared();
120     }
121
122     @Override
123     InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
124         return new InferredStatementContext<>(this, newParent);
125     }
126
127     @Override
128     boolean hasEmptySubstatements() {
129         return hasEmptyEffectiveSubstatements();
130     }
131
132     // Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
133     // BuildGlobalContext, hence it must be called at most once.
134     private List<Mutable<?, ?, ?>> createEffective() {
135         final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
136         final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
137         final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
138
139         for (final Mutable<?, ?, ?> stmtContext : declared) {
140             if (stmtContext.isSupportedByFeatures()) {
141                 copySubstatement(stmtContext, buffer);
142             }
143         }
144         for (final Mutable<?, ?, ?> stmtContext : effective) {
145             copySubstatement(stmtContext, buffer);
146         }
147
148         return buffer;
149     }
150
151     // Statement copy mess starts here
152     //
153     // FIXME: This is messy and is probably wrong in some corner case. Even if it is correct, the way how it is correct
154     //        relies on hard-coded maps. At the end of the day, the logic needs to be controlled by statement's
155     //        StatementSupport.
156     // FIXME: YANGTOOLS-652: this map looks very much like UsesStatementSupport.TOP_REUSED_DEF_SET
157     private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
158         YangStmtMapping.TYPE,
159         YangStmtMapping.TYPEDEF,
160         YangStmtMapping.USES);
161
162     private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer) {
163         final StatementDefinition def = substatement.getPublicDefinition();
164
165         // FIXME: YANGTOOLS-652: formerly known as "isReusedByUses"
166         if (REUSED_DEF_SET.contains(def)) {
167             LOG.debug("Reusing substatement {} for {}", substatement, this);
168             buffer.add(substatement);
169             return;
170         }
171
172         substatement.copyAsChildOf(this, childCopyType, targetModule).ifPresent(buffer::add);
173     }
174
175     // Statement copy mess ends here
176
177     /*
178      * KEEP THINGS ORGANIZED!
179      *
180      * below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
181      * properly updated there.
182      */
183     @Override
184     @Deprecated
185     public Optional<SchemaPath> getSchemaPath() {
186         return substatementGetSchemaPath();
187     }
188
189     @Override
190     public A getStatementArgument() {
191         return argument;
192     }
193
194     @Override
195     public StatementContextBase<?, ?, ?> getParentContext() {
196         return parent;
197     }
198
199     @Override
200     public StorageNodeType getStorageNodeType() {
201         return StorageNodeType.STATEMENT_LOCAL;
202     }
203
204     @Override
205     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
206         return parent;
207     }
208
209     @Override
210     public RootStatementContext<?, ?, ?> getRoot() {
211         return parent.getRoot();
212     }
213
214     @Override
215     public boolean isConfiguration() {
216         return isConfiguration(parent);
217     }
218
219     @Override
220     protected boolean isIgnoringIfFeatures() {
221         return isIgnoringIfFeatures(parent);
222     }
223
224     @Override
225     protected boolean isIgnoringConfig() {
226         return isIgnoringConfig(parent);
227     }
228
229     @Override
230     protected boolean isParentSupportedByFeatures() {
231         return parent.isSupportedByFeatures();
232     }
233 }