2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import static java.util.Objects.requireNonNull;
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;
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.
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);
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;
50 private InferredStatementContext(final InferredStatementContext<A, D, E> original,
51 final StatementContextBase<?, ?, ?> parent) {
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;
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);
72 // FIXME: YANGTOOLS-784: instantiate these lazily
73 addEffectiveSubstatements(createEffective());
77 public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
78 return ImmutableList.of();
82 public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
83 // No need to concat with declared
84 return effectiveSubstatements();
88 public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
89 // No need to concat with declared
90 return effectiveSubstatements().stream();
94 public StatementSourceReference getStatementSourceReference() {
95 return originalCtx.getStatementSourceReference();
99 public String rawStatementArgument() {
100 return originalCtx.rawStatementArgument();
104 public Optional<StmtContext<A, D, E>> getOriginalCtx() {
105 return Optional.of(originalCtx);
109 public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
110 return Optional.of(prototype);
114 public D buildDeclared() {
116 * Share original instance of declared statement between all effective statements which have been copied or
117 * derived from this original declared statement.
119 return originalCtx.buildDeclared();
123 InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
124 return new InferredStatementContext<>(this, newParent);
128 boolean hasEmptySubstatements() {
129 return hasEmptyEffectiveSubstatements();
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());
139 for (final Mutable<?, ?, ?> stmtContext : declared) {
140 if (stmtContext.isSupportedByFeatures()) {
141 copySubstatement(stmtContext, buffer);
144 for (final Mutable<?, ?, ?> stmtContext : effective) {
145 copySubstatement(stmtContext, buffer);
151 // Statement copy mess starts here
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
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);
162 private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer) {
163 final StatementDefinition def = substatement.getPublicDefinition();
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);
172 substatement.copyAsChildOf(this, childCopyType, targetModule).ifPresent(buffer::add);
175 // Statement copy mess ends here
178 * KEEP THINGS ORGANIZED!
180 * below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
181 * properly updated there.
185 public Optional<SchemaPath> getSchemaPath() {
186 return substatementGetSchemaPath();
190 public A getStatementArgument() {
195 public StatementContextBase<?, ?, ?> getParentContext() {
200 public StorageNodeType getStorageNodeType() {
201 return StorageNodeType.STATEMENT_LOCAL;
205 public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
210 public RootStatementContext<?, ?, ?> getRoot() {
211 return parent.getRoot();
215 public boolean isConfiguration() {
216 return isConfiguration(parent);
220 protected boolean isIgnoringIfFeatures() {
221 return isIgnoringIfFeatures(parent);
225 protected boolean isIgnoringConfig() {
226 return isIgnoringConfig(parent);
230 protected boolean isParentSupportedByFeatures() {
231 return parent.isSupportedByFeatures();