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;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.VerifyException;
15 import java.util.Collection;
17 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
28 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
34 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Real "core" reactor statement implementation of {@link Mutable}, supporting basic reactor lifecycle.
51 * @param <A> Argument type
52 * @param <D> Declared Statement representation
53 * @param <E> Effective Statement representation
55 abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
56 extends NamespaceStorageSupport implements Mutable<A, D, E> {
57 private static final Logger LOG = LoggerFactory.getLogger(ReactorStmtCtx.class);
60 * Substatement refcount tracking. This mechanics deals with retaining substatements for the purposes of
61 * instantiating their lazy copies in InferredStatementContext. It works in concert with {@link #buildEffective()}
62 * and {@link #buildDeclared()}: declared/effective statement views hold an implicit reference and refcount-based
63 * sweep is not activated until they are done (or this statement is not {@link #isSupportedToBuildEffective}).
66 * Reference count is hierarchical in that parent references also pin down their child statements and do not allow
70 * The counter's positive values are tracking incoming references via {@link #incRef()}/{@link #decRef()} methods.
71 * Once we transition to sweeping, this value becomes negative counting upwards to {@link #REFCOUNT_NONE} based on
72 * {@link #sweepOnChildDone()}. Once we reach that, we transition to {@link #REFCOUNT_SWEPT}.
74 private int refcount = REFCOUNT_NONE;
76 * No outstanding references, this statement is a potential candidate for sweeping, provided it has populated its
77 * declared and effective views and {@link #parentRef} is known to be absent.
79 private static final int REFCOUNT_NONE = 0;
81 * Reference count overflow or some other recoverable logic error. Do not rely on refcounts and do not sweep
85 * Note on value assignment:
86 * This allow our incRef() to naturally progress to being saturated. Others jump there directly.
87 * It also makes it it impossible to observe {@code Interger.MAX_VALUE} children, which we take advantage of for
88 * {@link #REFCOUNT_SWEEPING}.
90 private static final int REFCOUNT_DEFUNCT = Integer.MAX_VALUE;
92 * This statement is being actively swept. This is a transient value set when we are sweeping our children, so that
93 * we prevent re-entering this statement.
96 * Note on value assignment:
97 * The value is lower than any legal child refcount due to {@link #REFCOUNT_DEFUNCT} while still being higher than
98 * {@link #REFCOUNT_SWEPT}.
100 private static final int REFCOUNT_SWEEPING = -Integer.MAX_VALUE;
102 * This statement, along with its entire subtree has been swept and we positively know all our children have reached
103 * this state. We {@link #sweepNamespaces()} upon reaching this state.
106 * Note on value assignment:
107 * This is the lowest value observable, making it easier on checking others on equality.
109 private static final int REFCOUNT_SWEPT = Integer.MIN_VALUE;
111 private @Nullable E effectiveInstance;
113 // Master flag controlling whether this context can yield an effective statement
114 // FIXME: investigate the mechanics that are being supported by this, as it would be beneficial if we can get rid
115 // of this flag -- eliminating the initial alignment shadow used by below gap-filler fields.
116 private boolean isSupportedToBuildEffective = true;
118 // Flag bit assignments
119 private static final int IS_SUPPORTED_BY_FEATURES = 0x01;
120 private static final int HAVE_SUPPORTED_BY_FEATURES = 0x02;
121 private static final int IS_IGNORE_IF_FEATURE = 0x04;
122 private static final int HAVE_IGNORE_IF_FEATURE = 0x08;
123 // Note: these four are related
124 private static final int IS_IGNORE_CONFIG = 0x10;
125 private static final int HAVE_IGNORE_CONFIG = 0x20;
126 private static final int IS_CONFIGURATION = 0x40;
127 private static final int HAVE_CONFIGURATION = 0x80;
129 // Have-and-set flag constants, also used as masks
130 private static final int SET_SUPPORTED_BY_FEATURES = HAVE_SUPPORTED_BY_FEATURES | IS_SUPPORTED_BY_FEATURES;
131 private static final int SET_CONFIGURATION = HAVE_CONFIGURATION | IS_CONFIGURATION;
132 // Note: implies SET_CONFIGURATION, allowing fewer bit operations to be performed
133 private static final int SET_IGNORE_CONFIG = HAVE_IGNORE_CONFIG | IS_IGNORE_CONFIG | SET_CONFIGURATION;
134 private static final int SET_IGNORE_IF_FEATURE = HAVE_IGNORE_IF_FEATURE | IS_IGNORE_IF_FEATURE;
136 // Flags for use with SubstatementContext. These are hiding in the alignment shadow created by above boolean and
137 // hence improve memory layout.
140 // Flag for use with AbstractResumedStatement. This is hiding in the alignment shadow created by above boolean
141 // FIXME: move this out once we have JDK15+
142 private boolean fullyDefined;
144 // SchemaPath cache for use with SubstatementContext and InferredStatementContext. This hurts RootStatementContext
145 // a bit in terms of size -- but those are only a few and SchemaPath is on its way out anyway.
147 private volatile SchemaPath schemaPath;
153 ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
154 isSupportedToBuildEffective = original.isSupportedToBuildEffective;
155 fullyDefined = original.fullyDefined;
156 flags = original.flags;
161 // Common public interface contracts with simple mechanics. Please keep this in one logical block, so we do not end
162 // up mixing concerns and simple details with more complex logic.
167 public abstract StatementContextBase<?, ?, ?> getParentContext();
170 public abstract RootStatementContext<?, ?, ?> getRoot();
173 public abstract Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements();
176 public final @NonNull Registry getBehaviourRegistry() {
177 return getRoot().getBehaviourRegistryImpl();
181 public final YangVersion yangVersion() {
182 return getRoot().getRootVersionImpl();
186 public final void setRootVersion(final YangVersion version) {
187 getRoot().setRootVersionImpl(version);
191 public final void addMutableStmtToSeal(final MutableStatement mutableStatement) {
192 getRoot().addMutableStmtToSealImpl(mutableStatement);
196 public final void addRequiredSource(final SourceIdentifier dependency) {
197 getRoot().addRequiredSourceImpl(dependency);
201 public final void setRootIdentifier(final SourceIdentifier identifier) {
202 getRoot().setRootIdentifierImpl(identifier);
206 public final boolean isEnabledSemanticVersioning() {
207 return getRoot().isEnabledSemanticVersioningImpl();
211 public final ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
212 return getRoot().getSourceContext().newInferenceAction(phase);
216 public final StatementDefinition publicDefinition() {
217 return definition().getPublicView();
221 public final String toString() {
222 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
225 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
226 return toStringHelper.add("definition", definition()).add("rawArgument", rawArgument());
230 * Return the context in which this statement was defined.
232 * @return statement definition
234 abstract @NonNull StatementDefinitionContext<A, D, E> definition();
238 // NamespaceStorageSupport/Mutable integration methods. Keep these together.
243 public final <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
244 final Class<@NonNull N> type, final T key) {
245 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
249 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
250 return getNamespace(type);
254 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
255 final Class<N> type) {
256 return getLocalNamespace(type);
260 protected final void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
261 definition().checkNamespaceAllowed(type);
265 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
267 // definition().onNamespaceElementAdded(this, type, key, value);
272 // Statement build entry points -- both public and package-private.
277 public final E buildEffective() {
279 return (existing = effectiveInstance) != null ? existing : loadEffective();
282 private E loadEffective() {
283 // Creating an effective statement does not strictly require a declared instance -- there are statements like
284 // 'input', which are implicitly defined.
285 // Our implementation design makes an invariant assumption that buildDeclared() has been called by the time
286 // we attempt to create effective statement:
289 final E ret = effectiveInstance = createEffective();
290 // we have called createEffective(), substatements are no longer guarded by us. Let's see if we can clear up
292 if (refcount == REFCOUNT_NONE) {
298 abstract @NonNull E createEffective();
302 // Flags-based mechanics. These include public interfaces as well as all the crud we have lurking in our alignment
308 public final boolean isSupportedToBuildEffective() {
309 return isSupportedToBuildEffective;
313 public final void setIsSupportedToBuildEffective(final boolean isSupportedToBuildEffective) {
314 this.isSupportedToBuildEffective = isSupportedToBuildEffective;
318 public final boolean isSupportedByFeatures() {
319 final int fl = flags & SET_SUPPORTED_BY_FEATURES;
321 return fl == SET_SUPPORTED_BY_FEATURES;
323 if (isIgnoringIfFeatures()) {
324 flags |= SET_SUPPORTED_BY_FEATURES;
329 * If parent is supported, we need to check if-features statements of this context.
331 if (isParentSupportedByFeatures()) {
332 // If the set of supported features has not been provided, all features are supported by default.
333 final Set<QName> supportedFeatures = getFromNamespace(SupportedFeaturesNamespace.class,
334 SupportedFeatures.SUPPORTED_FEATURES);
335 if (supportedFeatures == null || StmtContextUtils.checkFeatureSupport(this, supportedFeatures)) {
336 flags |= SET_SUPPORTED_BY_FEATURES;
341 // Either parent is not supported or this statement is not supported
342 flags |= HAVE_SUPPORTED_BY_FEATURES;
346 protected abstract boolean isParentSupportedByFeatures();
349 * Config statements are not all that common which means we are performing a recursive search towards the root
350 * every time {@link #isConfiguration()} is invoked. This is quite expensive because it causes a linear search
351 * for the (usually non-existent) config statement.
354 * This method maintains a resolution cache, so once we have returned a result, we will keep on returning the same
355 * result without performing any lookups, solely to support {@link SubstatementContext#isConfiguration()}.
358 * Note: use of this method implies that {@link #isIgnoringConfig()} is realized with
359 * {@link #isIgnoringConfig(StatementContextBase)}.
361 final boolean isConfiguration(final StatementContextBase<?, ?, ?> parent) {
362 final int fl = flags & SET_CONFIGURATION;
364 return fl == SET_CONFIGURATION;
366 if (isIgnoringConfig(parent)) {
367 // Note: SET_CONFIGURATION has been stored in flags
371 final boolean isConfig;
372 final Optional<Boolean> optConfig = findSubstatementArgument(ConfigEffectiveStatement.class);
373 if (optConfig.isPresent()) {
374 isConfig = optConfig.orElseThrow();
376 // Validity check: if parent is config=false this cannot be a config=true
377 InferenceException.throwIf(!parent.isConfiguration(), sourceReference(),
378 "Parent node has config=false, this node must not be specifed as config=true");
381 // If "config" statement is not specified, the default is the same as the parent's "config" value.
382 isConfig = parent.isConfiguration();
385 // Resolved, make sure we cache this return
386 flags |= isConfig ? SET_CONFIGURATION : HAVE_CONFIGURATION;
390 protected abstract boolean isIgnoringConfig();
393 * This method maintains a resolution cache for ignore config, so once we have returned a result, we will
394 * keep on returning the same result without performing any lookups. Exists only to support
395 * {@link SubstatementContext#isIgnoringConfig()}.
398 * Note: use of this method implies that {@link #isConfiguration()} is realized with
399 * {@link #isConfiguration(StatementContextBase)}.
401 final boolean isIgnoringConfig(final StatementContextBase<?, ?, ?> parent) {
402 final int fl = flags & SET_IGNORE_CONFIG;
404 return fl == SET_IGNORE_CONFIG;
406 if (definition().support().isIgnoringConfig() || parent.isIgnoringConfig()) {
407 flags |= SET_IGNORE_CONFIG;
411 flags |= HAVE_IGNORE_CONFIG;
415 protected abstract boolean isIgnoringIfFeatures();
418 * This method maintains a resolution cache for ignore if-feature, so once we have returned a result, we will
419 * keep on returning the same result without performing any lookups. Exists only to support
420 * {@link SubstatementContext#isIgnoringIfFeatures()}.
422 final boolean isIgnoringIfFeatures(final StatementContextBase<?, ?, ?> parent) {
423 final int fl = flags & SET_IGNORE_IF_FEATURE;
425 return fl == SET_IGNORE_IF_FEATURE;
427 if (definition().support().isIgnoringIfFeatures() || parent.isIgnoringIfFeatures()) {
428 flags |= SET_IGNORE_IF_FEATURE;
432 flags |= HAVE_IGNORE_IF_FEATURE;
436 // These two exists only due to memory optimization, should live in AbstractResumedStatement. We are also reusing
437 // this for ReplicaStatementContext's refcount tracking.
438 final boolean fullyDefined() {
442 final void setFullyDefined() {
448 // Common SchemaPath cache. All of this is bound to be removed once YANGTOOLS-1066 is done.
452 abstract @NonNull Optional<SchemaPath> schemaPath();
454 // Exists only to support {SubstatementContext,InferredStatementContext}.schemaPath()
456 final @NonNull Optional<SchemaPath> substatementGetSchemaPath() {
457 SchemaPath local = schemaPath;
459 synchronized (this) {
462 schemaPath = local = createSchemaPath((StatementContextBase<?, ?, ?>) coerceParentContext());
467 return Optional.ofNullable(local);
471 private SchemaPath createSchemaPath(final StatementContextBase<?, ?, ?> parent) {
472 final Optional<SchemaPath> maybeParentPath = parent.schemaPath();
473 verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
474 final SchemaPath parentPath = maybeParentPath.get();
476 if (StmtContextUtils.isUnknownStatement(this)) {
477 return parentPath.createChild(publicDefinition().getStatementName());
479 final Object argument = argument();
480 if (argument instanceof QName) {
481 final QName qname = (QName) argument;
482 if (producesDeclared(UsesStatement.class)) {
483 return maybeParentPath.orElse(null);
486 return parentPath.createChild(qname);
488 if (argument instanceof String) {
489 // FIXME: This may yield illegal argument exceptions
490 final Optional<StmtContext<A, D, E>> originalCtx = getOriginalCtx();
491 final QName qname = StmtContextUtils.qnameFromArgument(originalCtx.orElse(this), (String) argument);
492 return parentPath.createChild(qname);
494 if (argument instanceof SchemaNodeIdentifier
495 && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
496 || producesDeclared(DeviationStatement.class))) {
498 return parentPath.createChild(((SchemaNodeIdentifier) argument).getNodeIdentifiers());
501 // FIXME: this does not look right
502 return maybeParentPath.orElse(null);
507 // Reference counting mechanics start. Please keep these methods in one block for clarity. Note this does not
508 // contribute to state visible outside of this package.
513 * Acquire a reference on this context. As long as there is at least one reference outstanding,
514 * {@link #buildEffective()} will not result in {@link #effectiveSubstatements()} being discarded.
516 * @throws VerifyException if {@link #effectiveSubstatements()} has already been discarded
518 final void incRef() {
519 final int current = refcount;
520 verify(current >= REFCOUNT_NONE, "Attempted to access reference count of %s", this);
521 if (current != REFCOUNT_DEFUNCT) {
522 // Note: can end up becoming REFCOUNT_DEFUNCT on overflow
523 refcount = current + 1;
525 LOG.debug("Disabled refcount increment of {}", this);
530 * Release a reference on this context. This call may result in {@link #effectiveSubstatements()} becoming
533 final void decRef() {
534 final int current = refcount;
535 if (current == REFCOUNT_DEFUNCT) {
537 LOG.debug("Disabled refcount decrement of {}", this);
540 if (current <= REFCOUNT_NONE) {
541 // Underflow, become defunct
542 LOG.warn("Statement refcount underflow, reference counting disabled for {}", this, new Throwable());
543 refcount = REFCOUNT_DEFUNCT;
547 refcount = current - 1;
548 LOG.trace("Refcount {} on {}", refcount, this);
550 // We are no longer guarded by effective instance
556 * Sweep this statement context as a result of {@link #sweepSubstatements()}, i.e. when parent is also being swept.
558 private void sweep() {
560 LOG.trace("Releasing {}", this);
565 static final void sweep(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
566 for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
571 static final int countUnswept(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
573 for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
574 if (stmt.refcount > REFCOUNT_NONE || !stmt.noImplictRef()) {
582 * Implementation-specific sweep action. This is expected to perform a recursive {@link #sweep(Collection)} on all
583 * {@link #declaredSubstatements()} and {@link #effectiveSubstatements()} and report the result of the sweep
587 * {@link #effectiveSubstatements()} as well as namespaces may become inoperable as a result of this operation.
589 * @return True if the entire tree has been completely swept, false otherwise.
591 abstract int sweepSubstatements();
593 // Called when this statement does not have an implicit reference and have reached REFCOUNT_NONE
594 private void sweepOnDecrement() {
595 LOG.trace("Sweeping on decrement {}", this);
596 if (noParentRefcount()) {
597 // No further parent references, sweep our state.
601 // Propagate towards parent if there is one
602 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
603 if (parent != null) {
604 parent.sweepOnChildDecrement();
608 // Called from child when it has lost its final reference
609 private void sweepOnChildDecrement() {
610 if (isAwaitingChildren()) {
611 // We are a child for which our parent is waiting. Notify it and we are done.
616 // Check parent reference count
617 final int refs = refcount;
618 if (refs > REFCOUNT_NONE || refs <= REFCOUNT_SWEEPING || !noImplictRef()) {
623 // parent is potentially reclaimable
624 if (noParentRefcount()) {
625 LOG.trace("Cleanup {} of parent {}", refcount, this);
627 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
628 if (parent != null) {
629 parent.sweepOnChildDecrement();
635 private boolean noImplictRef() {
636 return effectiveInstance != null || !isSupportedToBuildEffective();
639 // FIXME: cache the resolution of this
640 private boolean noParentRefcount() {
641 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
642 if (parent != null) {
643 // There are three possibilities:
644 // - REFCOUNT_NONE, in which case we need to search next parent
645 // - negative (< REFCOUNT_NONE), meaning parent is in some stage of sweeping, hence it does not have
647 // - positive (> REFCOUNT_NONE), meaning parent has an explicit refcount which is holding us down
648 final int refs = parent.refcount;
649 return refs == REFCOUNT_NONE ? parent.noParentRefcount() : refs < REFCOUNT_NONE;
654 private boolean isAwaitingChildren() {
655 return refcount > REFCOUNT_SWEEPING && refcount < REFCOUNT_NONE;
658 private boolean isSweepable() {
659 return refcount == REFCOUNT_NONE && noImplictRef();
662 private void sweepOnChildDone() {
663 LOG.trace("Sweeping on child done {}", this);
664 final int current = refcount;
665 if (current >= REFCOUNT_NONE) {
666 // no-op, perhaps we want to handle some cases differently?
667 LOG.trace("Ignoring child sweep of {} for {}", this, current);
670 verify(current != REFCOUNT_SWEPT, "Attempt to sweep a child of swept %s", this);
672 refcount = current + 1;
673 LOG.trace("Child refcount {}", refcount);
674 if (refcount == REFCOUNT_NONE) {
676 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
677 LOG.trace("Propagating to parent {}", parent);
678 if (parent != null && parent.isAwaitingChildren()) {
679 parent.sweepOnChildDone();
684 private void sweepDone() {
685 LOG.trace("Sweep done for {}", this);
686 refcount = REFCOUNT_SWEPT;
690 private boolean sweepState() {
691 refcount = REFCOUNT_SWEEPING;
692 final int childRefs = sweepSubstatements();
693 if (childRefs == 0) {
697 if (childRefs < 0 || childRefs >= REFCOUNT_DEFUNCT) {
698 LOG.warn("Negative child refcount {} cannot be stored, reference counting disabled for {}", childRefs, this,
700 refcount = REFCOUNT_DEFUNCT;
702 LOG.trace("Still {} outstanding children of {}", childRefs, this);
703 refcount = -childRefs;