Bug 5059: Do not fail when 'refine' targets an extension.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / GroupingUtils.java
index 6bb165fd6aa73942e2d3e4f03184d982dcce849d..8106aa6350fd0d086d7d6ddaa2df73502e89e7b3 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -8,8 +8,8 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -28,9 +28,13 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 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.StatementContextBase;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class GroupingUtils {
 
+    private static final Logger LOG = LoggerFactory.getLogger(GroupingUtils.class);
+
     private GroupingUtils() {
         throw new UnsupportedOperationException();
     }
@@ -113,52 +117,34 @@ public final class GroupingUtils {
         return true;
     }
 
-    public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
-
-        Set<StatementDefinition> noCopyDefSet = new HashSet<>();
-        noCopyDefSet.add(Rfc6020Mapping.USES);
-        noCopyDefSet.add(Rfc6020Mapping.TYPEDEF);
-        noCopyDefSet.add(Rfc6020Mapping.TYPE);
+    private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(
+        Rfc6020Mapping.USES, Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
+    private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
+        Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE);
+    private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(
+        Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE, Rfc6020Mapping.USES);
+    private static final Set<Rfc6020Mapping> TOP_REUSED_DEF_SET = ImmutableSet.of(
+        Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
 
-        final Set<StatementDefinition> noCopyFromGroupingSet = new HashSet<>();
-        noCopyFromGroupingSet.add(Rfc6020Mapping.DESCRIPTION);
-        noCopyFromGroupingSet.add(Rfc6020Mapping.REFERENCE);
-
-        StatementDefinition def = stmtContext.getPublicDefinition();
-        boolean dontCopyFromParentGrouping = noCopyFromGroupingSet.contains(def) && stmtContext.getParentContext()
-                .getPublicDefinition().equals(Rfc6020Mapping.GROUPING);
+    public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
+        final StatementDefinition def = stmtContext.getPublicDefinition();
 
-        return !noCopyDefSet.contains(def) && !dontCopyFromParentGrouping;
+        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) {
-
-        Set<StatementDefinition> reusedDefSet = new HashSet<>();
-        reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
-        reusedDefSet.add(Rfc6020Mapping.TYPE);
-        reusedDefSet.add(Rfc6020Mapping.USES);
-
-        StatementDefinition def = stmtContext.getPublicDefinition();
-        return reusedDefSet.contains(def);
+        return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
     }
 
     public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
-
-        Set<StatementDefinition> reusedDefSet = new HashSet<>();
-        reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
-        reusedDefSet.add(Rfc6020Mapping.TYPE);
-
-        StatementDefinition def = stmtContext.getPublicDefinition();
-        return reusedDefSet.contains(def);
+        return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
     }
 
     public static void resolveUsesNode(
             final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            final StatementContextBase<?, ?, ?> targetNodeStmtCtx)
-            throws SourceException {
-
-        Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode.declaredSubstatements();
-        for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
+            final StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
+        for (StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
                 performRefine(subStmtCtx, targetNodeStmtCtx);
             }
@@ -170,12 +156,20 @@ public final class GroupingUtils {
 
         Object refineArgument = refineCtx.getStatementArgument();
         Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
-            "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier", 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);
-        Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found.",
-                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());
+            refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
+            return;
+        }
 
         addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
         refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
@@ -183,9 +177,7 @@ public final class GroupingUtils {
 
     private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
-
-        Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = refineCtx.declaredSubstatements();
-        for (StatementContextBase<?, ?, ?> refineSubstatementCtx : declaredSubstatements) {
+        for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
             if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
                 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
             }
@@ -195,20 +187,14 @@ public final class GroupingUtils {
     private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
-        StatementDefinition refineSubstatementDef = refineSubstatementCtx
-                .getPublicDefinition();
-        StatementDefinition refineTargetNodeDef = refineTargetNodeCtx
-                .getPublicDefinition();
-
-        if (!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx)) {
-            throw new SourceException("Error in module '"
-                    + refineSubstatementCtx.getRoot().getStatementArgument()
-                    + "' in the refine of uses '"
-                    + refineSubstatementCtx.getParentContext().getStatementArgument()
-                    + "': can not perform refine of '" + refineSubstatementCtx.getPublicDefinition()
-                    + "' for the target '" + refineTargetNodeCtx.getPublicDefinition() + "'.",
-                    refineSubstatementCtx.getStatementSourceReference());
-        }
+        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());
 
         if (isAllowedToAddByRefine(refineSubstatementDef)) {
             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
@@ -218,15 +204,13 @@ public final class GroupingUtils {
         }
     }
 
-    private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
-        Set<StatementDefinition> allowedToAddByRefineDefSet = new HashSet<>();
-        allowedToAddByRefineDefSet.add(Rfc6020Mapping.MUST);
+    private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
 
-        return allowedToAddByRefineDefSet.contains(publicDefinition);
+    private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
+        return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
     }
 
     private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
-
         Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
             ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);