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.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Real "core" reactor statement implementation of {@link Mutable}, supporting basic reactor lifecycle.
44 * @param <A> Argument type
45 * @param <D> Declared Statement representation
46 * @param <E> Effective Statement representation
48 abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
49 extends NamespaceStorageSupport implements Mutable<A, D, E> {
50 private static final Logger LOG = LoggerFactory.getLogger(ReactorStmtCtx.class);
53 * Substatement refcount tracking. This mechanics deals with retaining substatements for the purposes of
54 * instantiating their lazy copies in InferredStatementContext. It works in concert with {@link #buildEffective()}
55 * and {@link #buildDeclared()}: declared/effective statement views hold an implicit reference and refcount-based
56 * sweep is not activated until they are done (or this statement is not {@link #isSupportedToBuildEffective}).
59 * Reference count is hierarchical in that parent references also pin down their child statements and do not allow
63 * The counter's positive values are tracking incoming references via {@link #incRef()}/{@link #decRef()} methods.
64 * Once we transition to sweeping, this value becomes negative counting upwards to {@link #REFCOUNT_NONE} based on
65 * {@link #sweepOnChildDone()}. Once we reach that, we transition to {@link #REFCOUNT_SWEPT}.
67 private int refcount = REFCOUNT_NONE;
69 * No outstanding references, this statement is a potential candidate for sweeping, provided it has populated its
70 * declared and effective views and {@link #parentRef} is known to be absent.
72 private static final int REFCOUNT_NONE = 0;
74 * Reference count overflow or some other recoverable logic error. Do not rely on refcounts and do not sweep
78 * Note on value assignment:
79 * This allow our incRef() to naturally progress to being saturated. Others jump there directly.
80 * It also makes it it impossible to observe {@code Interger.MAX_VALUE} children, which we take advantage of for
81 * {@link #REFCOUNT_SWEEPING}.
83 private static final int REFCOUNT_DEFUNCT = Integer.MAX_VALUE;
85 * This statement is being actively swept. This is a transient value set when we are sweeping our children, so that
86 * we prevent re-entering this statement.
89 * Note on value assignment:
90 * The value is lower than any legal child refcount due to {@link #REFCOUNT_DEFUNCT} while still being higher than
91 * {@link #REFCOUNT_SWEPT}.
93 private static final int REFCOUNT_SWEEPING = -Integer.MAX_VALUE;
95 * This statement, along with its entire subtree has been swept and we positively know all our children have reached
96 * this state. We {@link #sweepNamespaces()} upon reaching this state.
99 * Note on value assignment:
100 * This is the lowest value observable, making it easier on checking others on equality.
102 private static final int REFCOUNT_SWEPT = Integer.MIN_VALUE;
104 private @Nullable E effectiveInstance;
106 // Master flag controlling whether this context can yield an effective statement
107 // FIXME: investigate the mechanics that are being supported by this, as it would be beneficial if we can get rid
108 // of this flag -- eliminating the initial alignment shadow used by below gap-filler fields.
109 private boolean isSupportedToBuildEffective = true;
111 // Flag bit assignments
112 private static final int IS_SUPPORTED_BY_FEATURES = 0x01;
113 private static final int HAVE_SUPPORTED_BY_FEATURES = 0x02;
114 private static final int IS_IGNORE_IF_FEATURE = 0x04;
115 private static final int HAVE_IGNORE_IF_FEATURE = 0x08;
116 // Note: these four are related
117 private static final int IS_IGNORE_CONFIG = 0x10;
118 private static final int HAVE_IGNORE_CONFIG = 0x20;
119 private static final int IS_CONFIGURATION = 0x40;
120 private static final int HAVE_CONFIGURATION = 0x80;
122 // Have-and-set flag constants, also used as masks
123 private static final int SET_SUPPORTED_BY_FEATURES = HAVE_SUPPORTED_BY_FEATURES | IS_SUPPORTED_BY_FEATURES;
124 private static final int SET_CONFIGURATION = HAVE_CONFIGURATION | IS_CONFIGURATION;
125 // Note: implies SET_CONFIGURATION, allowing fewer bit operations to be performed
126 private static final int SET_IGNORE_CONFIG = HAVE_IGNORE_CONFIG | IS_IGNORE_CONFIG | SET_CONFIGURATION;
127 private static final int SET_IGNORE_IF_FEATURE = HAVE_IGNORE_IF_FEATURE | IS_IGNORE_IF_FEATURE;
129 // Flags for use with SubstatementContext. These are hiding in the alignment shadow created by above boolean and
130 // hence improve memory layout.
133 // Flag for use with AbstractResumedStatement. This is hiding in the alignment shadow created by above boolean
134 // FIXME: move this out once we have JDK15+
135 private boolean fullyDefined;
141 ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
142 isSupportedToBuildEffective = original.isSupportedToBuildEffective;
143 fullyDefined = original.fullyDefined;
144 flags = original.flags;
149 // Common public interface contracts with simple mechanics. Please keep this in one logical block, so we do not end
150 // up mixing concerns and simple details with more complex logic.
155 public abstract StatementContextBase<?, ?, ?> getParentContext();
158 public abstract RootStatementContext<?, ?, ?> getRoot();
161 public abstract Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements();
164 public final @NonNull Registry getBehaviourRegistry() {
165 return getRoot().getBehaviourRegistryImpl();
169 public final YangVersion getRootVersion() {
170 return getRoot().getRootVersionImpl();
174 public final void setRootVersion(final YangVersion version) {
175 getRoot().setRootVersionImpl(version);
179 public final void addMutableStmtToSeal(final MutableStatement mutableStatement) {
180 getRoot().addMutableStmtToSealImpl(mutableStatement);
184 public final void addRequiredSource(final SourceIdentifier dependency) {
185 getRoot().addRequiredSourceImpl(dependency);
189 public final void setRootIdentifier(final SourceIdentifier identifier) {
190 getRoot().setRootIdentifierImpl(identifier);
194 public final boolean isEnabledSemanticVersioning() {
195 return getRoot().isEnabledSemanticVersioningImpl();
199 public final ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
200 return getRoot().getSourceContext().newInferenceAction(phase);
204 public final StatementDefinition publicDefinition() {
205 return definition().getPublicView();
209 public final String toString() {
210 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
213 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
214 return toStringHelper.add("definition", definition()).add("rawArgument", rawArgument());
218 * Return the context in which this statement was defined.
220 * @return statement definition
222 abstract @NonNull StatementDefinitionContext<A, D, E> definition();
226 // NamespaceStorageSupport/Mutable integration methods. Keep these together.
231 public final <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
232 final Class<@NonNull N> type, final T key) {
233 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
237 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
238 return getNamespace(type);
242 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
243 final Class<N> type) {
244 return getLocalNamespace(type);
248 protected final void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
249 definition().checkNamespaceAllowed(type);
253 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
255 // definition().onNamespaceElementAdded(this, type, key, value);
260 // Statement build entry points -- both public and package-private.
265 public final E buildEffective() {
267 return (existing = effectiveInstance) != null ? existing : loadEffective();
270 private E loadEffective() {
271 // Creating an effective statement does not strictly require a declared instance -- there are statements like
272 // 'input', which are implicitly defined.
273 // Our implementation design makes an invariant assumption that buildDeclared() has been called by the time
274 // we attempt to create effective statement:
277 final E ret = effectiveInstance = createEffective();
278 // we have called createEffective(), substatements are no longer guarded by us. Let's see if we can clear up
280 if (refcount == REFCOUNT_NONE) {
286 abstract @NonNull E createEffective();
290 // Flags-based mechanics. These include public interfaces as well as all the crud we have lurking in our alignment
296 public final boolean isSupportedToBuildEffective() {
297 return isSupportedToBuildEffective;
301 public final void setIsSupportedToBuildEffective(final boolean isSupportedToBuildEffective) {
302 this.isSupportedToBuildEffective = isSupportedToBuildEffective;
306 public final boolean isSupportedByFeatures() {
307 final int fl = flags & SET_SUPPORTED_BY_FEATURES;
309 return fl == SET_SUPPORTED_BY_FEATURES;
311 if (isIgnoringIfFeatures()) {
312 flags |= SET_SUPPORTED_BY_FEATURES;
317 * If parent is supported, we need to check if-features statements of this context.
319 if (isParentSupportedByFeatures()) {
320 // If the set of supported features has not been provided, all features are supported by default.
321 final Set<QName> supportedFeatures = getFromNamespace(SupportedFeaturesNamespace.class,
322 SupportedFeatures.SUPPORTED_FEATURES);
323 if (supportedFeatures == null || StmtContextUtils.checkFeatureSupport(this, supportedFeatures)) {
324 flags |= SET_SUPPORTED_BY_FEATURES;
329 // Either parent is not supported or this statement is not supported
330 flags |= HAVE_SUPPORTED_BY_FEATURES;
334 protected abstract boolean isParentSupportedByFeatures();
337 * Config statements are not all that common which means we are performing a recursive search towards the root
338 * every time {@link #isConfiguration()} is invoked. This is quite expensive because it causes a linear search
339 * for the (usually non-existent) config statement.
342 * This method maintains a resolution cache, so once we have returned a result, we will keep on returning the same
343 * result without performing any lookups, solely to support {@link SubstatementContext#isConfiguration()}.
346 * Note: use of this method implies that {@link #isIgnoringConfig()} is realized with
347 * {@link #isIgnoringConfig(StatementContextBase)}.
349 final boolean isConfiguration(final StatementContextBase<?, ?, ?> parent) {
350 final int fl = flags & SET_CONFIGURATION;
352 return fl == SET_CONFIGURATION;
354 if (isIgnoringConfig(parent)) {
355 // Note: SET_CONFIGURATION has been stored in flags
359 final boolean isConfig;
360 final Optional<Boolean> optConfig = findSubstatementArgument(ConfigEffectiveStatement.class);
361 if (optConfig.isPresent()) {
362 isConfig = optConfig.orElseThrow();
364 // Validity check: if parent is config=false this cannot be a config=true
365 InferenceException.throwIf(!parent.isConfiguration(), sourceReference(),
366 "Parent node has config=false, this node must not be specifed as config=true");
369 // If "config" statement is not specified, the default is the same as the parent's "config" value.
370 isConfig = parent.isConfiguration();
373 // Resolved, make sure we cache this return
374 flags |= isConfig ? SET_CONFIGURATION : HAVE_CONFIGURATION;
378 protected abstract boolean isIgnoringConfig();
381 * This method maintains a resolution cache for ignore config, so once we have returned a result, we will
382 * keep on returning the same result without performing any lookups. Exists only to support
383 * {@link SubstatementContext#isIgnoringConfig()}.
386 * Note: use of this method implies that {@link #isConfiguration()} is realized with
387 * {@link #isConfiguration(StatementContextBase)}.
389 final boolean isIgnoringConfig(final StatementContextBase<?, ?, ?> parent) {
390 final int fl = flags & SET_IGNORE_CONFIG;
392 return fl == SET_IGNORE_CONFIG;
394 if (definition().support().isIgnoringConfig() || parent.isIgnoringConfig()) {
395 flags |= SET_IGNORE_CONFIG;
399 flags |= HAVE_IGNORE_CONFIG;
403 protected abstract boolean isIgnoringIfFeatures();
406 * This method maintains a resolution cache for ignore if-feature, so once we have returned a result, we will
407 * keep on returning the same result without performing any lookups. Exists only to support
408 * {@link SubstatementContext#isIgnoringIfFeatures()}.
410 final boolean isIgnoringIfFeatures(final StatementContextBase<?, ?, ?> parent) {
411 final int fl = flags & SET_IGNORE_IF_FEATURE;
413 return fl == SET_IGNORE_IF_FEATURE;
415 if (definition().support().isIgnoringIfFeatures() || parent.isIgnoringIfFeatures()) {
416 flags |= SET_IGNORE_IF_FEATURE;
420 flags |= HAVE_IGNORE_IF_FEATURE;
424 // These two exists only due to memory optimization, should live in AbstractResumedStatement
425 final boolean fullyDefined() {
429 final void setFullyDefined() {
435 // Reference counting mechanics start. Please keep these methods in one block for clarity. Note this does not
436 // contribute to state visible outside of this package.
441 * Acquire a reference on this context. As long as there is at least one reference outstanding,
442 * {@link #buildEffective()} will not result in {@link #effectiveSubstatements()} being discarded.
444 * @throws VerifyException if {@link #effectiveSubstatements()} has already been discarded
446 final void incRef() {
447 final int current = refcount;
448 verify(current >= REFCOUNT_NONE, "Attempted to access reference count of %s", this);
449 if (current != REFCOUNT_DEFUNCT) {
450 // Note: can end up becoming REFCOUNT_DEFUNCT on overflow
451 refcount = current + 1;
453 LOG.debug("Disabled refcount increment of {}", this);
458 * Release a reference on this context. This call may result in {@link #effectiveSubstatements()} becoming
461 final void decRef() {
462 final int current = refcount;
463 if (current == REFCOUNT_DEFUNCT) {
465 LOG.debug("Disabled refcount decrement of {}", this);
468 if (current <= REFCOUNT_NONE) {
469 // Underflow, become defunct
470 LOG.warn("Statement refcount underflow, reference counting disabled for {}", this, new Throwable());
471 refcount = REFCOUNT_DEFUNCT;
475 refcount = current - 1;
476 LOG.trace("Refcount {} on {}", refcount, this);
478 // We are no longer guarded by effective instance
484 * Sweep this statement context as a result of {@link #sweepSubstatements()}, i.e. when parent is also being swept.
486 private void sweep() {
488 LOG.trace("Releasing {}", this);
493 static final void sweep(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
494 for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
499 static final int countUnswept(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
501 for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
502 if (stmt.refcount > REFCOUNT_NONE || !stmt.noImplictRef()) {
510 * Implementation-specific sweep action. This is expected to perform a recursive {@link #sweep(Collection)} on all
511 * {@link #declaredSubstatements()} and {@link #effectiveSubstatements()} and report the result of the sweep
515 * {@link #effectiveSubstatements()} as well as namespaces may become inoperable as a result of this operation.
517 * @return True if the entire tree has been completely swept, false otherwise.
519 abstract int sweepSubstatements();
521 // Called when this statement does not have an implicit reference and have reached REFCOUNT_NONE
522 private void sweepOnDecrement() {
523 LOG.trace("Sweeping on decrement {}", this);
524 if (noParentRefcount()) {
525 // No further parent references, sweep our state.
529 // Propagate towards parent if there is one
530 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
531 if (parent != null) {
532 parent.sweepOnChildDecrement();
536 // Called from child when it has lost its final reference
537 private void sweepOnChildDecrement() {
538 if (isAwaitingChildren()) {
539 // We are a child for which our parent is waiting. Notify it and we are done.
544 // Check parent reference count
545 final int refs = refcount;
546 if (refs > REFCOUNT_NONE || refs <= REFCOUNT_SWEEPING || !noImplictRef()) {
551 // parent is potentially reclaimable
552 if (noParentRefcount()) {
553 LOG.trace("Cleanup {} of parent {}", refcount, this);
555 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
556 if (parent != null) {
557 parent.sweepOnChildDecrement();
563 private boolean noImplictRef() {
564 return effectiveInstance != null || !isSupportedToBuildEffective();
567 // FIXME: cache the resolution of this
568 private boolean noParentRefcount() {
569 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
570 if (parent != null) {
571 // There are three possibilities:
572 // - REFCOUNT_NONE, in which case we need to search next parent
573 // - negative (< REFCOUNT_NONE), meaning parent is in some stage of sweeping, hence it does not have
575 // - positive (> REFCOUNT_NONE), meaning parent has an explicit refcount which is holding us down
576 final int refs = parent.refcount;
577 return refs == REFCOUNT_NONE ? parent.noParentRefcount() : refs < REFCOUNT_NONE;
582 private boolean isAwaitingChildren() {
583 return refcount > REFCOUNT_SWEEPING && refcount < REFCOUNT_NONE;
586 private boolean isSweepable() {
587 return refcount == REFCOUNT_NONE && noImplictRef();
590 private void sweepOnChildDone() {
591 LOG.trace("Sweeping on child done {}", this);
592 final int current = refcount;
593 if (current >= REFCOUNT_NONE) {
594 // no-op, perhaps we want to handle some cases differently?
595 LOG.trace("Ignoring child sweep of {} for {}", this, current);
598 verify(current != REFCOUNT_SWEPT, "Attempt to sweep a child of swept %s", this);
600 refcount = current + 1;
601 LOG.trace("Child refcount {}", refcount);
602 if (refcount == REFCOUNT_NONE) {
604 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
605 LOG.trace("Propagating to parent {}", parent);
606 if (parent != null && parent.isAwaitingChildren()) {
607 parent.sweepOnChildDone();
612 private void sweepDone() {
613 LOG.trace("Sweep done for {}", this);
614 refcount = REFCOUNT_SWEPT;
618 private boolean sweepState() {
619 refcount = REFCOUNT_SWEEPING;
620 final int childRefs = sweepSubstatements();
621 if (childRefs == 0) {
625 if (childRefs < 0 || childRefs >= REFCOUNT_DEFUNCT) {
626 LOG.warn("Negative child refcount {} cannot be stored, reference counting disabled for {}", childRefs, this,
628 refcount = REFCOUNT_DEFUNCT;
630 LOG.trace("Still {} outstanding children of {}", childRefs, this);
631 refcount = -childRefs;