Bug 6669: Mandatory nodes cannot be added to node from another module via augment
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentUtils.java
index dd14bf87d90860cd2ea415a833ad71e8c254064d..159b473370854ab51939ed91799fd822636830af 100644 (file)
@@ -7,83 +7,55 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
 import com.google.common.collect.ImmutableList.Builder;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
-import java.util.regex.Pattern;
-import javax.annotation.Nullable;
 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.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
-import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 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.reactor.RootStatementContext;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
+// FIXME: Move this to the AugmentStatementDefinition#ApplyAction
 public final class AugmentUtils {
-
-    private static final Logger LOG = LoggerFactory.getLogger(AugmentUtils.class);
-    private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
-    private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
-
     private AugmentUtils() {
-        throw new UnsupportedOperationException();
-    }
-
-    public static Iterable<QName> parseAugmentPath(final StmtContext<?, ?, ?> ctx, final String path) {
-        Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(path).matches()
-            && !PATH_REL_PATTERN2.matcher(path).matches(),
-            "An argument for augment can be only absolute path; or descendant if used in uses");
-
-        return Utils.parseXPath(ctx, path);
     }
 
     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
-
+            final StatementContextBase<?, ?, ?> targetCtx) {
         copyDeclaredStmts(sourceCtx, targetCtx);
         copyEffectiveStmts(sourceCtx, targetCtx);
     }
 
-    public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
-
-        final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
-                .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
-        boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
-                Utils.getRootModuleQName(targetCtx));
+    // FIXME: Declared statements should not be copied.
+    private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
+            final StatementContextBase<?, ?, ?> targetCtx) {
 
-        TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
-                .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
+        final CopyType typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? CopyType.ADDED_BY_USES_AUGMENTATION
+                : CopyType.ADDED_BY_AUGMENTATION;
 
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
+            if (!StmtContextUtils.areFeaturesSupported(originalStmtCtx)) {
+                continue;
+            }
             if (needToCopyByAugment(originalStmtCtx)) {
-                validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
+                validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
 
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
+                final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
                 targetCtx.addEffectiveSubstatement(copy);
             } else if (isReusedByAugment(originalStmtCtx)) {
                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
@@ -91,22 +63,17 @@ public final class AugmentUtils {
         }
     }
 
-    public static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
+    private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
+            final StatementContextBase<?, ?, ?> targetCtx) {
+        final CopyType typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? CopyType.ADDED_BY_USES_AUGMENTATION
+                : CopyType.ADDED_BY_AUGMENTATION;
 
-        final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
-                .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
-        boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
-                Utils.getRootModuleQName(targetCtx));
-
-        TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
-                .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
-
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
             if (needToCopyByAugment(originalStmtCtx)) {
-                validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
+                validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
 
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
+                final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
                 targetCtx.addEffectiveSubstatement(copy);
             } else if (isReusedByAugment(originalStmtCtx)) {
                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
@@ -115,182 +82,125 @@ public final class AugmentUtils {
     }
 
     private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
-            final Iterable<StatementContextBase<?, ?, ?>> targetSubStatements,
-            final boolean sourceAndTargetInSameModule) {
+            final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy) {
 
-        if (WhenStatement.class.equals(sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
+        if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
             return;
         }
 
-        if (!sourceAndTargetInSameModule) {
-            for (final StatementContextBase<?, ?, ?> sourceSubStatement :
-                Iterables.concat(sourceCtx.declaredSubstatements(), sourceCtx.effectiveSubstatements())) {
-                Preconditions.checkArgument(!MandatoryStatement.class.equals(
-                    sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()),
-                    "An augment cannot add node '%s' because it is mandatory and in module different from target",
-                    sourceCtx.rawStatementArgument());
-            }
+        if (typeOfCopy == CopyType.ADDED_BY_AUGMENTATION && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
+            checkForMandatoryNodes(sourceCtx);
         }
 
+        final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
+                .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
+
         for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
 
-            final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(
-                sourceCtx.getPublicDefinition().getDeclaredRepresentationClass());
-            final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(
-                subStatement.getPublicDefinition().getDeclaredRepresentationClass());
-            Preconditions.checkState(!sourceIsDataNode || !targetIsDataNode
-                    || !Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument()),
-                "An augment cannot add node named '%s' because this name is already used in target",
-                sourceCtx.rawStatementArgument());
+            final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
+                    .getPublicDefinition().getDeclaredRepresentationClass());
+            final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
+                    .getPublicDefinition().getDeclaredRepresentationClass());
+            final boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
+                    && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
+
+            InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
+                    "An augment cannot add node named '%s' because this name is already used in target",
+                    sourceCtx.rawStatementArgument());
         }
     }
 
-    public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
-            final StatementContextBase<?, ?, ?> sourceCtx) {
-        Object targetStmtArgument = targetCtx.getStatementArgument();
-
-        final StatementContextBase<?, ?, ?> root = sourceCtx.getRoot();
-        final QNameModule sourceQNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root);
-
-        if (targetStmtArgument instanceof QName) {
-            QName targetQName = (QName) targetStmtArgument;
-            QNameModule targetQNameModule = targetQName.getModule();
-
-            if (targetQNameModule.equals(sourceQNameModule)) {
-                return null;
-            } else {
-                return targetQNameModule;
+    private static void checkForMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx) {
+        if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
+            /*
+             * We need to iterate over both declared and effective sub-statements,
+             * because a mandatory node can be:
+             * a) declared in augment body
+             * b) added to augment body also via uses of a grouping and
+             * such sub-statements are stored in effective sub-statements collection.
+             */
+            for (final StatementContextBase<?, ?, ?> sourceSubStatement : Iterables.concat(
+                    sourceCtx.declaredSubstatements(), sourceCtx.declaredSubstatements())) {
+                checkForMandatoryNodes(sourceSubStatement);
             }
-        } else {
-            return null;
         }
-    }
-
-    private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
 
-    public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
-        return !NOCOPY_DEV_SET.contains(stmtContext.getPublicDefinition());
-    }
-
-    private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
-
-    public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
-        return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
+        InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx),
+                sourceCtx.getStatementSourceReference(),
+                "An augment cannot add node '%s' because it is mandatory and in module different than target",
+                sourceCtx.rawStatementArgument());
     }
 
-    public static StatementContextBase<?, ?, ?> getAugmentTargetCtx(
-            final Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
-
-        final SchemaNodeIdentifier augmentTargetNode = augmentNode.getStatementArgument();
-        Preconditions.checkArgument(augmentTargetNode != null,
-                "Augment argument null, something bad happened in some of previous parsing phases");
-
-        List<StatementContextBase<?, ?, ?>> rootStatementCtxList = new ArrayList<>();
-        if (augmentTargetNode.isAbsolute()) {
-
-            QNameModule module = augmentTargetNode.getPathFromRoot().iterator().next().getModule();
-
-            StatementContextBase<?, ?, ?> rootStatementCtx =
-                    (StatementContextBase<?, ?, ?>) augmentNode.getFromNamespace(NamespaceToModule.class, module);
-            rootStatementCtxList.add(rootStatementCtx);
-
-            final Map<?, ?> subModules = rootStatementCtx.getAllFromNamespace(IncludedModuleContext.class);
-            if (subModules != null) {
-                rootStatementCtxList.addAll((Collection<? extends StatementContextBase<?, ?, ?>>) subModules.values());
-            }
-
-        } else {
-            StatementContextBase<?, ?, ?> parent = (StatementContextBase<?, ?, ?>) augmentNode.getParentContext();
-            if (StmtContextUtils.producesDeclared(parent, UsesStatement.class)) {
-                rootStatementCtxList.add(parent.getParentContext());
-            } else {
-                // error
-            }
+    private static boolean reguiredCheckOfMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx,
+            StatementContextBase<?, ?, ?> targetCtx) {
+        /*
+         * If the statement argument is not QName, it cannot be mandatory
+         * statement, therefore return false and skip mandatory nodes validation
+         */
+        if (!(sourceCtx.getStatementArgument() instanceof QName)) {
+            return false;
         }
-
-        StatementContextBase<?, ?, ?> augmentTargetCtx = null;
-        for (final StatementContextBase<?, ?, ?> rootStatementCtx : rootStatementCtxList) {
-            augmentTargetCtx = findCtxOfNodeInRoot(rootStatementCtx, augmentTargetNode);
-            if (augmentTargetCtx != null) {
-                break;
+        final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
+
+        final RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
+        do {
+            Verify.verify(targetCtx.getStatementArgument() instanceof QName,
+                    "Argument of augment target statement must be QName.");
+            final QName targetStmtQName = (QName) targetCtx.getStatementArgument();
+            /*
+             * If target is from another module, return true and perform
+             * mandatory nodes validation
+             */
+            if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
+                return true;
+            } else {
+                /*
+                 * If target or one of its parent is a presence container from
+                 * the same module, return false and skip mandatory nodes
+                 * validation
+                 */
+                if (StmtContextUtils.isPresenceContainer(targetCtx)) {
+                    return false;
+                }
             }
-        }
+        } while ((targetCtx = targetCtx.getParentContext()) != root);
 
-        return augmentTargetCtx;
+        /*
+         * All target node's parents belong to the same module as source node,
+         * therefore return false and skip mandatory nodes validation.
+         */
+        return false;
     }
 
-    @Nullable
-    public static StatementContextBase<?, ?, ?> findCtxOfNodeInSubstatements(final StatementContextBase<?, ?, ?> rootStmtCtx,
-            final Iterable<QName> path) {
-
-        StatementContextBase<?, ?, ?> parent = rootStmtCtx;
-
-        Iterator<QName> pathIter = path.iterator();
-        while (pathIter.hasNext()) {
-            QName nextPathQName = pathIter.next();
-            StatementContextBase<?, ?, ?> foundSubstatement = getSubstatementByQName(parent, nextPathQName);
+    private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES, Rfc6020Mapping.WHEN,
+            Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
 
-            if (foundSubstatement == null) {
-                return null;
-            }
-            if (!pathIter.hasNext()) {
-                return foundSubstatement;
-            }
-
-            parent = foundSubstatement;
-        }
-
-        return null;
+    public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
+        return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
     }
 
-    public static StatementContextBase<?, ?, ?> getSubstatementByQName(final StatementContextBase<?, ?, ?> parent,
-            final QName nextPathQName) {
-
-        Collection<StatementContextBase<?, ?, ?>> declaredSubstatement = parent.declaredSubstatements();
-        Collection<StatementContextBase<?, ?, ?>> effectiveSubstatement = parent.effectiveSubstatements();
-
-        for (StatementContextBase<?, ?, ?> substatement : Iterables.concat(declaredSubstatement, effectiveSubstatement)) {
-            Object substatementArgument = substatement.getStatementArgument();
-            QName substatementQName;
-            if (substatementArgument instanceof QName) {
-                substatementQName = (QName) substatementArgument;
-
-                if (nextPathQName.getLocalName().equals(
-                        substatementQName.getLocalName())) {
-                    if (isSupportedAugmentTarget(substatement)) {
-                        return substatement;
-                    } else if (Utils.isUnknownNode(substatement)) {
-                        LOG.warn("Module '{}': augment into unknown node '{}'.",
-                                substatement.getRoot().getStatementArgument(), substatementArgument);
-                        return substatement;
-                    }
-                }
-            }
-        }
+    private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
 
-        return null;
+    public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
+        return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
     }
 
-    public static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
+    static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
 
         /*
-         * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must be not for example
-         * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
-         * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
-         * Grouping must be ignored as disallowed augment target.
+         * :TODO Substatement must be allowed augment target type e.g.
+         * Container, etc... and must not be for example grouping, identity etc.
+         * It is problem in case when more than one substatements have the same
+         * QName, for example Grouping and Container are siblings and they have
+         * the same QName. We must find the Container and the Grouping must be
+         * ignored as disallowed augment target.
          */
 
-        Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
+        final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
                 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
 
         // if no allowed target is returned we consider all targets allowed
         return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
                 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
     }
-
-    @Nullable
-    public static StatementContextBase<?, ?, ?> findCtxOfNodeInRoot(final StatementContextBase<?, ?, ?> rootStmtCtx,
-            final SchemaNodeIdentifier node) {
-        return findCtxOfNodeInSubstatements(rootStmtCtx, node.getPathFromRoot());
-    }
 }