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