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 // FIXME: 5.0.0: ugly cast due getOriginalCtx() return type :(
72 this.originalCtx = (StmtContext<A, D, E>) prototype.getOriginalCtx().orElse(prototype);
74 // FIXME: YANGTOOLS-784: instantiate these lazily
75 addEffectiveSubstatements(createEffective());
79 public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
80 return ImmutableList.of();
84 public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
85 // No need to concat with declared
86 return effectiveSubstatements();
90 public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
91 // No need to concat with declared
92 return effectiveSubstatements().stream();
96 public StatementSourceReference getStatementSourceReference() {
97 return originalCtx.getStatementSourceReference();
101 public String rawStatementArgument() {
102 return originalCtx.rawStatementArgument();
106 public Optional<StmtContext<?, ?, ?>> getOriginalCtx() {
107 return Optional.of(originalCtx);
111 public Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx() {
112 return Optional.of(prototype);
116 public D buildDeclared() {
118 * Share original instance of declared statement between all effective statements which have been copied or
119 * derived from this original declared statement.
121 return originalCtx.buildDeclared();
125 InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
126 return new InferredStatementContext<>(this, newParent);
129 // Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
130 // BuildGlobalContext, hence it must be called at most once.
131 private List<Mutable<?, ?, ?>> createEffective() {
132 final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
133 final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
134 final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
136 for (final Mutable<?, ?, ?> stmtContext : declared) {
137 if (stmtContext.isSupportedByFeatures()) {
138 copySubstatement(stmtContext, buffer);
141 for (final Mutable<?, ?, ?> stmtContext : effective) {
142 copySubstatement(stmtContext, buffer);
148 // Statement copy mess starts here
150 // FIXME: This is messy and is probably wrong in some corner case. Even if it is correct, the way how it is correct
151 // relies on hard-coded maps. At the end of the day, the logic needs to be controlled by statement's
153 // FIXME: YANGTOOLS-652: this map looks very much like UsesStatementSupport.TOP_REUSED_DEF_SET
154 private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
155 YangStmtMapping.TYPE,
156 YangStmtMapping.TYPEDEF,
157 YangStmtMapping.USES);
159 private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer) {
160 final StatementDefinition def = substatement.getPublicDefinition();
162 // FIXME: YANGTOOLS-652: formerly known as "isReusedByUses"
163 if (REUSED_DEF_SET.contains(def)) {
164 LOG.debug("Reusing substatement {} for {}", substatement, this);
165 buffer.add(substatement);
169 substatement.copyAsChildOf(this, childCopyType, targetModule).ifPresent(buffer::add);
172 // Statement copy mess ends here
175 * KEEP THINGS ORGANIZED!
177 * below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
178 * properly updated there.
181 public Optional<SchemaPath> getSchemaPath() {
182 return substatementGetSchemaPath();
186 public A getStatementArgument() {
191 public StatementContextBase<?, ?, ?> getParentContext() {
196 public StorageNodeType getStorageNodeType() {
197 return StorageNodeType.STATEMENT_LOCAL;
201 public NamespaceStorageNode getParentNamespaceStorage() {
206 public RootStatementContext<?, ?, ?> getRoot() {
207 return parent.getRoot();
211 public boolean isConfiguration() {
212 return isConfiguration(parent);
216 protected boolean isIgnoringIfFeatures() {
217 return isIgnoringIfFeatures(parent);
221 protected boolean isIgnoringConfig() {
222 return isIgnoringConfig(parent);
226 protected boolean isParentSupportedByFeatures() {
227 return parent.isSupportedByFeatures();