Bug 8922 - Evaluation of if-features is done regardless of ancestors
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index 423a0cabb9ff5936f3c29c0147915f29c31a737e..c59c81552922bdb33385de73e9832e346fb2b246 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Iterator;
 import java.util.Optional;
 import java.util.Set;
 import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.util.OptionalBoolean;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
@@ -81,6 +82,8 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
     private final StatementDefinitionContext<A, D, E> definition;
     private final StatementSourceReference statementDeclSource;
+    private final StmtContext<?, ?, ?> originalCtx;
+    private final CopyHistory copyHistory;
     private final String rawArgument;
 
     private Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners = ImmutableMultimap.of();
@@ -89,29 +92,32 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     private Collection<StmtContext<?, ?, ?>> effectOfStatement = ImmutableList.of();
     private StatementMap substatements = StatementMap.empty();
 
-    private Boolean supportedByFeatures = null;
-    private CopyHistory copyHistory = CopyHistory.original();
     private boolean isSupportedToBuildEffective = true;
     private ModelProcessingPhase completedPhase = null;
-    private StmtContext<?, ?, ?> originalCtx;
     private D declaredInstance;
     private E effectiveInstance;
-    private int order = 0;
+
+    // BooleanFields value
+    private byte supportedByFeatures;
 
     StatementContextBase(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
             final String rawArgument) {
         this.definition = Preconditions.checkNotNull(def);
         this.statementDeclSource = Preconditions.checkNotNull(ref);
         this.rawArgument = def.internArgument(rawArgument);
+        this.copyHistory = CopyHistory.original();
+        this.originalCtx = null;
     }
 
-    StatementContextBase(final StatementContextBase<A, D, E> original) {
+    StatementContextBase(final StatementContextBase<A, D, E> original, final CopyType copyType) {
         this.definition = Preconditions.checkNotNull(original.definition,
                 "Statement context definition cannot be null copying from: %s", original.getStatementSourceReference());
         this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
                 "Statement context statementDeclSource cannot be null copying from: %s",
                 original.getStatementSourceReference());
         this.rawArgument = original.rawArgument;
+        this.copyHistory = CopyHistory.of(copyType, original.getCopyHistory());
+        this.originalCtx = original.getOriginalCtx().orElse(original);
     }
 
     @Override
@@ -141,17 +147,46 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
     @Override
     public boolean isSupportedByFeatures() {
-        if (supportedByFeatures == null) {
-            final Set<QName> supportedFeatures = getFromNamespace(SupportedFeaturesNamespace.class,
-                SupportedFeatures.SUPPORTED_FEATURES);
-            // If the set of supported features has not been provided, all features are supported by default.
-            supportedByFeatures = supportedFeatures == null ? Boolean.TRUE
-                    : StmtContextUtils.checkFeatureSupport(this, supportedFeatures);
+        if (OptionalBoolean.isPresent(supportedByFeatures)) {
+            return OptionalBoolean.get(supportedByFeatures);
+        }
+
+        if (isIgnoringIfFeatures()) {
+            supportedByFeatures = OptionalBoolean.of(true);
+            return true;
+        }
+
+        final boolean isParentSupported = isParentSupportedByFeatures();
+        /*
+         * If parent is not supported, then this context is also not supported.
+         * So we do not need to check if-features statements of this context and
+         * we can return false immediately.
+         */
+        if (!isParentSupported) {
+            supportedByFeatures = OptionalBoolean.of(false);
+            return false;
         }
 
-        return supportedByFeatures.booleanValue();
+        /*
+         * If parent is supported, we need to check if-features statements of
+         * this context.
+         */
+        // If the set of supported features has not been provided, all features are supported by default.
+        final Set<QName> supportedFeatures = getFromNamespace(SupportedFeaturesNamespace.class,
+                SupportedFeatures.SUPPORTED_FEATURES);
+        final boolean ret = supportedFeatures == null ? true
+                : StmtContextUtils.checkFeatureSupport(this, supportedFeatures);
+
+        supportedByFeatures = OptionalBoolean.of(ret);
+        return ret;
     }
 
+    protected abstract boolean isParentSupportedByFeatures();
+
+    protected abstract boolean isIgnoringIfFeatures();
+
+    protected abstract boolean isIgnoringConfig();
+
     @Override
     public boolean isSupportedToBuildEffective() {
         return isSupportedToBuildEffective;
@@ -168,28 +203,8 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     }
 
     @Override
-    public void appendCopyHistory(final CopyType typeOfCopy, final CopyHistory toAppend) {
-        copyHistory = copyHistory.append(typeOfCopy, toAppend);
-    }
-
-    @Override
-    public StmtContext<?, ?, ?> getOriginalCtx() {
-        return originalCtx;
-    }
-
-    @Override
-    public void setOriginalCtx(final StmtContext<?, ?, ?> originalCtx) {
-        this.originalCtx = originalCtx;
-    }
-
-    @Override
-    public void setOrder(final int order) {
-        this.order = order;
-    }
-
-    @Override
-    public int getOrder() {
-        return order;
+    public Optional<StmtContext<?, ?, ?>> getOriginalCtx() {
+        return Optional.ofNullable(originalCtx);
     }
 
     @Override
@@ -385,9 +400,8 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
         final Optional<StatementContextBase<?, ?, ?>> implicitStatement = definition.beforeSubStatementCreated(this,
             offset, def, ref, argument);
-        if(implicitStatement.isPresent()) {
-            final StatementContextBase<?, ?, ?> presentImplicitStmt = implicitStatement.get();
-            return presentImplicitStmt.createSubstatement(offset, def, ref, argument);
+        if (implicitStatement.isPresent()) {
+            return implicitStatement.get().createSubstatement(offset, def, ref, argument);
         }
 
         final StatementContextBase<CA, CD, CE> ret = new SubstatementContext<>(this, def, ref, argument);