Define a feature-parent
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ReplicaStatementContext.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 java.util.Collection;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.stream.Stream;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27
28 /**
29  * A replica of a different statement. It does not allow modification, but produces an effective statement from a
30  * designated source.
31  */
32 final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
33         extends ReactorStmtCtx<A, D, E> {
34     private final StatementContextBase<?, ?, ?> parent;
35     private final ReactorStmtCtx<A, D, E> source;
36     // We need to drop source's reference count when we are being swept.
37     private final boolean haveSourceRef;
38
39     ReplicaStatementContext(final StatementContextBase<?, ?, ?> parent, final ReactorStmtCtx<A, D, E> source) {
40         super(source, null);
41         this.parent = requireNonNull(parent);
42         this.source = requireNonNull(source);
43         if (source.isSupportedToBuildEffective()) {
44             source.incRef();
45             haveSourceRef = true;
46         } else {
47             haveSourceRef = false;
48         }
49     }
50
51     @Override
52     E createEffective() {
53         return source.buildEffective();
54     }
55
56     @Override
57     E createInferredEffective(final StatementFactory<A, D, E> factory, final InferredStatementContext<A, D, E> ctx,
58             final Stream<? extends ReactorStmtCtx<?, ?, ?>> declared,
59             final Stream<? extends ReactorStmtCtx<?, ?, ?>> effective) {
60         return source.createInferredEffective(factory, ctx, declared, effective);
61     }
62
63     @Override
64     ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource() {
65         return source.unmodifiedEffectiveSource();
66     }
67
68     @Override
69     public EffectiveConfig effectiveConfig() {
70         return source.effectiveConfig();
71     }
72
73     @Override
74     public D declared() {
75         return source.declared();
76     }
77
78     @Override
79     public A argument() {
80         return source.argument();
81     }
82
83     @Override
84     public StatementSourceReference sourceReference() {
85         return source.sourceReference();
86     }
87
88     @Override
89     public String rawArgument() {
90         return source.rawArgument();
91     }
92
93     @Override
94     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
95         return source.getOriginalCtx();
96     }
97
98     @Override
99     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
100         return source.mutableDeclaredSubstatements();
101     }
102
103     @Override
104     public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
105         return source.mutableEffectiveSubstatements();
106     }
107
108     @Override
109     byte executionOrder() {
110         return source.executionOrder();
111     }
112
113     @Override
114     public CopyHistory history() {
115         return source.history();
116     }
117
118     @Override
119     public Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement() {
120         return List.of();
121     }
122
123     @Override
124     ReplicaStatementContext<A, D, E> replicaAsChildOf(final StatementContextBase<?, ?, ?> newParent) {
125         return source.replicaAsChildOf(newParent);
126     }
127
128     @Override
129     public Optional<Mutable<A, D, E>> copyAsChildOf(final Mutable<?, ?, ?> newParent, final CopyType type,
130             final QNameModule targetModule) {
131         return source.copyAsChildOf(newParent, type, targetModule);
132     }
133
134     @Override
135     ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(final StatementContextBase<?, ?, ?> newParent, final CopyType type,
136             final QNameModule targetModule) {
137         final ReactorStmtCtx<?, ?, ?> ret = source.asEffectiveChildOf(newParent, type, targetModule);
138         return ret == null ? null : this;
139     }
140
141     @Override
142     StatementDefinitionContext<A, D, E> definition() {
143         return source.definition();
144     }
145
146     @Override
147     void markNoParentRef() {
148         // No-op
149     }
150
151     @Override
152     int sweepSubstatements() {
153         if (haveSourceRef) {
154             source.decRef();
155         }
156         return 0;
157     }
158
159     @Override
160     @Deprecated
161     public <K, V> void addToNs(final ParserNamespace<K, V> type, final K key, final V value) {
162         throw new UnsupportedOperationException();
163     }
164
165     @Override
166     @Deprecated
167     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
168         throw new UnsupportedOperationException();
169     }
170
171     @Override
172     @Deprecated
173     public void addAsEffectOfStatement(final Collection<? extends StmtContext<?, ?, ?>> ctxs) {
174         throw new UnsupportedOperationException();
175     }
176
177     @Override
178     @Deprecated
179     public Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type,
180             final QNameModule targetModule) {
181         throw new UnsupportedOperationException();
182     }
183
184     @Override
185     @Deprecated
186     boolean doTryToCompletePhase(final byte executionOrder) {
187         throw new UnsupportedOperationException();
188     }
189
190     @Override
191     @Deprecated
192     public <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
193             Mutable<X, Y, Z> createUndeclaredSubstatement(final StatementSupport<X, Y, Z> support, final X arg) {
194         throw new UnsupportedOperationException();
195     }
196
197     @Override
198     @Deprecated
199     public void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
200         throw new UnsupportedOperationException();
201     }
202
203     @Override
204     @Deprecated
205     public void addEffectiveSubstatements(final Collection<? extends Mutable<?, ?, ?>> statements) {
206         throw new UnsupportedOperationException();
207     }
208
209     @Override
210     @Deprecated
211     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
212         throw new UnsupportedOperationException();
213     }
214
215     @Override
216     @Deprecated
217     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
218             final String statementArg) {
219         throw new UnsupportedOperationException();
220     }
221
222     @Override
223     @Deprecated
224     public boolean hasImplicitParentSupport() {
225         throw new UnsupportedOperationException();
226     }
227
228     @Override
229     @Deprecated
230     public StmtContext<?, ?, ?> wrapWithImplicit(final StmtContext<?, ?, ?> original) {
231         throw new UnsupportedOperationException();
232     }
233
234     /*
235      * KEEP THINGS ORGANIZED!
236      *
237      * below methods exist in the same form in InferredStatementContext/SubstatementContext. If any adjustment is made
238      * here, make sure it is properly updated there.
239      */
240     @Override
241     public StatementContextBase<?, ?, ?> getParentContext() {
242         return parent;
243     }
244
245     @Override
246     public StatementContextBase<?, ?, ?> getParentStorage() {
247         return parent;
248     }
249
250     @Override
251     public RootStatementContext<?, ?, ?> getRoot() {
252         return parent.getRoot();
253     }
254
255     @Override
256     protected boolean isIgnoringIfFeatures() {
257         return isIgnoringIfFeatures(parent);
258     }
259
260     @Override
261     protected boolean isIgnoringConfig() {
262         return isIgnoringConfig(parent);
263     }
264
265     @Override
266     boolean computeSupportedByFeatures() {
267         return source.isSupportedByFeatures();
268     }
269
270     @Override
271     protected boolean isParentSupportedByFeatures() {
272         return parent.isSupportedByFeatures();
273     }
274 }