Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / SubstatementContext.java
index 5536dfe7c59fd16123001bfb4e8de484b1d1d161..1c78ae16c096664129221ca05b7e18452e9fa0ed 100644 (file)
@@ -10,12 +10,18 @@ package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Verify;
+import com.google.common.collect.ImmutableSet;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Set;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
@@ -23,7 +29,9 @@ import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
@@ -32,11 +40,13 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.GroupingUtils;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
         StatementContextBase<A, D, E> {
+    private static final Logger LOG = LoggerFactory.getLogger(SubstatementContext.class);
 
     private final StatementContextBase<?, ?, ?> parent;
     private final A argument;
@@ -50,13 +60,14 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
     @SuppressWarnings("unchecked")
     SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
-            final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
+            final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
         super(original);
         this.parent = newParent;
 
         if (newQNameModule != null) {
-            if (original.argument instanceof QName) {
-                final QName originalQName = (QName) original.argument;
+            final A originalArg = original.argument;
+            if (originalArg instanceof QName) {
+                final QName originalQName = (QName) originalArg;
                 this.argument = (A) getFromNamespace(QNameCacheNamespace.class,
                         QName.create(newQNameModule, originalQName.getLocalName()));
             } else if (StmtContextUtils.producesDeclared(original, KeyStatement.class)) {
@@ -70,37 +81,6 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         }
     }
 
-    private void copyDeclaredStmts(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
-            final TypeOfCopy typeOfCopy) {
-        final Collection<? extends StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original
-                .declaredSubstatements();
-        for (final StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
-            if (!StmtContextUtils.areFeaturesSupported(stmtContext)) {
-                continue;
-            }
-            if (GroupingUtils.needToCopyByUses(stmtContext)) {
-                final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
-                this.addEffectiveSubstatement(copy);
-            } else if (GroupingUtils.isReusedByUses(stmtContext)) {
-                this.addEffectiveSubstatement(stmtContext);
-            }
-        }
-    }
-
-    private void copyEffectiveStmts(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
-            final TypeOfCopy typeOfCopy) {
-        final Collection<? extends StatementContextBase<?, ?, ?>> originalEffectiveSubstatements = original
-                .effectiveSubstatements();
-        for (final StatementContextBase<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
-            if (GroupingUtils.needToCopyByUses(stmtContext)) {
-                final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
-                this.addEffectiveSubstatement(copy);
-            } else if (GroupingUtils.isReusedByUses(stmtContext)) {
-                this.addEffectiveSubstatement(stmtContext);
-            }
-        }
-    }
-
     @Override
     public StatementContextBase<?, ?, ?> getParentContext() {
         return parent;
@@ -116,6 +96,7 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         return parent.getBehaviourRegistry();
     }
 
+    @Nonnull
     @Override
     public RootStatementContext<?, ?, ?> getRoot() {
         return parent.getRoot();
@@ -128,17 +109,19 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
     @Override
     public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
-            final TypeOfCopy typeOfCopy) {
+            final CopyType typeOfCopy) {
         return createCopy(null, newParent, typeOfCopy);
     }
 
     @Override
     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
-            final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
+            final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
+        Preconditions.checkState(getCompletedPhase() == ModelProcessingPhase.EFFECTIVE_MODEL,
+                "Attempted to copy statement {} which has completed phase {}", this, getCompletedPhase());
+
         final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
 
-        copy.addAllToCopyHistory(this.getCopyHistory());
-        copy.addToCopyHistory(typeOfCopy);
+        copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
 
         if (this.getOriginalCtx() != null) {
             copy.setOriginalCtx(this.getOriginalCtx());
@@ -148,11 +131,72 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
         definition().onStatementAdded(copy);
 
-        copy.copyDeclaredStmts(this, newQNameModule, typeOfCopy);
-        copy.copyEffectiveStmts(this, newQNameModule, typeOfCopy);
+        copy.copyStatements(this, newQNameModule, typeOfCopy);
         return copy;
     }
 
+    private void copyStatements(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
+            final CopyType typeOfCopy) {
+        final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
+        final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
+        final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
+
+        for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
+            if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
+                copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
+            }
+        }
+
+        for (final StatementContextBase<?, ?, ?> stmtContext : effective) {
+            copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
+        }
+
+        addEffectiveSubstatements(buffer);
+    }
+
+    private void copySubstatement(final StatementContextBase<?, ?, ?> stmtContext,
+            final QNameModule newQNameModule, final CopyType typeOfCopy,
+            final Collection<StatementContextBase<?, ?, ?>> buffer) {
+        if (needToCopyByUses(stmtContext)) {
+            final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
+            LOG.debug("Copying substatement {} for {} as", stmtContext, this, copy);
+            buffer.add(copy);
+        } else if (isReusedByUses(stmtContext)) {
+            LOG.debug("Reusing substatement {} for {}", stmtContext, this);
+            buffer.add(stmtContext);
+        } else {
+            LOG.debug("Skipping statement {}", stmtContext);
+        }
+    }
+
+    // FIXME: revise this, as it seems to be wrong
+    private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
+        Rfc6020Mapping.DESCRIPTION,
+        Rfc6020Mapping.REFERENCE,
+        Rfc6020Mapping.STATUS);
+    private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(
+        Rfc6020Mapping.TYPE,
+        Rfc6020Mapping.TYPEDEF,
+        Rfc6020Mapping.USES);
+
+    private static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
+        final StatementDefinition def = stmtContext.getPublicDefinition();
+        if (REUSED_DEF_SET.contains(def)) {
+            LOG.debug("Will reuse {} statement {}", def, stmtContext);
+            return false;
+        }
+        if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
+            return !Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition());
+        }
+
+        LOG.debug("Will copy {} statement {}", def, stmtContext);
+        return true;
+    }
+
+    private static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
+        return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
+    }
+
     private boolean isSupportedAsShorthandCase() {
         final Collection<?> supportedCaseShorthands = getFromNamespace(ValidationBundlesNamespace.class,
                 ValidationBundleType.SUPPORTED_CASE_SHORTHANDS);
@@ -201,6 +245,7 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         return maybeParentPath.orNull();
     }
 
+    @Nonnull
     @Override
     public Optional<SchemaPath> getSchemaPath() {
         SchemaPath local = schemaPath;