c5c7d3862a41638657956bc0966f891b29d10c52
[yangtools.git] / yang / 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 com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
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.ModelProcessingPhase;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
29
30 /**
31  * A replica of a different statement. It does not allow modification, but produces an effective statement from a
32  * designated source.
33  */
34 final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
35         extends ReactorStmtCtx<A, D, E> {
36     private final StatementContextBase<?, ?, ?> parent;
37     private final ReactorStmtCtx<A, D, E> source;
38
39     ReplicaStatementContext(final StatementContextBase<?, ?, ?> parent, final ReactorStmtCtx<A, D, E> source) {
40         super(source);
41         this.parent = requireNonNull(parent);
42         this.source = requireNonNull(source);
43         if (source.isSupportedToBuildEffective()) {
44             verify(source.fullyDefined(), "Source %s is not fully defined", source);
45             source.incRef();
46         }
47     }
48
49     @Override
50     E createEffective() {
51         return source.buildEffective();
52     }
53
54     @Override
55     public EffectiveConfig effectiveConfig() {
56         return source.effectiveConfig();
57     }
58
59     @Override
60     public D declared() {
61         return source.declared();
62     }
63
64     @Override
65     public A argument() {
66         return source.argument();
67     }
68
69     @Override
70     public StatementSourceReference sourceReference() {
71         return source.sourceReference();
72     }
73
74     @Override
75     public String rawArgument() {
76         return source.rawArgument();
77     }
78
79     @Override
80     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
81         return source.getOriginalCtx();
82     }
83
84     @Override
85     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
86         return source.mutableDeclaredSubstatements();
87     }
88
89     @Override
90     public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
91         return source.mutableEffectiveSubstatements();
92     }
93
94     @Override
95     public ModelProcessingPhase getCompletedPhase() {
96         return source.getCompletedPhase();
97     }
98
99     @Override
100     public CopyHistory history() {
101         return source.history();
102     }
103
104     @Override
105     public Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement() {
106         return List.of();
107     }
108
109     @Override
110     public Mutable<A, D, E> replicaAsChildOf(final Mutable<?, ?, ?> newParent) {
111         return source.replicaAsChildOf(newParent);
112     }
113
114     @Override
115     public Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> newParent, final CopyType type,
116             final QNameModule targetModule) {
117         return source.copyAsChildOf(newParent, type, targetModule);
118     }
119
120     @Override
121     ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(final StatementContextBase<?, ?, ?> newParent, final CopyType type,
122             final QNameModule targetModule) {
123         return source.asEffectiveChildOf(newParent, type, targetModule);
124     }
125
126     @Override
127     StatementDefinitionContext<A, D, E> definition() {
128         return source.definition();
129     }
130
131     @Override
132     void markNoParentRef() {
133         // No-op
134     }
135
136     @Override
137     int sweepSubstatements() {
138         if (fullyDefined()) {
139             source.decRef();
140         }
141         return 0;
142     }
143
144     @Override
145     public <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(final Class<@NonNull N> type,
146             final T key, final U value) {
147         throw new UnsupportedOperationException();
148     }
149
150     @Override
151     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
152         throw new UnsupportedOperationException();
153     }
154
155     @Override
156     public <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(final Class<@NonNull N> namespace,
157             final KT key, final StmtContext<?, ?, ?> stmt) {
158         throw new UnsupportedOperationException();
159     }
160
161     @Override
162     public void addAsEffectOfStatement(final Collection<? extends StmtContext<?, ?, ?>> ctxs) {
163         throw new UnsupportedOperationException();
164     }
165
166     @Override
167     public Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type,
168             final QNameModule targetModule) {
169         throw new UnsupportedOperationException();
170     }
171
172     @Override boolean doTryToCompletePhase(final ModelProcessingPhase phase) {
173         throw new UnsupportedOperationException();
174     }
175
176     /*
177      * KEEP THINGS ORGANIZED!
178      *
179      * below methods exist in the same form in InferredStatementContext/SubstatementContext. If any adjustment is made
180      * here, make sure it is properly updated there.
181      */
182     @Override
183     @Deprecated
184     public Optional<SchemaPath> schemaPath() {
185         return substatementGetSchemaPath();
186     }
187
188     @Override
189     public StatementContextBase<?, ?, ?> getParentContext() {
190         return parent;
191     }
192
193     @Override
194     public StorageNodeType getStorageNodeType() {
195         return StorageNodeType.STATEMENT_LOCAL;
196     }
197
198     @Override
199     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
200         return parent;
201     }
202
203     @Override
204     public RootStatementContext<?, ?, ?> getRoot() {
205         return parent.getRoot();
206     }
207
208     @Override
209     protected boolean isIgnoringIfFeatures() {
210         return isIgnoringIfFeatures(parent);
211     }
212
213     @Override
214     protected boolean isIgnoringConfig() {
215         return isIgnoringConfig(parent);
216     }
217
218     @Override
219     protected boolean isParentSupportedByFeatures() {
220         return parent.isSupportedByFeatures();
221     }
222 }