BUG-6972: Remove GroupingUtils.needToCreateNewQName()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / GroupingUtils.java
index 0273150679d206076ac213532c5a1747781eb053..cff472e33d2b40baf71fad58a5978c5de2520bc4 100644 (file)
@@ -19,9 +19,9 @@ import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 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.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;
@@ -40,81 +40,62 @@ public final class GroupingUtils {
     }
 
     /**
-     * @param sourceGrpStmtCtx source grouping statement context
-     * @param targetCtx target context
-     * @param usesNode uses node
-     * @throws SourceException instance of SourceException
+     * @param sourceGrpStmtCtx
+     *            source grouping statement context
+     * @param targetCtx
+     *            target context
+     * @param usesNode
+     *            uses node
+     * @throws SourceException
+     *             instance of SourceException
      */
     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
             final StatementContextBase<?, ?, ?> targetCtx,
-            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode)
-            throws SourceException {
-
-        QNameModule newQNameModule = getNewQNameModule(targetCtx,
-                sourceGrpStmtCtx);
-        copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
-        copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
-                newQNameModule);
-    }
+            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
 
-    public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
-            final StatementContextBase<?, ?, ?> targetCtx,
-            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            final QNameModule newQNameModule) throws SourceException {
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
-            if (needToCopyByUses(originalStmtCtx)) {
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
-                    TypeOfCopy.ADDED_BY_USES);
-                targetCtx.addEffectiveSubstatement(copy);
-                usesNode.addAsEffectOfStatement(copy);
-            } else if (isReusedByUsesOnTop(originalStmtCtx)) {
-                targetCtx.addEffectiveSubstatement(originalStmtCtx);
-                usesNode.addAsEffectOfStatement(originalStmtCtx);
+        final QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
+        for (final StatementContextBase<?, ?, ?> original : sourceGrpStmtCtx.declaredSubstatements()) {
+            if (StmtContextUtils.areFeaturesSupported(original)) {
+                copyStatement(original, targetCtx, usesNode, newQNameModule);
             }
         }
+
+        for (final StatementContextBase<?, ?, ?> original : sourceGrpStmtCtx.effectiveSubstatements()) {
+            copyStatement(original, targetCtx, usesNode, newQNameModule);
+        }
     }
 
-    public static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
+    private static void copyStatement(final StatementContextBase<?, ?, ?> original,
             final StatementContextBase<?, ?, ?> targetCtx,
-            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            final QNameModule newQNameModule) throws SourceException {
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
-            if (needToCopyByUses(originalStmtCtx)) {
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
-                    TypeOfCopy.ADDED_BY_USES);
-                targetCtx.addEffectiveSubstatement(copy);
-                usesNode.addAsEffectOfStatement(copy);
-            } else if (isReusedByUsesOnTop(originalStmtCtx)) {
-                targetCtx.addEffectiveSubstatement(originalStmtCtx);
-                usesNode.addAsEffectOfStatement(originalStmtCtx);
-            }
+            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> targetUses,
+            final QNameModule targetModule) {
+        if (needToCopyByUses(original)) {
+            final StatementContextBase<?, ?, ?> copy = original.createCopy(targetModule, targetCtx,
+                    CopyType.ADDED_BY_USES);
+            targetCtx.addEffectiveSubstatement(copy);
+            targetUses.addAsEffectOfStatement(copy);
+        } else if (isReusedByUsesOnTop(original)) {
+            targetCtx.addEffectiveSubstatement(original);
+            targetUses.addAsEffectOfStatement(original);
         }
     }
 
     public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
             final StmtContext<?, ?, ?> stmtContext) {
-        if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
-            if (targetCtx.isRootContext()) {
-                return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
-            }
-            if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
-                return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
-            }
+        if (targetCtx.isRootContext()) {
+            return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
+        }
+        if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
+            return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
+        }
 
-            Object targetStmtArgument = targetCtx.getStatementArgument();
-            Object sourceStmtArgument = stmtContext.getStatementArgument();
-            if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
-                return ((QName) targetStmtArgument).getModule();
-            } else {
-                return null;
-            }
-        } else {
-            return null;
+        final Object targetStmtArgument = targetCtx.getStatementArgument();
+        final Object sourceStmtArgument = stmtContext.getStatementArgument();
+        if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
+            return ((QName) targetStmtArgument).getModule();
         }
-    }
 
-    public static boolean needToCreateNewQName(final StatementDefinition publicDefinition) {
-        return true;
+        return null;
     }
 
     private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES,
@@ -129,8 +110,8 @@ public final class GroupingUtils {
     public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
         final StatementDefinition def = stmtContext.getPublicDefinition();
 
-        return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def)
-                && Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition())));
+        return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def) && Rfc6020Mapping.GROUPING
+                .equals(stmtContext.getParentContext().getPublicDefinition())));
     }
 
     public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
@@ -143,8 +124,8 @@ public final class GroupingUtils {
 
     public static void resolveUsesNode(
             final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            final StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
-        for (StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
+            final StatementContextBase<?, ?, ?> targetNodeStmtCtx) {
+        for (final StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
                 performRefine(subStmtCtx, targetNodeStmtCtx);
             }
@@ -154,19 +135,21 @@ public final class GroupingUtils {
     private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
             final StatementContextBase<?, ?, ?> usesParentCtx) {
 
-        Object refineArgument = refineCtx.getStatementArgument();
+        final Object refineArgument = refineCtx.getStatementArgument();
         Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
-            "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
+                "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
                 refineCtx.getStatementSourceReference());
 
-        SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
-        StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
+        final SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
+        final StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx,
+                refineTargetNodeIdentifier);
         Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
                 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
         if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
-            LOG.warn("Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
-                    refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(), refineTargetNodeCtx.getStatementArgument(),
-                    refineCtx.getStatementSourceReference());
+            LOG.debug(
+                    "Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
+                    refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(),
+                    refineTargetNodeCtx.getStatementArgument(), refineCtx.getStatementSourceReference());
             refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
             return;
         }
@@ -177,7 +160,7 @@ public final class GroupingUtils {
 
     private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
-        for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
             if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
                 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
             }
@@ -187,14 +170,14 @@ public final class GroupingUtils {
     private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
-        StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
+        final StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
 
         SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
-            refineSubstatementCtx.getStatementSourceReference(),
-            "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
-                    refineSubstatementCtx.getRoot().getStatementArgument(),
-                    refineSubstatementCtx.getParentContext().getStatementArgument(),
-                    refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
+                refineSubstatementCtx.getStatementSourceReference(),
+                "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
+                refineSubstatementCtx.getRoot().getStatementArgument(), refineSubstatementCtx.getParentContext()
+                        .getStatementArgument(), refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx
+                        .getPublicDefinition());
 
         if (isAllowedToAddByRefine(refineSubstatementDef)) {
             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
@@ -211,8 +194,8 @@ public final class GroupingUtils {
     }
 
     private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
-        Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
-            ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
+        final Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
+                ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
 
         return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
                 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
@@ -222,8 +205,8 @@ public final class GroupingUtils {
     private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
-        Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
-            refineSubstatementCtx.getPublicDefinition());
+        final Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS
+                .get(refineSubstatementCtx.getPublicDefinition());
 
         return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
                 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());