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.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;
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.
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);
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;
51 private InferredStatementContext(final InferredStatementContext<A, D, E> original,
52 final StatementContextBase<?, ?, ?> parent) {
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;
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);
73 // FIXME: YANGTOOLS-784: instantiate these lazily
74 addEffectiveSubstatements(createEffective());
78 public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
79 return ImmutableList.of();
83 public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
84 // No need to concat with declared
85 return effectiveSubstatements();
89 public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
90 // No need to concat with declared
91 return effectiveSubstatements().stream();
95 public StatementSourceReference getStatementSourceReference() {
96 return originalCtx.getStatementSourceReference();
100 public String rawStatementArgument() {
101 return originalCtx.rawStatementArgument();
105 public Optional<StmtContext<A, D, E>> getOriginalCtx() {
106 return Optional.of(originalCtx);
110 public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
111 return Optional.of(prototype);
115 public D buildDeclared() {
117 * Share original instance of declared statement between all effective statements which have been copied or
118 * derived from this original declared statement.
120 return originalCtx.buildDeclared();
124 InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
125 return new InferredStatementContext<>(this, newParent);
128 // Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
129 // BuildGlobalContext, hence it must be called at most once.
130 private List<Mutable<?, ?, ?>> createEffective() {
131 final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
132 final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
133 final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
135 for (final Mutable<?, ?, ?> stmtContext : declared) {
136 if (stmtContext.isSupportedByFeatures()) {
137 copySubstatement(stmtContext, buffer);
140 for (final Mutable<?, ?, ?> stmtContext : effective) {
141 copySubstatement(stmtContext, buffer);
147 // Statement copy mess starts here
149 // FIXME: This is messy and is probably wrong in some corner case. Even if it is correct, the way how it is correct
150 // relies on hard-coded maps. At the end of the day, the logic needs to be controlled by statement's
152 // FIXME: YANGTOOLS-652: these maps look very much like those in UsesStatementImpl
153 private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
154 YangStmtMapping.TYPE,
155 YangStmtMapping.TYPEDEF,
156 YangStmtMapping.USES);
157 private static final ImmutableSet<YangStmtMapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
158 YangStmtMapping.DESCRIPTION,
159 YangStmtMapping.REFERENCE,
160 YangStmtMapping.STATUS);
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);
171 // FIXME: YANGTOOLS-652: formerly known as "needToCopyByUses" (note inverted check, though)
172 if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
173 // This is to say: if parent of source context is a grouping, ignore this statement.
174 if (YangStmtMapping.GROUPING.equals(substatement.coerceParentContext().getPublicDefinition())) {
175 LOG.debug("Skipping grouping statement {}", substatement);
180 // FIXME: YANGTOOLS-694: we are forcing a copy here, hence even statements not affected by parent, copyType
181 // or targetModule (and don't forget its substatements!). This really should be a callout
182 // to StatementSupport. Note if that callout is allowed to return an Optional, it can
183 // take care at least of the 'grouping from uses' case above.
184 final Mutable<?, ?, ?> copy = childCopyOf(substatement, childCopyType, targetModule);
185 LOG.debug("Copying substatement {} for {} as {}", substatement, this, copy);
189 // Statement copy mess ends here
192 * KEEP THINGS ORGANIZED!
194 * below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
195 * properly updated there.
198 public Optional<SchemaPath> getSchemaPath() {
199 return substatementGetSchemaPath();
203 public A getStatementArgument() {
208 public StatementContextBase<?, ?, ?> getParentContext() {
213 public StorageNodeType getStorageNodeType() {
214 return StorageNodeType.STATEMENT_LOCAL;
218 public NamespaceStorageNode getParentNamespaceStorage() {
223 public RootStatementContext<?, ?, ?> getRoot() {
224 return parent.getRoot();
228 public boolean isConfiguration() {
229 return isConfiguration(parent);
233 protected boolean isIgnoringIfFeatures() {
234 return isIgnoringIfFeatures(parent);
238 protected boolean isIgnoringConfig() {
239 return isIgnoringConfig(parent);
243 protected boolean isParentSupportedByFeatures() {
244 return parent.isSupportedByFeatures();