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 com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.VerifyException;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.Optional;
22 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
29 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextDefaults;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
42 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * A statement which has been inferred to exist. Functionally it is equivalent to a SubstatementContext, but it is not
48 * backed by a declaration (and declared statements). It is backed by a prototype StatementContextBase and has only
49 * effective substatements, which are either transformed from that prototype or added by inference.
51 final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
52 extends StatementContextBase<A, D, E> implements OnDemandSchemaTreeStorageNode {
53 private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
55 // Sentinel object for 'substatements'
56 private static final Object SWEPT_SUBSTATEMENTS = new Object();
58 private final @NonNull StatementContextBase<A, D, E> prototype;
59 private final @NonNull StatementContextBase<?, ?, ?> parent;
60 private final @NonNull StmtContext<A, D, E> originalCtx;
61 private final @NonNull CopyType childCopyType;
62 private final QNameModule targetModule;
63 private final A argument;
66 * Effective substatements, lazily materialized. This field can have four states:
68 * <li>it can be {@code null}, in which case no materialization has taken place</li>
69 * <li>it can be a {@link HashMap}, in which case partial materialization has taken place</li>
70 * <li>it can be a {@link List}, in which case full materialization has taken place</li>
71 * <li>it can be {@link SWEPT_SUBSTATEMENTS}, in which case materialized state is no longer available</li>
74 private Object substatements;
76 private InferredStatementContext(final InferredStatementContext<A, D, E> original,
77 final StatementContextBase<?, ?, ?> parent) {
79 this.parent = requireNonNull(parent);
80 this.childCopyType = original.childCopyType;
81 this.targetModule = original.targetModule;
82 this.prototype = original.prototype;
83 this.originalCtx = original.originalCtx;
84 this.argument = original.argument;
85 // Substatements are initialized here
86 this.substatements = ImmutableList.of();
89 InferredStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> prototype,
90 final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
91 super(prototype.definition(), CopyHistory.of(myCopyType, prototype.history()));
92 this.parent = requireNonNull(parent);
93 this.prototype = requireNonNull(prototype);
94 this.argument = targetModule == null ? prototype.argument()
95 : prototype.definition().adaptArgumentValue(prototype, targetModule);
96 this.childCopyType = requireNonNull(childCopyType);
97 this.targetModule = targetModule;
98 this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
100 // Mark prototype as blocking statement cleanup
105 public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
106 return ImmutableList.of();
110 public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
111 return mutableEffectiveSubstatements(ensureEffectiveSubstatements());
115 public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
116 // No need to concat with declared
117 return effectiveSubstatements();
121 public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
122 // No need to concat with declared
123 return effectiveSubstatements().stream();
127 public StatementSourceReference sourceReference() {
128 return originalCtx.sourceReference();
132 public String rawArgument() {
133 return originalCtx.rawArgument();
137 public Optional<StmtContext<A, D, E>> getOriginalCtx() {
138 return Optional.of(originalCtx);
142 public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
143 return Optional.of(prototype);
147 public D declared() {
149 * Share original instance of declared statement between all effective statements which have been copied or
150 * derived from this original declared statement.
152 return originalCtx.declared();
156 public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
157 substatements = removeStatementFromEffectiveSubstatements(ensureEffectiveSubstatements(), statementDef);
161 public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
162 final String statementArg) {
163 substatements = removeStatementFromEffectiveSubstatements(ensureEffectiveSubstatements(), statementDef,
168 public void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
169 substatements = addEffectiveSubstatement(ensureEffectiveSubstatements(), substatement);
173 void addEffectiveSubstatementsImpl(final Collection<? extends Mutable<?, ?, ?>> statements) {
174 substatements = addEffectiveSubstatementsImpl(ensureEffectiveSubstatements(), statements);
178 InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
179 return new InferredStatementContext<>(this, newParent);
183 boolean hasEmptySubstatements() {
184 if (substatements == null) {
185 return prototype.hasEmptySubstatements();
187 return substatements instanceof HashMap ? false : ((List<?>) substatements).isEmpty();
191 public <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
192 final @NonNull Class<Z> type) {
193 if (substatements instanceof List) {
194 return StmtContextDefaults.findSubstatementArgument(this, type);
197 final Optional<X> templateArg = prototype.findSubstatementArgument(type);
198 if (templateArg.isEmpty()) {
201 if (SchemaTreeEffectiveStatement.class.isAssignableFrom(type)) {
202 // X is known to be QName
203 return (Optional<X>) templateArg.map(template -> ((QName) template).bindTo(targetModule));
209 public boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
210 return substatements instanceof List ? StmtContextDefaults.hasSubstatement(prototype, type)
211 // We do not allow deletion of partially-materialized statements, hence this is accurate
212 : prototype.hasSubstatement(type);
216 public <Y extends DeclaredStatement<QName>, Z extends SchemaTreeEffectiveStatement<Y>>
217 StmtContext<QName, Y, Z> requestSchemaTreeChild(final QName qname) {
218 if (substatements instanceof List) {
219 // We have performed materialization, hence we have triggered creation of all our schema tree child
224 final QName templateQName = qname.bindTo(StmtContextUtils.getRootModuleQName(prototype));
225 LOG.debug("Materializing child {} from {}", qname, templateQName);
227 final StmtContext<?, ?, ?> template;
228 if (prototype instanceof InferredStatementContext) {
229 // Note: we need to access namespace here, as the target statement may have already been populated, in which
230 // case we want to obtain the statement in local namespace storage.
231 template = (StmtContext) ((InferredStatementContext<?, ?, ?>) prototype).getFromNamespace(
232 SchemaTreeNamespace.class, templateQName);
234 template = prototype.allSubstatementsStream()
235 .filter(stmt -> stmt.producesEffective(SchemaTreeEffectiveStatement.class)
236 && templateQName.equals(stmt.argument()))
241 if (template == null) {
242 // We do not have a template, this child does not exist. It may be added later, but that is someone else's
244 LOG.debug("Child {} does not have a template", qname);
248 @SuppressWarnings("unchecked")
249 final Mutable<QName, Y, Z> ret = (Mutable<QName, Y, Z>) copySubstatement((Mutable<?, ?, ?>) template)
251 () -> new InferenceException(this, "Failed to materialize child %s template %s", qname, template));
252 ensureCompletedPhase(ret);
253 addMaterialized(template, ret);
255 LOG.debug("Child {} materialized", qname);
259 // Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
260 // BuildGlobalContext, hence it must be called at most once.
261 private List<ReactorStmtCtx<?, ?, ?>> ensureEffectiveSubstatements() {
262 accessSubstatements();
263 return substatements instanceof List ? castEffective(substatements)
264 : initializeSubstatements(castMaterialized(substatements));
268 Iterable<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete() {
269 // When we have not initialized, there are no statements to catch up: we will catch up when we are copying
270 // from prototype (which is already at ModelProcessingPhase.EFFECTIVE_MODEL).
271 if (substatements == null) {
272 return ImmutableList.of();
274 accessSubstatements();
275 if (substatements instanceof HashMap) {
276 return castMaterialized(substatements).values();
278 return castEffective(substatements);
283 Stream<? extends StmtContext<?, ?, ?>> streamDeclared() {
284 return Stream.empty();
288 Stream<? extends StmtContext<?, ?, ?>> streamEffective() {
289 accessSubstatements();
290 return ensureEffectiveSubstatements().stream();
293 private void accessSubstatements() {
294 verify(substatements != SWEPT_SUBSTATEMENTS, "Attempted to access substatements of %s", this);
298 void markNoParentRef() {
299 final Object local = substatements;
301 markNoParentRef(castEffective(local));
306 int sweepSubstatements() {
307 final Object local = substatements;
308 substatements = SWEPT_SUBSTATEMENTS;
311 final List<ReactorStmtCtx<?, ?, ?>> list = castEffective(local);
313 count = countUnswept(list);
318 private List<ReactorStmtCtx<?, ?, ?>> initializeSubstatements(
319 final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
320 final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
321 final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
323 final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
324 for (final Mutable<?, ?, ?> stmtContext : declared) {
325 if (stmtContext.isSupportedByFeatures()) {
326 copySubstatement(stmtContext, buffer, materializedSchemaTree);
329 for (final Mutable<?, ?, ?> stmtContext : effective) {
330 copySubstatement(stmtContext, buffer, materializedSchemaTree);
333 final List<ReactorStmtCtx<?, ?, ?>> ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(), buffer.size());
334 ret.addAll((Collection) buffer);
341 // Statement copy mess starts here
343 // FIXME: This is messy and is probably wrong in some corner case. Even if it is correct, the way how it is correct
344 // relies on hard-coded maps. At the end of the day, the logic needs to be controlled by statement's
346 // FIXME: YANGTOOLS-652: this map looks very much like UsesStatementSupport.TOP_REUSED_DEF_SET
347 private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
348 YangStmtMapping.TYPE,
349 YangStmtMapping.TYPEDEF,
350 YangStmtMapping.USES);
352 private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer,
353 final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
354 final StatementDefinition def = substatement.publicDefinition();
356 // FIXME: YANGTOOLS-652: formerly known as "isReusedByUses"
357 if (REUSED_DEF_SET.contains(def)) {
358 LOG.trace("Reusing substatement {} for {}", substatement, this);
359 buffer.add(substatement.replicaAsChildOf(this));
363 // Consult materialized substatements. We are in a copy operation and will end up throwing materialized
364 // statements away -- hence we do not perform Map.remove() to save ourselves a mutation operation.
366 // We could also perform a Map.containsKey() and perform a bulk add, but that would mean the statement order
367 // against parent would change -- and we certainly do not want that to happen.
368 final ReactorStmtCtx<?, ?, ?> materialized = findMaterialized(materializedSchemaTree, substatement);
369 if (materialized == null) {
370 copySubstatement(substatement).ifPresent(copy -> {
371 ensureCompletedPhase(copy);
375 buffer.add(materialized);
379 private Optional<? extends Mutable<?, ?, ?>> copySubstatement(final Mutable<?, ?, ?> substatement) {
380 return substatement.copyAsChildOf(this, childCopyType, targetModule);
383 private void addMaterialized(final StmtContext<?, ?, ?> template, final Mutable<?, ?, ?> copy) {
384 final HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree;
385 if (substatements == null) {
386 // Lazy initialization of backing map. We do not expect this to be used often or multiple times -- each hit
387 // here means an inference along schema tree, such as deviate/augment. HashMap requires power-of-two and
388 // defaults to 0.75 load factor -- we therefore size it to 4, i.e. next two inserts will not cause a
389 // resizing operation.
390 materializedSchemaTree = new HashMap<>(4);
391 substatements = materializedSchemaTree;
393 verify(substatements instanceof HashMap, "Unexpected substatements %s", substatements);
394 materializedSchemaTree = castMaterialized(substatements);
397 final StmtContext<?, ?, ?> existing = materializedSchemaTree.put(template,
398 (StatementContextBase<?, ?, ?>) copy);
399 if (existing != null) {
400 throw new VerifyException(
401 "Unexpected duplicate request for " + copy.argument() + " previous result was " + existing);
405 private static @Nullable ReactorStmtCtx<?, ?, ?> findMaterialized(
406 final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree,
407 final StmtContext<?, ?, ?> template) {
408 return materializedSchemaTree == null ? null : materializedSchemaTree.get(template);
411 @SuppressWarnings("unchecked")
412 private static List<ReactorStmtCtx<?, ?, ?>> castEffective(final Object substatements) {
413 return (List<ReactorStmtCtx<?, ?, ?>>) substatements;
416 @SuppressWarnings("unchecked")
417 private static HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> castMaterialized(final Object substatements) {
418 return (HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>>) substatements;
421 // Statement copy mess ends here
424 * KEEP THINGS ORGANIZED!
426 * below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
427 * properly updated there.
431 public Optional<SchemaPath> schemaPath() {
432 return substatementGetSchemaPath();
436 public A argument() {
441 public StatementContextBase<?, ?, ?> getParentContext() {
446 public StorageNodeType getStorageNodeType() {
447 return StorageNodeType.STATEMENT_LOCAL;
451 public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
456 public RootStatementContext<?, ?, ?> getRoot() {
457 return parent.getRoot();
461 public EffectiveConfig effectiveConfig() {
462 return effectiveConfig(parent);
466 protected boolean isIgnoringIfFeatures() {
467 return isIgnoringIfFeatures(parent);
471 protected boolean isIgnoringConfig() {
472 return isIgnoringConfig(parent);
476 protected boolean isParentSupportedByFeatures() {
477 return parent.isSupportedByFeatures();